dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Server::TcpServerForSession< T > Class Template Reference

Convenience TcpServer that instantiates a fixed TcpSession type. More...

#include <dfx-server/TcpServerForSession.hpp>

Inheritance diagram for dfx::Server::TcpServerForSession< T >:
[legend]
Collaboration diagram for dfx::Server::TcpServerForSession< T >:
[legend]

Public Member Functions

 TcpServer (Options options, FdWatch::Poller &poller)
 Construct a TCP server bound to a poller.
Public Member Functions inherited from dfx::Server::TcpServer
 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

TcpSessionPtr makeSession (std::string host, std::string serv, TcpServer &server, std::chrono::milliseconds initialTimeout) override
 Create a new session of type T for an accepted connection.

Additional Inherited Members

Public Types inherited from dfx::Server::TcpServer
using NewSessionCallback = std::move_only_function<void (TcpSessionPtr)>
 Callback invoked when a new session is accepted and created.

Detailed Description

template<DerivedFromTcpSession T>
class dfx::Server::TcpServerForSession< T >

Convenience TcpServer that instantiates a fixed TcpSession type.

Template Parameters
TConcrete session type. Must derive from TcpSession.

TcpServerForSession is a small helper that removes boilerplate for the common case where a server always creates the same session type.

It simply overrides TcpServer::makeSession() and returns: std::make_shared<T>(host, serv, server, initialTimeout).

This is useful when:

  • you don’t need a custom server subclass just to implement the factory,
  • you want the session type to be selected at compile time,
  • you want to keep the server implementation header-only.
Constructor forwarding
This class inherits all constructors from TcpServer via using TcpServer::TcpServer;. You construct it exactly like a TcpServer, providing TcpServer::Options and a FdWatch::Poller reference.
Note
The session type T must be constructible with the signature: T(std::string host, std::string serv, TcpServer & server, std::chrono::milliseconds initialTimeout).

Member Function Documentation

◆ makeSession()

template<DerivedFromTcpSession T>
TcpSessionPtr dfx::Server::TcpServerForSession< T >::makeSession ( std::string host,
std::string serv,
TcpServer & server,
std::chrono::milliseconds initialTimeout )
inlineoverrideprotectedvirtual

Create a new session of type T for an accepted connection.

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
Shared pointer to the created session.

Implements dfx::Server::TcpServer.

◆ TcpServer()

template<DerivedFromTcpSession T>
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.

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