9#ifndef CPPSERVER_ASIO_TCP_CLIENT_H
10#define CPPSERVER_ASIO_TCP_CLIENT_H
14#include "system/uuid.h"
15#include "time/timespan.h"
29class 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_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; }
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);
307 virtual void onSent(
size_t sent,
size_t pending) {}
324 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
330 std::shared_ptr<Service> _service;
332 std::shared_ptr<asio::io_context> _io_context;
334 asio::io_context::strand _strand;
335 bool _strand_required;
337 std::string _address;
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;
347 uint64_t _bytes_pending;
348 uint64_t _bytes_sending;
349 uint64_t _bytes_sent;
350 uint64_t _bytes_received;
353 size_t _receive_buffer_limit{0};
354 std::vector<uint8_t> _receive_buffer;
355 HandlerStorage _receive_storage;
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;
365 bool _option_keep_alive;
366 bool _option_no_delay;
369 bool DisconnectInternal();
371 bool DisconnectInternalAsync(
bool dispatch);
382 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?
TCPClient & operator=(TCPClient &&)=delete
virtual bool Connect()
Connect the client (synchronous).
const std::string & address() const noexcept
Get the server address.
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.
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.
virtual void onDisconnecting()
Handle client disconnecting notification.
void SetupReceiveBufferSize(size_t size)
Setup option: receive buffer size.
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
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
uint64_t bytes_pending() const noexcept
Get the number of bytes pending sent by the client.
asio::io_context::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
std::shared_ptr< asio::io_context > & io_context() noexcept
Get the Asio IO context.
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.
virtual bool ConnectAsync()
Connect the client (asynchronous).
const std::string & scheme() const noexcept
Get the scheme name.
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.
asio::ip::tcp::endpoint & endpoint() noexcept
Get the client endpoint.
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.
TCPClient & operator=(const TCPClient &)=delete
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 void onConnecting()
Handle client connecting notification.
virtual bool Reconnect()
Reconnect the client (synchronous).
C++ Server project definitions.