CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
udp_client.h
Go to the documentation of this file.
1
8
9#ifndef CPPSERVER_ASIO_UDP_CLIENT_H
10#define CPPSERVER_ASIO_UDP_CLIENT_H
11
12#include "udp_resolver.h"
13
14#include "system/uuid.h"
15#include "time/timespan.h"
16
17#include <mutex>
18#include <vector>
19
20namespace CppServer {
21namespace Asio {
22
24
29class UDPClient : public std::enable_shared_from_this<UDPClient>
30{
31public:
33
38 UDPClient(const std::shared_ptr<Service>& service, const std::string& address, int port);
40
45 UDPClient(const std::shared_ptr<Service>& service, const std::string& address, const std::string& scheme);
47
51 UDPClient(const std::shared_ptr<Service>& service, const asio::ip::udp::endpoint& endpoint);
52 UDPClient(const UDPClient&) = delete;
53 UDPClient(UDPClient&&) = delete;
54 virtual ~UDPClient() = default;
55
56 UDPClient& operator=(const UDPClient&) = delete;
58
60 const CppCommon::UUID& id() const noexcept { return _id; }
61
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; }
72
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; }
79
81 uint64_t bytes_pending() const noexcept { return _bytes_sending; }
83 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
85 uint64_t bytes_received() const noexcept { return _bytes_received; }
87 uint64_t datagrams_sent() const noexcept { return _datagrams_sent; }
89 uint64_t datagrams_received() const noexcept { return _datagrams_received; }
90
92 bool option_reuse_address() const noexcept { return _option_reuse_address; }
94 bool option_reuse_port() const noexcept { return _option_reuse_port; }
96 bool option_multicast() const noexcept { return _option_multicast; }
98 size_t option_receive_buffer_limit() const noexcept { return _receive_buffer_limit; }
100 size_t option_receive_buffer_size() const;
102 size_t option_send_buffer_limit() const noexcept { return _send_buffer_limit; }
104 size_t option_send_buffer_size() const;
105
107 bool IsConnected() const noexcept { return _connected; }
108
110
113 virtual bool Connect();
115
119 virtual bool Connect(const std::shared_ptr<UDPResolver>& resolver);
121
124 virtual bool Disconnect() { return DisconnectInternal(); }
126
129 virtual bool Reconnect();
130
132
135 virtual bool ConnectAsync();
137
141 virtual bool ConnectAsync(const std::shared_ptr<UDPResolver>& resolver);
143
146 virtual bool DisconnectAsync() { return DisconnectInternalAsync(false); }
148
151 virtual bool ReconnectAsync();
152
154
157 virtual void JoinMulticastGroup(const std::string& address);
159
162 virtual void LeaveMulticastGroup(const std::string& address);
163
165
168 virtual void JoinMulticastGroupAsync(const std::string& address);
170
173 virtual void LeaveMulticastGroupAsync(const std::string& address);
174
176
181 virtual size_t Send(const void* buffer, size_t size);
183
187 virtual size_t Send(std::string_view text) { return Send(text.data(), text.size()); }
189
195 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size);
197
202 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, std::string_view text) { return Send(endpoint, text.data(), text.size()); }
203
205
211 virtual size_t Send(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
213
218 virtual size_t Send(std::string_view text, const CppCommon::Timespan& timeout) { return Send(text.data(), text.size(), timeout); }
220
227 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size, const CppCommon::Timespan& timeout);
229
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); }
236
238
243 virtual bool SendAsync(const void* buffer, size_t size);
245
249 virtual bool SendAsync(std::string_view text) { return SendAsync(text.data(), text.size()); }
251
257 virtual bool SendAsync(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size);
259
264 virtual bool SendAsync(const asio::ip::udp::endpoint& endpoint, std::string_view text) { return SendAsync(endpoint, text.data(), text.size()); }
265
267
273 virtual size_t Receive(asio::ip::udp::endpoint& endpoint, void* buffer, size_t size);
275
280 virtual std::string Receive(asio::ip::udp::endpoint& endpoint, size_t size);
281
283
290 virtual size_t Receive(asio::ip::udp::endpoint& endpoint, void* buffer, size_t size, const CppCommon::Timespan& timeout);
292
298 virtual std::string Receive(asio::ip::udp::endpoint& endpoint, size_t size, const CppCommon::Timespan& timeout);
299
301 virtual void ReceiveAsync();
302
304
309 void SetupReuseAddress(bool enable) noexcept { _option_reuse_address = enable; }
311
316 void SetupReusePort(bool enable) noexcept { _option_reuse_port = enable; }
318
321 void SetupMulticast(bool enable) noexcept { _option_reuse_address = enable; _option_multicast = enable; }
323
329 void SetupReceiveBufferLimit(size_t limit) noexcept { _receive_buffer_limit = limit; }
331
336 void SetupReceiveBufferSize(size_t size);
338
344 void SetupSendBufferLimit(size_t limit) noexcept { _send_buffer_limit = limit; }
346
351 void SetupSendBufferSize(size_t size);
352
353protected:
355 virtual void onConnecting() {}
357 virtual void onConnected() {}
359 virtual void onDisconnecting() {}
361 virtual void onDisconnected() {}
362
364
367 virtual void onJoinedMulticastGroup(const std::string& address) {}
369
372 virtual void onLeftMulticastGroup(const std::string& address) {}
373
375
383 virtual void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) {}
385
394 virtual void onSent(const asio::ip::udp::endpoint& endpoint, size_t sent) {}
395
397
402 virtual void onError(int error, const std::string& category, const std::string& message) {}
403
404private:
405 // Client Id
406 CppCommon::UUID _id;
407 // Asio service
408 std::shared_ptr<Service> _service;
409 // Asio IO context
410 std::shared_ptr<asio::io_context> _io_context;
411 // Asio service strand for serialized handler execution
412 asio::io_context::strand _strand;
413 bool _strand_required;
414 // Server address, scheme & port
415 std::string _address;
416 std::string _scheme;
417 int _port;
418 // Server endpoint & client socket
419 asio::ip::udp::endpoint _endpoint;
420 asio::ip::udp::socket _socket;
421 std::atomic<bool> _resolving;
422 std::atomic<bool> _connected;
423 // Client statistic
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;
429 // Receive and send endpoints
430 asio::ip::udp::endpoint _receive_endpoint;
431 asio::ip::udp::endpoint _send_endpoint;
432 // Receive buffer
433 bool _receiving;
434 size_t _receive_buffer_limit{0};
435 std::vector<uint8_t> _receive_buffer;
436 HandlerStorage _receive_storage;
437 // Send buffer
438 bool _sending;
439 size_t _send_buffer_limit{0};
440 std::vector<uint8_t> _send_buffer;
441 HandlerStorage _send_storage;
442 // Options
443 bool _option_reuse_address;
444 bool _option_reuse_port;
445 bool _option_multicast;
446
448 bool DisconnectInternal();
450 bool DisconnectInternalAsync(bool dispatch);
451
453 void TryReceive();
454
456 void ClearBuffers();
457
459 void SendError(std::error_code ec);
460};
461
464
465} // namespace Asio
466} // namespace CppServer
467
468#endif // CPPSERVER_ASIO_UDP_CLIENT_H
virtual size_t Send(std::string_view text)
Send text to the connected server (synchronous).
Definition udp_client.h:187
bool option_reuse_address() const noexcept
Get the option: reuse address.
Definition udp_client.h:92
virtual void LeaveMulticastGroup(const std::string &address)
Leave multicast group with a given address (synchronous).
virtual void onConnecting()
Handle client connecting notification.
Definition udp_client.h:355
virtual void onConnected()
Handle client connected notification.
Definition udp_client.h:357
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the client.
Definition udp_client.h:87
int port() const noexcept
Get the server port number.
Definition udp_client.h:78
const CppCommon::UUID & id() const noexcept
Get the client Id.
Definition udp_client.h:60
asio::ip::udp::endpoint & endpoint() noexcept
Get the client endpoint.
Definition udp_client.h:69
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).
Definition udp_client.h:235
bool option_multicast() const noexcept
Get the option: bind the socket to the multicast UDP server.
Definition udp_client.h:96
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).
Definition udp_client.h:218
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
Definition udp_client.h:85
virtual bool ReconnectAsync()
Reconnect the client (asynchronous).
virtual void onDisconnecting()
Handle client disconnecting notification.
Definition udp_client.h:359
void SetupMulticast(bool enable) noexcept
Setup option: bind the socket to the multicast UDP server.
Definition udp_client.h:321
bool IsConnected() const noexcept
Is the client connected?
Definition udp_client.h:107
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.
Definition udp_client.h:94
UDPClient & operator=(UDPClient &&)=delete
virtual void onDisconnected()
Handle client disconnected notification.
Definition udp_client.h:361
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text to the given endpoint (synchronous).
Definition udp_client.h:202
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.
Definition udp_client.h:394
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.
Definition udp_client.h:98
virtual bool SendAsync(std::string_view text)
Send text to the connected server (asynchronous).
Definition udp_client.h:249
virtual ~UDPClient()=default
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text to the given endpoint (asynchronous).
Definition udp_client.h:264
void SetupReuseAddress(bool enable) noexcept
Setup option: reuse address.
Definition udp_client.h:309
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition udp_client.h:402
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.
Definition udp_client.h:67
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
Definition udp_client.h:81
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
Definition udp_client.h:344
UDPClient(const UDPClient &)=delete
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
Definition udp_client.h:316
virtual void onJoinedMulticastGroup(const std::string &address)
Handle client joined multicast group notification.
Definition udp_client.h:367
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
Definition udp_client.h:65
virtual void ReceiveAsync()
Receive datagram from the server (asynchronous).
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
Definition udp_client.h:83
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the client.
Definition udp_client.h:89
asio::ip::udp::socket & socket() noexcept
Get the client socket.
Definition udp_client.h:71
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.
Definition udp_client.h:383
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.
Definition udp_client.h:102
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
Definition udp_client.h:146
virtual void onLeftMulticastGroup(const std::string &address)
Handle client left multicast group notification.
Definition udp_client.h:372
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.
Definition udp_client.h:63
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
Definition udp_client.h:329
const std::string & address() const noexcept
Get the server address.
Definition udp_client.h:74
virtual bool Disconnect()
Disconnect the client (synchronous).
Definition udp_client.h:124
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
const std::string & scheme() const noexcept
Get the scheme name.
Definition udp_client.h:76
virtual bool ConnectAsync()
Connect the client (asynchronous).
Asio definitions.
C++ Server project definitions.
Definition asio.h:56
UDP resolver definition.