… | |
… | |
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 | |
11 | This module provides an interface to libevent |
45 | This module provides an interface to libevent |
12 | (L<http://monkey.org/~provos/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. |
13 | |
52 | |
14 | =cut |
53 | =cut |
15 | |
54 | |
16 | package EV; |
55 | package EV; |
17 | |
56 | |
18 | use strict; |
57 | use strict; |
19 | |
58 | |
20 | BEGIN { |
59 | BEGIN { |
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 | |
|
|
71 | How many priority levels are available. |
|
|
72 | |
|
|
73 | =item $EV::DIED |
|
|
74 | |
|
|
75 | Must contain a reference to a function that is called when a callback |
|
|
76 | throws an exception (with $@ containing thr error). The default prints an |
|
|
77 | informative message and continues. |
|
|
78 | |
|
|
79 | If this callback throws an exception it will be silently ignored. |
|
|
80 | |
|
|
81 | =item $time = EV::now |
|
|
82 | |
|
|
83 | Returns the time in (fractional) seconds since the epoch. |
|
|
84 | |
|
|
85 | =item $version = EV::version |
|
|
86 | |
|
|
87 | =item $method = EV::method |
|
|
88 | |
|
|
89 | Return 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 | |
|
|
95 | Exit any active loop or dispatch after C<$after> seconds or immediately if |
|
|
96 | C<$after> is missing or zero. |
|
|
97 | |
|
|
98 | =item EV::dispatch |
|
|
99 | |
|
|
100 | Same as C<EV::loop 0>. |
|
|
101 | |
|
|
102 | =item EV::event $callback |
|
|
103 | |
|
|
104 | Creates 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 | |
|
|
110 | 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 |
|
|
112 | is disabled. |
|
|
113 | |
|
|
114 | Youc an 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 |
|
|
116 | case, and reaching a timeout will always stop the watcher even in the |
|
|
117 | EV::PERSIST case. |
|
|
118 | |
|
|
119 | If you want a timeout to occur only after a specific time of inactivity, set |
|
|
120 | a repeating timeout and do NOT use EV::PERSIST. |
|
|
121 | |
|
|
122 | Eventmask 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 | |
|
|
128 | The 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 | |
|
|
134 | Calls the callback after C<$after> seconds. If C<$repeat> is true, the |
|
|
135 | timer will be restarted after the callback returns. This means that the |
|
|
136 | callback would be called roughly every C<$after> seconds, prolonged by the |
|
|
137 | time the callback takes. |
|
|
138 | |
|
|
139 | The 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 | |
|
|
145 | Similar to EV::timer, but the time is given as an absolute point in time |
|
|
146 | (C<$at>), plus an optional C<$interval>. |
|
|
147 | |
|
|
148 | If the C<$interval> is zero, then the callback will be called at the time |
|
|
149 | C<$at> if that is in the future, or as soon as possible if its in the |
|
|
150 | past. It will not automatically repeat. |
|
|
151 | |
|
|
152 | If the C<$interval> is nonzero, then the watcher will always be scheduled |
|
|
153 | to time out at the next C<$at + integer * $interval> time. |
|
|
154 | |
|
|
155 | This can be used to schedule a callback to run at very regular intervals, |
|
|
156 | as long as the processing time is less then the interval (otherwise |
|
|
157 | obviously events will be skipped). |
|
|
158 | |
|
|
159 | Another way to think about it (for the mathematically inclined) is that |
|
|
160 | 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. |
|
|
162 | |
|
|
163 | The 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 | |
|
|
169 | Call the callback when signal $signum is received. |
|
|
170 | |
|
|
171 | The 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 |
177 | All EV functions creating an event watcher (designated by C<my $w => |
34 | |
178 | above) support the following methods on the returned watcher object: |
35 | The object oriented interface lets you configure your own encoding or |
|
|
36 | decoding style, within the limits of supported formats. |
|
|
37 | |
179 | |
38 | =over 4 |
180 | =over 4 |
39 | |
181 | |
|
|
182 | =item $w->add ($timeout) |
|
|
183 | |
|
|
184 | Stops and (re-)starts the event watcher, setting the optional timeout to |
|
|
185 | the given value, or clearing the timeout if none is given. |
|
|
186 | |
|
|
187 | =item $w->start |
|
|
188 | |
|
|
189 | Stops and (re-)starts the event watcher without touching the timeout. |
|
|
190 | |
|
|
191 | =item $w->del |
|
|
192 | |
|
|
193 | =item $w->stop |
|
|
194 | |
|
|
195 | Stop the event watcher if it was started. |
|
|
196 | |
|
|
197 | =item $current_callback = $w->cb |
|
|
198 | |
|
|
199 | =item $old_callback = $w->cb ($new_callback) |
|
|
200 | |
|
|
201 | Return 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 | |
|
|
207 | Returns 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 | |
|
|
213 | Returns the previously set event mask and optionally set a new one. |
|
|
214 | |
|
|
215 | =item $w->timeout ($after, $repeat) |
|
|
216 | |
|
|
217 | Resets the timeout (see C<EV::timer> for details). |
|
|
218 | |
|
|
219 | =item $w->timeout_abs ($at, $interval) |
|
|
220 | |
|
|
221 | Resets the timeout (see C<EV::timer_abs> for details). |
|
|
222 | |
|
|
223 | =item $w->priority_set ($priority) |
|
|
224 | |
|
|
225 | Set 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 | |
|
|
231 | Lots. Libevent itself isn't well tested and rather buggy, and this module |
|
|
232 | is quite new at the moment. |
|
|
233 | |
|
|
234 | Please note that the epoll method is not, in general, reliable in programs |
|
|
235 | that use fork (even if no libveent calls are being made in the forked |
|
|
236 | process). If your program behaves erratically, try setting the environment |
|
|
237 | variable C<EVENT_NOEPOLL> first when running the program. |
|
|
238 | |
|
|
239 | In general, if you fork, then you can only use the EV module in one of the |
|
|
240 | children. |
|
|
241 | |
44 | =cut |
242 | =cut |
|
|
243 | |
|
|
244 | our $DIED = sub { |
|
|
245 | warn "EV: error in callback (ignoring): $@"; |
|
|
246 | }; |
45 | |
247 | |
46 | our $NPRI = 4; |
248 | our $NPRI = 4; |
47 | our $BASE = init; |
249 | our $BASE = init; |
48 | priority_init $NPRI; |
250 | priority_init $NPRI; |
49 | |
251 | |
|
|
252 | push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; |
|
|
253 | |
50 | 1; |
254 | 1; |
|
|
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/ |