equal
deleted
inserted
replaced
|
1 #ifndef NETWORK_REACTOR_HH |
|
2 #define NETWORK_REACTOR_HH |
|
3 |
|
4 // forward-declare |
|
5 class NetworkReactor; |
|
6 |
|
7 /** |
|
8 * Events to poll for |
|
9 */ |
|
10 enum NetworkPollBit { |
|
11 POLL_READ = 0x01, |
|
12 POLL_WRITE = 0x02, |
|
13 }; |
|
14 |
|
15 /** |
|
16 * Poll event bitmask of NetworkPollBit's |
|
17 */ |
|
18 typedef int NetworkPollMask; |
|
19 |
|
20 #include "Socket.hh" |
|
21 #include "Error.hh" |
|
22 |
|
23 /* |
|
24 * Platform-specific includes |
|
25 */ |
|
26 #ifndef WIN32 |
|
27 // linux |
|
28 #include <sys/select.h> |
|
29 #else |
|
30 #error "This network code won't compile on win32 :)" |
|
31 #endif |
|
32 |
|
33 #include <list> |
|
34 |
|
35 /** |
|
36 * A Reactor manages a set of NetworkSockets, providing readyness notification and a poll method |
|
37 */ |
|
38 class NetworkReactor { |
|
39 protected: |
|
40 std::list<NetworkSocket*> sockets; |
|
41 |
|
42 public: |
|
43 /** |
|
44 * Construct the empty reactor |
|
45 */ |
|
46 NetworkReactor (void); |
|
47 |
|
48 /** |
|
49 * Our static global reactor |
|
50 */ |
|
51 static NetworkReactor *current; |
|
52 |
|
53 /** |
|
54 * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the |
|
55 * socket itself. |
|
56 * |
|
57 * @param socket the socket to watch |
|
58 */ |
|
59 void add_socket (NetworkSocket *socket) { sockets.push_back(socket); } |
|
60 |
|
61 /** |
|
62 * Remove a NetworkSocket from our list of sockets. |
|
63 * |
|
64 * @param socket the socket to stop watching |
|
65 */ |
|
66 void remove_socket (NetworkSocket *socket) { sockets.remove(socket); } |
|
67 |
|
68 /** |
|
69 * Poll our sockets and drive any I/O, optionally sleeping for the given timeout. This is efficient if our |
|
70 * sockets list is empty. |
|
71 */ |
|
72 void poll (timeval *timeout = NULL); |
|
73 }; |
|
74 |
|
75 /** |
|
76 * Reactor error |
|
77 */ |
|
78 class NetworkReactorError : public NetworkErrno { |
|
79 public: |
|
80 NetworkReactorError (std::string op) : NetworkErrno(op) { } |
|
81 }; |
|
82 |
|
83 #endif |