Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
connection_observer.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
11#pragma once
12
21#include <cstdint>
22#include <functional>
23#include <memory>
24#include <optional>
25#include <span>
26#include <string_view>
27#include <system_error>
28
30{
31
76 {
77 public:
78 virtual ~connection_observer() = default;
79
87 virtual auto on_receive(std::span<const uint8_t> data) -> void = 0;
88
94 virtual auto on_connected() -> void = 0;
95
103 virtual auto on_disconnected(std::optional<std::string_view> reason = std::nullopt) -> void
104 = 0;
105
110 virtual auto on_error(std::error_code ec) -> void = 0;
111 };
112
132 {
133 public:
134 auto on_receive(std::span<const uint8_t> /*data*/) -> void override {}
135 auto on_connected() -> void override {}
136 auto on_disconnected(std::optional<std::string_view> /*reason*/) -> void override {}
137 auto on_error(std::error_code /*ec*/) -> void override {}
138 };
139
166 {
167 public:
169 using receive_fn = std::function<void(std::span<const uint8_t>)>;
171 using connected_fn = std::function<void()>;
173 using disconnected_fn = std::function<void(std::optional<std::string_view>)>;
175 using error_fn = std::function<void(std::error_code)>;
176
183 {
184 receive_callback_ = std::move(fn);
185 return *this;
186 }
187
194 {
195 connected_callback_ = std::move(fn);
196 return *this;
197 }
198
205 {
206 disconnected_callback_ = std::move(fn);
207 return *this;
208 }
209
216 {
217 error_callback_ = std::move(fn);
218 return *this;
219 }
220
221 // connection_observer implementation
222 auto on_receive(std::span<const uint8_t> data) -> void override
223 {
225 {
226 receive_callback_(data);
227 }
228 }
229
230 auto on_connected() -> void override
231 {
233 {
235 }
236 }
237
238 auto on_disconnected(std::optional<std::string_view> reason) -> void override
239 {
241 {
243 }
244 }
245
246 auto on_error(std::error_code ec) -> void override
247 {
248 if (error_callback_)
249 {
250 error_callback_(ec);
251 }
252 }
253
254 private:
259 };
260
261} // namespace kcenon::network::interfaces
Adapter to use function callbacks with the observer pattern.
auto on_disconnected(std::optional< std::string_view > reason) -> void override
Called when the connection is closed.
std::function< void()> connected_fn
Callback type for connection established.
std::function< void(std::error_code)> error_fn
Callback type for errors.
auto on_error(error_fn fn) -> callback_adapter &
Sets the callback for errors.
auto on_error(std::error_code ec) -> void override
Called when an error occurs.
auto on_disconnected(disconnected_fn fn) -> callback_adapter &
Sets the callback for disconnection.
std::function< void(std::optional< std::string_view >)> disconnected_fn
Callback type for disconnection.
auto on_receive(std::span< const uint8_t > data) -> void override
Called when data is received from the server.
auto on_receive(receive_fn fn) -> callback_adapter &
Sets the callback for received data.
auto on_connected() -> void override
Called when the connection is established.
std::function< void(std::span< const uint8_t >)> receive_fn
Callback type for received data (using span)
auto on_connected(connected_fn fn) -> callback_adapter &
Sets the callback for connection established.
Observer interface for client connection events.
virtual auto on_connected() -> void=0
Called when the connection is established.
virtual auto on_receive(std::span< const uint8_t > data) -> void=0
Called when data is received from the server.
virtual auto on_disconnected(std::optional< std::string_view > reason=std::nullopt) -> void=0
Called when the connection is closed.
virtual auto on_error(std::error_code ec) -> void=0
Called when an error occurs.
No-op implementation of connection_observer.
auto on_disconnected(std::optional< std::string_view >) -> void override
Called when the connection is closed.
auto on_error(std::error_code) -> void override
Called when an error occurs.
auto on_connected() -> void override
Called when the connection is established.
auto on_receive(std::span< const uint8_t >) -> void override
Called when data is received from the server.