ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.pm
(Generate patch)

Comparing EV/EV.pm (file contents):
Revision 1.1 by root, Fri Oct 26 16:50:05 2007 UTC vs.
Revision 1.16 by root, Wed Oct 31 20:10:17 2007 UTC

2 2
3EV - perl interface to libevent, monkey.org/~provos/libevent/ 3EV - perl interface to libevent, monkey.org/~provos/libevent/
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use EV; 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 my $w = EV::timer_abs 0, 60, sub {
22 warn "is called every minute, on the minute, exactly";
23 };
24
25 # IO
26
27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub {
28 my ($w, $revents) = @_; # all callbacks get the watcher object and event mask
29 if ($revents & EV::TIMEOUT) {
30 warn "nothing 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 my $w = EV::timed_io \*STDIN, EV::READ, 30, sub {
38 my ($w, $revents) = @_;
39 if ($revents & EV::TIMEOUT) {
40 warn "nothing entered within 30 seconds, bye bye.\n";
41 $w->stop;
42 } else {
43 my $line = <STDIN>;
44 warn "you entered something, you again have 30 seconds.\n";
45 }
46 };
47
48 # SIGNALS
49
50 my $w = EV::signal 'QUIT', sub {
51 warn "sigquit received\n";
52 };
53
54 my $w = EV::signal 3, sub {
55 warn "sigquit received (this is GNU/Linux, right?)\n";
56 };
57
58 # CHILD/PID STATUS CHANGES
59
60 my $w = EV::child 666, sub {
61 my ($w, $revents, $status) = @_;
62 };
63
64 # MAINLOOP
65 EV::dispatch; # loop as long as watchers are active
66 EV::loop; # the same thing
67 EV::loop EV::LOOP_ONESHOT; # block until some events could be handles
68 EV::loop EV::LOOP_NONBLOCK; # check and handle some events, but do not wait
8 69
9=head1 DESCRIPTION 70=head1 DESCRIPTION
10 71
11This module provides an interface to libevent 72This module provides an interface to libev
12(L<http://monkey.org/~provos/libevent/>). 73(L<http://software.schmorp.de/pkg/libev.html>). You probably should
74acquaint yourself with its documentation and source code to be able to use
75this module fully.
13 76
14=cut 77=cut
15 78
16package EV; 79package EV;
17 80
18use strict; 81use strict;
19 82
20BEGIN { 83BEGIN {
21 our $VERSION = '0.01'; 84 our $VERSION = '0.03';
22 use XSLoader; 85 use XSLoader;
23 XSLoader::load "EV", $VERSION; 86 XSLoader::load "EV", $VERSION;
24} 87}
25 88
26=head1 FUNCTIONAL INTERFACE 89@EV::Io::ISA = "EV::Watcher";
90@EV::Time::ISA = "EV::Watcher";
91@EV::Timer::ISA = "EV::Time";
92@EV::Periodic::ISA = "EV::Time";
93@EV::Signal::ISA = "EV::Watcher";
94@EV::Idle::ISA = "EV::Watcher";
95@EV::Prepare::ISA = "EV::Watcher";
96@EV::Check::ISA = "EV::Watcher";
97
98=head1 BASIC INTERFACE
27 99
28=over 4 100=over 4
29 101
102=item $EV::NPRI
103
104How many priority levels are available.
105
106=item $EV::DIED
107
108Must contain a reference to a function that is called when a callback
109throws an exception (with $@ containing thr error). The default prints an
110informative message and continues.
111
112If this callback throws an exception it will be silently ignored.
113
114=item $time = EV::now
115
116Returns the time in (fractional) seconds since the epoch.
117
118=item $version = EV::version
119
120=item $method = EV::method
121
122Return version string and event polling method used.
123
124=item EV::loop $flags # EV::LOOP_ONCE, EV::LOOP_ONESHOT
125
126=item EV::loopexit $after
127
128Exit any active loop or dispatch after C<$after> seconds or immediately if
129C<$after> is missing or zero.
130
131=item EV::dispatch
132
133Same as C<EV::loop 0>.
134
135=item EV::event $callback
136
137Creates a new event watcher waiting for nothing, calling the given callback.
138
139=item my $w = EV::io $fileno_or_fh, $eventmask, $callback
140
141=item my $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
142
143As long as the returned watcher object is alive, call the C<$callback>
144when the events specified in C<$eventmask> happen. Initially, the timeout
145is disabled.
146
147You can additionall set a timeout to occur on the watcher, but note that
148this timeout will not be reset when you get an I/O event in the EV::PERSIST
149case, and reaching a timeout will always stop the watcher even in the
150EV::PERSIST case.
151
152If you want a timeout to occur only after a specific time of inactivity, set
153a repeating timeout and do NOT use EV::PERSIST.
154
155Eventmask can be one or more of these constants ORed together:
156
157 EV::READ wait until read() wouldn't block anymore
158 EV::WRITE wait until write() wouldn't block anymore
159 EV::PERSIST stay active after a (non-timeout) event occured
160
161The C<io_ns> variant doesn't add/start the newly created watcher.
162
163=item my $w = EV::timed_io $fileno_or_fh, $eventmask, $timeout, $callback
164
165=item my $w = EV::timed_io_ns $fileno_or_fh, $eventmask, $timeout, $callback
166
167Same as C<io> and C<io_ns>, but also specifies a timeout (as if there was
168a call to C<< $w->timeout ($timout, 1) >>. The persist flag is not allowed
169and will automatically be cleared. The watcher will be restarted after each event.
170
171If the timeout is zero or undef, no timeout will be set, and a normal
172watcher (with the persist flag set!) will be created.
173
174This has the effect of timing out after the specified period of inactivity
175has happened.
176
177Due to the design of libevent, this is also relatively inefficient, having
178one or two io watchers and a separate timeout watcher that you reset on
179activity (by calling its C<start> method) is usually more efficient.
180
181=item my $w = EV::timer $after, $repeat, $callback
182
183=item my $w = EV::timer_ns $after, $repeat, $callback
184
185Calls the callback after C<$after> seconds. If C<$repeat> is true, the
186timer will be restarted after the callback returns. This means that the
187callback would be called roughly every C<$after> seconds, prolonged by the
188time the callback takes.
189
190The C<timer_ns> variant doesn't add/start the newly created watcher.
191
192=item my $w = EV::timer_abs $at, $interval, $callback
193
194=item my $w = EV::timer_abs_ns $at, $interval, $callback
195
196Similar to EV::timer, but the time is given as an absolute point in time
197(C<$at>), plus an optional C<$interval>.
198
199If the C<$interval> is zero, then the callback will be called at the time
200C<$at> if that is in the future, or as soon as possible if its in the
201past. It will not automatically repeat.
202
203If the C<$interval> is nonzero, then the watcher will always be scheduled
204to time out at the next C<$at + integer * $interval> time.
205
206This can be used to schedule a callback to run at very regular intervals,
207as long as the processing time is less then the interval (otherwise
208obviously events will be skipped).
209
210Another way to think about it (for the mathematically inclined) is that
211C<timer_abs> will try to tun the callback at the next possible time where
212C<$time = $at (mod $interval)>, regardless of any time jumps.
213
214The C<timer_abs_ns> variant doesn't add/start the newly created watcher.
215
216=item my $w = EV::signal $signal, $callback
217
218=item my $w = EV::signal_ns $signal, $callback
219
220Call the callback when $signal is received (the signal can be specified
221by number or by name, just as with kill or %SIG). Signal watchers are
222persistent no natter what.
223
224EV will grab the signal for the process (the kernel only allows one
225component to receive signals) when you start a signal watcher, and
226removes it again when you stop it. Pelr does the same when you add/remove
227callbacks to %SIG, so watch out.
228
229Unfortunately, only one handler can be registered per signal. Screw
230libevent.
231
232The C<signal_ns> variant doesn't add/start the newly created watcher.
233
30=back 234=back
31 235
236=head1 THE EV::Event CLASS
32 237
33=head1 OBJECT-ORIENTED INTERFACE 238All EV functions creating an event watcher (designated by C<my $w =>
34 239above) support the following methods on the returned watcher object:
35The object oriented interface lets you configure your own encoding or
36decoding style, within the limits of supported formats.
37 240
38=over 4 241=over 4
39 242
243=item $w->add ($timeout)
244
245Stops and (re-)starts the event watcher, setting the optional timeout to
246the given value, or clearing the timeout if none is given.
247
248=item $w->start
249
250Stops and (re-)starts the event watcher without touching the timeout.
251
252=item $w->del
253
254=item $w->stop
255
256Stop the event watcher if it was started.
257
258=item $current_callback = $w->cb
259
260=item $old_callback = $w->cb ($new_callback)
261
262Return the previously set callback and optionally set a new one.
263
264=item $current_fh = $w->fh
265
266=item $old_fh = $w->fh ($new_fh)
267
268Returns the previously set filehandle and optionally set a new one (also
269clears the EV::SIGNAL flag when setting a filehandle).
270
271=item $current_signal = $w->signal
272
273=item $old_signal = $w->signal ($new_signal)
274
275Returns the previously set signal number and optionally set a new one (also sets
276the EV::SIGNAL flag when setting a signal).
277
278=item $current_eventmask = $w->events
279
280=item $old_eventmask = $w->events ($new_eventmask)
281
282Returns the previously set event mask and optionally set a new one.
283
284=item $w->timeout ($after, $repeat)
285
286Resets the timeout (see C<EV::timer> for details).
287
288=item $w->timeout_abs ($at, $interval)
289
290Resets the timeout (see C<EV::timer_abs> for details).
291
292=item $w->priority_set ($priority)
293
294Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI).
295
40=back 296=back
41 297
298=head1 THREADS
299
300Threads are not supported by this in any way. Perl pseudo-threads is evil
301and must die.
302
42=head1 BUGS 303=head1 BUGS
43 304
305Lots. Libevent itself isn't well tested and rather buggy, and this module
306is quite new at the moment.
307
308Please note that the epoll method is not, in general, reliable in programs
309that use fork (even if no libveent calls are being made in the forked
310process). If your program behaves erratically, try setting the environment
311variable C<EVENT_NOEPOLL> first when running the program.
312
313In general, if you fork, then you can only use the EV module in one of the
314children.
315
44=cut 316=cut
45 317
46our $NPRI = 4; 318our $DIED = sub {
47our $BASE = init; 319 warn "EV: error in callback (ignoring): $@";
48priority_init $NPRI; 320};
321
322init;
323
324push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"];
49 325
501; 3261;
327
328=head1 SEE ALSO
329
330 L<EV::DNS>, L<event(3)>, L<event.h>, L<evdns.h>.
331 L<EV::AnyEvent>.
51 332
52=head1 AUTHOR 333=head1 AUTHOR
53 334
54 Marc Lehmann <schmorp@schmorp.de> 335 Marc Lehmann <schmorp@schmorp.de>
55 http://home.schmorp.de/ 336 http://home.schmorp.de/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines