|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
Adapter to use function callbacks with the observer pattern. More...
#include <connection_observer.h>


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_ |
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.
Thread-safe for callback invocation. Setting callbacks should be done before starting the client.
Definition at line 165 of file connection_observer.h.
| using kcenon::network::interfaces::callback_adapter::connected_fn = std::function<void()> |
Callback type for connection established.
Definition at line 171 of file connection_observer.h.
| 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.
| 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.
| 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.
|
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.
References connected_callback_.
|
inline |
Sets the callback for connection established.
| fn | The callback function. |
Definition at line 193 of file connection_observer.h.
References connected_callback_.
|
inline |
Sets the callback for disconnection.
| fn | The callback function. |
Definition at line 204 of file connection_observer.h.
References disconnected_callback_.
|
inlineoverridevirtual |
Called when the connection is closed.
| reason | Optional reason for the disconnection. |
The reason may be empty for normal disconnections.
Implements kcenon::network::interfaces::connection_observer.
Definition at line 238 of file connection_observer.h.
References disconnected_callback_.
|
inline |
Sets the callback for errors.
| fn | The callback function. |
Definition at line 215 of file connection_observer.h.
References error_callback_.
|
inlineoverridevirtual |
Called when an error occurs.
| ec | The error code describing the error. |
Implements kcenon::network::interfaces::connection_observer.
Definition at line 246 of file connection_observer.h.
References error_callback_.
|
inline |
Sets the callback for received data.
| fn | The callback function. |
Definition at line 182 of file connection_observer.h.
References receive_callback_.
|
inlineoverridevirtual |
Called when data is received from the server.
| data | The received data as a span of bytes. |
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.
References receive_callback_.
|
private |
Definition at line 256 of file connection_observer.h.
Referenced by on_connected(), and on_connected().
|
private |
Definition at line 257 of file connection_observer.h.
Referenced by on_disconnected(), and on_disconnected().
|
private |
Definition at line 258 of file connection_observer.h.
Referenced by on_error(), and on_error().
|
private |
Definition at line 255 of file connection_observer.h.
Referenced by on_receive(), and on_receive().