![]() |
Tacopie
3.0.0
Tacopie is a TCP Client & Server C++11 library.
|
#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_service & | operator= (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_socket > | m_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_t > | m_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 |
service that operates IO Handling. It polls sockets for input and output, processes read and write operations and calls the appropriate callbacks.
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
tacopie::io_service::io_service | ( | std::size_t | nb_threads = __TACOPIE_IO_SERVICE_NB_WORKERS | ) |
ctor
nb_threads | defines the number of background threads that will be used to process read and write callbacks. This must be a strictly positive value. |
tacopie::io_service::~io_service | ( | void | ) |
dtor
|
delete |
copy ctor
|
private |
init m_poll_fds_info simply initialize m_polled_fds variable based on m_tracked_sockets information
|
delete |
assignment operator
|
private |
poll worker function main loop of the background thread in charge of the io_service in charge of polling fds
|
private |
process poll detected events called whenever select/poll completed to check read and write availablity
|
private |
process read event reported by select/poll for a given socket
fd | fd for which a read event has been reported |
socket | tracked_socket associated to the given fd |
|
private |
process write event reported by select/poll for a given socket
fd | fd for which a write event has been reported |
socket | tracked_socket associated to the given fd |
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
nb_threads | number of workers |
void tacopie::io_service::set_rd_callback | ( | const tcp_socket & | socket, |
const event_callback_t & | event_callback | ||
) |
void tacopie::io_service::set_wr_callback | ( | const tcp_socket & | socket, |
const event_callback_t & | event_callback | ||
) |
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
void tacopie::io_service::untrack | ( | const tcp_socket & | socket | ) |
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
|
private |
callback workers
|
private |
fd associated to the pipe used to wake up the poll call
|
private |
poll thread
|
private |
data structure given to select (list of fds to poll)
|
private |
data structure given to select (list of fds to poll for read)
|
private |
whether the worker should stop or not
|
private |
tracked sockets
|
private |
thread safety
|
private |
condition variable to wait on removal
|
private |
data structure given to select (list of fds to poll for write)