author | Tero Marttila <terom@fixme.fi> |
Fri, 16 Jan 2009 22:03:49 +0200 | |
changeset 400 | d64bf28c4340 |
parent 386 | 2f019ecb4aa9 |
permissions | -rw-r--r-- |
380 | 1 |
#ifndef NETWORK_REACTOR_HH |
2 |
#define NETWORK_REACTOR_HH |
|
3 |
||
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
4 |
/** |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
5 |
* @file |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
6 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
7 |
* A select() based reactor for NetworkSocket's |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
8 |
*/ |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
9 |
|
380 | 10 |
// forward-declare |
11 |
class NetworkReactor; |
|
12 |
||
13 |
/** |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
14 |
* Event types to poll for |
380 | 15 |
*/ |
16 |
enum NetworkPollBit { |
|
17 |
POLL_READ = 0x01, |
|
18 |
POLL_WRITE = 0x02, |
|
19 |
}; |
|
20 |
||
21 |
/** |
|
22 |
* Poll event bitmask of NetworkPollBit's |
|
23 |
*/ |
|
24 |
typedef int NetworkPollMask; |
|
25 |
||
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
26 |
#include "Platform.hh" |
380 | 27 |
#include "Socket.hh" |
28 |
#include "Error.hh" |
|
29 |
||
30 |
#include <list> |
|
31 |
||
32 |
/** |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
33 |
* A reactor maintains a list of NetworkSockets (which must register themselves to their reactor), and providers |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
34 |
* readyness notification of non-blocking I/O operations. |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
35 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
36 |
* NetworkSockets must call add_socket(), whereup the NetworkReactor will use NetworkSocket::get_poll() to build the |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
37 |
* set of sockets to notify. If activity is detected, I will call NetworkSocket::notify(). |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
38 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
39 |
* The poll() method can then be used by the application main loop to do timed sleeps, waking up on socket activity and |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
40 |
* driving the sockets. |
380 | 41 |
*/ |
42 |
class NetworkReactor { |
|
43 |
protected: |
|
44 |
std::list<NetworkSocket*> sockets; |
|
45 |
||
46 |
public: |
|
47 |
/** |
|
48 |
* Construct the empty reactor |
|
49 |
*/ |
|
50 |
NetworkReactor (void); |
|
51 |
||
52 |
/** |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
53 |
* The global NetworkReactor, used by default for all NetworkSockets. |
380 | 54 |
*/ |
55 |
static NetworkReactor *current; |
|
56 |
||
57 |
/** |
|
58 |
* Add a NetworkSocket to our list of sockets. The desired notification states are fetched directly from the |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
59 |
* socket itself using NetworkSocket::get_poll(), and it will be notified using NetworkSocket::notify(). |
380 | 60 |
* |
61 |
* @param socket the socket to watch |
|
62 |
*/ |
|
63 |
void add_socket (NetworkSocket *socket) { sockets.push_back(socket); } |
|
64 |
||
65 |
/** |
|
66 |
* Remove a NetworkSocket from our list of sockets. |
|
67 |
* |
|
68 |
* @param socket the socket to stop watching |
|
69 |
*/ |
|
70 |
void remove_socket (NetworkSocket *socket) { sockets.remove(socket); } |
|
71 |
||
72 |
/** |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
73 |
* Wait for activity on any of the sockets registered and with notification enabled, driving them using |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
74 |
* NetworkSocket::notify() if select() indicates activity. This method will sleep at most \a timeout, returning |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
75 |
* once there was socket activity, or the timeout ran out. |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
76 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
386
diff
changeset
|
77 |
* This is intended to be particularly efficient if the socket list is empty. |
380 | 78 |
*/ |
79 |
void poll (timeval *timeout = NULL); |
|
80 |
}; |
|
81 |
||
82 |
/** |
|
83 |
* Reactor error |
|
84 |
*/ |
|
85 |
class NetworkReactorError : public NetworkErrno { |
|
86 |
public: |
|
87 |
NetworkReactorError (std::string op) : NetworkErrno(op) { } |
|
88 |
}; |
|
89 |
||
90 |
#endif |