Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
Classes | Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
tacopie::tcp_client Class Reference

#include <tcp_client.hpp>

Classes

struct  read_request
 
struct  read_result
 
struct  write_request
 
struct  write_result
 

Public Types

typedef std::function< void(read_result &)> async_read_callback_t
 
typedef std::function< void(write_result &)> async_write_callback_t
 
typedef std::function< void()> disconnection_handler_t
 

Public Member Functions

 tcp_client (std::uint32_t num_io_workers=1)
 ctor & dtor More...
 
 ~tcp_client (void)
 
 tcp_client (tcp_socket &&socket)
 
 tcp_client (const tcp_client &)=delete
 copy ctor More...
 
tcp_clientoperator= (const tcp_client &)=delete
 assignment operator More...
 
bool operator== (const tcp_client &rhs) const
 
bool operator!= (const tcp_client &rhs) const
 
const std::string & get_host (void) const
 
std::uint32_t get_port (void) const
 
void connect (const std::string &host, std::uint32_t port, std::uint32_t timeout_msecs=0)
 
void disconnect (bool wait_for_removal=false)
 
bool is_connected (void) const
 
void async_read (const read_request &request)
 
void async_write (const write_request &request)
 
tacopie::tcp_socketget_socket (void)
 
const tacopie::tcp_socketget_socket (void) const
 
const std::shared_ptr< tacopie::io_service > & get_io_service (void) const
 
void set_on_disconnection_handler (const disconnection_handler_t &disconnection_handler)
 

Private Member Functions

void call_disconnection_handler (void)
 
void on_read_available (fd_t fd)
 
void on_write_available (fd_t fd)
 
void clear_read_requests (void)
 
void clear_write_requests (void)
 
async_read_callback_t process_read (read_result &result)
 
async_write_callback_t process_write (write_result &result)
 

Private Attributes

std::shared_ptr< io_servicem_io_service
 
tacopie::tcp_socket m_socket
 
std::atomic< bool > m_is_connected = ATOMIC_VAR_INIT(false)
 
std::queue< read_requestm_read_requests
 
std::queue< write_requestm_write_requests
 
std::mutex m_read_requests_mtx
 
std::mutex m_write_requests_mtx
 
disconnection_handler_t m_disconnection_handler
 

Member Typedef Documentation

◆ async_read_callback_t

callback to be called on async read completion takes the read_result as a parameter

◆ async_write_callback_t

callback to be called on async write completion takes the write_result as a parameter

◆ disconnection_handler_t

typedef std::function<void()> tacopie::tcp_client::disconnection_handler_t

disconnection handle called whenever a disconnection occured

Constructor & Destructor Documentation

◆ tcp_client() [1/3]

tacopie::tcp_client::tcp_client ( std::uint32_t  num_io_workers = 1)

ctor & dtor

◆ ~tcp_client()

tacopie::tcp_client::~tcp_client ( void  )

◆ tcp_client() [2/3]

tacopie::tcp_client::tcp_client ( tcp_socket &&  socket)
explicit

custom ctor build socket from existing socket

Parameters
sockettcp_socket instance to be used for building the client (socket will be moved)

◆ tcp_client() [3/3]

tacopie::tcp_client::tcp_client ( const tcp_client )
delete

copy ctor

Member Function Documentation

◆ async_read()

void tacopie::tcp_client::async_read ( const read_request request)

async read operation

Parameters
requestread request information

◆ async_write()

void tacopie::tcp_client::async_write ( const write_request request)

async write operation

Parameters
requestwrite request information

◆ call_disconnection_handler()

void tacopie::tcp_client::call_disconnection_handler ( void  )
private

Call the user-defined disconnection handler

◆ clear_read_requests()

void tacopie::tcp_client::clear_read_requests ( void  )
private

Clear pending read requests (basically empty the queue of read requests)

◆ clear_write_requests()

void tacopie::tcp_client::clear_write_requests ( void  )
private

Clear pending write requests (basically empty the queue of write requests)

◆ connect()

void tacopie::tcp_client::connect ( const std::string &  host,
std::uint32_t  port,
std::uint32_t  timeout_msecs = 0 
)

Connect the socket to the remote server.

