… | |
… | |
18 | |
18 | |
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 | |
|
|
24 | #include <cassert> |
23 | |
25 | |
24 | #include <sys/poll.h> |
26 | #include <sys/poll.h> |
25 | |
27 | |
26 | #include "callback.h" |
28 | #include "callback.h" |
27 | #include "slog.h" |
29 | #include "slog.h" |
… | |
… | |
53 | |
55 | |
54 | io_manager (); |
56 | io_manager (); |
55 | ~io_manager (); |
57 | ~io_manager (); |
56 | }; |
58 | }; |
57 | |
59 | |
58 | extern io_manager iom; |
60 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
59 | |
61 | |
60 | struct io_watcher : callback2<void, io_watcher &, short> { |
62 | struct io_watcher : callback2<void, io_watcher &, short> { |
61 | int fd; |
63 | pollfd *p; |
62 | short events; |
|
|
63 | |
64 | |
64 | template<class O1, class O2> |
65 | template<class O1, class O2> |
65 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
66 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
66 | : callback2<void, io_watcher &, short>(object,method) |
67 | : callback2<void, io_watcher &, short>(object,method) |
67 | { } |
68 | { } |
68 | |
69 | |
69 | ~io_watcher () |
70 | ~io_watcher (); |
|
|
71 | |
|
|
72 | void set(int fd, short events) |
70 | { |
73 | { |
71 | iom.unreg (this); |
74 | assert (p); |
|
|
75 | p->fd = fd; |
|
|
76 | p->events = events; |
72 | } |
77 | } |
73 | |
78 | |
74 | void start (int fd_, short events_) |
79 | void set(short events) |
75 | { |
80 | { |
76 | fd = fd_; |
81 | assert (p); |
77 | events = events_; |
82 | p->events = events; |
78 | iom.reg (this); |
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; |
79 | } |
91 | } |
80 | |
92 | |
81 | void stop () |
93 | void stop () |
82 | { |
94 | { |
83 | iom.unreg (this); |
95 | iom.unreg (this); |
… | |
… | |
94 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
106 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
95 | : callback1<void, time_watcher &>(object,method) |
107 | : callback1<void, time_watcher &>(object,method) |
96 | , registered(false) |
108 | , registered(false) |
97 | { } |
109 | { } |
98 | |
110 | |
99 | ~time_watcher () |
111 | ~time_watcher (); |
100 | { |
|
|
101 | iom.unreg (this); |
|
|
102 | } |
|
|
103 | |
112 | |
104 | void set (tstamp when); |
113 | void set (tstamp when); |
105 | void trigger (); |
114 | void trigger (); |
106 | |
115 | |
107 | void operator ()() |
116 | void operator ()() |
… | |
… | |
121 | } |
130 | } |
122 | |
131 | |
123 | void reset (tstamp when = TSTAMP_CANCEL) |
132 | void reset (tstamp when = TSTAMP_CANCEL) |
124 | { |
133 | { |
125 | stop (); |
134 | stop (); |
|
|
135 | |
126 | at = when; |
136 | at = when; |
127 | } |
137 | } |
128 | }; |
138 | }; |
129 | |
139 | |
130 | #endif |
140 | #endif |