Mercurial > code
comparison 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 |
comparison
equal
deleted
inserted
replaced
244:777bc3cb665a | 245:3c12f0e8bbb9 |
---|---|
25 | 25 |
26 /** | 26 /** |
27 * @class SocketTimeout | 27 * @class SocketTimeout |
28 * @brief thrown when a timeout occured | 28 * @brief thrown when a timeout occured |
29 */ | 29 */ |
30 class SocketTimeout : public std::exception { | 30 class SocketTimeout final : public std::exception { |
31 public: | 31 public: |
32 virtual const char * what(void) const throw(); | 32 const char *what() const noexcept override; |
33 }; | 33 }; |
34 | 34 |
35 /** | 35 /** |
36 * @class SocketListener | 36 * @class SocketListener |
37 * @brief Synchronous multiplexing | 37 * @brief Synchronous multiplexing |
38 * | 38 * |
39 * Convenient wrapper around the select() system call. | 39 * Convenient wrapper around the select() system call. |
40 */ | 40 */ |
41 class SocketListener { | 41 class SocketListener final { |
42 private: | 42 private: |
43 std::vector<Socket> m_clients; | 43 std::vector<Socket> m_sockets; |
44 | 44 |
45 public: | 45 public: |
46 /** | |
47 * Create a socket listener with a specific number of sockets to reserve. | |
48 * | |
49 * @param count the number of socket to reserve (default: 0) | |
50 */ | |
51 SocketListener(int count = 0); | |
52 | |
46 /** | 53 /** |
47 * Add a socket to listen to. | 54 * Add a socket to listen to. |
48 * | 55 * |
49 * @param s the socket | 56 * @param s the socket |
50 */ | 57 */ |
51 void add(Socket &s); | 58 void add(Socket &&s); |
52 | 59 |
53 /** | 60 /** |
54 * Remove a socket from the list. | 61 * Remove a socket from the list. |
55 * | 62 * |
56 * @param s the socket | 63 * @param s the socket |
57 */ | 64 */ |
58 void remove(Socket &s); | 65 void remove(const Socket &s); |
59 | 66 |
60 /** | 67 /** |
61 * Remove every sockets in the listener. | 68 * Remove every sockets in the listener. |
62 */ | 69 */ |
63 void clear(); | 70 void clear(); |
71 | 78 |
72 /** | 79 /** |
73 * Wait for an event in the socket list. If both s and us are set to 0 then | 80 * Wait for an event in the socket list. If both s and us are set to 0 then |
74 * it waits indefinitely. | 81 * it waits indefinitely. |
75 * | 82 * |
83 * Taking ownership of the returned socket is undefined behaviour. | |
84 * | |
76 * @param s the timeout in seconds | 85 * @param s the timeout in seconds |
77 * @param us the timeout in milliseconds | 86 * @param us the timeout in milliseconds |
78 * @return the socket ready | 87 * @see take |
88 * @return the socket ready owner by the SocketListener | |
79 * @throw SocketError on error | 89 * @throw SocketError on error |
80 * @throw SocketTimeout on timeout | 90 * @throw SocketTimeout on timeout |
81 */ | 91 */ |
82 Socket &select(int s = 0, int us = 0); | 92 Socket &select(int s = 0, int us = 0); |
93 | |
94 /** | |
95 * Take back all sockets that listener own. | |
96 * | |
97 * @param func the function to call (void f(Socket &&s)) | |
98 */ | |
99 template <typename Func> | |
100 void take(Func func) | |
101 { | |
102 for (auto &x : m_sockets) | |
103 func(std::move(x)); | |
104 | |
105 clear(); | |
106 } | |
83 }; | 107 }; |
84 | 108 |
85 #endif // !_SOCKET_LISTENER_H_ | 109 #endif // !_SOCKET_LISTENER_H_ |