Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
socket_concepts.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
28#include <concepts>
29#include <cstdint>
30#include <functional>
31#include <span>
32#include <system_error>
33#include <type_traits>
34#include <vector>
35
37
38// ============================================================================
39// Type traits for socket handler signatures
40// ============================================================================
41
45template <typename F>
47 std::invocable<F, std::error_code, std::size_t>;
48
52template <typename F>
53concept ErrorCompletionHandler = std::invocable<F, std::error_code>;
54
55// ============================================================================
56// Base Socket Concept
57// ============================================================================
58
77template <typename T>
78concept Socket = requires(T& socket) {
79 { socket.close() } -> std::same_as<void>;
80 { socket.is_closed() } -> std::convertible_to<bool>;
81};
82
83// ============================================================================
84// Stream Socket Concept (TCP, Secure TCP)
85// ============================================================================
86
111template <typename T>
112concept StreamSocket = Socket<T> && requires(
113 T& socket, std::vector<uint8_t> data,
114 std::function<void(std::error_code, std::size_t)> send_handler,
115 std::function<void(const std::vector<uint8_t>&)> recv_callback,
116 std::function<void(std::span<const uint8_t>)> recv_view_callback,
117 std::function<void(std::error_code)> error_callback) {
118 // Async send operation
119 { socket.async_send(std::move(data), send_handler) } -> std::same_as<void>;
120
121 // Read loop control
122 { socket.start_read() } -> std::same_as<void>;
123 { socket.stop_read() } -> std::same_as<void>;
124
125 // Callback registration
126 { socket.set_receive_callback(recv_callback) } -> std::same_as<void>;
127 { socket.set_receive_callback_view(recv_view_callback) } -> std::same_as<void>;
128 { socket.set_error_callback(error_callback) } -> std::same_as<void>;
129};
130
131// ============================================================================
132// Datagram Socket Concept (UDP)
133// ============================================================================
134
159template <typename T>
160concept DatagramSocket = Socket<T> && requires(T& socket) {
161 // Receive loop control
162 { socket.start_receive() } -> std::same_as<void>;
163 { socket.stop_receive() } -> std::same_as<void>;
164
165 // Error callback registration
166 {
167 socket.set_error_callback(std::function<void(std::error_code)>{})
168 } -> std::same_as<void>;
169};
170
187template <typename T, typename Endpoint>
189 DatagramSocket<T> && requires(
190 T& socket, std::vector<uint8_t> data, const Endpoint& endpoint,
191 std::function<void(std::error_code, std::size_t)> handler) {
192 { socket.async_send_to(std::move(data), endpoint, handler) } -> std::same_as<void>;
193 };
194
195// ============================================================================
196// Message Socket Concept (WebSocket)
197// ============================================================================
198
220template <typename T>
221concept MessageSocket = requires(
222 T& socket, std::string text, std::vector<uint8_t> binary,
223 std::function<void(std::error_code, std::size_t)> handler) {
224 // Connection state
225 { socket.is_open() } -> std::convertible_to<bool>;
226
227 // Read control
228 { socket.start_read() } -> std::same_as<void>;
229
230 // Error callback
231 {
232 socket.set_error_callback(std::function<void(std::error_code)>{})
233 } -> std::same_as<void>;
234};
235
236// ============================================================================
237// Backpressure-Aware Socket Concept
238// ============================================================================
239
263template <typename T>
265 T& socket, std::vector<uint8_t> data,
266 std::function<void(std::error_code, std::size_t)> handler,
267 std::function<void(bool)> bp_callback) {
268 // Backpressure state
269 { socket.pending_bytes() } -> std::convertible_to<std::size_t>;
270 { socket.is_backpressure_active() } -> std::convertible_to<bool>;
271
272 // Non-blocking send
273 { socket.try_send(std::move(data), handler) } -> std::convertible_to<bool>;
274
275 // Backpressure notification
276 { socket.set_backpressure_callback(bp_callback) } -> std::same_as<void>;
277};
278
279// ============================================================================
280// Socket Metrics Concept
281// ============================================================================
282
299template <typename T>
300concept MetricsAwareSocket = requires(T& socket, const T& const_socket) {
301 { const_socket.metrics() };
302 { socket.reset_metrics() } -> std::same_as<void>;
303};
304
305// ============================================================================
306// Secure Socket Concept
307// ============================================================================
308
329template <typename T>
330concept SecureSocket = StreamSocket<T> && requires(T& socket) {
331 // Note: The actual async_handshake requires SSL-specific types,
332 // so we only check for stream socket capabilities here.
333 // Type-specific handshake validation should be done at usage site.
334 requires true;
335};
336
337// ============================================================================
338// Generic Socket Utilities
339// ============================================================================
340
356template <StreamSocket S>
358{
359public:
360 explicit socket_read_guard(S& socket) : socket_(socket) {}
361 ~socket_read_guard() { socket_.stop_read(); }
362
365
366private:
368};
369
375template <DatagramSocket S>
377{
378public:
379 explicit datagram_receive_guard(S& socket) : socket_(socket) {}
380 ~datagram_receive_guard() { socket_.stop_receive(); }
381
384
385private:
387};
388
389} // namespace kcenon::network::concepts
RAII guard for datagram socket receive loop.
datagram_receive_guard & operator=(const datagram_receive_guard &)=delete
datagram_receive_guard(const datagram_receive_guard &)=delete
socket_read_guard(const socket_read_guard &)=delete
socket_read_guard & operator=(const socket_read_guard &)=delete
Standard completion handler signature for async operations.
Concept for sockets with backpressure control.
Datagram socket with specific endpoint type.
Concept for connectionless datagram sockets (UDP).
Error-only completion handler signature.
Concept for message-oriented sockets (WebSocket).
Concept for sockets with runtime metrics.
Concept for TLS/SSL-enabled sockets.
Base concept for all socket types.
Concept for connected stream sockets (TCP, TLS).
C++20 concepts for compile-time type validation in network_system.