2 iom.h -- generic I/O multiplexor
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 typedef double tstamp;
36 simplevec<io_watcher *> iow;
37 simplevec<time_watcher *> tw; // actually a heap
39 void idle_cb (time_watcher &w); time_watcher *idle;
42 void reg (io_watcher *w);
43 void unreg (io_watcher *w);
44 void reg (time_watcher *w);
45 void unreg (time_watcher *w);
53 extern io_manager iom; // a singleton, together with it's construction/destruction problems.
55 enum { EVENT_READ = 1, EVENT_WRITE = 2 };
57 struct io_watcher : callback2<void, io_watcher &, short> {
61 template<class O1, class O2>
62 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
63 : callback2<void, io_watcher &, short>(object,method)
68 void set(int fd_, short events_) { fd = fd_; events = events_; }
70 void set(short events_) { set (fd, events_); }
71 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
72 void stop () { iom.unreg (this); }
75 #define TSTAMP_CANCEL -1.
77 struct time_watcher : callback1<void, time_watcher &> {
80 template<class O1, class O2>
81 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
82 : callback1<void, time_watcher &>(object,method)
89 void set (tstamp when) { at = when; }
90 void operator ()() { trigger (); }
91 void start () { iom.reg (this); }
92 void start (tstamp when) { set (when); iom.reg (this); }
93 void stop () { iom.unreg (this); }
95 void reset (tstamp when = TSTAMP_CANCEL)