Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocol::udp Namespace Reference

Functions

auto create_connection (std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates a UDP connection (not yet started)
 
auto connect (const unified::endpoint_info &endpoint, std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates and starts a UDP connection in one call.
 
auto connect (std::string_view url, std::string_view id="") -> std::unique_ptr< unified::i_connection >
 Creates and starts a UDP connection using URL format.
 
auto create_listener (std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates a UDP listener (not yet listening)
 
auto listen (const unified::endpoint_info &bind_address, std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates and starts a UDP listener in one call.
 
auto listen (uint16_t port, std::string_view id="") -> std::unique_ptr< unified::i_listener >
 Creates and starts a UDP listener on a specific port.
 

Function Documentation

◆ connect() [1/2]

auto kcenon::network::protocol::udp::connect ( const unified::endpoint_info & endpoint,
std::string_view id = "" ) -> std::unique_ptr<unified::i_connection>
nodiscard

Creates and starts a UDP connection in one call.

Parameters
endpointThe target endpoint to send datagrams to
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance (running)

This is a convenience function that creates a UDP connection and immediately starts it with the specified target endpoint.

Usage Example

auto conn = protocol::udp::connect({"localhost", 5555});
conn->set_callbacks({
.on_data = [](std::span<const std::byte> data) { }
});
// UDP client is already running
auto connect(const unified::endpoint_info &endpoint, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and starts a UDP connection in one call.
Definition udp.cpp:39

Definition at line 39 of file udp.cpp.

41{
42 auto conn = create_connection(id);
43 // Initiate "connection" (for UDP, this starts the client with target endpoint)
44 (void)conn->connect(endpoint);
45 return conn;
46}

References create_connection().

Here is the call graph for this function:

◆ connect() [2/2]

auto kcenon::network::protocol::udp::connect ( std::string_view url,
std::string_view id = "" ) -> std::unique_ptr<unified::i_connection>
nodiscard

Creates and starts a UDP connection using URL format.

Parameters
urlThe URL to connect to (format: "udp://host:port" or "host:port")
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance (running)

Definition at line 48 of file udp.cpp.

50{
51 auto conn = create_connection(id);
52 (void)conn->connect(url);
53 return conn;
54}

References create_connection().

Here is the call graph for this function:

◆ create_connection()

auto kcenon::network::protocol::udp::create_connection ( std::string_view id = "") -> std::unique_ptr<unified::i_connection>
nodiscard

Creates a UDP connection (not yet started)

Parameters
idOptional unique identifier for the connection
Returns
Unique pointer to an i_connection instance

The returned connection is not started. Call connect() to set the target endpoint and start the UDP client.

UDP Semantics

Unlike TCP, UDP is connectionless. The "connection" object manages a UDP socket that sends datagrams to a configured target endpoint.

  • connect() sets the target and starts the client
  • is_connected() returns true while the client is running
  • send() sends datagrams to the target endpoint

Usage Example

auto conn = protocol::udp::create_connection("my-udp-client");
conn->set_callbacks({
.on_connected = []() { std::cout << "Started!\n"; },
.on_data = [](std::span<const std::byte> data) {
// Handle received datagram
}
});
conn->connect({"localhost", 5555});
auto create_connection(std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates a UDP connection (not yet started)
Definition udp.cpp:33

Definition at line 33 of file udp.cpp.

34{
35 std::string connection_id = id.empty() ? generate_unique_id("udp-conn") : std::string(id);
36 return std::make_unique<unified::adapters::udp_connection_adapter>(connection_id);
37}

Referenced by connect(), and connect().

Here is the caller graph for this function:

◆ create_listener()

auto kcenon::network::protocol::udp::create_listener ( std::string_view id = "") -> std::unique_ptr<unified::i_listener>
nodiscard

Creates a UDP listener (not yet listening)

Parameters
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance

The returned listener is not listening. Call start() to begin receiving datagrams.

UDP Semantics

Unlike TCP, UDP servers don't accept connections. Instead, they receive datagrams from any sender. The listener tracks unique sender endpoints as virtual "connections" for convenience.

  • on_accept is called when a new sender endpoint is seen
  • connection_id is formatted as "address:port"
  • send_to() sends datagrams back to specific endpoints

Usage Example

auto listener = protocol::udp::create_listener("my-udp-server");
listener->set_callbacks({
.on_accept = [](std::string_view conn_id) {
std::cout << "New endpoint: " << conn_id << "\n";
},
.on_data = [](std::string_view conn_id, std::span<const std::byte> data) {
// Handle received datagram from conn_id
}
});
listener->start(5555);
auto create_listener(std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates a UDP listener (not yet listening)
Definition udp.cpp:56

Definition at line 56 of file udp.cpp.

57{
58 std::string listener_id = id.empty() ? generate_unique_id("udp-listener") : std::string(id);
59 return std::make_unique<unified::adapters::udp_listener_adapter>(listener_id);
60}

Referenced by listen().

Here is the caller graph for this function:

◆ listen() [1/2]

auto kcenon::network::protocol::udp::listen ( const unified::endpoint_info & bind_address,
std::string_view id = "" ) -> std::unique_ptr<unified::i_listener>
nodiscard

Creates and starts a UDP listener in one call.

Parameters
bind_addressThe local address to bind to
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance (listening)

This is a convenience function that creates a listener and immediately starts listening on the specified address.

Usage Example

auto listener = protocol::udp::listen({"0.0.0.0", 5555});
listener->set_callbacks({
.on_data = [](std::string_view conn_id, std::span<const std::byte> data) { }
});
// Listener is already receiving datagrams
auto listen(const unified::endpoint_info &bind_address, std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a UDP listener in one call.
Definition udp.cpp:62

Definition at line 62 of file udp.cpp.

64{
65 auto listener = create_listener(id);
66 (void)listener->start(bind_address);
67 return listener;
68}

References create_listener().

Referenced by listen().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ listen() [2/2]

auto kcenon::network::protocol::udp::listen ( uint16_t port,
std::string_view id = "" ) -> std::unique_ptr<unified::i_listener>
nodiscard

Creates and starts a UDP listener on a specific port.

Parameters
portThe port number to listen on
idOptional unique identifier for the listener
Returns
Unique pointer to an i_listener instance (listening)

Convenience overload that binds to all interfaces (0.0.0.0).

Definition at line 70 of file udp.cpp.

71{
72 return listen(unified::endpoint_info{"0.0.0.0", port}, id);
73}
Network endpoint information (host/port or URL)
Definition types.h:56

References listen().

Here is the call graph for this function: