CppServer 1.0.5.0
C++ Server Library
Loading...
Searching...
No Matches
udp_server.h
Go to the documentation of this file.
1
9#ifndef CPPSERVER_ASIO_UDP_SERVER_H
10#define CPPSERVER_ASIO_UDP_SERVER_H
11
12#include "service.h"
13
14#include "system/uuid.h"
15
16namespace CppServer {
17namespace Asio {
18
20
25class UDPServer : public std::enable_shared_from_this<UDPServer>
26{
27public:
29
34 UDPServer(const std::shared_ptr<Service>& service, int port, InternetProtocol protocol = InternetProtocol::IPv4);
36
41 UDPServer(const std::shared_ptr<Service>& service, const std::string& address, int port);
43
47 UDPServer(const std::shared_ptr<Service>& service, const asio::ip::udp::endpoint& endpoint);
48 UDPServer(const UDPServer&) = delete;
49 UDPServer(UDPServer&&) = delete;
50 virtual ~UDPServer() = default;
51
52 UDPServer& operator=(const UDPServer&) = delete;
54
56 const CppCommon::UUID& id() const noexcept { return _id; }
57
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; }
67 asio::ip::udp::endpoint& multicast_endpoint() noexcept { return _multicast_endpoint; }
68
70 const std::string& address() const noexcept { return _address; }
72 int port() const noexcept { return _port; }
73
75 uint64_t bytes_pending() const noexcept { return _bytes_sending; }
77 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
79 uint64_t bytes_received() const noexcept { return _bytes_received; }
81 uint64_t datagrams_sent() const noexcept { return _datagrams_sent; }
83 uint64_t datagrams_received() const noexcept { return _datagrams_received; }
84
86 bool option_reuse_address() const noexcept { return _option_reuse_address; }
88 bool option_reuse_port() const noexcept { return _option_reuse_port; }
90 size_t option_receive_buffer_limit() const noexcept { return _receive_buffer_limit; }
92 size_t option_receive_buffer_size() const;
94 size_t option_send_buffer_limit() const noexcept { return _send_buffer_limit; }
96 size_t option_send_buffer_size() const;
97
99 bool IsStarted() const noexcept { return _started; }
100
102
105 virtual bool Start();
107
113 virtual bool Start(const std::string& multicast_address, int multicast_port);
115
120 virtual bool Start(const asio::ip::udp::endpoint& multicast_endpoint);
122
125 virtual bool Stop();
127
130 virtual bool Restart();
131
133
138 virtual size_t Multicast(const void* buffer, size_t size);
140
144 virtual size_t Multicast(std::string_view text) { return Multicast(text.data(), text.size()); }
145
147
153 virtual size_t Multicast(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
155
160 virtual size_t Multicast(std::string_view text, const CppCommon::Timespan& timeout) { return Multicast(text.data(), text.size(), timeout); }
161
163
168 virtual bool MulticastAsync(const void* buffer, size_t size);
170
174 virtual bool MulticastAsync(std::string_view text) { return MulticastAsync(text.data(), text.size()); }
175
177
183 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size);
185
190 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, std::string_view text) { return Send(endpoint, text.data(), text.size()); }
191
193
200 virtual size_t Send(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size, const CppCommon::Timespan& timeout);
202
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); }
209
211
217 virtual bool SendAsync(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size);
219
224 virtual bool SendAsync(const asio::ip::udp::endpoint& endpoint, std::string_view text) { return SendAsync(endpoint, text.data(), text.size()); }
225
227
233 virtual size_t Receive(asio::ip::udp::endpoint& endpoint, void* buffer, size_t size);
235
240 virtual std::string Receive(asio::ip::udp::endpoint& endpoint, size_t size);
241
243
250 virtual size_t Receive(asio::ip::udp::endpoint& endpoint, void* buffer, size_t size, const CppCommon::Timespan& timeout);
252
258 virtual std::string Receive(asio::ip::udp::endpoint& endpoint, size_t size, const CppCommon::Timespan& timeout);
259
261 virtual void ReceiveAsync();
262
264
269 void SetupReuseAddress(bool enable) noexcept { _option_reuse_address = enable; }
271
276 void SetupReusePort(bool enable) noexcept { _option_reuse_port = enable; }
278
284 void SetupReceiveBufferLimit(size_t limit) noexcept { _receive_buffer_limit = limit; }
286
291 void SetupReceiveBufferSize(size_t size);
293
299 void SetupSendBufferLimit(size_t limit) noexcept { _send_buffer_limit = limit; }
301
306 void SetupSendBufferSize(size_t size);
307
308protected:
310 virtual void onStarted() {}
312 virtual void onStopped() {}
313
315
323 virtual void onReceived(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) {}
325
334 virtual void onSent(const asio::ip::udp::endpoint& endpoint, size_t sent) {}
335
337
342 virtual void onError(int error, const std::string& category, const std::string& message) {}
343
344private:
345 // Server Id
346 CppCommon::UUID _id;
347 // Asio service
348 std::shared_ptr<Service> _service;
349 // Asio IO service
350 std::shared_ptr<asio::io_service> _io_service;
351 // Asio service strand for serialized handler execution
352 asio::io_service::strand _strand;
353 bool _strand_required;
354 // Server address, scheme & port
355 std::string _address;
356 int _port;
357 // Server endpoint & socket
358 asio::ip::udp::endpoint _endpoint;
359 asio::ip::udp::socket _socket;
360 std::atomic<bool> _started;
361 // Server statistic
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;
367 // Multicast, receive and send endpoints
368 asio::ip::udp::endpoint _multicast_endpoint;
369 asio::ip::udp::endpoint _receive_endpoint;
370 asio::ip::udp::endpoint _send_endpoint;
371 // Receive buffer
372 bool _receiving;
373 size_t _receive_buffer_limit{0};
374 std::vector<uint8_t> _receive_buffer;
375 HandlerStorage _receive_storage;
376 // Send buffer
377 bool _sending;
378 size_t _send_buffer_limit{0};
379 std::vector<uint8_t> _send_buffer;
380 HandlerStorage _send_storage;
381 // Options
382 bool _option_reuse_address;
383 bool _option_reuse_port;
384
386 void TryReceive();
387
389 void ClearBuffers();
390
392 void SendError(std::error_code ec);
393};
394
398} // namespace Asio
399} // namespace CppServer
400
401#endif // CPPSERVER_ASIO_UDP_SERVER_H
Asio allocate handler wrapper.
Definition memory.h:133
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition udp_server.h:61
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the server.
Definition udp_server.h:75
virtual bool Restart()
Restart the server.
int port() const noexcept
Get the server port number.
Definition udp_server.h:72
UDPServer(UDPServer &&)=delete
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the server.
Definition udp_server.h:77
uint64_t datagrams_received() const noexcept
Get the number datagrams received by the server.
Definition udp_server.h:83
virtual void onStarted()
Handle server started notification.
Definition udp_server.h:310
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
Definition udp_server.h:299
virtual ~UDPServer()=default
bool option_reuse_port() const noexcept
Get the option: reuse port.
Definition udp_server.h:88
virtual size_t Multicast(std::string_view text, const CppCommon::Timespan &timeout)
Multicast text to the prepared mulicast endpoint with timeout (synchronous)
Definition udp_server.h:160
const std::string & address() const noexcept
Get the server address.
Definition udp_server.h:70
UDPServer(const UDPServer &)=delete
virtual void ReceiveAsync()
Receive datagram from the client (asynchronous)
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text into the given endpoint (asynchronous)
Definition udp_server.h:224
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.
Definition udp_server.h:342
UDPServer & operator=(UDPServer &&)=delete
uint64_t bytes_received() const noexcept
Get the number of bytes received by the server.
Definition udp_server.h:79
bool option_reuse_address() const noexcept
Get the option: reuse address.
Definition udp_server.h:86
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.
Definition udp_server.h:269
virtual bool Start()
Start the server.
uint64_t datagrams_sent() const noexcept
Get the number datagrams sent by the server.
Definition udp_server.h:81
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)
Definition udp_server.h:208
virtual bool MulticastAsync(std::string_view text)
Multicast text to the prepared mulicast endpoint (asynchronous)
Definition udp_server.h:174
size_t option_send_buffer_size() const
Get the option: send buffer size.
const CppCommon::UUID & id() const noexcept
Get the server Id.
Definition udp_server.h:56
void SetupReusePort(bool enable) noexcept
Setup option: reuse port.
Definition udp_server.h:276
bool IsStarted() const noexcept
Is the server started?
Definition udp_server.h:99
virtual void onSent(const asio::ip::udp::endpoint &endpoint, size_t sent)
Handle datagram sent notification.
Definition udp_server.h:334
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition udp_server.h:63
virtual size_t Send(const asio::ip::udp::endpoint &endpoint, std::string_view text)
Send text into the given endpoint (synchronous)
Definition udp_server.h:190
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.
Definition udp_server.h:94
asio::ip::udp::endpoint & multicast_endpoint() noexcept
Get the server multicast endpoint.
Definition udp_server.h:67
virtual bool SendAsync(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Send datagram into the given endpoint (asynchronous)
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition udp_server.h:59
asio::ip::udp::endpoint & endpoint() noexcept
Get the server endpoint.
Definition udp_server.h:65
virtual void onReceived(const asio::ip::udp::endpoint &endpoint, const void *buffer, size_t size)
Handle datagram received notification.
Definition udp_server.h:323
UDPServer & operator=(const UDPServer &)=delete
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.
Definition udp_server.h:90
virtual size_t Multicast(std::string_view text)
Multicast text to the prepared mulicast endpoint (synchronous)
Definition udp_server.h:144
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
Definition udp_server.h:284
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
virtual void onStopped()
Handle server stopped notification.
Definition udp_server.h:312
InternetProtocol
Internet protocol.
Definition asio.h:66
@ IPv4
Internet Protocol version 4.
C++ Server project definitions.
Definition asio.h:56
Asio service definition.