Class SocketSelector#

Class Documentation#

class SocketSelector#

Multiplexer that allows to read form multiple sockets.

Public Functions

SocketSelector()#

Constructor.

~SocketSelector() = default#

Destructor.

SocketSelector(const SocketSelector &copy)#

Copy constructor.

SocketSelector(SocketSelector&&) noexcept#

Move constructor.

SocketSelector &operator=(SocketSelector&&) noexcept#

Move assignment operator.

void 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.

Parameters:

socket – Reference to the socket to add.

void Remove(Socket &socket) const#

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.

void Clear() const#

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 Wait(Utils::TimeStep timeout) const#

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 TRAP::Utils::TimeStep(0.0f) for infinity).

Returns:

True if there are sockets ready, false otherwise.

bool 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.

Parameters:

socketSocket to test.

Returns:

True if the socket is ready to read, false otherwise.

SocketSelector &operator=(const SocketSelector &right)#

Overload of assignment operator.

Parameters:

right – Instance to assign.

Returns:

Reference to self.