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

#include <tcp_server.hpp>

Public Types

typedef std::function< bool(const std::shared_ptr< tcp_client > &)> on_new_connection_callback_t
 

Public Member Functions

 tcp_server (void)
 ctor More...
 
 ~tcp_server (void)
 dtor More...
 
 tcp_server (const tcp_server &)=delete
 copy ctor More...
 
tcp_serveroperator= (const tcp_server &)=delete
 assignment operator More...
 
bool operator== (const tcp_server &rhs) const
 
bool operator!= (const tcp_server &rhs) const
 
void start (const std::string &host, std::uint32_t port, const on_new_connection_callback_t &callback=nullptr)
 
void stop (bool wait_for_removal=false, bool recursive_wait_for_removal=true)
 
bool is_running (void) const
 
tcp_socketget_socket (void)
 
const tcp_socketget_socket (void) const
 
const std::shared_ptr< tacopie::io_service > & get_io_service (void) const
 
const std::list< std::shared_ptr< tacopie::tcp_client > > & get_clients (void) const
 

Private Member Functions

void on_read_available (fd_t fd)
 
void on_client_disconnected (const std::shared_ptr< tcp_client > &client)
 

Private Attributes

std::shared_ptr< io_servicem_io_service
 
tacopie::tcp_socket m_socket
 
std::atomic< bool > m_is_running = ATOMIC_VAR_INIT(false)
 
std::list< std::shared_ptr< tacopie::tcp_client > > m_clients
 
std::mutex m_clients_mtx
 
on_new_connection_callback_t m_on_new_connection_callback
 

Detailed Description

tacopie::tcp_server is the class providing TCP Server features. The tcp_client works entirely asynchronously, waiting for the io_service to notify whenever a new client wished to connect.

Member Typedef Documentation

◆ on_new_connection_callback_t

typedef std::function<bool(const std::shared_ptr<tcp_client>&)> tacopie::tcp_server::on_new_connection_callback_t

callback called whenever a new client is connecting to the server

Takes as parameter a shared pointer to the tcp_client that wishes to connect Returning true means connection is handled by tcp_client wrapper and nothing will be done by tcp_server. Returning false means connection is handled by tcp_server, will be stored in an internal list and tcp_client disconection_handler overriden.

Constructor & Destructor Documentation

◆ tcp_server() [1/2]

tacopie::tcp_server::tcp_server ( void  )

ctor

◆ ~tcp_server()

tacopie::tcp_server::~tcp_server ( void  )

dtor

◆ tcp_server() [2/2]

tacopie::tcp_server::tcp_server ( const tcp_server )
delete

copy ctor

Member Function Documentation

◆ get_clients()

const std::list<std::shared_ptr<tacopie::tcp_client> >& tacopie::tcp_server::get_clients ( void  ) const
Returns
the list of tacopie::tcp_client connected to the server.

◆ get_io_service()

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

◆ get_socket() [1/2]

tcp_socket& tacopie::tcp_server::get_socket ( void  )
Returns
the tacopie::tcp_socket associated to the server. (non-const version)

◆ get_socket() [2/2]

const tcp_socket& tacopie::tcp_server::get_socket ( void  ) const
Returns
the tacopie::tcp_socket associated to the server. (const version)

◆ is_running()

bool tacopie::tcp_server::is_running ( void  ) const
Returns
whether the server is currently running or not

◆ on_client_disconnected()

void tacopie::tcp_server::on_client_disconnected ( const std::shared_ptr< tcp_client > &  client)
private

client disconnected called whenever a client disconnected from the tcp_server

Parameters
clientdisconnected client

◆ on_read_available()

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

io service read callback

Parameters
fdsocket that triggered the read callback

◆ operator!=()

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

comparison operator

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

◆ operator=()

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

assignment operator

◆ operator==()

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

comparison operator

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

◆ start()

void tacopie::tcp_server::start ( const std::string &  host,
std::uint32_t  port,
const on_new_connection_callback_t callback = nullptr 
)

Start the tcp_server at the given host and port.

Parameters
hosthostname to be connected to
portport to be connected to
callbackcallback to be called on new connections (may be null, connections are then handled automatically by the tcp_server object)

◆ stop()

void tacopie::tcp_server::stop ( bool  wait_for_removal = false,
bool  recursive_wait_for_removal = true 
)

Disconnect the tcp_server if it was currently running.

Parameters
wait_for_removalWhen sets to true, disconnect blocks until the underlying TCP server has been effectively removed from the io_service and that all the underlying callbacks have completed.
recursive_wait_for_removalWhen sets to true and wait_for_removal is also set to true, blocks until all the underlying TCP client connected to the TCP server have been effectively removed from the io_service and that all the underlying callbacks have completed.

Member Data Documentation

◆ m_clients

std::list<std::shared_ptr<tacopie::tcp_client> > tacopie::tcp_server::m_clients
private

clients

◆ m_clients_mtx

std::mutex tacopie::tcp_server::m_clients_mtx
private

clients thread safety

◆ m_io_service

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

store io_service prevent deletion of io_service before the tcp_server itself

◆ m_is_running

std::atomic<bool> tacopie::tcp_server::m_is_running = ATOMIC_VAR_INIT(false)
private

whether the server is currently running or not

◆ m_on_new_connection_callback

on_new_connection_callback_t tacopie::tcp_server::m_on_new_connection_callback
private

on new connection callback

◆ m_socket

tacopie::tcp_socket tacopie::tcp_server::m_socket
private

server socket


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