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::io_service Class Reference

#include <io_service.hpp>

Classes

struct  tracked_socket
 

Public Types

typedef std::function< void(fd_t)> event_callback_t
 

Public Member Functions

 io_service (std::size_t nb_threads=__TACOPIE_IO_SERVICE_NB_WORKERS)
 
 ~io_service (void)
 dtor More...
 
 io_service (const io_service &)=delete
 copy ctor More...
 
io_serviceoperator= (const io_service &)=delete
 assignment operator More...
 
void set_nb_workers (std::size_t nb_threads)
 
void track (const tcp_socket &socket, const event_callback_t &rd_callback=nullptr, const event_callback_t &wr_callback=nullptr)
 track & untrack socket More...
 
void set_rd_callback (const tcp_socket &socket, const event_callback_t &event_callback)
 
void set_wr_callback (const tcp_socket &socket, const event_callback_t &event_callback)
 
void untrack (const tcp_socket &socket)
 
void wait_for_removal (const tcp_socket &socket)
 

Private Member Functions

void poll (void)
 
int init_poll_fds_info (void)
 
void process_events (void)
 
void process_rd_event (const fd_t &fd, tracked_socket &socket)
 
void process_wr_event (const fd_t &fd, tracked_socket &socket)
 

Private Attributes

std::unordered_map< fd_t, tracked_socketm_tracked_sockets
 
std::atomic< bool > m_should_stop
 
std::thread m_poll_worker
 
utils::thread_pool m_callback_workers
 
std::mutex m_tracked_sockets_mtx
 
std::vector< fd_tm_polled_fds
 
fd_set m_rd_set
 
fd_set m_wr_set
 
std::condition_variable m_wait_for_removal_condvar
 
tacopie::self_pipe m_notifier
 

Detailed Description

service that operates IO Handling. It polls sockets for input and output, processes read and write operations and calls the appropriate callbacks.

Member Typedef Documentation

◆ event_callback_t

typedef std::function<void(fd_t)> tacopie::io_service::event_callback_t

callback handler typedef called on new socket event if register to io_service

Constructor & Destructor Documentation

◆ io_service() [1/2]

tacopie::io_service::io_service ( std::size_t  nb_threads = __TACOPIE_IO_SERVICE_NB_WORKERS)

ctor

Parameters
nb_threadsdefines the number of background threads that will be used to process read and write callbacks. This must be a strictly positive value.

◆ ~io_service()

tacopie::io_service::~io_service ( void  )

dtor

◆ io_service() [2/2]

tacopie::io_service::io_service ( const io_service )
delete

copy ctor

Member Function Documentation

◆ init_poll_fds_info()

int tacopie::io_service::init_poll_fds_info ( void  )
private

init m_poll_fds_info simply initialize m_polled_fds variable based on m_tracked_sockets information

Returns
maximum fd value polled

◆ operator=()

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

assignment operator

◆ poll()

void tacopie::io_service::poll ( void  )
private

poll worker function main loop of the background thread in charge of the io_service in charge of polling fds

◆ process_events()

void tacopie::io_service::process_events ( void  )
private

process poll detected events called whenever select/poll completed to check read and write availablity

◆ process_rd_event()

void tacopie::io_service::process_rd_event ( const fd_t fd,
tracked_socket socket 
)
private

process read event reported by select/poll for a given socket

Parameters
fdfd for which a read event has been reported
sockettracked_socket associated to the given fd

◆ process_wr_event()

void tacopie::io_service::process_wr_event ( const fd_t fd,
tracked_socket socket 
)
private

process write event reported by select/poll for a given socket

Parameters
fdfd for which a write event has been reported
sockettracked_socket associated to the given fd

◆ set_nb_workers()

void tacopie::io_service::set_nb_workers ( std::size_t  nb_threads)

reset number of io_service workers assigned to this io_service this can be safely called at runtime, even if the io_service is currently running it can be useful if you need to re-adjust the number of workers

Parameters
nb_threadsnumber of workers

◆ set_rd_callback()

void tacopie::io_service::set_rd_callback ( const tcp_socket socket,
const event_callback_t event_callback 
)

◆ set_wr_callback()

void tacopie::io_service::set_wr_callback ( const tcp_socket socket,
const event_callback_t event_callback 
)

◆ track()

void tacopie::io_service::track ( const tcp_socket socket,
const event_callback_t rd_callback = nullptr,
const event_callback_t wr_callback = nullptr 
)

track & untrack socket

◆ untrack()

void tacopie::io_service::untrack ( const tcp_socket socket)

◆ wait_for_removal()

void tacopie::io_service::wait_for_removal ( const tcp_socket socket)

wait until the socket has been effectively removed basically wait until all pending callbacks are executed

Member Data Documentation

◆ m_callback_workers

utils::thread_pool tacopie::io_service::m_callback_workers
private

callback workers

◆ m_notifier

tacopie::self_pipe tacopie::io_service::m_notifier
private

fd associated to the pipe used to wake up the poll call

◆ m_poll_worker

std::thread tacopie::io_service::m_poll_worker
private

poll thread

◆ m_polled_fds

std::vector<fd_t> tacopie::io_service::m_polled_fds
private

data structure given to select (list of fds to poll)

◆ m_rd_set

fd_set tacopie::io_service::m_rd_set
private

data structure given to select (list of fds to poll for read)

◆ m_should_stop

std::atomic<bool> tacopie::io_service::m_should_stop
private

whether the worker should stop or not

◆ m_tracked_sockets

std::unordered_map<fd_t, tracked_socket> tacopie::io_service::m_tracked_sockets
private

tracked sockets

◆ m_tracked_sockets_mtx

std::mutex tacopie::io_service::m_tracked_sockets_mtx
private

thread safety

◆ m_wait_for_removal_condvar

std::condition_variable tacopie::io_service::m_wait_for_removal_condvar
private

condition variable to wait on removal

◆ m_wr_set

fd_set tacopie::io_service::m_wr_set
private

data structure given to select (list of fds to poll for write)


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