9#ifndef CPPSERVER_ASIO_UDP_SERVER_H
10#define CPPSERVER_ASIO_UDP_SERVER_H
14#include "system/uuid.h"
25class UDPServer :
public std::enable_shared_from_this<UDPServer>
105 virtual bool Start();
153 virtual size_t Multicast(
const void*
buffer,
size_t size,
const CppCommon::Timespan& timeout);
200 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint,
const void*
buffer,
size_t size,
const CppCommon::Timespan& timeout);
250 virtual size_t Receive(asio::ip::udp::endpoint&
endpoint,
void*
buffer,
size_t size,
const CppCommon::Timespan& timeout);
258 virtual std::string
Receive(asio::ip::udp::endpoint&
endpoint,
size_t size,
const CppCommon::Timespan& timeout);
348 std::shared_ptr<Service> _service;
350 std::shared_ptr<asio::io_service> _io_service;
352 asio::io_service::strand _strand;
353 bool _strand_required;
355 std::string _address;
358 asio::ip::udp::endpoint _endpoint;
359 asio::ip::udp::socket _socket;
360 std::atomic<bool> _started;
368 asio::ip::udp::endpoint _multicast_endpoint;
369 asio::ip::udp::endpoint _receive_endpoint;
370 asio::ip::udp::endpoint _send_endpoint;
373 size_t _receive_buffer_limit{0};
374 std::vector<uint8_t> _receive_buffer;
375 HandlerStorage _receive_storage;
378 size_t _send_buffer_limit{0};
379 std::vector<uint8_t> _send_buffer;
380 HandlerStorage _send_storage;
382 bool _option_reuse_address;
383 bool _option_reuse_port;
392 void SendError(std::error_code ec);
Asio allocate handler wrapper.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
virtual bool Restart()
Restart the server.
int port() const noexcept
Get the server port number.
UDPServer(UDPServer &&)=delete
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the server.
virtual void onStarted()
Handle server started notification.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
virtual ~UDPServer()=default
bool option_reuse_port() const noexcept
Get the option: reuse port.
virtual size_t Multicast(std::string_view text, const CppCommon::Timespan &timeout)
Multicast text to the prepared mulicast endpoint with timeout (synchronous)
const std::string & address() const noexcept
Get the server address.
UDPServer(const UDPServer &)=delete
virtual void ReceiveAsync()
Receive datagram from the client (asynchronous)
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text into the given endpoint (asynchronous)
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
virtual size_t Multicast(const void *buffer, size_t size)
Multicast datagram to the prepared mulicast endpoint (synchronous)
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
UDPServer & operator=(UDPServer &&)=delete
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
bool option_reuse_address() const noexcept
Get the option: reuse address.
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
virtual bool MulticastAsync(const void *buffer, size_t size)
Multicast datagram to the prepared mulicast endpoint (asynchronous)
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
virtual bool Start()
Start the server.
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the server.
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)
virtual bool MulticastAsync(std::string_view text)
Multicast text to the prepared mulicast endpoint (asynchronous)
size_t option_send_buffer_size() const
Get the option: send buffer size.
const CppCommon::UUID & id() const noexcept
Get the server Id.
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
bool IsStarted() const noexcept
Is the server started?
virtual void onSent(const asio::ip::udp::endpoint &endpoint, size_t sent)
Handle datagram sent notification.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text into the given endpoint (synchronous)
virtual bool Stop()
Stop the server.
virtual size_t Receive(asio::ip::udp::endpoint &endpoint, void *buffer, size_t size)
Receive datagram from the given endpoint (synchronous)
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
asio::ip::udp::endpoint & multicast_endpoint() noexcept
Get the server multicast endpoint.
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Send datagram into the given endpoint (asynchronous)
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
asio::ip::udp::endpoint & endpoint() noexcept
Get the server endpoint.
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
UDPServer & operator=(const UDPServer &)=delete
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Send datagram into the given endpoint (synchronous)
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
virtual size_t Multicast(std::string_view text)
Multicast text to the prepared mulicast endpoint (synchronous)
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
virtual void onStopped()
Handle server stopped notification.
InternetProtocol
Internet protocol.
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.