Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Public Member Functions | List of all members
gf::SocketSelector Class Reference

A socket selector. More...

#include <gf/SocketSelector.h>

Inheritance diagram for gf::SocketSelector:
Inheritance graph
[legend]

Public Member Functions

 SocketSelector ()
 Constructor. More...
 
void addSocket (Socket &socket)
 Add a socket to the selector. More...
 
void removeSocket (Socket &socket)
 Remove a socket from the selector. More...
 
void clear ()
 Clear all the sockets in the selector. More...
 
SocketSelectorStatus wait (Time duration=seconds(-1))
 Wait for an event of the set of sockets. More...
 
bool isReady (Socket &socket)
 Tell if the provided socket is ready. More...
 

Detailed Description

A socket selector.

gf::TcpListener listener(service);
selector.addSocket(listener);
std::vector<gf::TcpSocket> sockets;
for (;;) {
for (auto& socket : sockets) {
if (selector.isReady(socket)) {
// read something on the socket
// ...
}
}
if (selector.isReady(listener)) {
// the listener is ready, accept a new connection
gf::TcpSocket socket = listener.accept();
sockets.push_back(std::move(socket));
selector.addSocket(sockets.back());
}
}
}

Constructor & Destructor Documentation

◆ SocketSelector()

gf::SocketSelector::SocketSelector ( )

Constructor.

Member Function Documentation

◆ addSocket()

void gf::SocketSelector::addSocket ( Socket socket)

Add a socket to the selector.

◆ clear()

void gf::SocketSelector::clear ( )

Clear all the sockets in the selector.

◆ isReady()

bool gf::SocketSelector::isReady ( Socket socket)

Tell if the provided socket is ready.

◆ removeSocket()

void gf::SocketSelector::removeSocket ( Socket socket)

Remove a socket from the selector.

◆ wait()

SocketSelectorStatus gf::SocketSelector::wait ( Time  duration = seconds(-1))

Wait for an event of the set of sockets.

A duration for a timeout can be provided. In this case, if the duration is negative, it indicates an infinite timeout. If the duration is zero, the call will return immediately. Otherwise, the timeout can occur after the provided duration if no event occurred.

Parameters
durationThe duration for the timeout