Fray Engine
Loading...
Searching...
No Matches
fray::ipc::TCPServer Class Reference

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>

Inheritance diagram for fray::ipc::TCPServer:

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

Detailed Description

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.

Constructor & Destructor Documentation

◆ TCPServer()

fray::ipc::TCPServer::TCPServer ( boost::asio::io_context & ctx,
F_ushort port,
IPCManager * manager )
inline

Binds a listening IPv4 socket on the port. Throws (boost) if the port cannot be bound.

Parameters
ctxThe io_context whose thread will run this server's handlers.
portTCP port to listen on; 0 lets the OS pick a free one (read it back with port()).
managerOwner that receives incoming frames; not owned and must outlive the server.

Definition at line 114 of file ipc_manager.hpp.

◆ ~TCPServer()

fray::ipc::TCPServer::~TCPServer ( )
inline

Logs that the server was destroyed.

Definition at line 128 of file ipc_manager.hpp.

Member Function Documentation

◆ accept()

F_void fray::ipc::TCPServer::accept ( )
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.

◆ broadcast()

F_void fray::ipc::TCPServer::broadcast ( Frame frame)
inline

Queues a message for every connected client. Network thread only; use IPCManager::send elsewhere.

Parameters
frameFrame to write to all clients; ignored once stopped.

Definition at line 1340 of file ipc_manager.hpp.

◆ closeSession()

F_void fray::ipc::TCPServer::closeSession ( Socket session)
inlineprivate

Closes the client socket and forgets its session and pending writes (queued frames are lost).

Parameters
sessionClient to disconnect.

Definition at line 1391 of file ipc_manager.hpp.

◆ drainAndStop()

F_void fray::ipc::TCPServer::drainAndStop ( )
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.

◆ port()

F_ushort fray::ipc::TCPServer::port ( ) const
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.

◆ sendTo()

F_void fray::ipc::TCPServer::sendTo ( Socket session,
Frame frame )
inline

Queues a message for one client. Network thread only. Logs an error and sends nothing if session or frame is null.

Parameters
sessionClient to send to.
frameFrame to send.

Definition at line 160 of file ipc_manager.hpp.

◆ setConnectionHandler()

F_void fray::ipc::TCPServer::setConnectionHandler ( ConnectionHandler handler)
inline

Sets the callback run on the network thread for each accepted client. Set before start().

Parameters
handlerCallback receiving the new client socket.

Definition at line 137 of file ipc_manager.hpp.

◆ start()

F_void fray::ipc::TCPServer::start ( )
inline

Begins accepting clients (called by IPCManager::createTCPServer). Network thread / before the io_context runs.

Definition at line 140 of file ipc_manager.hpp.

◆ startRead()

F_void fray::ipc::TCPServer::startRead ( std::shared_ptr< boost::asio::ip::tcp::socket > session)
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.

Parameters
sessionClient to read from.

Definition at line 1281 of file ipc_manager.hpp.

◆ stop()

F_void fray::ipc::TCPServer::stop ( )
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.

◆ stopIfDrained()

F_void fray::ipc::TCPServer::stopIfDrained ( )
inlineprivate

Stops the server once all locally queued writes have completed.

Definition at line 1401 of file ipc_manager.hpp.

◆ write()

F_void fray::ipc::TCPServer::write ( Socket session,
Frame frame )
inlineprivate

Appends a frame to the session's write queue and starts writing if none is in flight.

Parameters
sessionClient to write to.
frameFrame to send.

Definition at line 1350 of file ipc_manager.hpp.

◆ writeNext()

F_void fray::ipc::TCPServer::writeNext ( std::shared_ptr< boost::asio::ip::tcp::socket > session)
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.

Parameters
sessionClient to write to.

Definition at line 1361 of file ipc_manager.hpp.

Member Data Documentation

◆ _accepting

F_bool fray::ipc::TCPServer::_accepting = true
private

False once accepting must stop.

Definition at line 56 of file ipc_manager.hpp.

◆ _acceptor

boost::asio::ip::tcp::acceptor fray::ipc::TCPServer::_acceptor
private

Accepts new client connections.

Definition at line 54 of file ipc_manager.hpp.

◆ _connectionHandler

ConnectionHandler fray::ipc::TCPServer::_connectionHandler = nullptr
private

Optional callback run for each accepted client.

Definition at line 70 of file ipc_manager.hpp.

◆ _draining

F_bool fray::ipc::TCPServer::_draining = false
private

True while existing local socket writes drain before stop.

Definition at line 60 of file ipc_manager.hpp.

◆ _drainTimer

boost::asio::steady_timer fray::ipc::TCPServer::_drainTimer
private

Limits local socket draining if a write cannot complete.

Definition at line 62 of file ipc_manager.hpp.

◆ _manager

IPCManager* fray::ipc::TCPServer::_manager = nullptr
private

Not owned; the IPCManager that owns this server and receives its frames.

Definition at line 68 of file ipc_manager.hpp.

◆ _outgoing

std::map<std::shared_ptr<boost::asio::ip::tcp::socket>, std::deque<Frame> > fray::ipc::TCPServer::_outgoing
private

Definition at line 73 of file ipc_manager.hpp.

◆ _port

F_ushort fray::ipc::TCPServer::_port = 0
private

The port actually bound (resolved by the OS when 0 was requested).

Definition at line 64 of file ipc_manager.hpp.

◆ _sessions

std::set<std::shared_ptr<boost::asio::ip::tcp::socket> > fray::ipc::TCPServer::_sessions
private

Currently connected client sockets.

Definition at line 66 of file ipc_manager.hpp.

◆ _stopped

F_bool fray::ipc::TCPServer::_stopped = false
private

True after stop(); all handlers then do nothing.

Definition at line 58 of file ipc_manager.hpp.


The documentation for this class was generated from the following file: