Mercurial > code
view C++/SocketListener.h @ 245:3c12f0e8bbb9
Converter: add cstring missing
Sockets:
* Starts a PIMPL idiom to safely cast Socket with the future SocketSsl and be
able to return a class of type "Socket" whilst the interface returns
effectively a SocketSsl.
* Rename some function to be more cute to use like getDomain -> domain().
* SocketListener now owns completely sockets and must be moved to it, the
dedicated function select() always return a socket owned by SocketListener.
* Operation close is not needed anymore but the function stays in case it's
needed.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 28 Sep 2014 21:15:26 +0200 |
parents | 9fc5f917872b |
children | 806dbb6011c7 |
line wrap: on
line source
/* * SocketListener.h -- portable select() wrapper * * Copyright (c) 2013, David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _SOCKET_LISTENER_H_ #define _SOCKET_LISTENER_H_ #include <vector> #include "Socket.h" /** * @class SocketTimeout * @brief thrown when a timeout occured */ class SocketTimeout final : public std::exception { public: const char *what() const noexcept override; }; /** * @class SocketListener * @brief Synchronous multiplexing * * Convenient wrapper around the select() system call. */ class SocketListener final { private: std::vector<Socket> m_sockets; public: /** * Create a socket listener with a specific number of sockets to reserve. * * @param count the number of socket to reserve (default: 0) */ SocketListener(int count = 0); /** * Add a socket to listen to. * * @param s the socket */ void add(Socket &&s); /** * Remove a socket from the list. * * @param s the socket */ void remove(const Socket &s); /** * Remove every sockets in the listener. */ void clear(); /** * Get the number of clients in listener. * * @return the number of clients in the listener. */ unsigned size(); /** * Wait for an event in the socket list. If both s and us are set to 0 then * it waits indefinitely. * * Taking ownership of the returned socket is undefined behaviour. * * @param s the timeout in seconds * @param us the timeout in milliseconds * @see take * @return the socket ready owner by the SocketListener * @throw SocketError on error * @throw SocketTimeout on timeout */ Socket &select(int s = 0, int us = 0); /** * Take back all sockets that listener own. * * @param func the function to call (void f(Socket &&s)) */ template <typename Func> void take(Func func) { for (auto &x : m_sockets) func(std::move(x)); clear(); } }; #endif // !_SOCKET_LISTENER_H_