9#ifndef CPPSERVER_ASIO_UDP_CLIENT_H
10#define CPPSERVER_ASIO_UDP_CLIENT_H
14#include "system/uuid.h"
15#include "time/timespan.h"
29class UDPClient :
public std::enable_shared_from_this<UDPClient>
60 const CppCommon::UUID&
id() const noexcept {
return _id; }
63 std::shared_ptr<Service>&
service() noexcept {
return _service; }
65 std::shared_ptr<asio::io_context>&
io_context() noexcept {
return _io_context; }
67 asio::io_context::strand&
strand() noexcept {
return _strand; }
69 asio::ip::udp::endpoint&
endpoint() noexcept {
return _endpoint; }
71 asio::ip::udp::socket&
socket() noexcept {
return _socket; }
74 const std::string&
address() const noexcept {
return _address; }
76 const std::string&
scheme() const noexcept {
return _scheme; }
78 int port() const noexcept {
return _port; }
83 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
119 virtual bool Connect(
const std::shared_ptr<UDPResolver>& resolver);
141 virtual bool ConnectAsync(
const std::shared_ptr<UDPResolver>& resolver);
181 virtual size_t Send(
const void* buffer,
size_t size);
187 virtual size_t Send(std::string_view text) {
return Send(text.data(), text.size()); }
195 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size);
202 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint, std::string_view text) {
return Send(
endpoint, text.data(), text.size()); }
211 virtual size_t Send(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
218 virtual size_t Send(std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(text.data(), text.size(), timeout); }
227 virtual size_t Send(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
235 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); }
243 virtual bool SendAsync(
const void* buffer,
size_t size);
257 virtual bool SendAsync(
const asio::ip::udp::endpoint&
endpoint,
const void* buffer,
size_t size);
273 virtual size_t Receive(asio::ip::udp::endpoint&
endpoint,
void* buffer,
size_t size);
280 virtual std::string
Receive(asio::ip::udp::endpoint&
endpoint,
size_t size);
290 virtual size_t Receive(asio::ip::udp::endpoint&
endpoint,
void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
298 virtual std::string
Receive(asio::ip::udp::endpoint&
endpoint,
size_t size,
const CppCommon::Timespan& timeout);
321 void SetupMulticast(
bool enable)
noexcept { _option_reuse_address = enable; _option_multicast = enable; }
402 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
408 std::shared_ptr<Service> _service;
410 std::shared_ptr<asio::io_context> _io_context;
412 asio::io_context::strand _strand;
413 bool _strand_required;
415 std::string _address;
419 asio::ip::udp::endpoint _endpoint;
420 asio::ip::udp::socket _socket;
421 std::atomic<bool> _resolving;
422 std::atomic<bool> _connected;
424 uint64_t _bytes_sending;
425 uint64_t _bytes_sent;
426 uint64_t _bytes_received;
427 uint64_t _datagrams_sent;
428 uint64_t _datagrams_received;
430 asio::ip::udp::endpoint _receive_endpoint;
431 asio::ip::udp::endpoint _send_endpoint;
434 size_t _receive_buffer_limit{0};
435 std::vector<uint8_t> _receive_buffer;
436 HandlerStorage _receive_storage;
439 size_t _send_buffer_limit{0};
440 std::vector<uint8_t> _send_buffer;
441 HandlerStorage _send_storage;
443 bool _option_reuse_address;
444 bool _option_reuse_port;
445 bool _option_multicast;
448 bool DisconnectInternal();
450 bool DisconnectInternalAsync(
bool dispatch);
459 void SendError(std::error_code ec);
virtual size_t Send(std::string_view text)
Send text to the connected server (synchronous).
bool option_reuse_address() const noexcept
Get the option: reuse address.
virtual void LeaveMulticastGroup(const std::string &address)
Leave multicast group with a given address (synchronous).
virtual void onConnecting()
Handle client connecting notification.
virtual void onConnected()
Handle client connected notification.
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the client.
int port() const noexcept
Get the server port number.
const CppCommon::UUID & id() const noexcept
Get the client Id.
asio::ip::udp::endpoint & endpoint() noexcept
Get the client endpoint.
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
virtual size_t Receive(asio::ip::udp::endpoint &endpoint, void *buffer, size_t size)
Receive datagram from the given endpoint (synchronous).
UDPClient(UDPClient &&)=delete
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, std::string_view text, const CppCommon::Timespan &timeout)
Send text to the given endpoint with timeout (synchronous).
bool option_multicast() const noexcept
Get the option: bind the socket to the multicast UDP server.
UDPClient & operator=(const UDPClient &)=delete
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the connected server with timeout (synchronous).
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
virtual bool ReconnectAsync()
Reconnect the client (asynchronous).
virtual void onDisconnecting()
Handle client disconnecting notification.
void SetupMulticast(bool enable) noexcept
Setup option: bind the socket to the multicast UDP server.
bool IsConnected() const noexcept
Is the client connected?
UDPClient(const std::shared_ptr< Service > &service, const std::string &address, int port)
Initialize UDP client with a given Asio service, server address and port number.
bool option_reuse_port() const noexcept
Get the option: reuse port.
UDPClient & operator=(UDPClient &&)=delete
virtual void onDisconnected()
Handle client disconnected notification.
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text to the given endpoint (synchronous).
virtual void LeaveMulticastGroupAsync(const std::string &address)
Leave multicast group with a given address (asynchronous).
virtual void onSent(const asio::ip::udp::endpoint &endpoint, size_t sent)
Handle datagram sent notification.
virtual void JoinMulticastGroupAsync(const std::string &address)
Join multicast group with a given address (asynchronous).
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
virtual bool SendAsync(std::string_view text)
Send text to the connected server (asynchronous).
virtual ~UDPClient()=default
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text to the given endpoint (asynchronous).
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
virtual void JoinMulticastGroup(const std::string &address)
Join multicast group with a given address (synchronous).
virtual bool Reconnect()
Reconnect the client (synchronous).
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
UDPClient(const UDPClient &)=delete
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
virtual void onJoinedMulticastGroup(const std::string &address)
Handle client joined multicast group notification.
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
virtual void ReceiveAsync()
Receive datagram from the server (asynchronous).
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the client.
asio::ip::udp::socket & socket() noexcept
Get the client socket.
virtual bool Connect()
Connect the client (synchronous).
size_t option_send_buffer_size() const
Get the option: send buffer size.
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
virtual bool SendAsync(const void *buffer, size_t size)
Send datagram to the connected server (asynchronous).
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
virtual void onLeftMulticastGroup(const std::string &address)
Handle client left multicast group notification.
virtual size_t Send(const void *buffer, size_t size)
Send datagram to the connected server (synchronous).
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
const std::string & address() const noexcept
Get the server address.
virtual bool Disconnect()
Disconnect the client (synchronous).
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
const std::string & scheme() const noexcept
Get the scheme name.
virtual bool ConnectAsync()
Connect the client (asynchronous).
C++ Server project definitions.