![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
Asynchronous TCP server integrated with a FdWatch::Poller. More...
#include <dfx-server/TcpServer.hpp>
Classes | |
| struct | Options |
| TCP server configuration. More... | |
Public Types | |
| using | NewSessionCallback = std::move_only_function<void (TcpSessionPtr)> |
| Callback invoked when a new session is accepted and created. | |
Public Member Functions | |
| TcpServer (Options options, FdWatch::Poller &poller) | |
| Construct a TCP server bound to a poller. | |
| DISABLE_COPY_AND_MOVE (TcpServer) | |
| TcpServer are not copyable and not movable. | |
| virtual | ~TcpServer () |
| Stop the server and destroy all sessions. | |
| void | setNewSessionCallback (NewSessionCallback cb) |
| Set a callback invoked whenever a new session is accepted. | |
| void | listen () |
| Bind, listen, and register the server socket on the poller. | |
| void | stop () noexcept |
| Stop listening and detach from the poller. This function is safe to call multiple times. | |
| bool | isListening () const noexcept |
| Whether the server socket is currently registered to the poller. | |
| void | terminateSession (TcpSessionPtr session) |
| Terminate (close and remove) a session currently managed by the server. | |
| void | forEachSession (std::move_only_function< void(TcpSessionPtr)> f) |
| Iterate over all currently active sessions. | |
Protected Member Functions | |
| virtual TcpSessionPtr | makeSession (std::string host, std::string serv, TcpServer &server, std::chrono::milliseconds initialTimeout)=0 |
| Factory used to create a new session for an accepted connection. | |
Asynchronous TCP server integrated with a FdWatch::Poller.
TcpServer owns a listening socket registered to a FdWatch::Poller, accepts incoming connections, and manages a set of active TcpSession instances.
This class is an accept loop + session registry:
Concrete servers decide what session type to create by overriding makeSession().
The server typically uses the timer callback to ask sessions whether they should be closed due to inactivity (implementation-defined inside sessions).
This does not mean the whole server is "fully thread-safe" by design; it means the session container is protected against concurrent/re-entrant access. The poller itself usually defines the threading model.
| using dfx::Server::TcpServer::NewSessionCallback = std::move_only_function<void (TcpSessionPtr)> |
Callback invoked when a new session is accepted and created.
| dfx::Server::TcpServer::TcpServer | ( | Options | options, |
| FdWatch::Poller & | poller ) |
Construct a TCP server bound to a poller.
| options | Server configuration (bind address, port, timeouts). |
| poller | Poller used to watch the listening socket and to run the timer. |
|
virtual |
Stop the server and destroy all sessions.
| dfx::Server::TcpServer::DISABLE_COPY_AND_MOVE | ( | TcpServer | ) |
TcpServer are not copyable and not movable.
| void dfx::Server::TcpServer::forEachSession | ( | std::move_only_function< void(TcpSessionPtr)> | f | ) |
Iterate over all currently active sessions.
The iteration is performed under the session registry lock. The callback receives the session shared pointer, so it may safely keep it alive.
| f | Function invoked for each session. |
|
inlinenoexcept |
Whether the server socket is currently registered to the poller.
| void dfx::Server::TcpServer::listen | ( | ) |
Bind, listen, and register the server socket on the poller.
After calling this, isListening() returns true and the server will accept new connections whenever the poller reports readiness.
|
protectedpure virtual |
Factory used to create a new session for an accepted connection.
Derived classes implement this to provide the concrete TcpSession type and to configure it (timeouts, callbacks, poller integration, etc.).
| host | Remote host string (typically from getnameinfo / accept). |
| serv | Remote service/port string. |
| server | Reference to the owning server. |
| initialTimeout | Initial inactivity timeout for the new session. |
Implemented in dfx::Server::TcpServerForSession< T >, and dfx::Server::TcpServerForSession< WriteOnlySession >.
|
inline |
Set a callback invoked whenever a new session is accepted.
The callback is optional. If unset, sessions are still created and managed.
| cb | Callback called with the newly created session. |
|
noexcept |
Stop listening and detach from the poller. This function is safe to call multiple times.
| void dfx::Server::TcpServer::terminateSession | ( | TcpSessionPtr | session | ) |
Terminate (close and remove) a session currently managed by the server.
Removes the session from the internal registry and triggers the session shutdown. Safe to call even if called from within session callbacks.
| session | Session to terminate. |