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

selectset provides a simple abstraction over – contrary to its name – poll(2). More...

#include <select.hpp>

Public Types

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

Public Member Functions

 selectset ()
 Constructor. More...
 
void add_fd (const SocketT &sock, int method)
 Add a socket to the internal sets. More...
 
std::pair< std::vector< SocketT * >, std::vector< SocketT * > > wait (long long microsecs=0)
 Waits for a possibility to read or write data to emerge. More...
 

Private Attributes

std::map< int, SocketT * > fdsockmap
 
bool set_up
 Stores if the class has been initiated. More...
 
std::vector< poll::pollfd > pollfd_set
 

Detailed Description

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

selectset provides a simple abstraction over – contrary to its name – poll(2).

To watch different sockets for new data to read or a possibility to write without using threads, use this class. It is rather simple to use; add file descriptors (socket ids) using add_fd() specifying whether to watch them for reading or writing and then call wait(); once there's data to be read or written it returns a std::pair with vectors of SocketT*; the first vector contains sockets ready for reading, the second one contains those sockets ready for writing.

If you poll sockets of different types, you need to use a superclass as template argument to selectset (usually socket or inet_socket) and then dynamic_cast the sockets returned in the ready_socks structure (see examples++/server.cpp for an (artificial) example).

In order to determine which socket in a ready_socks set belongs to which connection/server/etc., you should either use the pointer values of submitted sockets or the file descriptors managed by your sockets (via Socket::getfd()).

Definition at line 102 of file select.hpp.

Constructor & Destructor Documentation

◆ selectset()

template<typename SockT >
libsocket::selectset< SockT >::selectset ( void  )

Constructor.

Initializes the sets.

Definition at line 134 of file select.hpp.

Member Function Documentation

◆ add_fd()

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

Add a socket to the internal sets.

Parameters
sockSome socket. May be server or client socket.
methodLIBSOCKET_READ/LIBSOCKET_WRITE or an ORed combination thereof. Determines if the socket is checked on the possibility to read or to write.

Definition at line 146 of file select.hpp.

◆ wait()

template<typename SockT >
selectset< SockT >::ready_socks libsocket::selectset< SockT >::wait ( long long  microsecs = 0)

Waits for a possibility to read or write data to emerge.

Parameters
microsecsA timeout in microseconds (for 5 seconds simply write 5e6, for ten seconds 10e6 and so on). 0 for no timeout (possibly infinite waiting)
Returns
A pair of vectors of pointers to sockets. Information about the type of socket is lost; use dynamic_cast<>() and check for NULL to re-convert it. The first vector contains sockets available for reading, the second one those available for writing.

Hint: Save pointers to the added objects to be able to compare and distinguish them after wait().

Definition at line 185 of file select.hpp.

Member Data Documentation

◆ fdsockmap

template<typename SocketT>
std::map<int, SocketT*> libsocket::selectset< SocketT >::fdsockmap
private

A map containing the relations between the filedescriptors and the socket objects

Definition at line 105 of file select.hpp.

◆ set_up

template<typename SocketT>
bool libsocket::selectset< SocketT >::set_up
private

Stores if the class has been initiated.

Definition at line 108 of file select.hpp.


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