… | |
… | |
23 | |
23 | |
24 | #include <sys/poll.h> |
24 | #include <sys/poll.h> |
25 | |
25 | |
26 | #include "slog.h" |
26 | #include "slog.h" |
27 | |
27 | |
28 | typedef double timestamp; |
28 | typedef double tstamp; |
29 | |
29 | |
30 | extern timestamp NOW; |
30 | extern tstamp NOW; |
31 | |
31 | |
32 | template<class R, class A> class callback; |
32 | template<class R, class A> class callback; |
33 | struct io_watcher; |
33 | struct io_watcher; |
34 | struct time_watcher; |
34 | struct time_watcher; |
35 | |
35 | |
36 | class io_manager { |
36 | class io_manager { |
37 | vector<pollfd> pfs; |
37 | vector<pollfd> pfs; |
38 | vector<io_watcher *> iow; |
38 | vector<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
39 | vector<time_watcher *> tw; // actually a heap |
|
|
40 | |
|
|
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
40 | public: |
42 | public: |
41 | |
43 | |
42 | // register a watcher |
44 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
45 | void reg (int fd, short events, io_watcher *w); |
44 | void unreg (io_watcher *w); |
46 | void unreg (io_watcher *w); |
… | |
… | |
87 | |
89 | |
88 | R call(A arg) |
90 | R call(A arg) |
89 | { |
91 | { |
90 | return prxy->call (obj, meth, arg); |
92 | return prxy->call (obj, meth, arg); |
91 | } |
93 | } |
|
|
94 | |
|
|
95 | R operator ()(A arg) |
|
|
96 | { |
|
|
97 | return call (arg); |
|
|
98 | } |
92 | }; |
99 | }; |
93 | |
100 | |
94 | struct io_watcher : callback<void, short> { |
101 | struct io_watcher : callback<void, short> { |
95 | template<class O1, class O2> |
102 | template<class O1, class O2> |
96 | io_watcher (int fd, short events, O1 *object, void (O2::*method)(short revents)) |
103 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
97 | : callback<void, short>(object,method) |
104 | : callback<void, short>(object,method) |
|
|
105 | { } |
|
|
106 | |
|
|
107 | void start (int fd, short events) |
98 | { |
108 | { |
99 | iom.reg (fd, events, this); |
109 | iom.reg (fd, events, this); |
100 | } |
110 | } |
101 | |
111 | |
102 | ~io_watcher () |
112 | void stop () |
103 | { |
113 | { |
104 | iom.unreg (this); |
114 | iom.unreg (this); |
105 | } |
115 | } |
106 | }; |
116 | }; |
107 | |
117 | |
108 | struct time_watcher : callback<void, timestamp &> { |
118 | #define TSTAMP_CANCEL -1. |
109 | timestamp at; |
|
|
110 | |
119 | |
111 | void set (timestamp when); |
120 | struct time_watcher : callback<void, tstamp &> { |
|
|
121 | tstamp at; |
112 | |
122 | |
113 | template<class O1, class O2> |
123 | template<class O1, class O2> |
114 | time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &)) |
124 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
115 | : callback<void, timestamp &>(object,method) |
125 | : callback<void, tstamp &>(object,method) |
116 | , at(when) |
126 | { } |
|
|
127 | |
|
|
128 | void set (tstamp when); |
|
|
129 | void trigger (); |
|
|
130 | |
|
|
131 | void operator ()() |
117 | { |
132 | { |
118 | iom.reg (this); |
133 | trigger (); |
119 | } |
134 | } |
120 | |
135 | |
121 | ~time_watcher () |
136 | void start (); |
|
|
137 | void start (tstamp when) |
|
|
138 | { |
|
|
139 | set (when); |
|
|
140 | } |
|
|
141 | |
|
|
142 | void stop () |
122 | { |
143 | { |
123 | iom.unreg (this); |
144 | iom.unreg (this); |
|
|
145 | } |
|
|
146 | |
|
|
147 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
148 | { |
|
|
149 | stop (); |
|
|
150 | at = when; |
124 | } |
151 | } |
125 | }; |
152 | }; |
126 | |
153 | |
127 | #endif |
154 | #endif |
128 | |
155 | |