Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
tcp.cppm
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
21module;
22
23// =============================================================================
24// Global Module Fragment - Standard Library Headers
25// =============================================================================
26#include <atomic>
27#include <chrono>
28#include <functional>
29#include <future>
30#include <memory>
31#include <mutex>
32#include <optional>
33#include <span>
34#include <string>
35#include <string_view>
36#include <vector>
37
38// Third-party headers
39#include <asio.hpp>
40
41export module kcenon.network:tcp;
42
43import :core;
44
45// =============================================================================
46// TCP Client
47// =============================================================================
48
49export namespace kcenon::network::core {
50
85public:
90 explicit messaging_client(std::string_view client_id);
91
95 virtual ~messaging_client() noexcept;
96
97 // Non-copyable, movable
99 messaging_client& operator=(const messaging_client&) = delete;
101 messaging_client& operator=(messaging_client&&) noexcept;
102
109 bool start_client(std::string_view host, uint16_t port);
110
115
121 bool wait_for_stop(std::chrono::milliseconds timeout = std::chrono::milliseconds(5000));
122
128 bool send_packet(std::span<const uint8_t> data);
129
135 bool send_packet(std::string_view data);
136
141 bool is_connected() const noexcept;
142
147 bool is_running() const noexcept;
148
153 connection_state get_state() const noexcept;
154
159 std::string_view client_id() const noexcept;
160
166
172
178
184
190 void set_auto_reconnect(bool enable, std::chrono::seconds delay = std::chrono::seconds(5));
191
197 void set_heartbeat(bool enable, std::chrono::seconds interval = std::chrono::seconds(30));
198
199private:
200 struct impl;
201 std::unique_ptr<impl> pimpl_;
202};
203
204// =============================================================================
205// TCP Server
206// =============================================================================
207
238public:
242 using client_connected_callback = std::function<void(const std::string& session_id)>;
243
247 using client_disconnected_callback = std::function<void(const std::string& session_id)>;
248
252 using client_data_callback = std::function<void(
253 const std::string& session_id,
254 std::span<const uint8_t> data)>;
255
260 explicit messaging_server(std::string_view server_id);
261
265 virtual ~messaging_server() noexcept;
266
267 // Non-copyable, movable
269 messaging_server& operator=(const messaging_server&) = delete;
271 messaging_server& operator=(messaging_server&&) noexcept;
272
279 bool start_server(uint16_t port, int backlog = 128);
280
288 bool start_server(std::string_view address, uint16_t port, int backlog = 128);
289
293 void stop_server();
294
300 bool wait_for_stop(std::chrono::milliseconds timeout = std::chrono::milliseconds(5000));
301
308 bool send_to(std::string_view session_id, std::span<const uint8_t> data);
309
316 bool send_to(std::string_view session_id, std::string_view data);
317
322 void broadcast(std::span<const uint8_t> data);
323
328 void broadcast(std::string_view data);
329
334 void disconnect(std::string_view session_id);
335
340 bool is_running() const noexcept;
341
346 size_t client_count() const noexcept;
347
352 std::string_view server_id() const noexcept;
353
358 void set_client_connected_callback(client_connected_callback callback);
359
364 void set_client_disconnected_callback(client_disconnected_callback callback);
365
370 void set_client_data_callback(client_data_callback callback);
371
377
378private:
379 struct impl;
380 std::unique_ptr<impl> pimpl_;
381};
382
383// =============================================================================
384// Messaging Session
385// =============================================================================
386
394class messaging_session : public std::enable_shared_from_this<messaging_session> {
395public:
400 explicit messaging_session(std::string session_id);
401
405 virtual ~messaging_session() noexcept;
406
411 const std::string& session_id() const noexcept;
412
417 bool is_active() const noexcept;
418
423 std::string remote_address() const;
424
429 uint16_t remote_port() const;
430
436 bool send(std::span<const uint8_t> data);
437
441 void close();
442
443private:
444 struct impl;
445 std::unique_ptr<impl> pimpl_;
446};
447
448} // namespace kcenon::network::core
A basic TCP client that connects to a remote host, sends/receives data using asynchronous operations,...
Definition tcp.cppm:84
auto client_id() const -> const std::string &
Returns the client identifier.
virtual ~messaging_client() noexcept
Destructor - automatically stops the client if running.
auto wait_for_stop() -> void override
Waits for the client to stop.
void set_disconnection_callback(disconnection_callback callback)
Set disconnection callback.
auto is_connected() const -> bool override
Checks if the client is connected to the server (IProtocolClient interface).
auto start_client(std::string_view host, unsigned short port) -> VoidResult
Starts the client and connects to the specified host and port.
auto stop_client() -> VoidResult
Stops the client and disconnects from the server.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
auto send_packet(std::vector< uint8_t > &&data) -> VoidResult
Sends data to the connected server.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the connected server (IProtocolClient interface).
void set_data_callback(data_callback callback)
Set data received callback.
void set_connection_callback(connection_callback callback)
Set connection callback.
connection_state get_state() const noexcept
Get the current connection state.
messaging_client(std::string_view client_id)
Construct a client with a given client_id.
std::unique_ptr< impl > pimpl_
Definition tcp.cppm:201
auto is_running() const -> bool override
Checks if the client is currently running.
void set_heartbeat(bool enable, std::chrono::seconds interval=std::chrono::seconds(30))
Enable heartbeat mechanism.
void set_auto_reconnect(bool enable, std::chrono::seconds delay=std::chrono::seconds(5))
Enable automatic reconnection.
A server class that manages incoming TCP connections, creating messaging_session instances for each a...
Definition tcp.cppm:237
messaging_server(std::string_view server_id)
Construct a server with a given server_id.
std::function< void(const std::string &session_id)> client_disconnected_callback
Callback type for client disconnection events.
Definition tcp.cppm:247
virtual ~messaging_server() noexcept
Destructor - automatically stops the server if running.
std::function< void(const std::string &session_id)> client_connected_callback
Callback type for client connection events.
Definition tcp.cppm:242
std::function< void( const std::string &session_id, std::span< const uint8_t > data)> client_data_callback
Callback type for client data events.
Definition tcp.cppm:252
Represents a client session on the server.
Definition tcp.cppm:394
messaging_session(std::string session_id)
Construct a session with a unique ID.
virtual ~messaging_session() noexcept
Destructor.
std::function< void()> disconnection_callback
Definition core.cppm:411
std::function< void()> connection_callback
Callback type aliases for messaging.
Definition core.cppm:410
std::function< void(const std::string &)> error_callback
Definition core.cppm:412
connection_state
Connection state enumeration.
Definition core.cppm:385
std::function< void(std::span< const uint8_t >)> data_callback
Definition core.cppm:413