1 | /* |
1 | /* |
2 | iom.h -- I/O multiplexor |
2 | iom.h -- generic I/O multiplexor |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
21 | |
22 | |
22 | #include <vector> |
23 | #include <vector> |
23 | |
24 | |
24 | #include <cassert> |
25 | #include <cassert> |
25 | |
26 | |
26 | #include <sys/poll.h> |
27 | #include "poll.h" |
27 | |
28 | |
28 | #include "callback.h" |
29 | #include "callback.h" |
29 | #include "slog.h" |
30 | #include "slog.h" |
30 | |
31 | |
31 | typedef double tstamp; |
32 | typedef double tstamp; |
… | |
… | |
58 | }; |
59 | }; |
59 | |
60 | |
60 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
61 | extern io_manager iom; // a singleton, together with it's construction/destruction problems. |
61 | |
62 | |
62 | struct io_watcher : callback2<void, io_watcher &, short> { |
63 | struct io_watcher : callback2<void, io_watcher &, short> { |
63 | pollfd *p; |
64 | bool registered; // already registered? |
|
|
65 | int fd; |
|
|
66 | short events; |
64 | |
67 | |
65 | template<class O1, class O2> |
68 | template<class O1, class O2> |
66 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
69 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
67 | : callback2<void, io_watcher &, short>(object,method) |
70 | : callback2<void, io_watcher &, short>(object,method) |
|
|
71 | , registered(false) |
68 | { } |
72 | { } |
69 | |
73 | |
70 | ~io_watcher (); |
74 | ~io_watcher (); |
71 | |
75 | |
72 | void set(int fd, short events) |
76 | void set(int fd_, short events_); |
|
|
77 | |
|
|
78 | void set(short events_) |
73 | { |
79 | { |
74 | assert (p); |
80 | set (fd, events_); |
75 | p->fd = fd; |
|
|
76 | p->events = events; |
|
|
77 | } |
81 | } |
78 | |
82 | |
79 | void set(short events) |
83 | void start (int fd_, short events_) |
80 | { |
84 | { |
81 | assert (p); |
85 | set (fd_, events_); |
82 | p->events = events; |
86 | 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; |
|
|
91 | } |
87 | } |
92 | |
88 | |
93 | void stop () |
89 | void stop () |
94 | { |
90 | { |
95 | iom.unreg (this); |
91 | iom.unreg (this); |
… | |
… | |
116 | void operator ()() |
112 | void operator ()() |
117 | { |
113 | { |
118 | trigger (); |
114 | trigger (); |
119 | } |
115 | } |
120 | |
116 | |
121 | void start (); |
117 | void start () |
|
|
118 | { |
|
|
119 | iom.reg (this); |
|
|
120 | } |
|
|
121 | |
122 | void start (tstamp when) |
122 | void start (tstamp when) |
123 | { |
123 | { |
124 | set (when); |
124 | set (when); |
|
|
125 | iom.reg (this); |
125 | } |
126 | } |
126 | |
127 | |
127 | void stop () |
128 | void stop () |
128 | { |
129 | { |
129 | iom.unreg (this); |
130 | iom.unreg (this); |
130 | } |
131 | } |
131 | |
132 | |
132 | void reset (tstamp when = TSTAMP_CANCEL) |
133 | void reset (tstamp when = TSTAMP_CANCEL) |
133 | { |
134 | { |
134 | stop (); |
135 | stop (); |
135 | |
|
|
136 | at = when; |
136 | at = when; |
137 | } |
137 | } |
138 | }; |
138 | }; |
139 | |
139 | |
140 | #endif |
140 | #endif |