Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket.cpp
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
8
9#include <atomic>
10#include <chrono>
11#include <sstream>
12
14
15namespace {
16
20auto generate_unique_id(std::string_view prefix) -> std::string
21{
22 static std::atomic<uint64_t> counter{0};
23 auto now = std::chrono::steady_clock::now().time_since_epoch().count();
24 auto count = counter.fetch_add(1);
25
26 std::ostringstream oss;
27 oss << prefix << "-" << now << "-" << count;
28 return oss.str();
29}
30
31} // namespace
32
33auto create_connection(std::string_view id) -> std::unique_ptr<unified::i_connection>
34{
35 std::string connection_id = id.empty() ? generate_unique_id("ws-conn") : std::string(id);
36 return std::make_unique<unified::adapters::ws_connection_adapter>(connection_id);
37}
38
39auto connect(std::string_view url, std::string_view id)
40 -> std::unique_ptr<unified::i_connection>
41{
42 auto conn = create_connection(id);
43 (void)conn->connect(url);
44 return conn;
45}
46
47auto connect(const unified::endpoint_info& endpoint,
48 std::string_view path,
49 std::string_view id)
50 -> std::unique_ptr<unified::i_connection>
51{
52 std::string connection_id = id.empty() ? generate_unique_id("ws-conn") : std::string(id);
53 auto adapter = std::make_unique<unified::adapters::ws_connection_adapter>(connection_id);
54 adapter->set_path(path);
55 (void)adapter->connect(endpoint);
56 return adapter;
57}
58
59auto create_listener(std::string_view id) -> std::unique_ptr<unified::i_listener>
60{
61 std::string listener_id = id.empty() ? generate_unique_id("ws-listener") : std::string(id);
62 return std::make_unique<unified::adapters::ws_listener_adapter>(listener_id);
63}
64
65auto listen(const unified::endpoint_info& bind_address,
66 std::string_view path,
67 std::string_view id)
68 -> std::unique_ptr<unified::i_listener>
69{
70 std::string listener_id = id.empty() ? generate_unique_id("ws-listener") : std::string(id);
71 auto adapter = std::make_unique<unified::adapters::ws_listener_adapter>(listener_id);
72 adapter->set_path(path);
73 (void)adapter->start(bind_address);
74 return adapter;
75}
76
77auto listen(uint16_t port, std::string_view path, std::string_view id)
78 -> std::unique_ptr<unified::i_listener>
79{
80 return listen(unified::endpoint_info{"0.0.0.0", port}, path, id);
81}
82
83} // namespace kcenon::network::protocol::websocket
auto create_connection(std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates a WebSocket connection (not yet started)
Definition websocket.cpp:33
auto listen(const unified::endpoint_info &bind_address, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a WebSocket listener in one call.
Definition websocket.cpp:65
auto create_listener(std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates a WebSocket listener (not yet listening)
Definition websocket.cpp:59
auto connect(std::string_view url, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and starts a WebSocket connection in one call.
Definition websocket.cpp:39
Network endpoint information (host/port or URL)
Definition types.h:56
WebSocket protocol connection factory functions.