SFML 3.0.0

- Main Page
- Topics
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Loading...
Searching...
No Matches
Public Member Functions | List of all members
sf::SocketSelector Class Reference
Network module

Multiplexer that allows to read from multiple sockets. More...

`#include <SFML/Network/SocketSelector.hpp>`

## Public Member Functions
| SocketSelector ()
| Default constructor.
| ~SocketSelector ()
| Destructor.
| SocketSelector (const SocketSelector &copy)
| Copy constructor.
SocketSelector & | operator= (const SocketSelector &right)
| Overload of assignment operator.
| SocketSelector (SocketSelector &&) noexcept
| Move constructor.
SocketSelector & | operator= (SocketSelector &&) noexcept
| Move assignment.
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 (Time timeout=Time::Zero)
| 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.

## Detailed Description

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:

- `sf::TcpListener`
- `sf::TcpSocket`
- `sf::UdpSocket`

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

Usage example:

// Create a socket to listen to new connections
sf::TcpListener listener;
if (listener.listen(55001) != sf::Socket::Status::Done)
{
// Handle error...
}
// Create a list to store the future clients
std::vector<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;
if (listener.accept(client) == sf::Socket::Status::Done)
{
// Add the new client to the selector so that we will
// be notified when they send something
selector.add(client);
// Add the new client to the clients list
clients.push_back(std::move(client));
}
else
{
// Handle error...
}
}
else
{
// The listener socket is not ready, test all other sockets (the clients)
for (sf::TcpSocket& client : clients)
{
if (selector.isReady(client))
{
// The client has sent some data, we can receive it
sf::Packet packet;
if (client.receive(packet) == sf::Socket::Status::Done)
{
...
}
}
}
}
}
}
sf::Packet
Utility class to build blocks of data to transfer over the network.
Definition Packet.hpp:49
sf::SocketSelector
Multiplexer that allows to read from multiple sockets.
Definition SocketSelector.hpp:46
sf::SocketSelector::isReady
bool isReady(Socket &socket) const
Test a socket to know if it is ready to receive data.
sf::SocketSelector::wait
bool wait(Time timeout=Time::Zero)
Wait until one or more sockets are ready to receive.
sf::SocketSelector::add
void add(Socket &socket)
Add a new socket to the selector.
sf::Socket::Status::Done
@ Done
The socket has sent / received the data.
sf::TcpListener
Socket that listens to new TCP connections.
Definition TcpListener.hpp:45
sf::TcpListener::listen
Status listen(unsigned short port, IpAddress address=IpAddress::Any)
Start listening for incoming connection attempts.
sf::TcpListener::accept
Status accept(TcpSocket &socket)
Accept a new connection.
sf::TcpSocket
Specialized socket using the TCP protocol.
Definition TcpSocket.hpp:54
sf::TcpSocket::receive
Status receive(void *data, std::size_t size, std::size_t &received)
Receive raw data from the remote peer.

See also
  `sf::Socket`

Definition at line 45 of file SocketSelector.hpp.

## Constructor & Destructor Documentation

## ◆ SocketSelector() [1/3]

sf::SocketSelector::SocketSelector | ( |  | ) |

Default constructor.

## ◆ ~SocketSelector()

sf::SocketSelector::~SocketSelector | ( |  | ) |

Destructor.

## ◆ SocketSelector() [2/3]

sf::SocketSelector::SocketSelector | ( | const SocketSelector & | copy | ) |

Copy constructor.

Parameters

  copy | Instance to copy

## ◆ SocketSelector() [3/3]

sf::SocketSelector::SocketSelector | ( | SocketSelector && |  | ) |
| noexcept

Move constructor.

## Member Function Documentation

## ◆ add()

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.

Parameters

  socket | Reference to the socket to add

See also
  `remove`, `clear`

## ◆ clear()

void sf::SocketSelector::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.

See also
  `add`, `remove`

## ◆ isReady()

bool sf::SocketSelector::isReady | ( | Socket & | socket | ) | const
| nodiscard

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

  socket | Socket to test

Returns
  `true` if the socket is ready to read, `false` otherwise

See also
  `isReady`

## ◆ operator=() [1/2]

SocketSelector & sf::SocketSelector::operator= | ( | const SocketSelector & | right | ) |

Overload of assignment operator.

Parameters

  right | Instance to assign

Returns
  Reference to self

## ◆ operator=() [2/2]

SocketSelector & sf::SocketSelector::operator= | ( | SocketSelector && |  | ) |
| noexcept

Move assignment.

## ◆ remove()

void sf::SocketSelector::remove | ( | Socket & | 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

See also
  `add`, `clear`

## ◆ wait()

bool sf::SocketSelector::wait | ( | Time | timeout = Time::Zero | ) |
| nodiscard

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

See also
  `isReady`

The documentation for this class was generated from the following file:

- SocketSelector.hpp