libsocket
Public Types | Public Member Functions | Private Attributes | List of all members
libsocket::epollset< SocketT > Class Template Reference

Class abstracting calls to the epoll API of Linux. More...

#include <epoll.hpp>

Public Types

typedef std::pair< std::vector< SocketT * >, std::vector< SocketT * > > ready_socks
 

Public Member Functions

 epollset (unsigned int maxevents=128)
 Construct a new epollset. More...
 
 epollset (const epollset &)=delete
 
 epollset (epollset &&)
 Move constructor for epollsets. More...
 
void add_fd (SocketT &sock, int method)
 Add a socket to an epollset. More...
 
void del_fd (const SocketT &sock)
 Remove a file descriptor from an epoll set. More...
 
ready_socks wait (int timeout=-1)
 Wait for an event on any file descriptor. More...
 

Private Attributes

unsigned int maxevents
 maxevents is passed to epoll_wait. More...
 
int epollfd
 The file descriptor used by the epoll API. More...
 
struct epoll_event * events
 Array of structures, filled on the return of epoll_wait. More...
 

Detailed Description

template<typename SocketT>
class libsocket::epollset< SocketT >

Class abstracting calls to the epoll API of Linux.

Being a template, this class needs to be initialized with a class. The choice is easy if you only have one type of socket to run epoll on; however, if you have (for example) an inet_stream and an inet_stream_server socket, you will need to use either socket or inet_socket as type parameter so the automatic upcasting works as intended.

Upon return of wait(), you'll have to identify the sockets (using the address – this class is practically zero-copy) and cast them back with the help of dynamic_cast.

Definition at line 69 of file epoll.hpp.

Constructor & Destructor Documentation

◆ epollset() [1/2]

template<typename SocketT >
libsocket::epollset< SocketT >::epollset ( unsigned int  maxevs = 128)

Construct a new epollset.

Parameters
maxevsMaximum event number returned by epoll_wait. Default is 128.

Definition at line 102 of file epoll.hpp.

◆ epollset() [2/2]

template<typename SocketT >
libsocket::epollset< SocketT >::epollset ( epollset< SocketT > &&  new_epollset)

Move constructor for epollsets.

Definition at line 116 of file epoll.hpp.

Member Function Documentation

◆ add_fd()

template<typename SocketT>
void libsocket::epollset< SocketT >::add_fd ( SocketT &  sock,
int  method 
)

Add a socket to an epollset.

Parameters
sockThe socket to be added.
methodAny combination of LIBSOCKET_READ and LIBSOCKET_WRITE.

Definition at line 138 of file epoll.hpp.

◆ del_fd()

template<typename SocketT>
void libsocket::epollset< SocketT >::del_fd ( const SocketT &  sock)

Remove a file descriptor from an epoll set.

Parameters
sockThe socket to remove.

Definition at line 160 of file epoll.hpp.

◆ wait()

template<typename SocketT >
epollset< SocketT >::ready_socks libsocket::epollset< SocketT >::wait ( int  timeout = -1)

Wait for an event on any file descriptor.

Parameters
timeout(default: -1) Timeout, in milliseconds, after which to return if no event has occurred yet. -1 for indefinite waiting, 0 for non-blocking access.
Returns
A pair of vectors containing pointers to SocketTs: (ready_for_reading[],ready_for_writing[]). With r being the returned pair, access the sockets using statements like r.first.size() > 0 ? r.first[0] : nullptr or the like.

Hint: If you need to know what socket has been returned, maintain a mapping from socket pointers to some identification code. Using that mapping, you will be able to identify the sockets.

Hint 2: It never does any harm to check the length of the returned vectors; with the included example http_epoll.cpp, spurious empty-returning epoll calls could be observed. However, it is not clear if this is a bug in the libsocket implementation or usual behavior of epoll.

Definition at line 188 of file epoll.hpp.

Member Data Documentation

◆ maxevents

template<typename SocketT>
unsigned int libsocket::epollset< SocketT >::maxevents
private

maxevents is passed to epoll_wait.

Definition at line 85 of file epoll.hpp.

◆ epollfd

template<typename SocketT>
int libsocket::epollset< SocketT >::epollfd
private

The file descriptor used by the epoll API.

Definition at line 87 of file epoll.hpp.

◆ events

template<typename SocketT>
struct epoll_event* libsocket::epollset< SocketT >::events
private

Array of structures, filled on the return of epoll_wait.

Definition at line 89 of file epoll.hpp.


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