Parameters
hostHostname of the target server
portPort of the target server
timeout_msecsmaximum time to connect (will block until connect succeed or timeout expire). 0 will block undefinitely. If timeout expires, connection fails

◆ disconnect()

void tacopie::tcp_client::disconnect ( bool  wait_for_removal = false)

Disconnect the tcp_client if it was currently connected.

Parameters
wait_for_removalWhen sets to true, disconnect blocks until the underlying TCP client has been effectively removed from the io_service and that all the underlying callbacks have completed.

◆ get_host()

const std::string& tacopie::tcp_client::get_host ( void  ) const
Returns
the hostname associated with the underlying socket.

◆ get_io_service()

const std::shared_ptr<tacopie::io_service>& tacopie::tcp_client::get_io_service ( void  ) const
Returns
io service monitoring this tcp connection

◆ get_port()

std::uint32_t tacopie::tcp_client::get_port ( void  ) const
Returns
the port associated with the underlying socket.

◆ get_socket() [1/2]

tacopie::tcp_socket& tacopie::tcp_client::get_socket ( void  )
Returns
underlying tcp_socket (non-const version)

◆ get_socket() [2/2]

const tacopie::tcp_socket& tacopie::tcp_client::get_socket ( void  ) const
Returns
underlying tcp_socket (const version)

◆ is_connected()

bool tacopie::tcp_client::is_connected ( void  ) const
Returns
whether the client is currently connected or not

◆ on_read_available()

void tacopie::tcp_client::on_read_available ( fd_t  fd)
private

io service read callback called by the io service whenever the socket is readable

Parameters
fdfile description of the socket for which the read is available

◆ on_write_available()

void tacopie::tcp_client::on_write_available ( fd_t  fd)
private

io service write callback called by the io service whenever the socket is writable

Parameters
fdfile description of the socket for which the write is available

◆ operator!=()

bool tacopie::tcp_client::operator!= ( const tcp_client rhs) const

comparison operator

Returns
true when the underlying sockets are different (different file descriptor or socket type).

◆ operator=()

tcp_client& tacopie::tcp_client::operator= ( const tcp_client )
delete

assignment operator

◆ operator==()

bool tacopie::tcp_client::operator== ( const tcp_client rhs) const

comparison operator

Returns
true when the underlying sockets are the same (same file descriptor and socket type).

◆ process_read()

async_read_callback_t tacopie::tcp_client::process_read ( read_result result)
private

process read operations when available basically called whenever on_read_available is called and try to read from the socket handle possible case of failure and fill in the result

Parameters
resultresult of the read operation
Returns
the callback to be executed (set in the read request) on read completion (may be null)

◆ process_write()

async_write_callback_t tacopie::tcp_client::process_write ( write_result result)
private

process write operations when available basically called whenever on_write_available is called and try to write to the socket handle possible case of failure and fill in the result

Parameters
resultresult of the write operation
Returns
the callback to be executed (set in the write request) on read completion (may be null)

◆ set_on_disconnection_handler()

void tacopie::tcp_client::set_on_disconnection_handler ( const disconnection_handler_t disconnection_handler)

set on disconnection handler

Parameters
disconnection_handlerthe handler to be called on disconnection

Member Data Documentation

◆ m_disconnection_handler

disconnection_handler_t tacopie::tcp_client::m_disconnection_handler
private

disconnection handler

◆ m_io_service

std::shared_ptr<io_service> tacopie::tcp_client::m_io_service
private

store io_service prevent deletion of io_service before the tcp_client itself

◆ m_is_connected

std::atomic<bool> tacopie::tcp_client::m_is_connected = ATOMIC_VAR_INIT(false)
private

whether the client is currently connected or not

◆ m_read_requests

std::queue<read_request> tacopie::tcp_client::m_read_requests
private

read requests

◆ m_read_requests_mtx

std::mutex tacopie::tcp_client::m_read_requests_mtx
private

read requests thread safety

◆ m_socket

tacopie::tcp_socket tacopie::tcp_client::m_socket
private

client socket

◆ m_write_requests

std::queue<write_request> tacopie::tcp_client::m_write_requests
private

write requests

◆ m_write_requests_mtx

std::mutex tacopie::tcp_client::m_write_requests_mtx
private

write requests thread safety


The documentation for this class was generated from the following file: