CppServer 1.0.6.0
C++ Server Library
Loading...
Searching...
No Matches
tcp_client.h
Go to the documentation of this file.
1
8
9#ifndef CPPSERVER_ASIO_TCP_CLIENT_H
10#define CPPSERVER_ASIO_TCP_CLIENT_H
11
12#include "tcp_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 TCPClient : public std::enable_shared_from_this<TCPClient>
30{
31public:
33
38 TCPClient(const std::shared_ptr<Service>& service, const std::string& address, int port);
40
45 TCPClient(const std::shared_ptr<Service>& service, const std::string& address, const std::string& scheme);
47
51 TCPClient(const std::shared_ptr<Service>& service, const asio::ip::tcp::endpoint& endpoint);
52 TCPClient(const TCPClient&) = delete;
53 TCPClient(TCPClient&&) = delete;
54 virtual ~TCPClient() = default;
55
56 TCPClient& operator=(const TCPClient&) = 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::tcp::endpoint& endpoint() noexcept { return _endpoint; }
71 asio::ip::tcp::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_pending + _bytes_sending; }
83 uint64_t bytes_sent() const noexcept { return _bytes_sent; }
85 uint64_t bytes_received() const noexcept { return _bytes_received; }
86
88 bool option_keep_alive() const noexcept { return _option_keep_alive; }
90 bool option_no_delay() const noexcept { return _option_no_delay; }
92 size_t option_receive_buffer_limit() const noexcept { return _receive_buffer_limit; }
94 size_t option_receive_buffer_size() const;
96 size_t option_send_buffer_limit() const noexcept { return _send_buffer_limit; }
98 size_t option_send_buffer_size() const;
99
101 bool IsConnected() const noexcept { return _connected; }
102
104
110 virtual bool Connect();
112
119 virtual bool Connect(const std::shared_ptr<TCPResolver>& 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<TCPResolver>& resolver);
143
146 virtual bool DisconnectAsync() { return DisconnectInternalAsync(false); }
148
151 virtual bool ReconnectAsync();
152
154
159 virtual size_t Send(const void* buffer, size_t size);
161
165 virtual size_t Send(std::string_view text) { return Send(text.data(), text.size()); }
166
168
174 virtual size_t Send(const void* buffer, size_t size, const CppCommon::Timespan& timeout);
176
181 virtual size_t Send(std::string_view text, const CppCommon::Timespan& timeout) { return Send(text.data(), text.size(), timeout); }
182
184
189 virtual bool SendAsync(const void* buffer, size_t size);
191
195 virtual bool SendAsync(std::string_view text) { return SendAsync(text.data(), text.size()); }
196
198
203 virtual size_t Receive(void* buffer, size_t size);
205
209 virtual std::string Receive(size_t size);
210
212
218 virtual size_t Receive(void* buffer, size_t size, const CppCommon::Timespan& timeout);
220
225 virtual std::string Receive(size_t size, const CppCommon::Timespan& timeout);
226
228 virtual void ReceiveAsync();
229
231
236 void SetupKeepAlive(bool enable) noexcept { _option_keep_alive = enable; }
238
245 void SetupNoDelay(bool enable) noexcept { _option_no_delay = enable; }
247
253 void SetupReceiveBufferLimit(size_t limit) noexcept { _receive_buffer_limit = limit; }
255
260 void SetupReceiveBufferSize(size_t size);
262
268 void SetupSendBufferLimit(size_t limit) noexcept { _send_buffer_limit = limit; }
270
275 void SetupSendBufferSize(size_t size);
276
277protected:
279 virtual void onConnecting() {}
281 virtual void onConnected() {}
283 virtual void onDisconnecting() {}
285 virtual void onDisconnected() {}
286
288
295 virtual void onReceived(const void* buffer, size_t size) {}
297
307 virtual void onSent(size_t sent, size_t pending) {}
308
310
316 virtual void onEmpty() {}
317
319
324 virtual void onError(int error, const std::string& category, const std::string& message) {}
325
326private:
327 // Client Id
328 CppCommon::UUID _id;
329 // Asio service
330 std::shared_ptr<Service> _service;
331 // Asio IO context
332 std::shared_ptr<asio::io_context> _io_context;
333 // Asio service strand for serialized handler execution
334 asio::io_context::strand _strand;
335 bool _strand_required;
336 // Server address, scheme & port
337 std::string _address;
338 std::string _scheme;
339 int _port;
340 // Server endpoint & client socket
341 asio::ip::tcp::endpoint _endpoint;
342 asio::ip::tcp::socket _socket;
343 std::atomic<bool> _resolving;
344 std::atomic<bool> _connecting;
345 std::atomic<bool> _connected;
346 // Client statistic
347 uint64_t _bytes_pending;
348 uint64_t _bytes_sending;
349 uint64_t _bytes_sent;
350 uint64_t _bytes_received;
351 // Receive buffer
352 bool _receiving;
353 size_t _receive_buffer_limit{0};
354 std::vector<uint8_t> _receive_buffer;
355 HandlerStorage _receive_storage;
356 // Send buffer
357 bool _sending;
358 std::mutex _send_lock;
359 size_t _send_buffer_limit{0};
360 std::vector<uint8_t> _send_buffer_main;
361 std::vector<uint8_t> _send_buffer_flush;
362 size_t _send_buffer_flush_offset;
363 HandlerStorage _send_storage;
364 // Options
365 bool _option_keep_alive;
366 bool _option_no_delay;
367
369 bool DisconnectInternal();
371 bool DisconnectInternalAsync(bool dispatch);
372
374 void TryReceive();
376 void TrySend();
377
379 void ClearBuffers();
380
382 void SendError(std::error_code ec);
383};
384
386
387} // namespace Asio
388} // namespace CppServer
389
390#endif // CPPSERVER_ASIO_TCP_CLIENT_H
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
Definition tcp_client.h:236
size_t option_send_buffer_size() const
Get the option: send buffer size.
virtual size_t Receive(void *buffer, size_t size)
Receive data from the server (synchronous).
bool IsConnected() const noexcept
Is the client connected?
Definition tcp_client.h:101
TCPClient & operator=(TCPClient &&)=delete
virtual bool Connect()
Connect the client (synchronous).
const std::string & address() const noexcept
Get the server address.
Definition tcp_client.h:74
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous).
asio::ip::tcp::socket & socket() noexcept
Get the client socket.
Definition tcp_client.h:71
TCPClient(const std::shared_ptr< Service > &service, const std::string &address, int port)
Initialize TCP client with a given Asio service, server address and port number.
virtual bool Disconnect()
Disconnect the client (synchronous).
Definition tcp_client.h:124
TCPClient(const TCPClient &)=delete
virtual bool SendAsync(const void *buffer, size_t size)
Send data to the server (asynchronous).
virtual void onSent(size_t sent, size_t pending)
Handle buffer sent notification.
Definition tcp_client.h:307
virtual bool ReconnectAsync()
Reconnect the client (asynchronous).
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
Definition tcp_client.h:295
virtual size_t Send(std::string_view text)
Send text to the server (synchronous).
Definition tcp_client.h:165
virtual void onDisconnected()
Handle client disconnected notification.
Definition tcp_client.h:285
TCPClient(TCPClient &&)=delete
virtual void onConnected()
Handle client connected notification.
Definition tcp_client.h:281
virtual void onDisconnecting()
Handle client disconnecting notification.
Definition tcp_client.h:283
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
Definition tcp_client.h:245
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
Definition tcp_client.h:268
int port() const noexcept
Get the server port number.
Definition tcp_client.h:78
virtual void onEmpty()
Handle empty send buffer notification.
Definition tcp_client.h:316
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the server with timeout (synchronous).
Definition tcp_client.h:181
virtual ~TCPClient()=default
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition tcp_client.h:324
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition tcp_client.h:63
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
Definition tcp_client.h:81
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition tcp_client.h:67
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
Definition tcp_client.h:65
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
Definition tcp_client.h:83
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
Definition tcp_client.h:85
virtual bool ConnectAsync()
Connect the client (asynchronous).
const std::string & scheme() const noexcept
Get the scheme name.
Definition tcp_client.h:76
bool option_no_delay() const noexcept
Get the option: no delay.
Definition tcp_client.h:90
size_t option_receive_buffer_limit() const noexcept
Get the option: receive buffer limit.
Definition tcp_client.h:92
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
Definition tcp_client.h:69
virtual bool SendAsync(std::string_view text)
Send text to the server (asynchronous).
Definition tcp_client.h:195
virtual bool DisconnectAsync()
Disconnect the client (asynchronous).
Definition tcp_client.h:146
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
Definition tcp_client.h:96
TCPClient & operator=(const TCPClient &)=delete
bool option_keep_alive() const noexcept
Get the option: keep alive.
Definition tcp_client.h:88
virtual void ReceiveAsync()
Receive data from the server (asynchronous).
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
Definition tcp_client.h:253
void SetupSendBufferSize(size_t size)
Setup option: send buffer size.
size_t option_receive_buffer_size() const
Get the option: receive buffer size.
const CppCommon::UUID & id() const noexcept
Get the client Id.
Definition tcp_client.h:60
virtual void onConnecting()
Handle client connecting notification.
Definition tcp_client.h:279
virtual bool Reconnect()
Reconnect the client (synchronous).
Asio definitions.
C++ Server project definitions.
Definition asio.h:56
TCP resolver definition.