CppServer  1.0.4.0
C++ Server Library
udp_client.h
Go to the documentation of this file.
1 
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 
20 namespace CppServer {
21 namespace Asio {
22 
24 
29 class UDPClient : public std::enable_shared_from_this<UDPClient>
30 {
31 public:
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_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; }
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 
353 protected:
355  virtual void onConnected() {}
357  virtual void onDisconnected() {}
358 
360 
363  virtual void onJoinedMulticastGroup(const std::string& address) {}
365 
368  virtual void onLeftMulticastGroup(const std::string& address) {}
369 
371 
379  virtual void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) {}
381 
390  virtual void onSent(const asio::ip::udp::endpoint& endpoint, size_t sent) {}
391 
393 
398  virtual void onError(int error, const std::string& category, const std::string& message) {}
399 
400 private:
401  // Client Id
402  CppCommon::UUID _id;
403  // Asio service
404  std::shared_ptr<Service> _service;
405  // Asio IO service
406  std::shared_ptr<asio::io_service> _io_service;
407  // Asio service strand for serialized handler execution
408  asio::io_service::strand _strand;
409  bool _strand_required;
410  // Server address, scheme & port
411  std::string _address;
412  std::string _scheme;
413  int _port;
414  // Server endpoint & client socket
415  asio::ip::udp::endpoint _endpoint;
416  asio::ip::udp::socket _socket;
417  std::atomic<bool> _resolving;
418  std::atomic<bool> _connected;
419  // Client statistic
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;
425  // Receive and send endpoints
426  asio::ip::udp::endpoint _receive_endpoint;
427  asio::ip::udp::endpoint _send_endpoint;
428  // Receive buffer
429  bool _receiving;
430  size_t _receive_buffer_limit{0};
431  std::vector<uint8_t> _receive_buffer;
432  HandlerStorage _receive_storage;
433  // Send buffer
434  bool _sending;
435  size_t _send_buffer_limit{0};
436  std::vector<uint8_t> _send_buffer;
437  HandlerStorage _send_storage;
438  // Options
439  bool _option_reuse_address;
440  bool _option_reuse_port;
441  bool _option_multicast;
442 
444  bool DisconnectInternal();
446  bool DisconnectInternalAsync(bool dispatch);
447 
449  void TryReceive();
450 
452  void ClearBuffers();
453 
455  void SendError(std::error_code ec);
456 };
457 
461 } // namespace Asio
462 } // namespace CppServer
463 
464 #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)
Definition: udp_client.cpp:433
virtual void onConnected()
Handle client connected notification.
Definition: udp_client.h:355
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the client.
Definition: udp_client.h:87
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition: udp_client.h:65
int port() const noexcept
Get the server port number.
Definition: udp_client.h:78
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
Definition: udp_client.cpp:111
virtual size_t Receive(asio::ip::udp::endpoint &endpoint, void *buffer, size_t size)
Receive datagram from the given endpoint (synchronous)
Definition: udp_client.cpp:665
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
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)
Definition: udp_client.cpp:408
const std::string & scheme() const noexcept
Get the scheme name.
Definition: udp_client.h:76
void SetupMulticast(bool enable) noexcept
Setup option: bind the socket to the multicast UDP server.
Definition: udp_client.h:321
UDPClient & operator=(const UDPClient &)=delete
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.
Definition: udp_client.cpp:14
const std::string & address() const noexcept
Get the server address.
Definition: udp_client.h:74
bool option_reuse_port() const noexcept
Get the option: reuse port.
Definition: udp_client.h:94
virtual void onDisconnected()
Handle client disconnected notification.
Definition: udp_client.h:357
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)
Definition: udp_client.cpp:461
virtual void onSent(const asio::ip::udp::endpoint &endpoint, size_t sent)
Handle datagram sent notification.
Definition: udp_client.h:390
virtual void JoinMulticastGroupAsync(const std::string &address)
Join multicast group with a given address (asynchronous)
Definition: udp_client.cpp:447
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:398
virtual void JoinMulticastGroup(const std::string &address)
Join multicast group with a given address (synchronous)
Definition: udp_client.cpp:419
virtual bool Reconnect()
Reconnect the client (synchronous)
Definition: udp_client.cpp:267
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
const CppCommon::UUID & id() const noexcept
Get the client Id.
Definition: udp_client.h:60
UDPClient & operator=(UDPClient &&)=delete
virtual void onJoinedMulticastGroup(const std::string &address)
Handle client joined multicast group notification.
Definition: udp_client.h:363
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition: udp_client.h:63
asio::ip::udp::socket & socket() noexcept
Get the client socket.
Definition: udp_client.h:71
virtual void ReceiveAsync()
Receive datagram from the server (asynchronous)
Definition: udp_client.cpp:773
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
Definition: udp_client.h:83
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition: udp_client.h:67
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the client.
Definition: udp_client.h:89
virtual bool Connect()
Connect the client (synchronous)
Definition: udp_client.cpp:123
size_t option_send_buffer_size() const
Get the option: send buffer size.
Definition: udp_client.cpp:104
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
Definition: udp_client.h:379
virtual bool SendAsync(const void *buffer, size_t size)
Send datagram to the connected server (asynchronous)
Definition: udp_client.cpp:587
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
Definition: udp_client.cpp:117
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:368
virtual size_t Send(const void *buffer, size_t size)
Send datagram to the connected server (synchronous)
Definition: udp_client.cpp:475
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
Definition: udp_client.h:329
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.
Definition: udp_client.cpp:97
asio::ip::udp::endpoint & endpoint() noexcept
Get the client endpoint.
Definition: udp_client.h:69
virtual bool ConnectAsync()
Connect the client (asynchronous)
Definition: udp_client.cpp:275
C++ Server project definitions.
Definition: asio.h:56
UDP resolver definition.