Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::interfaces::connection_observer Interface Referenceabstract

Observer interface for client connection events. More...

#include <connection_observer.h>

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

Public Member Functions

virtual ~connection_observer ()=default
 
virtual auto on_receive (std::span< const uint8_t > data) -> void=0
 Called when data is received from the server.
 
virtual auto on_connected () -> void=0
 Called when the connection is established.
 
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.
 

Detailed Description

Observer interface for client connection events.

This interface provides a unified way to handle all connection-related events through the Observer pattern, replacing the individual callback setters (set_receive_callback, set_connected_callback, etc.).

Design Goals

  • Single responsibility: One observer handles all connection events
  • Extensibility: New events can be added without breaking changes
  • Testability: Easy to mock for unit testing

Thread Safety

  • Observer methods may be invoked from I/O threads
  • Implementations must be thread-safe if shared across connections

Usage Example

class my_observer : public connection_observer {
public:
void on_receive(std::span<const uint8_t> data) override {
// Handle received data
}
void on_connected() override {
// Handle connection established
}
void on_disconnected(std::optional<std::string_view> reason) override {
// Handle disconnection
}
void on_error(std::error_code ec) override {
// Handle error
}
};
auto observer = std::make_shared<my_observer>();
client->set_observer(observer);
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.
See also
i_client
callback_adapter
null_connection_observer
Examples
observer_pattern.cpp.

Definition at line 75 of file connection_observer.h.

Constructor & Destructor Documentation

◆ ~connection_observer()

virtual kcenon::network::interfaces::connection_observer::~connection_observer ( )
virtualdefault

Member Function Documentation

◆ on_connected()

virtual auto kcenon::network::interfaces::connection_observer::on_connected ( ) -> void
pure virtual

Called when the connection is established.

This is called after a successful connection to the server.

Implemented in chat_observer, kcenon::network::interfaces::callback_adapter, and kcenon::network::interfaces::null_connection_observer.

◆ on_disconnected()

virtual auto kcenon::network::interfaces::connection_observer::on_disconnected ( std::optional< std::string_view > reason = std::nullopt) -> void
pure virtual

Called when the connection is closed.

Parameters
reasonOptional reason for the disconnection.

Note

The reason may be empty for normal disconnections.

Implemented in chat_observer, kcenon::network::interfaces::callback_adapter, and kcenon::network::interfaces::null_connection_observer.

◆ on_error()

virtual auto kcenon::network::interfaces::connection_observer::on_error ( std::error_code ec) -> void
pure virtual

Called when an error occurs.

Parameters
ecThe error code describing the error.

Implemented in chat_observer, kcenon::network::interfaces::callback_adapter, and kcenon::network::interfaces::null_connection_observer.

◆ on_receive()

virtual auto kcenon::network::interfaces::connection_observer::on_receive ( std::span< const uint8_t > data) -> void
pure virtual

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.

Implemented in chat_observer, kcenon::network::interfaces::callback_adapter, kcenon::network::interfaces::null_connection_observer, and receive_only_observer.


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