comparison C++/SocketListener.h @ 170:fd138f2a9773

Add C++ portable sockets
author David Demelier <markand@malikania.fr>
date Tue, 10 Sep 2013 15:17:56 +0200
parents
children ce3e1c3d6fed
comparison
equal deleted inserted replaced
169:29531c2f8213 170:fd138f2a9773
1 /*
2 * SocketListener.h -- portable select() wrapper
3 *
4 * Copyright (c) 2013, David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _SOCKET_LISTENER_H_
20 #define _SOCKET_LISTENER_H_
21
22 #include <vector>
23
24 #include "Socket.h"
25
26 /**
27 * @class SocketTimeout
28 * @brief thrown when a timeout occured
29 */
30 class SocketTimeout : public std::exception
31 {
32 public:
33 virtual const char * what(void) const throw();
34 };
35
36 class SocketListener
37 {
38 public:
39
40 private:
41 std::vector<Socket> m_clients;
42
43 public:
44 /**
45 * Add a socket to listen to.
46 *
47 * @param s the socket
48 */
49 void add(Socket &s);
50
51 /**
52 * Remove a socket from the list.
53 *
54 * @param s the socket
55 */
56 void remove(Socket &s);
57
58 /**
59 * Remove every sockets in the listener.
60 */
61 void clear();
62
63 /**
64 * Wait for an event in the socket list. If both s and us are set to 0 then
65 * it waits indefinitely.
66 *
67 * @param s the timeout in seconds
68 * @param us the timeout in milliseconds
69 * @return the socket ready
70 * @throw SocketError on error
71 * @throw SocketTimeout on timeout
72 */
73 Socket &select(int s = 0, int us = 0);
74 };
75
76 #endif // !_SOCKET_LISTENER_H_