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.9 by root, Sun Oct 28 06:40:46 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines