Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::facade::websocket_facade Class Reference

Simplified facade for creating WebSocket clients and servers. More...

#include <websocket_facade.h>

Collaboration diagram for kcenon::network::facade::websocket_facade:
Collaboration graph

Classes

struct  client_config
 Configuration for creating a WebSocket client. More...
 
struct  server_config
 Configuration for creating a WebSocket server. More...
 

Public Member Functions

auto create_client (const client_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_client > >
 Creates a WebSocket client with the specified configuration.
 
auto create_server (const server_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_server > >
 Creates a WebSocket server with the specified configuration.
 

Static Private Member Functions

static auto generate_client_id () -> std::string
 Generates a unique client ID.
 
static auto generate_server_id () -> std::string
 Generates a unique server ID.
 
static auto validate_client_config (const client_config &config) -> VoidResult
 Validates client configuration.
 
static auto validate_server_config (const server_config &config) -> VoidResult
 Validates server configuration.
 

Detailed Description

Simplified facade for creating WebSocket clients and servers.

This facade provides a simple, unified API for creating WebSocket protocol clients and servers, hiding the complexity of underlying implementation from the user.

Design Goals

  • Simplicity: No template parameters or protocol tags
  • Consistency: Same API pattern across all protocol facades
  • Type Safety: Returns unified protocol interfaces (i_protocol_client/i_protocol_server)
  • Zero Cost: No performance overhead compared to direct instantiation

Thread Safety

All methods are thread-safe and can be called concurrently.

Usage Example

using namespace kcenon::network::facade;
// Create WebSocket client
auto client = facade.create_client({
.client_id = "my-ws-client",
.ping_interval = std::chrono::seconds(30)
});
// Create WebSocket server
auto server = facade.create_server({
.port = 8080,
.path = "/ws",
.server_id = "my-ws-server"
});
Simplified facade for creating WebSocket clients and servers.
auto create_server(const server_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_server > >
Creates a WebSocket server with the specified configuration.
auto create_client(const client_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_client > >
Creates a WebSocket client with the specified configuration.
See also
interfaces::i_protocol_client
interfaces::i_protocol_server
Examples
websocket_chat.cpp.

Definition at line 76 of file websocket_facade.h.

Member Function Documentation

◆ create_client()

auto kcenon::network::facade::websocket_facade::create_client ( const client_config & config) const -> Result<std::shared_ptr<interfaces::i_protocol_client>>
nodiscard

Creates a WebSocket client with the specified configuration.

Parameters
configClient configuration.
Returns
Result containing shared pointer to i_protocol_client, or error.

Behavior

  • Creates a WebSocket client adapter wrapping messaging_ws_client
  • Client ID is auto-generated if not provided
  • Default ping interval is 30 seconds
  • Returns unified i_protocol_client interface for protocol-agnostic usage

Protocol-Specific Notes

  • send() sends data as binary WebSocket frames
  • For text messages or WebSocket-specific features, cast to i_websocket_client

Error Conditions

  • Returns error if ping_interval is zero or negative
Examples
websocket_chat.cpp.

Definition at line 68 of file websocket_facade.cpp.

70{
71 auto validation = validate_client_config(config);
72 if (validation.is_err())
73 {
75 validation.error().code, validation.error().message, "websocket_facade");
76 }
77
78 const auto client_id = config.client_id.empty() ? generate_client_id() : config.client_id;
79
80 // Create adapter with ping interval from config
81 // Note: Path must be set by caller via dynamic_cast if non-default path needed
82 return ok(std::shared_ptr<interfaces::i_protocol_client>(
83 std::make_shared<internal::adapters::ws_client_adapter>(
84 client_id, config.ping_interval)));
85}
static auto validate_client_config(const client_config &config) -> VoidResult
Validates client configuration.
static auto generate_client_id() -> std::string
Generates a unique client ID.
tracing_config config
Definition exporters.cpp:29
VoidResult ok()

References config, kcenon::network::error, and kcenon::network::ok().

Referenced by run_client().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_server()

auto kcenon::network::facade::websocket_facade::create_server ( const server_config & config) const -> Result<std::shared_ptr<interfaces::i_protocol_server>>
nodiscard

Creates a WebSocket server with the specified configuration.

Parameters
configServer configuration.
Returns
Result containing shared pointer to i_protocol_server, or error.

Behavior

  • Creates a WebSocket server adapter wrapping messaging_ws_server
  • Server ID is auto-generated if not provided
  • Server must be started manually using start() method
  • Returns unified i_protocol_server interface for protocol-agnostic usage

Protocol-Specific Notes

  • Receive callback delivers binary data via the unified interface
  • For text messages or WebSocket-specific features, cast to i_websocket_server

Error Conditions

  • Returns error if port is 0 or > 65535
  • Returns error if path is empty or does not start with '/'
Examples
websocket_chat.cpp.

Definition at line 87 of file websocket_facade.cpp.

89{
90 auto validation = validate_server_config(config);
91 if (validation.is_err())
92 {
94 validation.error().code, validation.error().message, "websocket_facade");
95 }
96
97 const auto server_id = config.server_id.empty() ? generate_server_id() : config.server_id;
98
99 // Create adapter and configure path
100 auto adapter = std::make_shared<internal::adapters::ws_server_adapter>(server_id);
101 adapter->set_path(config.path);
102
103 return ok(std::shared_ptr<interfaces::i_protocol_server>(adapter));
104}
static auto generate_server_id() -> std::string
Generates a unique server ID.
static auto validate_server_config(const server_config &config) -> VoidResult
Validates server configuration.

References config, kcenon::network::error, and kcenon::network::ok().

Referenced by run_server().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_client_id()

auto kcenon::network::facade::websocket_facade::generate_client_id ( ) -> std::string
staticnodiscardprivate

Generates a unique client ID.

Definition at line 27 of file websocket_facade.cpp.

28{
29 const auto id = g_client_id_counter.fetch_add(1, std::memory_order_relaxed);
30 std::ostringstream oss;
31 oss << "ws_client_" << std::setfill('0') << std::setw(8) << id;
32 return oss.str();
33}

◆ generate_server_id()

auto kcenon::network::facade::websocket_facade::generate_server_id ( ) -> std::string
staticnodiscardprivate

Generates a unique server ID.

Definition at line 35 of file websocket_facade.cpp.

36{
37 const auto id = g_server_id_counter.fetch_add(1, std::memory_order_relaxed);
38 std::ostringstream oss;
39 oss << "ws_server_" << std::setfill('0') << std::setw(8) << id;
40 return oss.str();
41}

◆ validate_client_config()

auto kcenon::network::facade::websocket_facade::validate_client_config ( const client_config & config) -> VoidResult
staticprivate

Validates client configuration.

Definition at line 43 of file websocket_facade.cpp.

44{
45 if (config.ping_interval.count() <= 0)
46 {
47 return error_void(-1, "websocket_facade: ping_interval must be positive", "websocket_facade");
48 }
49
50 return ok();
51}
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")

References config, kcenon::network::error_void(), and kcenon::network::ok().

Here is the call graph for this function:

◆ validate_server_config()

auto kcenon::network::facade::websocket_facade::validate_server_config ( const server_config & config) -> VoidResult
staticprivate

Validates server configuration.

Definition at line 53 of file websocket_facade.cpp.

54{
55 if (config.port == 0 || config.port > 65535)
56 {
57 return error_void(-1, "websocket_facade: port must be between 1 and 65535", "websocket_facade");
58 }
59
60 if (config.path.empty() || config.path[0] != '/')
61 {
62 return error_void(-1, "websocket_facade: path must start with '/'", "websocket_facade");
63 }
64
65 return ok();
66}

References config, kcenon::network::error_void(), and kcenon::network::ok().

Here is the call graph for this function:

The documentation for this class was generated from the following files: