Multiplexer that allows to read from multiple sockets. More...
#include <SocketSelector.hpp>
Public Member Functions | |
SocketSelector () | |
Default constructor. | |
SocketSelector (const SocketSelector ©) | |
Copy constructor. | |
~SocketSelector () | |
Destructor. | |
void | Add (Socket &socket) |
Add a new socket to the selector. | |
void | Remove (Socket &socket) |
Remove a socket from the selector. | |
void | Clear () |
Remove all the sockets stored in the selector. | |
bool | Wait (Uint32 timeout=0) |
Wait until one or more sockets are ready to receive. | |
bool | IsReady (Socket &socket) const |
Test a socket to know if it is ready to receive data. | |
SocketSelector & | operator= (const SocketSelector &right) |
Overload of assignment operator. |
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 oustide and make sure that they are alive as long as they are used in the selector.
Using a selector is simple:
Usage example:
// Create a socket to listen to new connections sf::TcpListener listener; listener.Listen(55001); // Create a list to store the future clients std::list<sf::TcpSocket*> clients; // Create a selector sf::SocketSelector selector; // Add the listener to the selector selector.Add(listener); // Endless loop that waits for new connections while (running) { // Make the selector wait for data on any socket if (selector.Wait()) { // Test the listener if (selector.IsReady(listener)) { // The listener is ready: there is a pending connection sf::TcpSocket* client = new sf::TcpSocket; if (listener.accept(*client) == sf::Socket::Done) { // Add the new client to the clients list clients.push_back(client); // Add the new client to the selector so that we will // be notified when he sends something selector.Add(*client); } } else { // The listener socket is not ready, test all other sockets (the clients) for (std::list<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); ++it) { sf::TcpSocket& client = **it; if (selector.IsReady(client)) { // The client has sent some data, we can receive it sf::Packet packet; if (client.Receive(packet) == sf::Socket::Done) { ... } } } } } }
Definition at line 42 of file SocketSelector.hpp.
sf::SocketSelector::SocketSelector | ( | ) |
Default constructor.
sf::SocketSelector::SocketSelector | ( | const SocketSelector & | copy | ) |
Copy constructor.
copy | Instance to copy |
sf::SocketSelector::~SocketSelector | ( | ) |
Destructor.
void sf::SocketSelector::Add | ( | Socket & | socket | ) |
void sf::SocketSelector::Clear | ( | ) |
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 |
SocketSelector& sf::SocketSelector::operator= | ( | const SocketSelector & | right | ) |
Overload of assignment operator.
right | Instance to assign |
void sf::SocketSelector::Remove | ( | Socket & | socket | ) |
bool sf::SocketSelector::Wait | ( | Uint32 | timeout = 0 |
) |
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, in milliseconds (use 0 for infinity) |