Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
i_websocket_server.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
9
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <system_error>
16#include <vector>
17
19
21{
22
31 {
32 public:
38 [[nodiscard]] virtual auto send_text(std::string&& message) -> VoidResult = 0;
39
45 [[nodiscard]] virtual auto send_binary(std::vector<uint8_t>&& data) -> VoidResult = 0;
46
52 virtual auto close(uint16_t code, std::string_view reason = "") -> void = 0;
53
58 [[nodiscard]] virtual auto path() const -> std::string_view = 0;
59 };
60
80 {
81 public:
83 using connection_callback_t = std::function<void(std::shared_ptr<i_websocket_session>)>;
84
86 using disconnection_callback_t = std::function<void(
87 std::string_view session_id,
88 uint16_t code,
89 std::string_view reason)>;
90
92 using text_callback_t = std::function<void(std::string_view, const std::string&)>;
93
95 using binary_callback_t = std::function<void(std::string_view, const std::vector<uint8_t>&)>;
96
98 using error_callback_t = std::function<void(std::string_view, std::error_code)>;
99
113 [[nodiscard]] virtual auto start(uint16_t port) -> VoidResult = 0;
114
123 [[nodiscard]] virtual auto stop() -> VoidResult = 0;
124
129 [[nodiscard]] virtual auto connection_count() const -> size_t = 0;
130
135 virtual auto set_connection_callback(connection_callback_t callback) -> void = 0;
136
141 virtual auto set_disconnection_callback(disconnection_callback_t callback) -> void = 0;
142
147 virtual auto set_text_callback(text_callback_t callback) -> void = 0;
148
153 virtual auto set_binary_callback(binary_callback_t callback) -> void = 0;
154
159 virtual auto set_error_callback(error_callback_t callback) -> void = 0;
160 };
161
162} // namespace kcenon::network::interfaces
Base interface for all network components.
Interface for a single client session on the server side.
Definition i_session.h:49
Interface for WebSocket server components.
virtual auto start(uint16_t port) -> VoidResult=0
Starts the WebSocket server on the specified port.
std::function< void(std::string_view, const std::vector< uint8_t > &)> binary_callback_t
Callback type for binary messages (session_id, data)
std::function< void(std::string_view, std::error_code)> error_callback_t
Callback type for errors (session_id, error)
std::function< void(std::string_view, const std::string &)> text_callback_t
Callback type for text messages (session_id, message)
virtual auto stop() -> VoidResult=0
Stops the WebSocket server.
std::function< void( std::string_view session_id, uint16_t code, std::string_view reason)> disconnection_callback_t
Callback type for disconnections (with close code and reason)
std::function< void(std::shared_ptr< i_websocket_session >)> connection_callback_t
Callback type for new connections.
virtual auto connection_count() const -> size_t=0
Gets the number of active WebSocket connections.
Interface for a WebSocket session on the server side.
virtual auto send_binary(std::vector< uint8_t > &&data) -> VoidResult=0
Sends a binary message to the client.
virtual auto send_text(std::string &&message) -> VoidResult=0
Sends a text message to the client.
virtual auto close(uint16_t code, std::string_view reason="") -> void=0
Closes the WebSocket connection gracefully.
virtual auto path() const -> std::string_view=0
Gets the requested path from the handshake.
Base interface for all network components.
Session interface representing an active client-server connection.
Network-specific error and result type definitions.