]>
Commit | Line | Data |
---|---|---|
ab3e8a6c BH |
1 | #include <boost/function.hpp> |
2 | #include <boost/any.hpp> | |
a1dfcec8 | 3 | #include <boost/shared_array.hpp> |
ab3e8a6c BH |
4 | #include <map> |
5 | #include <stdexcept> | |
6 | #include <string> | |
7 | ||
8 | class FDMultiplexerException : public std::runtime_error | |
9 | { | |
10 | public: | |
11 | FDMultiplexerException(const std::string& str) : std::runtime_error(str) | |
12 | {} | |
13 | }; | |
14 | ||
a1dfcec8 BH |
15 | |
16 | /** Very simple FD multiplexer, based on callbacks and boost::any parameters | |
17 | As a special service, this parameter is kept around and can be modified, | |
18 | allowing for state to be stored inside the multiplexer. | |
19 | ||
20 | It has some "interesting" semantics | |
21 | */ | |
ab3e8a6c BH |
22 | class FDMultiplexer |
23 | { | |
24 | protected: | |
25 | typedef boost::function< void(int, boost::any&) > callbackfunc_t; | |
26 | struct Callback | |
27 | { | |
28 | callbackfunc_t d_callback; | |
29 | boost::any d_parameter; | |
30 | }; | |
31 | ||
32 | public: | |
33 | FDMultiplexer() : d_inrun(false) | |
34 | {} | |
35 | virtual ~FDMultiplexer() | |
36 | {} | |
37 | ||
38 | virtual int run(struct timeval* tv=0) = 0; | |
39 | ||
a1dfcec8 | 40 | //! Add an fd to the read watch list - currently an fd can only be on one list at a time! |
ab3e8a6c BH |
41 | virtual void addReadFD(int fd, callbackfunc_t toDo, boost::any parameter=boost::any()) |
42 | { | |
a1dfcec8 | 43 | this->addFD(d_readCallbacks, fd, toDo, parameter); |
ab3e8a6c BH |
44 | } |
45 | ||
a1dfcec8 | 46 | //! Add an fd to the write watch list - currently an fd can only be on one list at a time! |
ab3e8a6c BH |
47 | virtual void addWriteFD(int fd, callbackfunc_t toDo, boost::any parameter=boost::any()) |
48 | { | |
a1dfcec8 | 49 | this->addFD(d_writeCallbacks, fd, toDo, parameter); |
ab3e8a6c BH |
50 | } |
51 | ||
a1dfcec8 | 52 | //! Remove an fd from the read watch list. You can't call this function on an fd that is closed already! |
ab3e8a6c BH |
53 | virtual void removeReadFD(int fd) |
54 | { | |
a1dfcec8 | 55 | this->removeFD(d_readCallbacks, fd); |
ab3e8a6c BH |
56 | } |
57 | ||
a1dfcec8 BH |
58 | //! Remove an fd from the write watch list. You can't call this function on an fd that is closed already! |
59 | virtual void removeWriteFD(int fd) | |
ab3e8a6c | 60 | { |
a1dfcec8 | 61 | this->removeFD(d_writeCallbacks, fd); |
ab3e8a6c BH |
62 | } |
63 | ||
64 | protected: | |
65 | typedef std::map<int, Callback> callbackmap_t; | |
66 | callbackmap_t d_readCallbacks, d_writeCallbacks; | |
ab3e8a6c BH |
67 | |
68 | virtual void addFD(callbackmap_t& cbmap, int fd, callbackfunc_t toDo, boost::any parameter)=0; | |
69 | virtual void removeFD(callbackmap_t& cbmap, int fd)=0; | |
70 | bool d_inrun; | |
a1dfcec8 | 71 | callbackmap_t::iterator d_iter; |
ab3e8a6c BH |
72 | |
73 | }; | |
74 | ||
a1dfcec8 | 75 | FDMultiplexer* getMultiplexer(); |