380
|
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>
|
386
|
29 |
#include <errno.h>
|
380
|
30 |
#else
|
|
31 |
#error "This network code won't compile on win32 :)"
|
|
32 |
#endif
|
|
33 |
|
|
34 |
#include <list>
|
|
35 |
|
|
36 |
/**
|
|
37 |
* A Reactor manages a set of NetworkSockets, providing readyness notification and a poll method
|
|
38 |
*/
|
|
39 |
class NetworkReactor {
|
|
40 |
protected:
|
|
41 |
std::list<NetworkSocket*> sockets;
|
|
42 |
|
|
43 |
public:
|
|
44 |
/**
|
|
45 |
* Construct the empty reactor
|
|
46 |
*/
|
|
47 |
NetworkReactor (void);
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Our static global reactor
|
|
51 |
*/
|
|
52 |
static NetworkReactor *current;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the
|
|
56 |
* socket itself.
|
|
57 |
*
|
|
58 |
* @param socket the socket to watch
|
|
59 |
*/
|
|
60 |
void add_socket (NetworkSocket *socket) { sockets.push_back(socket); }
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Remove a NetworkSocket from our list of sockets.
|
|
64 |
*
|
|
65 |
* @param socket the socket to stop watching
|
|
66 |
*/
|
|
67 |
void remove_socket (NetworkSocket *socket) { sockets.remove(socket); }
|
|
68 |
|
|
69 |
/**
|
|
70 |
* Poll our sockets and drive any I/O, optionally sleeping for the given timeout. This is efficient if our
|
|
71 |
* sockets list is empty.
|
|
72 |
*/
|
|
73 |
void poll (timeval *timeout = NULL);
|
|
74 |
};
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Reactor error
|
|
78 |
*/
|
|
79 |
class NetworkReactorError : public NetworkErrno {
|
|
80 |
public:
|
|
81 |
NetworkReactorError (std::string op) : NetworkErrno(op) { }
|
|
82 |
};
|
|
83 |
|
|
84 |
#endif
|