9 #ifndef CPPSERVER_ASIO_UDP_SERVER_H
10 #define CPPSERVER_ASIO_UDP_SERVER_H
14 #include "system/uuid.h"
25 class UDPServer :
public std::enable_shared_from_this<UDPServer>
56 const CppCommon::UUID&
id() const noexcept {
return _id; }
59 std::shared_ptr<Service>&
service() noexcept {
return _service; }
61 std::shared_ptr<asio::io_service>&
io_service() noexcept {
return _io_service; }
63 asio::io_service::strand&
strand() noexcept {
return _strand; }
65 asio::ip::udp::endpoint&
endpoint() noexcept {
return _endpoint; }
70 const std::string&
address() const noexcept {
return _address; }
72 int port() const noexcept {
return _port; }
77 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
99 bool IsStarted() const noexcept {
return _started; }
105 virtual bool Start();
113 virtual bool Start(
const std::string& multicast_address,
int multicast_port);
138 virtual size_t Multicast(
const void* buffer,
size_t size);
153 virtual size_t Multicast(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
160 virtual size_t Multicast(std::string_view text,
const CppCommon::Timespan& timeout) {
return Multicast(text.data(), text.size(), timeout); }
183 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size);
190 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint, std::string_view text) {
return Send(
endpoint, text.data(), text.size()); }
200 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
208 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint, std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(
endpoint, text.data(), text.size(), timeout); }
217 virtual bool SendAsync(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size);
233 virtual size_t Receive(asio::ip::udp::endpoint&
endpoint,
void* buffer,
size_t size);
240 virtual std::string
Receive(asio::ip::udp::endpoint&
endpoint,
size_t size);
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);
342 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
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;
362 uint64_t _bytes_sending;
363 uint64_t _bytes_sent;
364 uint64_t _bytes_received;
365 uint64_t _datagrams_sent;
366 uint64_t _datagrams_received;
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);
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
asio::ip::udp::endpoint & endpoint() noexcept
Get the server endpoint.
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.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
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.
asio::ip::udp::endpoint & multicast_endpoint() noexcept
Get the server multicast endpoint.
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)
UDPServer(const UDPServer &)=delete
virtual void ReceiveAsync()
Receive datagram from the client (asynchronous)
UDPServer & operator=(const UDPServer &)=delete
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text into the given endpoint (asynchronous)
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
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.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
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.
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.
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.
const std::string & address() const noexcept
Get the server address.
UDPServer & operator=(UDPServer &&)=delete
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Send datagram into the given endpoint (asynchronous)
UDPServer(const std::shared_ptr< Service > &service, int port, InternetProtocol protocol=InternetProtocol::IPv4)
Initialize UDP server with a given Asio service and port number.
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
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.
const CppCommon::UUID & id() const noexcept
Get the server Id.
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.