CppServer
1.0.4.0
C++ Server Library
|
UDP server. More...
#include <udp_server.h>
Public Member Functions | |
UDPServer (const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4) | |
Initialize UDP server with a given Asio service and port number. More... | |
UDPServer (const std::shared_ptr< Service > &service, const std::string &address, int port) | |
Initialize UDP server with a given Asio service, server address and port number. More... | |
UDPServer (const std::shared_ptr< Service > &service, const asio::ip::udp::endpoint &endpoint) | |
Initialize UDP server with a given Asio service and endpoint. More... | |
UDPServer (const UDPServer &)=delete | |
UDPServer (UDPServer &&)=delete | |
virtual | ~UDPServer ()=default |
UDPServer & | operator= (const UDPServer &)=delete |
UDPServer & | operator= (UDPServer &&)=delete |
const CppCommon::UUID & | id () const noexcept |
Get the server Id. More... | |
std::shared_ptr< Service > & | service () noexcept |
Get the Asio service. More... | |
std::shared_ptr< asio::io_service > & | io_service () noexcept |
Get the Asio IO service. More... | |
asio::io_service::strand & | strand () noexcept |
Get the Asio service strand for serialized handler execution. More... | |
asio::ip::udp::endpoint & | endpoint () noexcept |
Get the server endpoint. More... | |
asio::ip::udp::endpoint & | multicast_endpoint () noexcept |
Get the server multicast endpoint. More... | |
const std::string & | address () const noexcept |
Get the server address. More... | |
int | port () const noexcept |
Get the server port number. More... | |
uint64_t | bytes_pending () const noexcept |
Get the number of bytes pending sent by the server. More... | |
uint64_t | bytes_sent () const noexcept |
Get the number of bytes sent by the server. More... | |
uint64_t | bytes_received () const noexcept |
Get the number of bytes received by the server. More... | |
uint64_t | datagrams_sent () const noexcept |
Get the number datagrams sent by the server. More... | |
uint64_t | datagrams_received () const noexcept |
Get the number datagrams received by the server. More... | |
bool | option_reuse_address () const noexcept |
Get the option: reuse address. More... | |
bool | option_reuse_port () const noexcept |
Get the option: reuse port. More... | |
size_t | option_receive_buffer_limit () const noexcept |
Get the option: receive buffer limit. More... | |
size_t | option_receive_buffer_size () const |
Get the option: receive buffer size. More... | |
size_t | option_send_buffer_limit () const noexcept |
Get the option: send buffer limit. More... | |
size_t | option_send_buffer_size () const |
Get the option: send buffer size. More... | |
bool | IsStarted () const noexcept |
Is the server started? More... | |
virtual bool | Start () |
Start the server. More... | |
virtual bool | Start (const std::string &multicast_address, int multicast_port) |
Start the server with a given multicast address and port number. More... | |
virtual bool | Start (const asio::ip::udp::endpoint &multicast_endpoint) |
Start the server with a given multicast endpoint. More... | |
virtual bool | Stop () |
Stop the server. More... | |
virtual bool | Restart () |
Restart the server. More... | |
virtual size_t | Multicast (const void *buffer, size_t size) |
Multicast datagram to the prepared mulicast endpoint (synchronous) More... | |
virtual size_t | Multicast (std::string_view text) |
Multicast text to the prepared mulicast endpoint (synchronous) More... | |
virtual size_t | Multicast (const void *buffer, size_t size, const CppCommon::Timespan &timeout) |
Multicast datagram to the prepared mulicast endpoint with timeout (synchronous) More... | |
virtual size_t | Multicast (std::string_view text, const CppCommon::Timespan &timeout) |
Multicast text to the prepared mulicast endpoint with timeout (synchronous) More... | |
virtual bool | MulticastAsync (const void *buffer, size_t size) |
Multicast datagram to the prepared mulicast endpoint (asynchronous) More... | |
virtual bool | MulticastAsync (std::string_view text) |
Multicast text to the prepared mulicast endpoint (asynchronous) More... | |
virtual size_t | Send (const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size) |
Send datagram into the given endpoint (synchronous) More... | |
virtual size_t | Send (const asio::ip::udp::endpoint &endpoint, std::string_view text) |
Send text into the given endpoint (synchronous) More... | |
virtual size_t | Send (const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size, const CppCommon::Timespan &timeout) |
Send datagram into the given endpoint with timeout (synchronous) More... | |
virtual size_t | Send (const asio::ip::udp::endpoint &endpoint, std::string_view text, const CppCommon::Timespan &timeout) |
Send text into the given endpoint with timeout (synchronous) More... | |
virtual bool | SendAsync (const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size) |
Send datagram into the given endpoint (asynchronous) More... | |
virtual bool | SendAsync (const asio::ip::udp::endpoint &endpoint, std::string_view text) |
Send text into the given endpoint (asynchronous) More... | |
virtual size_t | Receive (asio::ip::udp::endpoint &endpoint, void *buffer, size_t size) |
Receive datagram from the given endpoint (synchronous) More... | |
virtual std::string | Receive (asio::ip::udp::endpoint &endpoint, size_t size) |
Receive text from the given endpoint (synchronous) More... | |
virtual size_t | Receive (asio::ip::udp::endpoint &endpoint, void *buffer, size_t size, const CppCommon::Timespan &timeout) |
Receive datagram from the given endpoint with timeout (synchronous) More... | |
virtual std::string | Receive (asio::ip::udp::endpoint &endpoint, size_t size, const CppCommon::Timespan &timeout) |
Receive text from the given endpoint with timeout (synchronous) More... | |
virtual void | ReceiveAsync () |
Receive datagram from the client (asynchronous) More... | |
void | SetupReuseAddress (bool enable) noexcept |
Setup option: reuse address. More... | |
void | SetupReusePort (bool enable) noexcept |
Setup option: reuse port. More... | |
void | SetupReceiveBufferLimit (size_t limit) noexcept |
Setup option: receive buffer limit. More... | |
void | SetupReceiveBufferSize (size_t size) |
Setup option: receive buffer size. More... | |
void | SetupSendBufferLimit (size_t limit) noexcept |
Setup option: send buffer limit. More... | |
void | SetupSendBufferSize (size_t size) |
Setup option: send buffer size. More... | |
Protected Member Functions | |
virtual void | onStarted () |
Handle server started notification. More... | |
virtual void | onStopped () |
Handle server stopped notification. More... | |
virtual void | onReceived (const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size) |
Handle datagram received notification. More... | |
virtual void | onSent (const asio::ip::udp::endpoint &endpoint, size_t sent) |
Handle datagram sent notification. More... | |
virtual void | onError (int error, const std::string &category, const std::string &message) |
Handle error notification. More... | |
UDP server.
UDP server is used to send or multicast datagrams to UDP endpoints.
Thread-safe.
Definition at line 25 of file udp_server.h.
CppServer::Asio::UDPServer::UDPServer | ( | const std::shared_ptr< Service > & | service, |
int | port, | ||
InternetProtocol | protocol = InternetProtocol::IPv4 |
||
) |
Initialize UDP server with a given Asio service and port number.
service | - Asio service |
port | - Port number |
protocol | - Internet protocol type (default is IPv4) |
Definition at line 14 of file udp_server.cpp.
CppServer::Asio::UDPServer::UDPServer | ( | const std::shared_ptr< Service > & | service, |
const std::string & | address, | ||
int | port | ||
) |
Initialize UDP server with a given Asio service, server address and port number.
service | - Asio service |
address | - Server address |
port | - Port number |
Definition at line 49 of file udp_server.cpp.
CppServer::Asio::UDPServer::UDPServer | ( | const std::shared_ptr< Service > & | service, |
const asio::ip::udp::endpoint & | endpoint | ||
) |
Initialize UDP server with a given Asio service and endpoint.
service | - Asio service |
endpoint | - Server UDP endpoint |
Definition at line 77 of file udp_server.cpp.
|
delete |
|
delete |
|
virtualdefault |
|
inlinenoexcept |
Get the server address.
Definition at line 70 of file udp_server.h.
|
inlinenoexcept |
Get the number of bytes pending sent by the server.
Definition at line 75 of file udp_server.h.
|
inlinenoexcept |
Get the number of bytes received by the server.
Definition at line 79 of file udp_server.h.
|
inlinenoexcept |
Get the number of bytes sent by the server.
Definition at line 77 of file udp_server.h.
|
inlinenoexcept |
Get the number datagrams received by the server.
Definition at line 83 of file udp_server.h.
|
inlinenoexcept |
Get the number datagrams sent by the server.
Definition at line 81 of file udp_server.h.
|
inlinenoexcept |
Get the server endpoint.
Definition at line 65 of file udp_server.h.
|
inlinenoexcept |
Get the server Id.
Definition at line 56 of file udp_server.h.
|
inlinenoexcept |
Get the Asio IO service.
Definition at line 61 of file udp_server.h.
|
inlinenoexcept |
Is the server started?
Definition at line 99 of file udp_server.h.
|
virtual |
Multicast datagram to the prepared mulicast endpoint (synchronous)
buffer | - Datagram buffer to multicast |
size | - Datagram buffer size |
Definition at line 237 of file udp_server.cpp.
|
virtual |
Multicast datagram to the prepared mulicast endpoint with timeout (synchronous)
buffer | - Datagram buffer to multicast |
size | - Datagram buffer size |
timeout | - Timeout |
Definition at line 243 of file udp_server.cpp.
|
inlinevirtual |
Multicast text to the prepared mulicast endpoint (synchronous)
text | - Text to multicast |
Definition at line 144 of file udp_server.h.
|
inlinevirtual |
Multicast text to the prepared mulicast endpoint with timeout (synchronous)
text | - Text to multicast |
timeout | - Timeout |
Definition at line 160 of file udp_server.h.
|
inlinenoexcept |
Get the server multicast endpoint.
Definition at line 67 of file udp_server.h.
|
virtual |
Multicast datagram to the prepared mulicast endpoint (asynchronous)
buffer | - Datagram buffer to multicast |
size | - Datagram buffer size |
Definition at line 249 of file udp_server.cpp.
|
inlinevirtual |
Multicast text to the prepared mulicast endpoint (asynchronous)
text | - Text to multicast |
Definition at line 174 of file udp_server.h.
|
inlineprotectedvirtual |
Handle error notification.
error | - Error code |
category | - Error category |
message | - Error message |
Definition at line 342 of file udp_server.h.
|
inlineprotectedvirtual |
Handle datagram received notification.
Notification is called when another datagram was received from some endpoint.
endpoint | - Received endpoint |
buffer | - Received datagram buffer |
size | - Received datagram buffer size |
Definition at line 323 of file udp_server.h.
|
inlineprotectedvirtual |
Handle datagram sent notification.
Notification is called when a datagram was sent to the client.
This handler could be used to send another datagram to the client for instance when the pending size is zero.
endpoint | - Endpoint of sent datagram |
sent | - Size of sent datagram buffer |
Definition at line 334 of file udp_server.h.
|
inlineprotectedvirtual |
Handle server started notification.
Definition at line 310 of file udp_server.h.
|
inlineprotectedvirtual |
Handle server stopped notification.
Definition at line 312 of file udp_server.h.
|
inlinenoexcept |
Get the option: receive buffer limit.
Definition at line 90 of file udp_server.h.
size_t CppServer::Asio::UDPServer::option_receive_buffer_size | ( | ) | const |
Get the option: receive buffer size.
Definition at line 101 of file udp_server.cpp.
|
inlinenoexcept |
Get the option: reuse address.
Definition at line 86 of file udp_server.h.
|
inlinenoexcept |
Get the option: reuse port.
Definition at line 88 of file udp_server.h.
|
inlinenoexcept |
Get the option: send buffer limit.
Definition at line 94 of file udp_server.h.
size_t CppServer::Asio::UDPServer::option_send_buffer_size | ( | ) | const |
Get the option: send buffer size.
Definition at line 108 of file udp_server.cpp.
|
inlinenoexcept |
Get the server port number.
Definition at line 72 of file udp_server.h.
|
virtual |
Receive text from the given endpoint (synchronous)
endpoint | - Endpoint to receive from |
size | - Text size to receive |
Definition at line 459 of file udp_server.cpp.
|
virtual |
Receive text from the given endpoint with timeout (synchronous)
endpoint | - Endpoint to receive from |
size | - Text size to receive |
timeout | - Timeout |
Definition at line 523 of file udp_server.cpp.
|
virtual |
Receive datagram from the given endpoint (synchronous)
endpoint | - Endpoint to receive from |
buffer | - Datagram buffer to receive |
size | - Datagram buffer size to receive |
Definition at line 428 of file udp_server.cpp.
|
virtual |
Receive datagram from the given endpoint with timeout (synchronous)
endpoint | - Endpoint to receive from |
buffer | - Datagram buffer to receive |
size | - Datagram buffer size to receive |
timeout | - Timeout |
Definition at line 466 of file udp_server.cpp.
|
virtual |
Receive datagram from the client (asynchronous)
Definition at line 530 of file udp_server.cpp.
|
virtual |
Restart the server.
Definition at line 226 of file udp_server.cpp.
|
virtual |
Send datagram into the given endpoint (synchronous)
endpoint | - Endpoint to send |
buffer | - Datagram buffer to send |
size | - Datagram buffer size |
Definition at line 255 of file udp_server.cpp.
|
virtual |
Send datagram into the given endpoint with timeout (synchronous)
endpoint | - Endpoint to send |
buffer | - Datagram buffer to send |
size | - Datagram buffer size |
timeout | - Timeout |
Definition at line 288 of file udp_server.cpp.
|
inlinevirtual |
Send text into the given endpoint (synchronous)
endpoint | - Endpoint to send |
text | - Text to send |
Definition at line 190 of file udp_server.h.
|
inlinevirtual |
Send text into the given endpoint with timeout (synchronous)
endpoint | - Endpoint to send |
text | - Text to send |
timeout | - Timeout |
Definition at line 208 of file udp_server.h.
|
virtual |
Send datagram into the given endpoint (asynchronous)
endpoint | - Endpoint to send |
buffer | - Datagram buffer to send |
size | - Datagram buffer size |
Definition at line 349 of file udp_server.cpp.
|
inlinevirtual |
Send text into the given endpoint (asynchronous)
endpoint | - Endpoint to send |
text | - Text to send |
Definition at line 224 of file udp_server.h.
|
inlinenoexcept |
Get the Asio service.
Definition at line 59 of file udp_server.h.
|
inlinenoexcept |
Setup option: receive buffer limit.
The receive operation will fail if the receive buffer limit is met. Default is unlimited.
limit | - Receive buffer limit |
Definition at line 284 of file udp_server.h.
void CppServer::Asio::UDPServer::SetupReceiveBufferSize | ( | size_t | size | ) |
Setup option: receive buffer size.
This option will setup SO_RCVBUF if the OS support this feature.
size | - Receive buffer size |
Definition at line 115 of file udp_server.cpp.
|
inlinenoexcept |
Setup option: reuse address.
This option will enable/disable SO_REUSEADDR if the OS support this feature.
enable | - Enable/disable option |
Definition at line 269 of file udp_server.h.
|
inlinenoexcept |
Setup option: reuse port.
This option will enable/disable SO_REUSEPORT if the OS support this feature.
enable | - Enable/disable option |
Definition at line 276 of file udp_server.h.
|
inlinenoexcept |
Setup option: send buffer limit.
The send operation will fail if the send buffer limit is met. Default is unlimited.
limit | - Send buffer limit |
Definition at line 299 of file udp_server.h.
void CppServer::Asio::UDPServer::SetupSendBufferSize | ( | size_t | size | ) |
Setup option: send buffer size.
This option will setup SO_SNDBUF if the OS support this feature.
size | - Send buffer size |
Definition at line 121 of file udp_server.cpp.
|
virtual |
Start the server.
Definition at line 127 of file udp_server.cpp.
|
virtual |
Start the server with a given multicast endpoint.
multicast_endpoint | - Multicast UDP endpoint |
Definition at line 183 of file udp_server.cpp.
|
virtual |
Start the server with a given multicast address and port number.
multicast_address | - Multicast address |
multicast_port | - Multicast port number |
Definition at line 177 of file udp_server.cpp.
|
virtual |
Stop the server.
Definition at line 189 of file udp_server.cpp.
|
inlinenoexcept |
Get the Asio service strand for serialized handler execution.
Definition at line 63 of file udp_server.h.