libsocket
|
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 |
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.
libsocket::selectset< SockT >::selectset | ( | void | ) |
void libsocket::selectset< SocketT >::add_fd | ( | const SocketT & | sock, |
int | method | ||
) |
Add a socket to the internal sets.
sock | Some socket. May be server or client socket. |
method | LIBSOCKET_READ /LIBSOCKET_WRITE or an OR ed combination thereof. Determines if the socket is checked on the possibility to read or to write. |
Definition at line 146 of file select.hpp.
selectset< SockT >::ready_socks libsocket::selectset< SockT >::wait | ( | long long | microsecs = 0 | ) |
Waits for a possibility to read or write data to emerge.
microsecs | A timeout in microseconds (for 5 seconds simply write 5e6, for ten seconds 10e6 and so on). 0 for no timeout (possibly infinite waiting) |
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.
|
private |
A map containing the relations between the filedescriptors and the socket objects
Definition at line 105 of file select.hpp.
|
private |
Stores if the class has been initiated.
Definition at line 108 of file select.hpp.