dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
UnixServer.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 <chrono>
13#include <mutex>
14#include <unordered_map>
15
16// Project includes
17#include "UnixSession.hpp"
19#include <dfx-fdwatch/PollerFd.hpp>
20#include <dfx-fdwatch/Timer.hpp>
22#include <dfx-utilities/FileSystem.hpp>
23
24namespace dfx::FdWatch
25{
26class Poller;
27} // !namespace dfx::FdWatch
28
29namespace dfx::Utils
30{
31class SystemConfig;
32} // !namespace dfx::Utils
33
34namespace dfx::Server
35{
36class UnixRouter;
37
93{
94public:
99 struct Options
100 {
101 fs::path socket;
102 uint32_t permission = 0;
103 uint32_t backlog = 0;
104
109 std::chrono::milliseconds inactivityTimeout{0};
110
115 std::chrono::milliseconds timeoutAccuracy{0};
116
120
126 };
127
128public:
139 UnixServer(Utils::SystemConfig & sysConfig, FdWatch::Poller & poller, UnixRouter & router, std::string const & defaultSocketPath = "/var/run/dfx.sock");
140
143
146
151 void listen();
152
160 void stop() noexcept;
161
163 bool isListening() const noexcept { return _serverSocket.isRegistered(); }
164
172
173private:
174 void _onTimerTimeout();
175 void _serverSocketReadyRead(FdWatch::EventTriggers events);
176 void _onSessionReadyRead(UnixSessionPtr session, FdWatch::EventTriggers events);
177
178private:
179 FdWatch::Poller & _poller;
180 UnixRouter & _router;
181 Options _options;
182 bool _isInit = false;
183 FdWatch::PollerFd _serverSocket;
184 FdWatch::Timer _timer;
185
186private:
187 std::mutex _sessionMutex;
188 std::unordered_map<FdWatch::BorrowedFd, UnixSessionPtr> _sessions;
189};
190} // !namespace dfx::Server
Convenience macros to explicitly control copy and move semantics.
Event interest and trigger flags for file-descriptor watching (Linux/epoll).
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
JSON command router for the Unix domain socket control protocol.
Definition UnixRouter.hpp:78
void stop() noexcept
Stop listening and remove the socket file from the filesystem.
~UnixServer()
Stop listening and release resources.
void listen()
Start listening on the configured unix socket path.
DISABLE_COPY_AND_MOVE(UnixServer)
UnixServer are not copyable and not movable.
UnixServer(Utils::SystemConfig &sysConfig, FdWatch::Poller &poller, UnixRouter &router, std::string const &defaultSocketPath="/var/run/dfx.sock")
Construct a Unix control server and register SystemConfig entries.
bool isListening() const noexcept
Whether the server socket is currently registered and listening.
Definition UnixServer.hpp:163
void terminateSession(UnixSessionPtr session)
Terminate and forget a session.
System configuration registry with typed values, entry metadata, and change callbacks.
Definition SystemConfig.hpp:81
Definition SocketClient.hpp:23
Definition BaseCommandHandler.hpp:16
std::shared_ptr< UnixSession > UnixSessionPtr
Shared ownership pointer type for sessions.
Definition BaseCommandHandler.hpp:20
Definition SystemConfigCommandHandler.hpp:15
Server configuration values (mostly controlled via Utils::SystemConfig).
Definition UnixServer.hpp:100
std::chrono::milliseconds inactivityTimeout
Inactivity timeout for sessions.
Definition UnixServer.hpp:109
uint32_t backlog
Backlog value used for listen(2).
Definition UnixServer.hpp:103
uint64_t consecutiveErrCountBeforeClose
Maximum number of consecutive error responses sent to a client before closing.
Definition UnixServer.hpp:125
uint32_t permission
Permission mask applied to the socket file (octal, e.g. 0600). Must be <= 0777.
Definition UnixServer.hpp:102
uint64_t errCountBeforeClose
Maximum number of total error responses sent to a client before closing. Value 0 disables this limit.
Definition UnixServer.hpp:119
std::chrono::milliseconds timeoutAccuracy
Periodic timer interval for timeout/error checks.
Definition UnixServer.hpp:115
fs::path socket
Path to the unix domain socket file.
Definition UnixServer.hpp:101