A TCP connection from a client. More...
#include <io.hpp>
Public Member Functions | |
Connection (IoCore &parent, uv_tcp_t *tcp, Player &player, size_t id) | |
Constructs a Connection. More... | |
~Connection () | |
Destructs a Connection. More... | |
Connection (const Connection &)=delete | |
Connection cannot be copied. | |
Connection & | operator= (const Connection &)=delete |
Connection cannot be copy-assigned. | |
void | Respond (const Response &response) |
Emits a Response via this Connection. More... | |
void | Read (ssize_t nread, const uv_buf_t *buf) |
Processes a data read on this connection. More... | |
void | Shutdown () |
Gracefully shuts this connection down. More... | |
void | Depool () |
Removes this connection from its connection pool. More... | |
std::string | Name () |
Retrieves a name for this connection. More... | |
Private Member Functions | |
Response | RunCommand (const std::vector< std::string > &msg) |
Handles a tokenised command line. More... | |
Private Attributes | |
IoCore & | parent |
The pool on which this connection is running. | |
uv_tcp_t * | tcp |
The libuv handle for the TCP connection. | |
Tokeniser | tokeniser |
The Tokeniser to which data read on this connection should be sent. | |
Player & | player |
The Player to which finished commands should be sent. | |
size_t | id |
The Connection's ID in the connection pool. | |
A TCP connection from a client.
This class wraps a libuv TCP stream representing a client connection, allowing it to be sent responses (directly, or via a broadcast), removed from its IoCore, and queried for its name.
Constructs a Connection.
parent | The connection pool to which this Connection belongs. |
tcp | The underlying libuv TCP stream. |
player | The player to which read commands should be sent. |
id | The ID of this Connection in the IoCore. |
Definition at line 392 of file io.cpp.
References Name().
Connection::~Connection | ( | ) |
Destructs a Connection.
This causes libuv to close and free the libuv TCP stream.
Definition at line 398 of file io.cpp.
References Name(), tcp, and UvCloseCallback().
void Connection::Depool | ( | ) |
Removes this connection from its connection pool.
Since the pool may contain a shared reference to this connection, calling this can result in the connection being destructed.
Definition at line 535 of file io.cpp.
References parent, and IoCore::Remove().
Referenced by Read().
std::string Connection::Name | ( | ) |
Retrieves a name for this connection.
This will be of the form "HOST:PORT", unless errors occur.
Definition at line 429 of file io.cpp.
References tcp.
Referenced by Connection(), Read(), and ~Connection().
void Connection::Read | ( | ssize_t | nread, |
const uv_buf_t * | buf | ||
) |
Processes a data read on this connection.
nread | The number of bytes read. |
buf | The buffer containing the read data. |
Definition at line 464 of file io.cpp.
References Depool(), Tokeniser::Feed(), Name(), Respond(), RunCommand(), and tokeniser.
void Connection::Respond | ( | const Response & | response | ) |
Emits a Response via this Connection.
response | The response to send. |
Definition at line 404 of file io.cpp.
References Response::Pack(), tcp, and UvWriteCallback().
Referenced by Read().
|
private |
Handles a tokenised command line.
msg | A vector of command words representing a command line. |
Definition at line 499 of file io.cpp.
References Player::Dump(), Player::Eject(), Player::End(), Response::Invalid(), Player::Load(), MSG_CMD_INVALID, MSG_CMD_SHORT, player, Player::Pos(), and Player::SetPlaying().
Referenced by Read().
void Connection::Shutdown | ( | ) |
Gracefully shuts this connection down.
This is similar to (and implemented in terms of) Depool, but waits for all writes to finish first.
Definition at line 524 of file io.cpp.
References tcp, and UvShutdownCallback().