View on GitHub

DSFML

dsfml.network.socketselector

A module contianing the SocketSelector class

class SocketSelector;
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.

this();
Default constructor

void add(TcpListener listener);
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.

Params:
TcpListener listener Reference to the listener to add.

void add(TcpSocket socket);
Add a new TcpSocket 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.





Params:
TcpSocket socket Reference to the socket to add,

void add(UdpSocket socket);
Add a new UdpSocket 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.





Params:
UdpSocket socket Reference to the socket to add,

void clear();
Remove all the sockets stored in the selector.

This function doesn't destroy any instance, it simply removes all the references that the selector has to external sockets.

bool isReady(TcpListener listener);
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.

bool isReady(TcpSocket socket);
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.

bool isReady(UdpSocket socket);
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.

void remove(TcpListener socket);
void remove(TcpSocket socket);
void remove(UdpSocket socket);
Remove a socket from the selector.

This function doesn't destroy the socket, it simply removes the reference that the selector has to it.





Parameters

socket = Reference to the socket to remove.

bool wait(Time timeout = Time.Zero);
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.





Parameters

timeout = Maximum time to wait, (use Time::Zero for infinity).





Returns:
True if there are sockets ready, false otherwise.