Multiplexer that allows to read from multiple sockets. More...
|Default constructor. More...|
|SocketSelector (const SocketSelector ©)|
|Copy constructor. More...|
|void||add (Socket &socket)|
|Add a new socket to the selector. More...|
|void||remove (Socket &socket)|
|Remove a socket from the selector. More...|
|Remove all the sockets stored in the selector. More...|
|bool||wait (Time timeout=Time::Zero)|
|Wait until one or more sockets are ready to receive. More...|
|bool||isReady (Socket &socket) const|
|Test a socket to know if it is ready to receive data. More...|
|SocketSelector &||operator= (const SocketSelector &right)|
|Overload of assignment operator. More...|
Multiplexer that allows to read from multiple sockets.
Socket selectors provide a way to wait until some data is available on a set of sockets, instead of just one.
This is convenient when you have multiple sockets that may possibly receive data, but you don't know which one will be ready first. In particular, it avoids to use a thread for each socket; with selectors, a single thread can handle all the sockets.
All types of sockets can be used in a selector:
A selector doesn't store its own copies of the sockets (socket classes are not copyable anyway), it simply keeps a reference to the original sockets that you pass to the "add" function. Therefore, you can't use the selector as a socket container, you must store them outside and make sure that they are alive as long as they are used in the selector.
Using a selector is simple:
- populate the selector with all the sockets that you want to observe
- make it wait until there is data available on any of the sockets
- test each socket to find out which ones are ready
- See also
Constructor & Destructor Documentation
|sf::SocketSelector::SocketSelector||(||const SocketSelector &||copy||)|
copy Instance to copy
Member Function Documentation
|void sf::SocketSelector::add||(||Socket &||socket||)|
Add a new socket to the selector.
This function keeps a weak reference to the socket, so you have to make sure that the socket is not destroyed while it is stored in the selector. This function does nothing if the socket is not valid.
socket Reference to the socket to add
|bool sf::SocketSelector::isReady||(||Socket &||socket||)||const|
Test a socket to know if it is ready to receive data.
This function must be used after a call to Wait, to know which sockets are ready to receive data. If a socket is ready, a call to receive will never block because we know that there is data available to read. Note that if this function returns true for a TcpListener, this means that it is ready to accept a new connection.
socket Socket to test
- True if the socket is ready to read, false otherwise
- See also
Overload of assignment operator.
right Instance to assign
- Reference to self
|void sf::SocketSelector::remove||(||Socket &||socket||)|
Wait until one or more sockets are ready to receive.
This function returns as soon as at least one socket has some data available to be received. To know which sockets are ready, use the isReady function. If you use a timeout and no socket is ready before the timeout is over, the function returns false.
timeout Maximum time to wait, (use Time::Zero for infinity)
- True if there are sockets ready, false otherwise
- See also
The documentation for this class was generated from the following file: