… | |
… | |
19 | #ifndef VPE_IOM_H__ |
19 | #ifndef VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
20 | #define VPE_IOM_H__ |
21 | |
21 | |
22 | #include <vector> |
22 | #include <vector> |
23 | |
23 | |
|
|
24 | #include <cassert> |
|
|
25 | |
24 | #include <sys/poll.h> |
26 | #include <sys/poll.h> |
25 | |
27 | |
|
|
28 | #include "callback.h" |
26 | #include "slog.h" |
29 | #include "slog.h" |
27 | |
30 | |
28 | typedef double tstamp; |
31 | typedef double tstamp; |
29 | |
32 | |
30 | extern tstamp NOW; |
33 | extern tstamp NOW; |
31 | |
34 | |
32 | template<class R, class A> class callback; |
|
|
33 | struct io_watcher; |
35 | struct io_watcher; |
34 | struct time_watcher; |
36 | struct time_watcher; |
35 | |
37 | |
36 | class io_manager { |
38 | class io_manager { |
37 | vector<pollfd> pfs; |
39 | vector<pollfd> pfs; |
38 | vector<io_watcher *> iow; |
40 | vector<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
41 | vector<time_watcher *> tw; // actually a heap |
|
|
42 | |
|
|
43 | void idle_cb (time_watcher &w); time_watcher *idle; |
40 | public: |
44 | public: |
41 | |
45 | |
|
|
46 | void reschedule_time_watchers (); |
|
|
47 | |
42 | // register a watcher |
48 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
49 | void reg (io_watcher *w); |
44 | void unreg (io_watcher *w); |
50 | void unreg (io_watcher *w); |
45 | void reg (time_watcher *w); |
51 | void reg (time_watcher *w); |
46 | void unreg (time_watcher *w); |
52 | void unreg (time_watcher *w); |
47 | |
53 | |
48 | void loop (); |
54 | void loop (); |
49 | |
55 | |
50 | io_manager (); |
56 | io_manager (); |
51 | ~io_manager (); |
57 | ~io_manager (); |
52 | }; |
58 | }; |
53 | |
59 | |
54 | extern io_manager iom; |
60 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
55 | |
61 | |
56 | template<class R, class A> |
62 | struct io_watcher : callback2<void, io_watcher &, short> { |
57 | class callback { |
63 | pollfd *p; |
58 | struct object { }; |
|
|
59 | |
64 | |
60 | void *obj; |
65 | template<class O1, class O2> |
61 | R (object::*meth)(A arg); |
66 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
67 | : callback2<void, io_watcher &, short>(object,method) |
|
|
68 | { } |
62 | |
69 | |
63 | // a proxy is a kind of recipe on how to call a specific class method |
70 | ~io_watcher (); |
64 | struct proxy_base { |
|
|
65 | virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
|
|
66 | }; |
|
|
67 | template<class O1, class O2> |
|
|
68 | struct proxy : proxy_base { |
|
|
69 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
|
|
70 | { |
|
|
71 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
|
|
72 | (arg); |
|
|
73 | } |
|
|
74 | }; |
|
|
75 | |
71 | |
76 | proxy_base *prxy; |
72 | void set(int fd, short events) |
77 | |
|
|
78 | public: |
|
|
79 | template<class O1, class O2> |
|
|
80 | callback (O1 *object, void (O2::*method)(A)) |
|
|
81 | { |
73 | { |
82 | static proxy<O1,O2> p; |
74 | assert (p); |
83 | obj = reinterpret_cast<void *>(object); |
75 | p->fd = fd; |
84 | meth = reinterpret_cast<void (object::*)(A)>(method); |
76 | p->events = events; |
85 | prxy = &p; |
|
|
86 | } |
77 | } |
87 | |
78 | |
88 | R call(A arg) |
79 | void set(short events) |
89 | { |
80 | { |
90 | return prxy->call (obj, meth, arg); |
81 | assert (p); |
|
|
82 | p->events = events; |
|
|
83 | } |
|
|
84 | |
|
|
85 | void start (int fd, short events) |
|
|
86 | { |
|
|
87 | iom.reg (this); // make sure pfd is set |
|
|
88 | |
|
|
89 | p->fd = fd; |
|
|
90 | p->events = events; |
91 | } |
91 | } |
92 | |
92 | |
93 | void stop () |
93 | void stop () |
94 | { |
94 | { |
95 | iom.unreg (this); |
95 | iom.unreg (this); |
96 | } |
96 | } |
97 | }; |
97 | }; |
98 | |
98 | |
99 | struct io_watcher : callback<void, short> { |
99 | #define TSTAMP_CANCEL -1. |
100 | template<class O1, class O2> |
|
|
101 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
|
|
102 | : callback<void, short>(object,method) |
|
|
103 | { } |
|
|
104 | |
100 | |
105 | void start (int fd, short events) |
|
|
106 | { |
|
|
107 | iom.reg (fd, events, this); |
|
|
108 | } |
|
|
109 | |
|
|
110 | }; |
|
|
111 | |
|
|
112 | struct time_watcher : callback<void, tstamp &> { |
101 | struct time_watcher : callback1<void, time_watcher &> { |
|
|
102 | bool registered; // already registered? |
113 | tstamp at; |
103 | tstamp at; |
114 | |
104 | |
115 | template<class O1, class O2> |
105 | template<class O1, class O2> |
116 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
106 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
117 | : callback<void, tstamp &>(object,method) |
107 | : callback1<void, time_watcher &>(object,method) |
|
|
108 | , registered(false) |
118 | { } |
109 | { } |
119 | |
110 | |
|
|
111 | ~time_watcher (); |
|
|
112 | |
120 | void set (tstamp when); |
113 | void set (tstamp when); |
|
|
114 | void trigger (); |
121 | |
115 | |
122 | void trigger () |
116 | void operator ()() |
123 | { |
117 | { |
124 | call (at); |
118 | trigger (); |
125 | } |
119 | } |
126 | |
120 | |
|
|
121 | void start (); |
127 | void start (tstamp when = NOW) |
122 | void start (tstamp when) |
128 | { |
123 | { |
129 | set (when); |
124 | set (when); |
|
|
125 | } |
|
|
126 | |
|
|
127 | void stop () |
|
|
128 | { |
|
|
129 | iom.unreg (this); |
|
|
130 | } |
|
|
131 | |
|
|
132 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
133 | { |
|
|
134 | stop (); |
|
|
135 | |
|
|
136 | at = when; |
130 | } |
137 | } |
131 | }; |
138 | }; |
132 | |
139 | |
133 | #endif |
140 | #endif |
134 | |
141 | |