9 #ifndef CPPSERVER_ASIO_UDP_CLIENT_H
10 #define CPPSERVER_ASIO_UDP_CLIENT_H
14 #include "system/uuid.h"
15 #include "time/timespan.h"
29 class 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_service>&
io_service() noexcept {
return _io_service; }
67 asio::io_service::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; }
398 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
404 std::shared_ptr<Service> _service;
406 std::shared_ptr<asio::io_service> _io_service;
408 asio::io_service::strand _strand;
409 bool _strand_required;
411 std::string _address;
415 asio::ip::udp::endpoint _endpoint;
416 asio::ip::udp::socket _socket;
417 std::atomic<bool> _resolving;
418 std::atomic<bool> _connected;
420 uint64_t _bytes_sending;
421 uint64_t _bytes_sent;
422 uint64_t _bytes_received;
423 uint64_t _datagrams_sent;
424 uint64_t _datagrams_received;
426 asio::ip::udp::endpoint _receive_endpoint;
427 asio::ip::udp::endpoint _send_endpoint;
430 size_t _receive_buffer_limit{0};
431 std::vector<uint8_t> _receive_buffer;
432 HandlerStorage _receive_storage;
435 size_t _send_buffer_limit{0};
436 std::vector<uint8_t> _send_buffer;
437 HandlerStorage _send_storage;
439 bool _option_reuse_address;
440 bool _option_reuse_port;
441 bool _option_multicast;
444 bool DisconnectInternal();
446 bool DisconnectInternalAsync(
bool dispatch);
455 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 onConnected()
Handle client connected notification.
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the client.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
int port() const noexcept
Get the server port number.
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.
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)
const std::string & scheme() const noexcept
Get the scheme name.
void SetupMulticast(bool enable) noexcept
Setup option: bind the socket to the multicast UDP server.
UDPClient & operator=(const UDPClient &)=delete
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.
const std::string & address() const noexcept
Get the server address.
bool option_reuse_port() const noexcept
Get the option: reuse port.
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)
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.
const CppCommon::UUID & id() const noexcept
Get the client Id.
UDPClient & operator=(UDPClient &&)=delete
virtual void onJoinedMulticastGroup(const std::string &address)
Handle client joined multicast group notification.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
asio::ip::udp::socket & socket() noexcept
Get the client socket.
virtual void ReceiveAsync()
Receive datagram from the server (asynchronous)
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the client.
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)
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
virtual bool Disconnect()
Disconnect the client (synchronous)
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
asio::ip::udp::endpoint & endpoint() noexcept
Get the client endpoint.
virtual bool ConnectAsync()
Connect the client (asynchronous)
C++ Server project definitions.