dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
TcpServer.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <functional>
13#include <mutex>
14#include <unordered_map>
15
16// Project includes
17#include "TcpSession.hpp"
18#include <dfx-fdwatch/PollerFd.hpp>
19#include <dfx-fdwatch/Timer.hpp>
21
22namespace dfx::FdWatch
23{
24class Poller;
25} // !namespace dfx::FdWatch
26
27namespace dfx::Server
28{
66{
67public:
69 using NewSessionCallback = std::move_only_function<void (TcpSessionPtr)>;
70
71public:
73 struct Options
74 {
76 std::string host;
78 uint16_t port = 0;
80 uint32_t backlog = 5;
81
84 std::chrono::milliseconds inactivityTimeout{5000};
85
89 std::chrono::milliseconds timeoutAccuracy{50};
90 };
91
92public:
97 TcpServer(Options options, FdWatch::Poller & poller);
98
101
106 virtual ~TcpServer();
107
113 void setNewSessionCallback(NewSessionCallback cb) { _newSessionCb = std::move(cb); }
114
119 void listen();
120
123 void stop() noexcept;
124
126 bool isListening() const noexcept { return _socket.isRegistered(); }
127
128public:
136
146 void forEachSession(std::move_only_function<void (TcpSessionPtr)> f);
147
148protected:
160 virtual TcpSessionPtr makeSession(std::string host, std::string serv,
161 TcpServer & server, std::chrono::milliseconds initialTimeout) = 0;
162
163private:
164 void _onTimerTimeout();
165 void _onServerSocketReadyRead(FdWatch::EventTriggers events);
166
167private:
168 FdWatch::Poller & _poller;
169 Options _options;
170 FdWatch::PollerFd _socket;
171 FdWatch::Timer _timer;
172
173private:
174 std::recursive_mutex _sessionMutex;
175 std::unordered_map<FdWatch::BorrowedFd, TcpSessionPtr> _sessions;
176 NewSessionCallback _newSessionCb;
177};
178} // !namespace dfx::Server
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