ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.pm
Revision: 1.7
Committed: Sat Oct 27 14:54:20 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
Changes since 1.6: +22 -17 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 EV - perl interface to libevent, monkey.org/~provos/libevent/
4
5 =head1 SYNOPSIS
6
7 use EV;
8
9 # TIMER
10
11 my $w = EV::timer 2, 0, sub {
12 warn "is called after 2s";
13 };
14
15 my $w = EV::timer 2, 1, sub {
16 warn "is called roughly every 2s (repeat = 1)";
17 };
18
19 undef $w; # destroy event watcher again
20
21 # IO
22
23 my $w = EV::timer_abs 0, 60, sub {
24 warn "is called every minute, on the minute, exactly";
25 };
26
27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub {
28 my ($w, $events) = @_; # all callbacks get the watcher object and event mask
29 if ($events & EV::TIMEOUT) {
30 warn "nothign received on stdin for 10 seconds, retrying";
31 } else {
32 warn "stdin is readable, you entered: ", <STDIN>;
33 }
34 };
35 $w->timeout (10);
36
37 # MAINLOOP
38 EV::dispatch; # loop as long as watchers are active
39 EV::loop; # the same thing
40 EV::loop EV::LOOP_ONCE;
41 EV::loop EV::LOOP_ONSHOT;
42
43 =head1 DESCRIPTION
44
45 This module provides an interface to libevent
46 (L<http://monkey.org/~provos/libevent/>). You probably should acquaint
47 yourself with its documentation and source code to be able to use this
48 module fully.
49
50 Please note thta this module disables the libevent EPOLL method by
51 default, see BUGS, below, if you need to enable it.
52
53 =cut
54
55 package EV;
56
57 use strict;
58
59 BEGIN {
60 our $VERSION = '0.01';
61 use XSLoader;
62 XSLoader::load "EV", $VERSION;
63 }
64
65 =head1 FUNCTIONAL INTERFACE
66
67 =over 4
68
69 =item $EV::NPRI
70
71 How many priority levels are available.
72
73 =item $time = EV::now
74
75 Returns the time in (fractional) seconds since the epoch.
76
77 =item $version = EV::version
78
79 =item $method = EV::method
80
81 Return version string and event polling method used.
82
83 =item EV::loop $flags # EV::LOOP_ONCE, EV::LOOP_ONESHOT
84
85 =item EV::loopexit $after
86
87 Exit any active loop or dispatch after C<$after> seconds or immediately if
88 C<$after> is missing or zero.
89
90 =item EV::dispatch
91
92 Same as C<EV::loop 0>.
93
94 =item EV::event $callback
95
96 Creates a new event watcher waiting for nothing, calling the given callback.
97
98 =item my $w = EV::io $fileno_or_fh, $eventmask, $callback
99
100 =item my $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
101
102 As long as the returned watcher object is alive, call the C<$callback>
103 when the events specified in C<$eventmask> happen. Initially, the timeout
104 is disabled.
105
106 Youc an additionall set a timeout to occur on the watcher, but note that
107 this timeout will not be reset when you get an I/O event in the EV::PERSIST
108 case, and reaching a timeout will always stop the watcher even in the
109 EV::PERSIST case.
110
111 If you want a timeout to occur only after a specific time of inactivity, set
112 a repeating timeout and do NOT use EV::PERSIST.
113
114 Eventmask can be one or more of these constants ORed together:
115
116 EV::READ wait until read() wouldn't block anymore
117 EV::WRITE wait until write() wouldn't block anymore
118 EV::PERSIST stay active after a (non-timeout) event occured
119
120 The C<io_ns> variant doesn't add/start the newly created watcher.
121
122 =item my $w = EV::timer $after, $repeat, $callback
123
124 =item my $w = EV::timer_ns $after, $repeat, $callback
125
126 Calls the callback after C<$after> seconds. If C<$repeat> is true, the
127 timer will be restarted after the callback returns. This means that the
128 callback would be called roughly every C<$after> seconds, prolonged by the
129 time the callback takes.
130
131 The C<timer_ns> variant doesn't add/start the newly created watcher.
132
133 =item my $w = EV::timer_abs $at, $interval, $callback
134
135 =item my $w = EV::timer_abs_ns $at, $interval, $callback
136
137 Similar to EV::timer, but the time is given as an absolute point in time
138 (C<$at>), plus an optional C<$interval>.
139
140 If the C<$interval> is zero, then the callback will be called at the time
141 C<$at> if that is in the future, or as soon as possible if its in the
142 past. It will not automatically repeat.
143
144 If the C<$interval> is nonzero, then the watcher will always be scheduled
145 to time out at the next C<$at + integer * $interval> time.
146
147 This can be used to schedule a callback to run at very regular intervals,
148 as long as the processing time is less then the interval (otherwise
149 obviously events will be skipped).
150
151 Another way to think about it (for the mathematically inclined) is that
152 C<timer_abs> will try to tun the callback at the next possible time where
153 C<$time = $at (mod $interval)>, regardless of any time jumps.
154
155 The C<timer_abs_ns> variant doesn't add/start the newly created watcher.
156
157 =item my $w = EV::signal $signum, $callback
158
159 =item my $w = EV::signal_ns $signum, $callback
160
161 Call the callback when signal $signum is received.
162
163 The C<signal_ns> variant doesn't add/start the newly created watcher.
164
165 =back
166
167 =head1 THE EV::Event CLASS
168
169 All EV functions creating an event watcher (designated by C<my $w =>
170 above) support the following methods on the returned watcher object:
171
172 =over 4
173
174 =item $w->add ($timeout)
175
176 Stops and (re-)starts the event watcher, setting the optional timeout to
177 the given value, or clearing the timeout if none is given.
178
179 =item $w->start
180
181 Stops and (re-)starts the event watcher without touching the timeout.
182
183 =item $w->del
184
185 =item $w->stop
186
187 Stop the event watcher if it was started.
188
189 =item $current_callback = $w->cb
190
191 =item $old_callback = $w->cb ($new_callback)
192
193 Return the previously set callback and optionally set a new one.
194
195 =item $current_fh = $w->fh
196
197 =item $old_fh = $w->fh ($new_fh)
198
199 Returns the previously set filehandle and optionally set a new one.
200
201 =item $current_eventmask = $w->events
202
203 =item $old_eventmask = $w->events ($new_eventmask)
204
205 Returns the previously set event mask and optionally set a new one.
206
207 =item $w->timeout ($after, $repeat)
208
209 Resets the timeout (see C<EV::timer> for details).
210
211 =item $w->timeout_abs ($at, $interval)
212
213 Resets the timeout (see C<EV::timer_abs> for details).
214
215 =item $w->priority_set ($priority)
216
217 Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI).
218
219 =back
220
221 =head1 BUGS
222
223 Lots. Libevent itself isn't well tested and rather buggy, and this module
224 is quite new at the moment.
225
226 Please note that the epoll method is not, in general, reliable in programs
227 that use fork (even if no libveent calls are being made in the forked
228 process). If your program behaves erratically, try setting the environment
229 variable C<EVENT_NOEPOLL> first when running the program.
230
231 In general, if you fork, then you can only use the EV module in one of the
232 children.
233
234 =cut
235
236 our $NPRI = 4;
237 our $BASE = init;
238 priority_init $NPRI;
239
240 push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"];
241
242 1;
243
244 =head1 SEE ALSO
245
246 L<EV::DNS>, L<event(3)>, L<event.h>, L<evdns.h>.
247 L<EV::AnyEvent>.
248
249 =head1 AUTHOR
250
251 Marc Lehmann <schmorp@schmorp.de>
252 http://home.schmorp.de/
253
254 =cut
255