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

Adapter to use function callbacks with the observer pattern. More...

#include <connection_observer.h>

Inheritance diagram for kcenon::network::interfaces::callback_adapter:
Inheritance graph
Collaboration diagram for kcenon::network::interfaces::callback_adapter:
Collaboration graph

Public Types

using receive_fn = std::function<void(std::span<const uint8_t>)>
 Callback type for received data (using span)
 
using connected_fn = std::function<void()>
 Callback type for connection established.
 
using disconnected_fn = std::function<void(std::optional<std::string_view>)>
 Callback type for disconnection.
 
using error_fn = std::function<void(std::error_code)>
 Callback type for errors.
 

Public Member Functions

auto on_receive (receive_fn fn) -> callback_adapter &
 Sets the callback for received data.
 
auto on_connected (connected_fn fn) -> callback_adapter &
 Sets the callback for connection established.
 
auto on_disconnected (disconnected_fn fn) -> callback_adapter &
 Sets the callback for disconnection.
 
auto on_error (error_fn fn) -> callback_adapter &
 Sets the callback for errors.
 
auto on_receive (std::span< const uint8_t > data) -> void override
 Called when data is received from the server.
 
auto on_connected () -> void override
 Called when the connection is established.
 
auto on_disconnected (std::optional< std::string_view > reason) -> void override
 Called when the connection is closed.
 
auto on_error (std::error_code ec) -> void override
 Called when an error occurs.
 
- Public Member Functions inherited from kcenon::network::interfaces::connection_observer
virtual ~connection_observer ()=default
 

Private Attributes

receive_fn receive_callback_
 
connected_fn connected_callback_
 
disconnected_fn disconnected_callback_
 
error_fn error_callback_
 

Detailed Description

Adapter to use function callbacks with the observer pattern.

This class enables gradual migration from the callback-based API to the observer pattern by wrapping std::function callbacks.

Usage

auto adapter = std::make_shared<callback_adapter>();
adapter->on_receive([](std::span<const uint8_t> data) {
// Handle data
}).on_connected([]() {
// Handle connection
}).on_error([](std::error_code ec) {
// Handle error
});
client->set_observer(adapter);
auto on_error(error_fn fn) -> callback_adapter &
Sets the callback for errors.
auto on_connected() -> void override
Called when the connection is established.

Thread Safety

Thread-safe for callback invocation. Setting callbacks should be done before starting the client.

Definition at line 165 of file connection_observer.h.

Member Typedef Documentation

◆ connected_fn

Callback type for connection established.

Definition at line 171 of file connection_observer.h.

◆ disconnected_fn

using kcenon::network::interfaces::callback_adapter::disconnected_fn = std::function<void(std::optional<std::string_view>)>

Callback type for disconnection.

Definition at line 173 of file connection_observer.h.

◆ error_fn

using kcenon::network::interfaces::callback_adapter::error_fn = std::function<void(std::error_code)>

Callback type for errors.

Definition at line 175 of file connection_observer.h.

◆ receive_fn

using kcenon::network::interfaces::callback_adapter::receive_fn = std::function<void(std::span<const uint8_t>)>

Callback type for received data (using span)

Definition at line 169 of file connection_observer.h.

Member Function Documentation

◆ on_connected() [1/2]

auto kcenon::network::interfaces::callback_adapter::on_connected ( ) -> void
inlineoverridevirtual

Called when the connection is established.

This is called after a successful connection to the server.

Implements kcenon::network::interfaces::connection_observer.

Definition at line 230 of file connection_observer.h.

231 {
233 {
235 }
236 }

References connected_callback_.

◆ on_connected() [2/2]

auto kcenon::network::interfaces::callback_adapter::on_connected ( connected_fn fn) -> callback_adapter&
inline

Sets the callback for connection established.

Parameters
fnThe callback function.
Returns
Reference to this adapter for chaining.

Definition at line 193 of file connection_observer.h.

194 {
195 connected_callback_ = std::move(fn);
196 return *this;
197 }

References connected_callback_.

◆ on_disconnected() [1/2]

auto kcenon::network::interfaces::callback_adapter::on_disconnected ( disconnected_fn fn) -> callback_adapter&
inline

Sets the callback for disconnection.

Parameters
fnThe callback function.
Returns
Reference to this adapter for chaining.

Definition at line 204 of file connection_observer.h.

205 {
206 disconnected_callback_ = std::move(fn);
207 return *this;
208 }

References disconnected_callback_.

◆ on_disconnected() [2/2]

auto kcenon::network::interfaces::callback_adapter::on_disconnected ( std::optional< std::string_view > reason) -> void
inlineoverridevirtual

Called when the connection is closed.

Parameters
reasonOptional reason for the disconnection.

Note

The reason may be empty for normal disconnections.

Implements kcenon::network::interfaces::connection_observer.

Definition at line 238 of file connection_observer.h.

239 {
241 {
243 }
244 }

References disconnected_callback_.

◆ on_error() [1/2]

auto kcenon::network::interfaces::callback_adapter::on_error ( error_fn fn) -> callback_adapter&
inline

Sets the callback for errors.

Parameters
fnThe callback function.
Returns
Reference to this adapter for chaining.

Definition at line 215 of file connection_observer.h.

216 {
217 error_callback_ = std::move(fn);
218 return *this;
219 }

References error_callback_.

◆ on_error() [2/2]

auto kcenon::network::interfaces::callback_adapter::on_error ( std::error_code ec) -> void
inlineoverridevirtual

Called when an error occurs.

Parameters
ecThe error code describing the error.

Implements kcenon::network::interfaces::connection_observer.

Definition at line 246 of file connection_observer.h.

247 {
248 if (error_callback_)
249 {
250 error_callback_(ec);
251 }
252 }

References error_callback_.

◆ on_receive() [1/2]

auto kcenon::network::interfaces::callback_adapter::on_receive ( receive_fn fn) -> callback_adapter&
inline

Sets the callback for received data.

Parameters
fnThe callback function.
Returns
Reference to this adapter for chaining.

Definition at line 182 of file connection_observer.h.

183 {
184 receive_callback_ = std::move(fn);
185 return *this;
186 }

References receive_callback_.

◆ on_receive() [2/2]

auto kcenon::network::interfaces::callback_adapter::on_receive ( std::span< const uint8_t > data) -> void
inlineoverridevirtual

Called when data is received from the server.

Parameters
dataThe received data as a span of bytes.

Thread Safety

May be called from I/O threads. Implementation must be thread-safe.

Implements kcenon::network::interfaces::connection_observer.

Definition at line 222 of file connection_observer.h.

223 {
225 {
226 receive_callback_(data);
227 }
228 }

References receive_callback_.

Member Data Documentation

◆ connected_callback_

connected_fn kcenon::network::interfaces::callback_adapter::connected_callback_
private

Definition at line 256 of file connection_observer.h.

Referenced by on_connected(), and on_connected().

◆ disconnected_callback_

disconnected_fn kcenon::network::interfaces::callback_adapter::disconnected_callback_
private

Definition at line 257 of file connection_observer.h.

Referenced by on_disconnected(), and on_disconnected().

◆ error_callback_

error_fn kcenon::network::interfaces::callback_adapter::error_callback_
private

Definition at line 258 of file connection_observer.h.

Referenced by on_error(), and on_error().

◆ receive_callback_

receive_fn kcenon::network::interfaces::callback_adapter::receive_callback_
private

Definition at line 255 of file connection_observer.h.

Referenced by on_receive(), and on_receive().


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