dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Server::TcpServer Class Referenceabstract

Asynchronous TCP server integrated with a FdWatch::Poller. More...

#include <dfx-server/TcpServer.hpp>

Inheritance diagram for dfx::Server::TcpServer:
[legend]

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.

Detailed Description

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:

  • it handles the listening socket readiness callback (accept),
  • it creates sessions via the virtual factory makeSession(),
  • it keeps track of sessions so they can be iterated or terminated,
  • it runs a periodic timer used for inactivity/timeout handling.

Concrete servers decide what session type to create by overriding makeSession().

Event loop integration
The server does not create its own thread. It is driven by the provided poller:
Session timeout model
Two timing knobs are exposed via Options :

The server typically uses the timer callback to ask sessions whether they should be closed due to inactivity (implementation-defined inside sessions).

Thread-safety
The session registry is protected by a std::recursive_mutex. This allows:
  • terminating a session while iterating over sessions,
  • callbacks that re-enter server APIs (typical with networking stacks).

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.

Member Typedef Documentation

◆ NewSessionCallback

using dfx::Server::TcpServer::NewSessionCallback = std::move_only_function<void (TcpSessionPtr)>

Callback invoked when a new session is accepted and created.

Constructor & Destructor Documentation

◆ TcpServer()

dfx::Server::TcpServer::TcpServer ( Options options,
FdWatch::Poller & poller )

Construct a TCP server bound to a poller.

Parameters
optionsServer configuration (bind address, port, timeouts).
pollerPoller used to watch the listening socket and to run the timer.

◆ ~TcpServer()

virtual dfx::Server::TcpServer::~TcpServer ( )
virtual

Stop the server and destroy all sessions.

  • deregister the listening socket from the poller,
  • stop the timer,
  • close and release sessions.

Member Function Documentation

◆ DISABLE_COPY_AND_MOVE()

dfx::Server::TcpServer::DISABLE_COPY_AND_MOVE ( TcpServer )

TcpServer are not copyable and not movable.

◆ forEachSession()

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.

Parameters
fFunction invoked for each session.
Warning
Avoid long blocking operations in the callback; it may delay accept/timeout handling.

◆ isListening()

bool dfx::Server::TcpServer::isListening ( ) const
inlinenoexcept

Whether the server socket is currently registered to the poller.

◆ listen()

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.

◆ makeSession()

virtual TcpSessionPtr dfx::Server::TcpServer::makeSession ( std::string host,
std::string serv,
TcpServer & server,
std::chrono::milliseconds initialTimeout )
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.).

Parameters
hostRemote host string (typically from getnameinfo / accept).
servRemote service/port string.
serverReference to the owning server.
initialTimeoutInitial inactivity timeout for the new session.
Returns
A newly created session instance.

Implemented in dfx::Server::TcpServerForSession< T >, and dfx::Server::TcpServerForSession< WriteOnlySession >.

◆ setNewSessionCallback()

void dfx::Server::TcpServer::setNewSessionCallback ( NewSessionCallback cb)
inline

Set a callback invoked whenever a new session is accepted.

The callback is optional. If unset, sessions are still created and managed.

Parameters
cbCallback called with the newly created session.

◆ stop()

void dfx::Server::TcpServer::stop ( )
noexcept

Stop listening and detach from the poller. This function is safe to call multiple times.

◆ terminateSession()

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.

Parameters
sessionSession to terminate.

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