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>
119 virtual bool Connect(
const std::shared_ptr<TCPResolver>& resolver);
141 virtual bool ConnectAsync(
const std::shared_ptr<TCPResolver>& resolver);
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); }
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);
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;
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);
Asio allocate handler wrapper.
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.
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.
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.
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
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
bool option_keep_alive() const noexcept
Get the option: keep alive.
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
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.