… | |
… | |
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<const 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 | |
40 | |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
41 | void idle_cb (time_watcher &w); time_watcher *idle; |
42 | public: |
42 | public: |
43 | |
43 | |
44 | void reschedule_time_watchers (); |
44 | void reschedule_time_watchers (); |
45 | |
45 | |
46 | // register a watcher |
46 | // register a watcher |
47 | void reg (int fd, short events, io_watcher *w); |
47 | void reg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
49 | void reg (time_watcher *w); |
49 | void reg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
51 | |
51 | |
52 | void loop (); |
52 | void loop (); |
… | |
… | |
55 | ~io_manager (); |
55 | ~io_manager (); |
56 | }; |
56 | }; |
57 | |
57 | |
58 | extern io_manager iom; |
58 | extern io_manager iom; |
59 | |
59 | |
60 | struct io_watcher : callback2<void, int, short> { |
60 | struct io_watcher : callback2<void, io_watcher &, short> { |
|
|
61 | int fd; |
|
|
62 | short events; |
|
|
63 | |
61 | template<class O1, class O2> |
64 | template<class O1, class O2> |
62 | io_watcher (O1 *object, void (O2::*method)(int fd, short revents)) |
65 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
63 | : callback2<void, int, short>(object,method) |
66 | : callback2<void, io_watcher &, short>(object,method) |
64 | { } |
67 | { } |
65 | |
68 | |
66 | ~io_watcher () |
69 | ~io_watcher () |
67 | { |
70 | { |
68 | iom.unreg (this); |
71 | iom.unreg (this); |
69 | } |
72 | } |
70 | |
73 | |
71 | void start (int fd, short events) |
74 | void start (int fd_, short events_) |
72 | { |
75 | { |
|
|
76 | fd = fd_; |
|
|
77 | events = events_; |
73 | iom.reg (fd, events, this); |
78 | iom.reg (this); |
74 | } |
79 | } |
75 | |
80 | |
76 | void stop () |
81 | void stop () |
77 | { |
82 | { |
78 | iom.unreg (this); |
83 | iom.unreg (this); |
79 | } |
84 | } |
80 | }; |
85 | }; |
81 | |
86 | |
82 | #define TSTAMP_CANCEL -1. |
87 | #define TSTAMP_CANCEL -1. |
83 | |
88 | |
84 | struct time_watcher : callback1<void, tstamp &> { |
89 | struct time_watcher : callback1<void, time_watcher &> { |
85 | bool registered; // already registered? |
90 | bool registered; // already registered? |
86 | tstamp at; |
91 | tstamp at; |
87 | |
92 | |
88 | template<class O1, class O2> |
93 | template<class O1, class O2> |
89 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
94 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
90 | : callback1<void, tstamp &>(object,method) |
95 | : callback1<void, time_watcher &>(object,method) |
91 | , registered(false) |
96 | , registered(false) |
92 | { } |
97 | { } |
93 | |
98 | |
94 | ~time_watcher () |
99 | ~time_watcher () |
95 | { |
100 | { |