.. index:: pair: class; EE::Network::SocketSelector .. _doxid-class_e_e_1_1_network_1_1_socket_selector: class EE::Network::SocketSelector ================================= .. toctree:: :hidden: Overview ~~~~~~~~ Multiplexer that allows to read from multiple sockets :ref:`Socket ` selectors provide a way to wait until some data is available on a set of sockets, instead of just one. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class SocketSelector { public: // construction :ref:`SocketSelector`(); :ref:`SocketSelector`(const SocketSelector& copy); :ref:`~SocketSelector`(); // methods void :ref:`add`(:ref:`Socket`& socket); void :ref:`remove`(:ref:`Socket`& socket); void :ref:`clear`(); bool :ref:`wait`(:ref:`Time` timeout = Time::Zero); bool :ref:`isReady`(:ref:`Socket`& socket) const; SocketSelector& :ref:`operator=`(const SocketSelector& right); }; .. _details-class_e_e_1_1_network_1_1_socket_selector: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Multiplexer that allows to read from multiple sockets :ref:`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: * EE::NetworkTcpListener * EE::NetworkTcpSocket * EE::NetworkUdpSocket 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: * 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: .. ref-code-block:: cpp // Create a socket to listen to new connections TcpListener listener; listener.listen(55001); // Create a list to store the future clients std::vector clients; // Create a selector 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 TcpSocket* client = new TcpSocket; if (listener.accept(*client) == 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 { // Error, we won't get a new connection, delete the socket delete client; } } else { // The listener socket is not ready, test all other sockets (the clients) for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it) { TcpSocket& client = **it; if (selector.isReady(client)) { // The client has sent some data, we can receive it Packet packet; if (client.Receive(packet) == Socket::Done) { ... } } } } } } .. rubric:: See also: :ref:`EE::Network::Socket ` Construction ------------ .. index:: pair: function; SocketSelector .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1af5227ee5d6159406de3a7db20efa35b6: .. ref-code-block:: cpp :class: doxyrest-title-code-block SocketSelector() Default constructor. .. index:: pair: function; SocketSelector .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1aef3f310459da4da3cb9448b742027f93: .. ref-code-block:: cpp :class: doxyrest-title-code-block SocketSelector(const SocketSelector& copy) Copy constructor. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - copy - Instance to copy .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1acfd463076337628e9efdecda799d1129: .. ref-code-block:: cpp :class: doxyrest-title-code-block ~SocketSelector() Destructor. Methods ------- .. index:: pair: function; add .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1a7c6efbb9f0907ef9ede2d0c8ee38c5e4: .. ref-code-block:: cpp :class: doxyrest-title-code-block void add(:ref:`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. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - socket - Reference to the socket to add .. rubric:: See also: Remove, Clear .. index:: pair: function; remove .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1ab65835a597967ed4c5ce5a16e485dc77: .. ref-code-block:: cpp :class: doxyrest-title-code-block void remove(:ref:`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. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - socket - Reference to the socket to remove .. rubric:: See also: Add, Clear .. index:: pair: function; clear .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1a7e39a12f5b27e8969720855f8909e103: .. ref-code-block:: cpp :class: doxyrest-title-code-block 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. .. rubric:: See also: Add, Remove .. index:: pair: function; wait .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1a2b41cf572a436697bdbda4385c4f72f6: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool wait(:ref:`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. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - timeout - Maximum time to wait, (use Time::Zero for infinity) .. rubric:: Returns: True if there are sockets ready, false otherwise .. rubric:: See also: IsReady .. index:: pair: function; isReady .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1aa31f96691ccaaf60ceb324ef2710ae23: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool isReady(:ref:`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 :ref:`TcpListener `, this means that it is ready to accept a new connection. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - socket - :ref:`Socket ` to test .. rubric:: Returns: True if the socket is ready to read, false otherwise .. rubric:: See also: IsReady .. index:: pair: function; operator= .. _doxid-class_e_e_1_1_network_1_1_socket_selector_1aa20156d22549d14769b23c93e8bbf02f: .. ref-code-block:: cpp :class: doxyrest-title-code-block SocketSelector& operator=(const SocketSelector& right) Overload of assignment operator. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - right - Instance to assign .. rubric:: Returns: Reference to self