equal
deleted
inserted
replaced
1 #ifndef NETWORK_REACTOR_HH |
1 #ifndef NETWORK_REACTOR_HH |
2 #define NETWORK_REACTOR_HH |
2 #define NETWORK_REACTOR_HH |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * |
|
7 * A select() based reactor for NetworkSocket's |
|
8 */ |
3 |
9 |
4 // forward-declare |
10 // forward-declare |
5 class NetworkReactor; |
11 class NetworkReactor; |
6 |
12 |
7 /** |
13 /** |
8 * Events to poll for |
14 * Event types to poll for |
9 */ |
15 */ |
10 enum NetworkPollBit { |
16 enum NetworkPollBit { |
11 POLL_READ = 0x01, |
17 POLL_READ = 0x01, |
12 POLL_WRITE = 0x02, |
18 POLL_WRITE = 0x02, |
13 }; |
19 }; |
15 /** |
21 /** |
16 * Poll event bitmask of NetworkPollBit's |
22 * Poll event bitmask of NetworkPollBit's |
17 */ |
23 */ |
18 typedef int NetworkPollMask; |
24 typedef int NetworkPollMask; |
19 |
25 |
|
26 #include "Platform.hh" |
20 #include "Socket.hh" |
27 #include "Socket.hh" |
21 #include "Error.hh" |
28 #include "Error.hh" |
22 |
|
23 /* |
|
24 * Platform-specific includes |
|
25 */ |
|
26 #ifndef WIN32 |
|
27 // linux |
|
28 #include <sys/select.h> |
|
29 #include <errno.h> |
|
30 #else |
|
31 #error "This network code won't compile on win32 :)" |
|
32 #endif |
|
33 |
29 |
34 #include <list> |
30 #include <list> |
35 |
31 |
36 /** |
32 /** |
37 * A Reactor manages a set of NetworkSockets, providing readyness notification and a poll method |
33 * A reactor maintains a list of NetworkSockets (which must register themselves to their reactor), and providers |
|
34 * readyness notification of non-blocking I/O operations. |
|
35 * |
|
36 * NetworkSockets must call add_socket(), whereup the NetworkReactor will use NetworkSocket::get_poll() to build the |
|
37 * set of sockets to notify. If activity is detected, I will call NetworkSocket::notify(). |
|
38 * |
|
39 * The poll() method can then be used by the application main loop to do timed sleeps, waking up on socket activity and |
|
40 * driving the sockets. |
38 */ |
41 */ |
39 class NetworkReactor { |
42 class NetworkReactor { |
40 protected: |
43 protected: |
41 std::list<NetworkSocket*> sockets; |
44 std::list<NetworkSocket*> sockets; |
42 |
45 |
45 * Construct the empty reactor |
48 * Construct the empty reactor |
46 */ |
49 */ |
47 NetworkReactor (void); |
50 NetworkReactor (void); |
48 |
51 |
49 /** |
52 /** |
50 * Our static global reactor |
53 * The global NetworkReactor, used by default for all NetworkSockets. |
51 */ |
54 */ |
52 static NetworkReactor *current; |
55 static NetworkReactor *current; |
53 |
56 |
54 /** |
57 /** |
55 * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the |
58 * Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the |
56 * socket itself. |
59 * socket itself using NetworkSocket::get_poll(), and it will be notified using NetworkSocket::notify(). |
57 * |
60 * |
58 * @param socket the socket to watch |
61 * @param socket the socket to watch |
59 */ |
62 */ |
60 void add_socket (NetworkSocket *socket) { sockets.push_back(socket); } |
63 void add_socket (NetworkSocket *socket) { sockets.push_back(socket); } |
61 |
64 |
65 * @param socket the socket to stop watching |
68 * @param socket the socket to stop watching |
66 */ |
69 */ |
67 void remove_socket (NetworkSocket *socket) { sockets.remove(socket); } |
70 void remove_socket (NetworkSocket *socket) { sockets.remove(socket); } |
68 |
71 |
69 /** |
72 /** |
70 * Poll our sockets and drive any I/O, optionally sleeping for the given timeout. This is efficient if our |
73 * Wait for activity on any of the sockets registered and with notification enabled, driving them using |
71 * sockets list is empty. |
74 * NetworkSocket::notify() if select() indicates activity. This method will sleep at most \a timeout, returning |
|
75 * once there was socket activity, or the timeout ran out. |
|
76 * |
|
77 * This is intended to be particularly efficient if the socket list is empty. |
72 */ |
78 */ |
73 void poll (timeval *timeout = NULL); |
79 void poll (timeval *timeout = NULL); |
74 }; |
80 }; |
75 |
81 |
76 /** |
82 /** |