The IO core, which services input, routes responses, and executes the Player update routine periodically. More...
#include <io.hpp>
Public Member Functions | |
IoCore (Player &player) | |
Constructs an IoCore. More... | |
IoCore (const IoCore &)=delete | |
Deleted copy constructor. | |
IoCore & | operator= (const IoCore &)=delete |
Deleted copy-assignment. | |
void | Run (const std::string &host, const std::string &port) |
Runs the reactor. More... | |
void | Accept (uv_stream_t *server) |
Accepts a new connection. More... | |
void | Remove (size_t id) |
Removes a connection. More... | |
void | UpdatePlayer () |
Performs a player update cycle. More... | |
void | Respond (size_t id, const Response &response) const override |
Outputs a response. More... | |
void | Shutdown () |
Shuts down the IoCore by terminating all IO loop tasks. | |
Public Member Functions inherited from ResponseSink | |
virtual | ~ResponseSink ()=default |
Empty virtual destructor for ResponseSink. | |
Private Member Functions | |
void | InitAcceptor (const std::string &address, const std::string &port) |
Initialises a TCP acceptor on the given address and port. More... | |
void | InitUpdateTimer () |
Sets up a periodic timer to run the playd update loop. | |
void | InitSignals () |
Initialises playd's signal handling. More... | |
size_t | NextConnectionID () |
Acquires the next available connection ID. More... | |
void | ExpandPool () |
Adds a new connection slot to the connection pool. More... | |
void | Broadcast (const Response &response) const |
Sends the given response to all connections. More... | |
void | Unicast (size_t id, const Response &response) const |
Sends the given response to the identified connection. More... | |
Private Attributes | |
uv_loop_t * | loop |
The loop this IoCore is using. | |
uv_signal_t | sigint |
The libuv handle for the Ctrl-C signal. | |
uv_tcp_t | server |
The libuv handle for the TCP server. | |
uv_timer_t | updater |
The libuv handle for the update timer. | |
Player & | player |
The player. | |
std::vector< std::shared_ptr< Connection > > | pool |
The set of connections inside this IoCore. | |
std::vector< size_t > | free_list |
A list of free 1-indexed slots inside pool. More... | |
Static Private Attributes | |
static const uint16_t | PLAYER_UPDATE_PERIOD = 5 |
The period between player updates. | |
The IO core, which services input, routes responses, and executes the Player update routine periodically.
The IO core also maintains a pool of connections which can be sent responses via their IDs inside the pool. It ensures that each connection is given an ID that is unique up until the removal of said connection.
|
explicit |
void IoCore::Accept | ( | uv_stream_t * | server | ) |
Accepts a new connection.
This accepts the connection, and adds it to this IoCore's connection pool.
This should be called with a server that has just received a new connection.
server | Pointer to the libuv server accepting connections. |
Definition at line 190 of file io.cpp.
References Response::IAMA, loop, MSG_OHAI_BIFROST, MSG_OHAI_PLAYD, NextConnectionID(), Response::NOREQUEST, Response::OHAI, player, pool, Respond(), Response::Success(), UvAlloc(), UvCloseCallback(), and UvReadCallback().
|
private |
Sends the given response to all connections.
response | The response to broadcast. |
Definition at line 316 of file io.cpp.
References Response::Pack(), and pool.
Referenced by Respond().
|
private |
Adds a new connection slot to the connection pool.
This is called by NextConnectionID when the number of currently running connections is larger than the number of existing pool slots, and will eventually fail (when the number of simultaneous connections reaches absurd proportions).
Definition at line 243 of file io.cpp.
References free_list, MSG_TOO_MANY_CONNS, and pool.
Referenced by NextConnectionID().
|
private |
Initialises a TCP acceptor on the given address and port.
address | The IPv4 address on which the TCP server should listen. |
port | The TCP port on which the TCP server should listen. |
Definition at line 349 of file io.cpp.
References loop, MSG_IO_CANNOT_ALLOC, server, and UvListenCallback().
Referenced by Run().
|
private |
Initialises playd's signal handling.
We trap SIGINT, and the equivalent emulated signal on Windows, to make playd close gracefully when Ctrl-C is sent.
Definition at line 372 of file io.cpp.
References loop, MSG_IO_CANNOT_ALLOC, player, sigint, and UvSigintCallback().
Referenced by Run().
|
private |
Acquires the next available connection ID.
This ID may have been assigned to a connection in the past, but is guaranteed not to match any currently assigned IDs.
Definition at line 223 of file io.cpp.
References ExpandPool(), free_list, and pool.
Referenced by Accept().
void IoCore::Remove | ( | size_t | id | ) |
Removes a connection.
As the IoCore owns the Connection, it will be destroyed by this operation.
id | The ID of the connection to remove. |
Definition at line 260 of file io.cpp.
References free_list, and pool.
Referenced by Connection::Depool().
|
overridevirtual |
Outputs a response.
id | The ID of the client of the ResponseSink receiving this response. Use 0 for broadcasts. |
response | The Response to output. |
Reimplemented from ResponseSink.
Definition at line 305 of file io.cpp.
References Broadcast(), pool, and Unicast().
Referenced by Accept().
void IoCore::Run | ( | const std::string & | host, |
const std::string & | port | ||
) |
Runs the reactor.
It will block until it terminates.
Definition at line 174 of file io.cpp.
References InitAcceptor(), InitSignals(), InitUpdateTimer(), loop, and MSG_IO_CANNOT_ALLOC.
Referenced by main().
|
private |
Sends the given response to the identified connection.
id | The ID of the recipient connection. |
response | The response to broadcast. |
Definition at line 327 of file io.cpp.
References Response::Pack(), and pool.
Referenced by Respond().
void IoCore::UpdatePlayer | ( | ) |
Performs a player update cycle.
If the player is closing, IoCore will announce this fact to all current connections, close them, and end the I/O loop.
Definition at line 274 of file io.cpp.
References player, Shutdown(), and Player::Update().
|
private |
A list of free 1-indexed slots inside pool.
These slots may be re-used instead of creating a new slot.
Definition at line 118 of file io.hpp.
Referenced by ExpandPool(), NextConnectionID(), and Remove().