14#include <unordered_map>
17#include "TcpSession.hpp"
18#include <dfx-fdwatch/PollerFd.hpp>
19#include <dfx-fdwatch/Timer.hpp>
126 bool isListening() const noexcept {
return _socket.isRegistered(); }
161 TcpServer & server, std::chrono::milliseconds initialTimeout) = 0;
164 void _onTimerTimeout();
165 void _onServerSocketReadyRead(FdWatch::EventTriggers events);
174 std::recursive_mutex _sessionMutex;
175 std::unordered_map<FdWatch::BorrowedFd, TcpSessionPtr> _sessions;
Convenience macros to explicitly control copy and move semantics.
RAII wrapper for the registration of a FD in a Poller.
Definition PollerFd.hpp:42
Abstract interface for FD-based event polling.
Definition Poller.hpp:37
FD-integrated timer utility (timerfd-backed).
Definition Timer.hpp:52
void terminateSession(TcpSessionPtr session)
Terminate (close and remove) a session currently managed by the server.
void stop() noexcept
Stop listening and detach from the poller. This function is safe to call multiple times.
virtual ~TcpServer()
Stop the server and destroy all sessions.
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.
void listen()
Bind, listen, and register the server socket on the poller.
TcpServer(Options options, FdWatch::Poller &poller)
Construct a TCP server bound to a poller.
void forEachSession(std::move_only_function< void(TcpSessionPtr)> f)
Iterate over all currently active sessions.
void setNewSessionCallback(NewSessionCallback cb)
Set a callback invoked whenever a new session is accepted.
Definition TcpServer.hpp:113
std::move_only_function< void(TcpSessionPtr)> NewSessionCallback
Callback invoked when a new session is accepted and created.
Definition TcpServer.hpp:69
bool isListening() const noexcept
Whether the server socket is currently registered to the poller.
Definition TcpServer.hpp:126
DISABLE_COPY_AND_MOVE(TcpServer)
TcpServer are not copyable and not movable.
Definition SocketClient.hpp:23
Definition BaseCommandHandler.hpp:16
std::shared_ptr< TcpSession > TcpSessionPtr
Shared ownership pointer type for sessions.
Definition TcpSession.hpp:227
TCP server configuration.
Definition TcpServer.hpp:74
std::chrono::milliseconds timeoutAccuracy
Timer resolution used to check timeouts. This is not a hard realtime guarantee; it controls how often...
Definition TcpServer.hpp:89
uint16_t port
Host/interface to bind to (e.g. "127.0.0.1", "0.0.0.0", "::1", ...). TCP port to bind to....
Definition TcpServer.hpp:78
std::chrono::milliseconds inactivityTimeout
Per-session inactivity timeout. Sessions that remain inactive beyond this timeout are eligible for te...
Definition TcpServer.hpp:84
std::string host
Host/interface to bind to (e.g. "127.0.0.1", "0.0.0.0", "::1", ...).
Definition TcpServer.hpp:76
uint32_t backlog
Listen backlog passed to listen(2).
Definition TcpServer.hpp:80