PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
accept_worker.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
19#ifndef PACS_NETWORK_DETAIL_ACCEPT_WORKER_HPP
20#define PACS_NETWORK_DETAIL_ACCEPT_WORKER_HPP
21
22#include <kcenon/thread/core/thread_base.h>
23#include <kcenon/common/patterns/result.h>
24
25#include <atomic>
26#include <chrono>
27#include <functional>
28#include <memory>
29#include <string>
30
31#ifdef _WIN32
32#include <winsock2.h>
33#include <ws2tcpip.h>
34#pragma comment(lib, "ws2_32.lib")
35#else
36#include <arpa/inet.h>
37#include <netinet/in.h>
38#include <sys/socket.h>
39#include <unistd.h>
40#endif
41
43
44namespace common = kcenon::common;
45
80class accept_worker : public kcenon::thread::thread_base {
81public:
82 // =========================================================================
83 // Type Aliases
84 // =========================================================================
85
86 using result_void = common::VoidResult;
87
90 using connection_callback = std::function<void(uint64_t session_id)>;
91
93 using maintenance_callback = std::function<void()>;
94
95 // =========================================================================
96 // Construction / Destruction
97 // =========================================================================
98
110 explicit accept_worker(
111 uint16_t port,
112 connection_callback on_connection,
113 maintenance_callback on_maintenance = nullptr);
114
120 ~accept_worker() override;
121
122 // Non-copyable, non-movable (owns thread resources)
123 accept_worker(const accept_worker&) = delete;
127
128 // =========================================================================
129 // Configuration
130 // =========================================================================
131
138 void set_max_pending_connections(int backlog);
139
144 [[nodiscard]] uint16_t port() const noexcept;
145
150 [[nodiscard]] int max_pending_connections() const noexcept;
151
152 // =========================================================================
153 // Status
154 // =========================================================================
155
160 [[nodiscard]] bool is_accepting() const noexcept;
161
166 [[nodiscard]] std::string to_string() const override;
167
168protected:
169 // =========================================================================
170 // thread_base Overrides
171 // =========================================================================
172
181 result_void before_start() override;
182
198 result_void do_work() override;
199
208 result_void after_stop() override;
209
219 [[nodiscard]] bool should_continue_work() const override;
220
232 void on_stop_requested() override;
233
234private:
235 // =========================================================================
236 // Private Methods
237 // =========================================================================
238
243 [[nodiscard]] uint64_t next_session_id();
244
245 // =========================================================================
246 // Member Variables
247 // =========================================================================
248
250 uint16_t port_;
251
254
257
259 int backlog_{128};
260
262 std::atomic<uint64_t> session_id_counter_{0};
263
265 std::atomic<bool> accepting_{false};
266
267#ifdef _WIN32
269 SOCKET listen_socket_{INVALID_SOCKET};
270
272 bool wsa_initialized_{false};
273#else
276#endif
277};
278
279} // namespace kcenon::pacs::network::detail
280
281#endif // PACS_NETWORK_DETAIL_ACCEPT_WORKER_HPP
connection_callback on_connection_
Callback for new connections.
int backlog_
Maximum pending connections in listen queue.
result_void after_stop() override
Cleans up resources after the worker thread stops.
result_void do_work() override
Main work routine - checks for incoming connections.
void set_max_pending_connections(int backlog)
Sets the maximum number of pending connections in the listen queue.
int listen_socket_
Listen socket file descriptor (POSIX)
uint64_t next_session_id()
Generates a unique session ID for new connections.
std::function< void()> maintenance_callback
Callback type for periodic maintenance tasks.
void on_stop_requested() override
Called when stop() is requested.
accept_worker & operator=(const accept_worker &)=delete
bool should_continue_work() const override
Determines whether there is pending work that must complete.
std::function< void(uint64_t session_id)> connection_callback
Callback type for new connection events.
accept_worker(const accept_worker &)=delete
result_void before_start() override
Initializes resources before the worker thread starts.
uint16_t port_
TCP port to listen on.
std::atomic< uint64_t > session_id_counter_
Session ID counter for unique connection identification.
maintenance_callback on_maintenance_
Optional callback for maintenance tasks.
uint16_t port() const noexcept
Gets the configured port number.
int max_pending_connections() const noexcept
Gets the current backlog setting.
accept_worker(uint16_t port, connection_callback on_connection, maintenance_callback on_maintenance=nullptr)
Constructs an accept_worker with the specified configuration.
bool is_accepting() const noexcept
Checks if the worker is actively accepting connections.
std::atomic< bool > accepting_
Flag indicating if actively accepting connections.
accept_worker & operator=(accept_worker &&)=delete