… | |
… | |
2 | |
2 | |
3 | EV - perl interface to libevent, monkey.org/~provos/libevent/ |
3 | EV - 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 | |
8 | |
9 | # TIMER |
9 | # TIMER |
10 | |
10 | |
11 | my $w = EV::timer 2, 0, sub { |
11 | my $w = EV::timer 2, 0, sub { |
12 | warn "is called after 2s"; |
12 | warn "is called after 2s"; |
13 | }; |
13 | }; |
14 | |
14 | |
15 | my $w = EV::timer 2, 1, sub { |
15 | my $w = EV::timer 2, 1, sub { |
16 | warn "is called roughly every 2s (repeat = 1)"; |
16 | warn "is called roughly every 2s (repeat = 1)"; |
17 | }; |
17 | }; |
18 | |
18 | |
19 | undef $w; # destroy event watcher again |
19 | undef $w; # destroy event watcher again |
20 | |
20 | |
21 | # IO |
|
|
22 | |
|
|
23 | my $w = EV::timer_abs 0, 60, sub { |
21 | my $w = EV::timer_abs 0, 60, sub { |
24 | warn "is called every minute, on the minute, exactly"; |
22 | warn "is called every minute, on the minute, exactly"; |
25 | }; |
23 | }; |
26 | |
24 | |
|
|
25 | # IO |
|
|
26 | |
27 | my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { |
27 | my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { |
28 | my ($w, $events) = @_; # all callbacks get the watcher object and event mask |
28 | my ($w, $events) = @_; # all callbacks get the watcher object and event mask |
29 | if ($events & EV::TIMEOUT) { |
29 | if ($events & EV::TIMEOUT) { |
30 | warn "nothign received on stdin for 10 seconds, retrying"; |
30 | warn "nothing received on stdin for 10 seconds, retrying"; |
31 | } else { |
31 | } else { |
32 | warn "stdin is readable, you entered: ", <STDIN>; |
32 | warn "stdin is readable, you entered: ", <STDIN>; |
33 | } |
33 | } |
34 | }; |
34 | }; |
35 | $w->timeout (10); |
35 | $w->timeout (10); |
36 | |
36 | |
|
|
37 | my $w = EV::timed_io \*STDIN, EV::READ, 30, sub { |
|
|
38 | my ($w, $events) = @_; |
|
|
39 | if ($_[1] & 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 | |
37 | # MAINLOOP |
58 | # MAINLOOP |
38 | EV::dispatch; # loop as long as watchers are active |
59 | EV::dispatch; # loop as long as watchers are active |
39 | EV::loop; # the same thing |
60 | EV::loop; # the same thing |
40 | EV::loop EV::LOOP_ONCE; |
61 | EV::loop EV::LOOP_ONCE; # block until some events could be handles |
41 | EV::loop EV::LOOP_ONSHOT; |
62 | EV::loop EV::LOOP_NONBLOCK; # check and handle some events, but do not wait |
42 | |
63 | |
43 | =head1 DESCRIPTION |
64 | =head1 DESCRIPTION |
44 | |
65 | |
45 | This module provides an interface to libevent |
66 | This module provides an interface to libevent |
46 | (L<http://monkey.org/~provos/libevent/>). You probably should acquaint |
67 | (L<http://monkey.org/~provos/libevent/>). You probably should acquaint |
… | |
… | |
55 | package EV; |
76 | package EV; |
56 | |
77 | |
57 | use strict; |
78 | use strict; |
58 | |
79 | |
59 | BEGIN { |
80 | BEGIN { |
60 | our $VERSION = '0.02'; |
81 | our $VERSION = '0.03'; |
61 | use XSLoader; |
82 | use XSLoader; |
62 | XSLoader::load "EV", $VERSION; |
83 | XSLoader::load "EV", $VERSION; |
63 | } |
84 | } |
64 | |
85 | |
|
|
86 | @EV::Io::ISA = "EV::Watcher"; |
|
|
87 | @EV::Time::ISA = "EV::Watcher"; |
|
|
88 | @EV::Timer::ISA = "EV::Time"; |
|
|
89 | @EV::Periodic::ISA = "EV::Time"; |
|
|
90 | @EV::Signal::ISA = "EV::Watcher"; |
|
|
91 | @EV::Idle::ISA = "EV::Watcher"; |
|
|
92 | @EV::Prepare::ISA = "EV::Watcher"; |
|
|
93 | @EV::Check::ISA = "EV::Watcher"; |
|
|
94 | |
65 | =head1 BASIC INTERFACE |
95 | =head1 BASIC INTERFACE |
66 | |
96 | |
67 | =over 4 |
97 | =over 4 |
68 | |
98 | |
69 | =item $EV::NPRI |
99 | =item $EV::NPRI |
… | |
… | |
109 | |
139 | |
110 | As long as the returned watcher object is alive, call the C<$callback> |
140 | As long as the returned watcher object is alive, call the C<$callback> |
111 | when the events specified in C<$eventmask> happen. Initially, the timeout |
141 | when the events specified in C<$eventmask> happen. Initially, the timeout |
112 | is disabled. |
142 | is disabled. |
113 | |
143 | |
114 | Youc an additionall set a timeout to occur on the watcher, but note that |
144 | You can additionall set a timeout to occur on the watcher, but note that |
115 | this timeout will not be reset when you get an I/O event in the EV::PERSIST |
145 | this timeout will not be reset when you get an I/O event in the EV::PERSIST |
116 | case, and reaching a timeout will always stop the watcher even in the |
146 | case, and reaching a timeout will always stop the watcher even in the |
117 | EV::PERSIST case. |
147 | EV::PERSIST case. |
118 | |
148 | |
119 | If you want a timeout to occur only after a specific time of inactivity, set |
149 | If you want a timeout to occur only after a specific time of inactivity, set |
… | |
… | |
124 | EV::READ wait until read() wouldn't block anymore |
154 | EV::READ wait until read() wouldn't block anymore |
125 | EV::WRITE wait until write() wouldn't block anymore |
155 | EV::WRITE wait until write() wouldn't block anymore |
126 | EV::PERSIST stay active after a (non-timeout) event occured |
156 | EV::PERSIST stay active after a (non-timeout) event occured |
127 | |
157 | |
128 | The C<io_ns> variant doesn't add/start the newly created watcher. |
158 | The C<io_ns> variant doesn't add/start the newly created watcher. |
|
|
159 | |
|
|
160 | =item my $w = EV::timed_io $fileno_or_fh, $eventmask, $timeout, $callback |
|
|
161 | |
|
|
162 | =item my $w = EV::timed_io_ns $fileno_or_fh, $eventmask, $timeout, $callback |
|
|
163 | |
|
|
164 | Same as C<io> and C<io_ns>, but also specifies a timeout (as if there was |
|
|
165 | a call to C<< $w->timeout ($timout, 1) >>. The persist flag is not allowed |
|
|
166 | and will automatically be cleared. The watcher will be restarted after each event. |
|
|
167 | |
|
|
168 | If the timeout is zero or undef, no timeout will be set, and a normal |
|
|
169 | watcher (with the persist flag set!) will be created. |
|
|
170 | |
|
|
171 | This has the effect of timing out after the specified period of inactivity |
|
|
172 | has happened. |
|
|
173 | |
|
|
174 | Due to the design of libevent, this is also relatively inefficient, having |
|
|
175 | one or two io watchers and a separate timeout watcher that you reset on |
|
|
176 | activity (by calling its C<start> method) is usually more efficient. |
129 | |
177 | |
130 | =item my $w = EV::timer $after, $repeat, $callback |
178 | =item my $w = EV::timer $after, $repeat, $callback |
131 | |
179 | |
132 | =item my $w = EV::timer_ns $after, $repeat, $callback |
180 | =item my $w = EV::timer_ns $after, $repeat, $callback |
133 | |
181 | |
… | |
… | |
160 | C<timer_abs> will try to tun the callback at the next possible time where |
208 | C<timer_abs> will try to tun the callback at the next possible time where |
161 | C<$time = $at (mod $interval)>, regardless of any time jumps. |
209 | C<$time = $at (mod $interval)>, regardless of any time jumps. |
162 | |
210 | |
163 | The C<timer_abs_ns> variant doesn't add/start the newly created watcher. |
211 | The C<timer_abs_ns> variant doesn't add/start the newly created watcher. |
164 | |
212 | |
165 | =item my $w = EV::signal $signum, $callback |
213 | =item my $w = EV::signal $signal, $callback |
166 | |
214 | |
167 | =item my $w = EV::signal_ns $signum, $callback |
215 | =item my $w = EV::signal_ns $signal, $callback |
168 | |
216 | |
169 | Call the callback when signal $signum is received. |
217 | Call the callback when $signal is received (the signal can be specified |
|
|
218 | by number or by name, just as with kill or %SIG). Signal watchers are |
|
|
219 | persistent no natter what. |
|
|
220 | |
|
|
221 | EV will grab the signal for the process (the kernel only allows one |
|
|
222 | component to receive signals) when you start a signal watcher, and |
|
|
223 | removes it again when you stop it. Pelr does the same when you add/remove |
|
|
224 | callbacks to %SIG, so watch out. |
|
|
225 | |
|
|
226 | Unfortunately, only one handler can be registered per signal. Screw |
|
|
227 | libevent. |
170 | |
228 | |
171 | The C<signal_ns> variant doesn't add/start the newly created watcher. |
229 | The C<signal_ns> variant doesn't add/start the newly created watcher. |
172 | |
230 | |
173 | =back |
231 | =back |
174 | |
232 | |
… | |
… | |
202 | |
260 | |
203 | =item $current_fh = $w->fh |
261 | =item $current_fh = $w->fh |
204 | |
262 | |
205 | =item $old_fh = $w->fh ($new_fh) |
263 | =item $old_fh = $w->fh ($new_fh) |
206 | |
264 | |
207 | Returns the previously set filehandle and optionally set a new one. |
265 | Returns the previously set filehandle and optionally set a new one (also |
|
|
266 | clears the EV::SIGNAL flag when setting a filehandle). |
|
|
267 | |
|
|
268 | =item $current_signal = $w->signal |
|
|
269 | |
|
|
270 | =item $old_signal = $w->signal ($new_signal) |
|
|
271 | |
|
|
272 | Returns the previously set signal number and optionally set a new one (also sets |
|
|
273 | the EV::SIGNAL flag when setting a signal). |
208 | |
274 | |
209 | =item $current_eventmask = $w->events |
275 | =item $current_eventmask = $w->events |
210 | |
276 | |
211 | =item $old_eventmask = $w->events ($new_eventmask) |
277 | =item $old_eventmask = $w->events ($new_eventmask) |
212 | |
278 | |
… | |
… | |
223 | =item $w->priority_set ($priority) |
289 | =item $w->priority_set ($priority) |
224 | |
290 | |
225 | Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI). |
291 | Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI). |
226 | |
292 | |
227 | =back |
293 | =back |
|
|
294 | |
|
|
295 | =head1 THREADS |
|
|
296 | |
|
|
297 | Threads are not supported by this in any way. Perl pseudo-threads is evil |
|
|
298 | and must die. |
228 | |
299 | |
229 | =head1 BUGS |
300 | =head1 BUGS |
230 | |
301 | |
231 | Lots. Libevent itself isn't well tested and rather buggy, and this module |
302 | Lots. Libevent itself isn't well tested and rather buggy, and this module |
232 | is quite new at the moment. |
303 | is quite new at the moment. |
… | |
… | |
243 | |
314 | |
244 | our $DIED = sub { |
315 | our $DIED = sub { |
245 | warn "EV: error in callback (ignoring): $@"; |
316 | warn "EV: error in callback (ignoring): $@"; |
246 | }; |
317 | }; |
247 | |
318 | |
248 | our $NPRI = 4; |
319 | init; |
249 | our $BASE = init; |
|
|
250 | priority_init $NPRI; |
|
|
251 | |
320 | |
252 | push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; |
321 | push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; |
253 | |
322 | |
254 | 1; |
323 | 1; |
255 | |
324 | |