Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
chat_observer Class Reference

Custom observer that handles all connection events. More...

Inheritance diagram for chat_observer:
Inheritance graph
Collaboration diagram for chat_observer:
Collaboration graph

Public Member Functions

 chat_observer (const std::string &name)
 
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_receive (std::span< const uint8_t > data) -> void override
 Called when data is received from the server.
 
auto on_error (std::error_code ec) -> void override
 Called when an error occurs.
 
auto message_count () const -> int
 
- Public Member Functions inherited from kcenon::network::interfaces::connection_observer
virtual ~connection_observer ()=default
 

Private Attributes

std::string name_
 
int message_count_ = 0
 

Detailed Description

Custom observer that handles all connection events.

This demonstrates the recommended pattern for handling client events. All events go through a single object, making it easy to maintain state.

Examples
observer_pattern.cpp.

Definition at line 51 of file observer_pattern.cpp.

Constructor & Destructor Documentation

◆ chat_observer()

chat_observer::chat_observer ( const std::string & name)
inlineexplicit
Examples
observer_pattern.cpp.

Definition at line 53 of file observer_pattern.cpp.

53: name_(name) {}

Member Function Documentation

◆ message_count()

auto chat_observer::message_count ( ) const -> int
inlinenodiscard
Examples
observer_pattern.cpp.

Definition at line 77 of file observer_pattern.cpp.

77{ return message_count_; }

References message_count_.

◆ on_connected()

auto chat_observer::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.

Examples
observer_pattern.cpp.

Definition at line 55 of file observer_pattern.cpp.

55 {
56 std::cout << "[" << name_ << "] Connected to server." << std::endl;
57 }

References name_.

◆ on_disconnected()

auto chat_observer::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.

Examples
observer_pattern.cpp.

Definition at line 59 of file observer_pattern.cpp.

59 {
60 std::cout << "[" << name_ << "] Disconnected";
61 if (reason.has_value()) {
62 std::cout << " (reason: " << reason.value() << ")";
63 }
64 std::cout << std::endl;
65 }

References name_.

◆ on_error()

auto chat_observer::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.

Examples
observer_pattern.cpp.

Definition at line 73 of file observer_pattern.cpp.

73 {
74 std::cerr << "[" << name_ << "] Error: " << ec.message() << std::endl;
75 }

References name_.

◆ on_receive()

auto chat_observer::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.

Examples
observer_pattern.cpp.

Definition at line 67 of file observer_pattern.cpp.

67 {
68 std::string message(data.begin(), data.end());
69 std::cout << "[" << name_ << "] Received: " << message << std::endl;
71 }

References kcenon::network::message, message_count_, and name_.

Member Data Documentation

◆ message_count_

int chat_observer::message_count_ = 0
private
Examples
observer_pattern.cpp.

Definition at line 81 of file observer_pattern.cpp.

Referenced by message_count(), and on_receive().

◆ name_

std::string chat_observer::name_
private
Examples
observer_pattern.cpp.

Definition at line 80 of file observer_pattern.cpp.

Referenced by on_connected(), on_disconnected(), on_error(), and on_receive().


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