9 #ifndef CPPSERVER_ASIO_TCP_CLIENT_H
10 #define CPPSERVER_ASIO_TCP_CLIENT_H
14 #include "system/uuid.h"
15 #include "time/timespan.h"
29 class TCPClient :
public std::enable_shared_from_this<TCPClient>
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::tcp::endpoint&
endpoint() noexcept {
return _endpoint; }
71 asio::ip::tcp::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; }
81 uint64_t
bytes_pending() const noexcept {
return _bytes_pending + _bytes_sending; }
83 uint64_t
bytes_sent() const noexcept {
return _bytes_sent; }
119 virtual bool Connect(
const std::shared_ptr<TCPResolver>& resolver);
141 virtual bool ConnectAsync(
const std::shared_ptr<TCPResolver>& resolver);
159 virtual size_t Send(
const void* buffer,
size_t size);
165 virtual size_t Send(std::string_view text) {
return Send(text.data(), text.size()); }
174 virtual size_t Send(
const void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
181 virtual size_t Send(std::string_view text,
const CppCommon::Timespan& timeout) {
return Send(text.data(), text.size(), timeout); }
189 virtual bool SendAsync(
const void* buffer,
size_t size);
203 virtual size_t Receive(
void* buffer,
size_t size);
209 virtual std::string
Receive(
size_t size);
218 virtual size_t Receive(
void* buffer,
size_t size,
const CppCommon::Timespan& timeout);
225 virtual std::string
Receive(
size_t size,
const CppCommon::Timespan& timeout);
303 virtual void onSent(
size_t sent,
size_t pending) {}
320 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
326 std::shared_ptr<Service> _service;
328 std::shared_ptr<asio::io_service> _io_service;
330 asio::io_service::strand _strand;
331 bool _strand_required;
333 std::string _address;
337 asio::ip::tcp::endpoint _endpoint;
338 asio::ip::tcp::socket _socket;
339 std::atomic<bool> _resolving;
340 std::atomic<bool> _connecting;
341 std::atomic<bool> _connected;
343 uint64_t _bytes_pending;
344 uint64_t _bytes_sending;
345 uint64_t _bytes_sent;
346 uint64_t _bytes_received;
349 size_t _receive_buffer_limit{0};
350 std::vector<uint8_t> _receive_buffer;
351 HandlerStorage _receive_storage;
354 std::mutex _send_lock;
355 size_t _send_buffer_limit{0};
356 std::vector<uint8_t> _send_buffer_main;
357 std::vector<uint8_t> _send_buffer_flush;
358 size_t _send_buffer_flush_offset;
359 HandlerStorage _send_storage;
361 bool _option_keep_alive;
362 bool _option_no_delay;
365 bool DisconnectInternal();
367 bool DisconnectInternalAsync(
bool dispatch);
378 void SendError(std::error_code ec);
void SetupKeepAlive(bool enable) noexcept
Setup option: keep alive.
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?
virtual bool Connect()
Connect the client (synchronous)
virtual size_t Send(const void *buffer, size_t size)
Send data to the server (synchronous)
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)
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.
virtual bool ReconnectAsync()
Reconnect the client (asynchronous)
virtual void onReceived(const void *buffer, size_t size)
Handle buffer received notification.
virtual size_t Send(std::string_view text)
Send text to the server (synchronous)
virtual void onDisconnected()
Handle client disconnected notification.
TCPClient(TCPClient &&)=delete
virtual void onConnected()
Handle client connected notification.
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
asio::ip::tcp::socket & socket() noexcept
Get the client socket.
TCPClient & operator=(const TCPClient &)=delete
void SetupNoDelay(bool enable) noexcept
Setup option: no delay.
void SetupSendBufferLimit(size_t limit) noexcept
Setup option: send buffer limit.
int port() const noexcept
Get the server port number.
virtual void onEmpty()
Handle empty send buffer notification.
virtual size_t Send(std::string_view text, const CppCommon::Timespan &timeout)
Send text to the server with timeout (synchronous)
virtual ~TCPClient()=default
TCPClient & operator=(TCPClient &&)=delete
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
const std::string & address() const noexcept
Get the server address.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
uint64_t bytes_sent() const noexcept
Get the number of bytes sent by the client.
uint64_t bytes_received() const noexcept
Get the number of bytes received by the client.
const std::string & scheme() const noexcept
Get the scheme name.
virtual bool ConnectAsync()
Connect the client (asynchronous)
bool option_no_delay() const noexcept
Get the option: no delay.
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 server (asynchronous)
virtual bool DisconnectAsync()
Disconnect the client (asynchronous)
size_t option_send_buffer_limit() const noexcept
Get the option: send buffer limit.
bool option_keep_alive() const noexcept
Get the option: keep alive.
virtual void ReceiveAsync()
Receive data from the server (asynchronous)
void SetupReceiveBufferLimit(size_t limit) noexcept
Setup option: receive buffer limit.
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.
virtual bool Reconnect()
Reconnect the client (synchronous)
C++ Server project definitions.