|
Fray Engine
|
Listens on one TCP port and exchanges frames with the connected clients. All members and handlers run on the network thread (the thread running the io_context); the only exceptions are the constructor/destructor and port(). Shared ownership keeps it alive while async operations are pending. More...
#include <ipc_manager.hpp>
Public Member Functions | |
| TCPServer (boost::asio::io_context &ctx, F_ushort port, IPCManager *manager) | |
| Binds a listening IPv4 socket on the port. Throws (boost) if the port cannot be bound. | |
| ~TCPServer () | |
| Logs that the server was destroyed. | |
| F_void | setConnectionHandler (ConnectionHandler handler) |
| Sets the callback run on the network thread for each accepted client. Set before start(). | |
| F_void | start () |
| Begins accepting clients (called by IPCManager::createTCPServer). Network thread / before the io_context runs. | |
| F_void | stop () |
| Stops accepting, closes every client, drops pending writes and asks the owning IPCManager to remove this server. Network thread only. Idempotent. | |
| F_void | drainAndStop () |
| Stops accepting and drains queued socket writes for at most one second. Network thread only. | |
| F_void | broadcast (Frame frame) |
| Queues a message for every connected client. Network thread only; use IPCManager::send elsewhere. | |
| F_void | sendTo (Socket session, Frame frame) |
| Queues a message for one client. Network thread only. Logs an error and sends nothing if session or frame is null. | |
| F_ushort | port () const |
| The port the server is listening on (the OS-chosen one if 0 was requested). | |
Private Member Functions | |
| F_void | accept () |
| Starts (and re-arms) an asynchronous accept; each client gets a session and starts being read. Does nothing once stopped. | |
| F_void | startRead (std::shared_ptr< boost::asio::ip::tcp::socket > session) |
| Reads one frame (prefix, then header + payload) from the client and hands it to IPCManager::queueMessage, then reads the next. A read error or malformed prefix closes the session. | |
| F_void | write (Socket session, Frame frame) |
| Appends a frame to the session's write queue and starts writing if none is in flight. | |
| F_void | writeNext (std::shared_ptr< boost::asio::ip::tcp::socket > session) |
| Writes the frame at the front of the session's queue; on completion pops it and writes the next. Only one write is in flight per socket. | |
| F_void | closeSession (Socket session) |
| Closes the client socket and forgets its session and pending writes (queued frames are lost). | |
| F_void | stopIfDrained () |
| Stops the server once all locally queued writes have completed. | |
Private Attributes | |
| boost::asio::ip::tcp::acceptor | _acceptor |
| F_bool | _accepting = true |
| F_bool | _stopped = false |
| F_bool | _draining = false |
| boost::asio::steady_timer | _drainTimer |
| F_ushort | _port = 0 |
| std::set< std::shared_ptr< boost::asio::ip::tcp::socket > > | _sessions |
| IPCManager * | _manager = nullptr |
| ConnectionHandler | _connectionHandler = nullptr |
| std::map< std::shared_ptr< boost::asio::ip::tcp::socket >, std::deque< Frame > > | _outgoing |
Listens on one TCP port and exchanges frames with the connected clients. All members and handlers run on the network thread (the thread running the io_context); the only exceptions are the constructor/destructor and port(). Shared ownership keeps it alive while async operations are pending.
Definition at line 50 of file ipc_manager.hpp.
|
inline |
Binds a listening IPv4 socket on the port. Throws (boost) if the port cannot be bound.
| ctx | The io_context whose thread will run this server's handlers. |
| port | TCP port to listen on; 0 lets the OS pick a free one (read it back with port()). |
| manager | Owner that receives incoming frames; not owned and must outlive the server. |
Definition at line 114 of file ipc_manager.hpp.
|
inline |
Logs that the server was destroyed.
Definition at line 128 of file ipc_manager.hpp.
|
inlineprivate |
Starts (and re-arms) an asynchronous accept; each client gets a session and starts being read. Does nothing once stopped.
Definition of TCPServer::accept.
Definition at line 1256 of file ipc_manager.hpp.
Queues a message for every connected client. Network thread only; use IPCManager::send elsewhere.
| frame | Frame to write to all clients; ignored once stopped. |
Definition at line 1340 of file ipc_manager.hpp.
Closes the client socket and forgets its session and pending writes (queued frames are lost).
| session | Client to disconnect. |
Definition at line 1391 of file ipc_manager.hpp.
|
inline |
Stops accepting and drains queued socket writes for at most one second. Network thread only.
Definition at line 1411 of file ipc_manager.hpp.
|
inline |
The port the server is listening on (the OS-chosen one if 0 was requested).
Definition at line 172 of file ipc_manager.hpp.
Queues a message for one client. Network thread only. Logs an error and sends nothing if session or frame is null.
| session | Client to send to. |
| frame | Frame to send. |
Definition at line 160 of file ipc_manager.hpp.
|
inline |
Sets the callback run on the network thread for each accepted client. Set before start().
| handler | Callback receiving the new client socket. |
Definition at line 137 of file ipc_manager.hpp.
|
inline |
Begins accepting clients (called by IPCManager::createTCPServer). Network thread / before the io_context runs.
Definition at line 140 of file ipc_manager.hpp.
|
inlineprivate |
Reads one frame (prefix, then header + payload) from the client and hands it to IPCManager::queueMessage, then reads the next. A read error or malformed prefix closes the session.
| session | Client to read from. |
Definition at line 1281 of file ipc_manager.hpp.
|
inline |
Stops accepting, closes every client, drops pending writes and asks the owning IPCManager to remove this server. Network thread only. Idempotent.
Definition at line 1437 of file ipc_manager.hpp.
|
inlineprivate |
Stops the server once all locally queued writes have completed.
Definition at line 1401 of file ipc_manager.hpp.
Appends a frame to the session's write queue and starts writing if none is in flight.
| session | Client to write to. |
| frame | Frame to send. |
Definition at line 1350 of file ipc_manager.hpp.
|
inlineprivate |
Writes the frame at the front of the session's queue; on completion pops it and writes the next. Only one write is in flight per socket.
| session | Client to write to. |
Definition at line 1361 of file ipc_manager.hpp.
|
private |
False once accepting must stop.
Definition at line 56 of file ipc_manager.hpp.
|
private |
Accepts new client connections.
Definition at line 54 of file ipc_manager.hpp.
|
private |
Optional callback run for each accepted client.
Definition at line 70 of file ipc_manager.hpp.
|
private |
True while existing local socket writes drain before stop.
Definition at line 60 of file ipc_manager.hpp.
|
private |
Limits local socket draining if a write cannot complete.
Definition at line 62 of file ipc_manager.hpp.
|
private |
Not owned; the IPCManager that owns this server and receives its frames.
Definition at line 68 of file ipc_manager.hpp.
|
private |
Definition at line 73 of file ipc_manager.hpp.
|
private |
The port actually bound (resolved by the OS when 0 was requested).
Definition at line 64 of file ipc_manager.hpp.
|
private |
Currently connected client sockets.
Definition at line 66 of file ipc_manager.hpp.
|
private |
True after stop(); all handlers then do nothing.
Definition at line 58 of file ipc_manager.hpp.