… | |
… | |
10 | use Fcntl (); |
10 | use Fcntl (); |
11 | use Errno qw/EAGAIN EINTR/; |
11 | use Errno qw/EAGAIN EINTR/; |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | This module is experimental. |
18 | |
18 | |
|
|
19 | =cut |
|
|
20 | |
19 | our $VERSION = '0.02'; |
21 | our $VERSION = '0.04'; |
20 | |
22 | |
21 | =head1 SYNOPSIS |
23 | =head1 SYNOPSIS |
22 | |
24 | |
23 | use AnyEvent; |
25 | use AnyEvent; |
24 | use AnyEvent::Handle; |
26 | use AnyEvent::Handle; |
… | |
… | |
43 | $cv->wait; |
45 | $cv->wait; |
44 | |
46 | |
45 | =head1 DESCRIPTION |
47 | =head1 DESCRIPTION |
46 | |
48 | |
47 | This module is a helper module to make it easier to do event-based I/O on |
49 | This module is a helper module to make it easier to do event-based I/O on |
48 | filehandles (and sockets, see L<AnyEvent::Socket> for an easy way to make |
50 | filehandles. For utility functions for doing non-blocking connects and accepts |
49 | non-blocking resolves and connects). |
51 | on sockets see L<AnyEvent::Util>. |
50 | |
52 | |
51 | In the following, when the documentation refers to of "bytes" then this |
53 | In the following, when the documentation refers to of "bytes" then this |
52 | means characters. As sysread and syswrite are used for all I/O, their |
54 | means characters. As sysread and syswrite are used for all I/O, their |
53 | treatment of characters applies to this module as well. |
55 | treatment of characters applies to this module as well. |
54 | |
56 | |
… | |
… | |
70 | The filehandle this L<AnyEvent::Handle> object will operate on. |
72 | The filehandle this L<AnyEvent::Handle> object will operate on. |
71 | |
73 | |
72 | NOTE: The filehandle will be set to non-blocking (using |
74 | NOTE: The filehandle will be set to non-blocking (using |
73 | AnyEvent::Util::fh_nonblocking). |
75 | AnyEvent::Util::fh_nonblocking). |
74 | |
76 | |
|
|
77 | =item on_eof => $cb->($self) |
|
|
78 | |
|
|
79 | Set the callback to be called on EOF. |
|
|
80 | |
|
|
81 | While not mandatory, it is highly recommended to set an eof callback, |
|
|
82 | otherwise you might end up with a closed socket while you are still |
|
|
83 | waiting for data. |
|
|
84 | |
75 | =item on_error => $cb->($self) [MANDATORY] |
85 | =item on_error => $cb->($self) |
76 | |
86 | |
77 | This is the fatal error callback, that is called when a fatal error ocurs, |
87 | This is the fatal error callback, that is called when, well, a fatal error |
78 | such as not being able to resolve the hostname, failure to connect or a |
88 | occurs, such as not being able to resolve the hostname, failure to connect |
79 | read error. |
89 | or a read error. |
80 | |
90 | |
81 | The object will not be in a usable state when this callback has been |
91 | The object will not be in a usable state when this callback has been |
82 | called. |
92 | called. |
83 | |
93 | |
84 | On callback entrance, the value of C<$!> contains the opertaing system |
94 | On callback entrance, the value of C<$!> contains the operating system |
85 | error (or C<ENOSPC> or C<EPIPE>). |
95 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
86 | |
96 | |
87 | =item on_eof => $cb->($self) [MANDATORY] |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
88 | |
98 | you will not be notified of errors otherwise. The default simply calls |
89 | Set the callback to be called on EOF. |
99 | die. |
90 | |
100 | |
91 | =item on_read => $cb->($self) |
101 | =item on_read => $cb->($self) |
92 | |
102 | |
93 | This sets the default read callback, which is called when data arrives |
103 | This sets the default read callback, which is called when data arrives |
94 | and no read request is in the queue. If the read callback is C<undef> |
104 | and no read request is in the queue. |
95 | or has never been set, than AnyEvent::Handle will cease reading from the |
|
|
96 | filehandle. |
|
|
97 | |
105 | |
98 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
106 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
99 | method or acces sthe C<$self->{rbuf}> member directly. |
107 | method or access the C<$self->{rbuf}> member directly. |
100 | |
108 | |
101 | When an EOF condition is detected then AnyEvent::Handle will first try to |
109 | When an EOF condition is detected then AnyEvent::Handle will first try to |
102 | feed all the remaining data to the queued callbacks and C<on_read> before |
110 | feed all the remaining data to the queued callbacks and C<on_read> before |
103 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
111 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
104 | error will be raised (with C<$!> set to C<EPIPE>). |
112 | error will be raised (with C<$!> set to C<EPIPE>). |
… | |
… | |
131 | |
139 | |
132 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
140 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
133 | buffer: If the write reaches this size or gets even samller it is |
141 | buffer: If the write reaches this size or gets even samller it is |
134 | considered empty. |
142 | considered empty. |
135 | |
143 | |
|
|
144 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
|
|
145 | |
|
|
146 | When this parameter is given, it enables TLS (SSL) mode, that means it |
|
|
147 | will start making tls handshake and will transparently encrypt/decrypt |
|
|
148 | data. |
|
|
149 | |
|
|
150 | TLS mode requires Net::SSLeay to be installed (it will be loaded |
|
|
151 | automatically when you try to create a TLS handle). |
|
|
152 | |
|
|
153 | For the TLS server side, use C<accept>, and for the TLS client side of a |
|
|
154 | connection, use C<connect> mode. |
|
|
155 | |
|
|
156 | You can also provide your own TLS connection object, but you have |
|
|
157 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
|
|
158 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
|
|
159 | AnyEvent::Handle. |
|
|
160 | |
|
|
161 | See the C<starttls> method if you need to start TLs negotiation later. |
|
|
162 | |
|
|
163 | =item tls_ctx => $ssl_ctx |
|
|
164 | |
|
|
165 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
|
|
166 | (unless a connection object was specified directly). If this parameter is |
|
|
167 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
|
|
168 | |
136 | =back |
169 | =back |
137 | |
170 | |
138 | =cut |
171 | =cut |
139 | |
172 | |
140 | sub new { |
173 | sub new { |
… | |
… | |
144 | |
177 | |
145 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
178 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
146 | |
179 | |
147 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
180 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
148 | |
181 | |
149 | $self->on_error ((delete $self->{on_error}) or Carp::croak "mandatory argument on_error is missing"); |
182 | if ($self->{tls}) { |
150 | $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); |
183 | require Net::SSLeay; |
|
|
184 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
|
|
185 | } |
151 | |
186 | |
|
|
187 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
|
|
188 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
152 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
189 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
153 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
190 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
|
|
191 | |
|
|
192 | $self->start_read; |
154 | |
193 | |
155 | $self |
194 | $self |
156 | } |
195 | } |
157 | |
196 | |
158 | sub _shutdown { |
197 | sub _shutdown { |
… | |
… | |
169 | { |
208 | { |
170 | local $!; |
209 | local $!; |
171 | $self->_shutdown; |
210 | $self->_shutdown; |
172 | } |
211 | } |
173 | |
212 | |
|
|
213 | if ($self->{on_error}) { |
174 | $self->{on_error}($self); |
214 | $self->{on_error}($self); |
|
|
215 | } else { |
|
|
216 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
|
|
217 | } |
175 | } |
218 | } |
176 | |
219 | |
177 | =item $fh = $handle->fh |
220 | =item $fh = $handle->fh |
178 | |
221 | |
179 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
222 | This method returns the file handle of the L<AnyEvent::Handle> object. |
180 | |
223 | |
181 | =cut |
224 | =cut |
182 | |
225 | |
183 | sub fh { $_[0]->{fh} } |
226 | sub fh { $_[0]->{fh} } |
184 | |
227 | |
… | |
… | |
196 | |
239 | |
197 | Replace the current C<on_eof> callback (see the C<on_eof> constructor argument). |
240 | Replace the current C<on_eof> callback (see the C<on_eof> constructor argument). |
198 | |
241 | |
199 | =cut |
242 | =cut |
200 | |
243 | |
201 | ############################################################################# |
|
|
202 | |
|
|
203 | sub on_eof { |
244 | sub on_eof { |
204 | $_[0]{on_eof} = $_[1]; |
245 | $_[0]{on_eof} = $_[1]; |
205 | } |
246 | } |
|
|
247 | |
|
|
248 | ############################################################################# |
|
|
249 | |
|
|
250 | =back |
|
|
251 | |
|
|
252 | =head2 WRITE QUEUE |
|
|
253 | |
|
|
254 | AnyEvent::Handle manages two queues per handle, one for writing and one |
|
|
255 | for reading. |
|
|
256 | |
|
|
257 | The write queue is very simple: you can add data to its end, and |
|
|
258 | AnyEvent::Handle will automatically try to get rid of it for you. |
|
|
259 | |
|
|
260 | When data could be written and the write buffer is shorter then the low |
|
|
261 | water mark, the C<on_drain> callback will be invoked. |
|
|
262 | |
|
|
263 | =over 4 |
206 | |
264 | |
207 | =item $handle->on_drain ($cb) |
265 | =item $handle->on_drain ($cb) |
208 | |
266 | |
209 | Sets the C<on_drain> callback or clears it (see the description of |
267 | Sets the C<on_drain> callback or clears it (see the description of |
210 | C<on_drain> in the constructor). |
268 | C<on_drain> in the constructor). |
… | |
… | |
226 | want (only limited by the available memory), as C<AnyEvent::Handle> |
284 | want (only limited by the available memory), as C<AnyEvent::Handle> |
227 | buffers it independently of the kernel. |
285 | buffers it independently of the kernel. |
228 | |
286 | |
229 | =cut |
287 | =cut |
230 | |
288 | |
231 | sub push_write { |
289 | sub _drain_wbuf { |
232 | my ($self, $data) = @_; |
290 | my ($self) = @_; |
233 | |
291 | |
234 | $self->{wbuf} .= $data; |
292 | if (!$self->{ww} && length $self->{wbuf}) { |
235 | |
|
|
236 | unless ($self->{ww}) { |
|
|
237 | Scalar::Util::weaken $self; |
293 | Scalar::Util::weaken $self; |
238 | my $cb = sub { |
294 | my $cb = sub { |
239 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
295 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
240 | |
296 | |
241 | if ($len > 0) { |
297 | if ($len >= 0) { |
242 | substr $self->{wbuf}, 0, $len, ""; |
298 | substr $self->{wbuf}, 0, $len, ""; |
243 | |
|
|
244 | |
299 | |
245 | $self->{on_drain}($self) |
300 | $self->{on_drain}($self) |
246 | if $self->{low_water_mark} >= length $self->{wbuf} |
301 | if $self->{low_water_mark} >= length $self->{wbuf} |
247 | && $self->{on_drain}; |
302 | && $self->{on_drain}; |
248 | |
303 | |
… | |
… | |
256 | |
311 | |
257 | $cb->($self); |
312 | $cb->($self); |
258 | }; |
313 | }; |
259 | } |
314 | } |
260 | |
315 | |
|
|
316 | our %WH; |
|
|
317 | |
|
|
318 | sub register_write_type($$) { |
|
|
319 | $WH{$_[0]} = $_[1]; |
|
|
320 | } |
|
|
321 | |
|
|
322 | sub push_write { |
|
|
323 | my $self = shift; |
|
|
324 | |
|
|
325 | if (@_ > 1) { |
|
|
326 | my $type = shift; |
|
|
327 | |
|
|
328 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
|
|
329 | ->($self, @_); |
|
|
330 | } |
|
|
331 | |
|
|
332 | if ($self->{filter_w}) { |
|
|
333 | $self->{filter_w}->($self, \$_[0]); |
|
|
334 | } else { |
|
|
335 | $self->{wbuf} .= $_[0]; |
|
|
336 | $self->_drain_wbuf; |
|
|
337 | } |
|
|
338 | } |
|
|
339 | |
|
|
340 | =item $handle->push_write (type => @args) |
|
|
341 | |
|
|
342 | =item $handle->unshift_write (type => @args) |
|
|
343 | |
|
|
344 | Instead of formatting your data yourself, you can also let this module do |
|
|
345 | the job by specifying a type and type-specific arguments. |
|
|
346 | |
|
|
347 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
348 | drop by and tell us): |
|
|
349 | |
|
|
350 | =over 4 |
|
|
351 | |
|
|
352 | =item netstring => $string |
|
|
353 | |
|
|
354 | Formats the given value as netstring |
|
|
355 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
|
|
356 | |
|
|
357 | =back |
|
|
358 | |
|
|
359 | =cut |
|
|
360 | |
|
|
361 | register_write_type netstring => sub { |
|
|
362 | my ($self, $string) = @_; |
|
|
363 | |
|
|
364 | sprintf "%d:%s,", (length $string), $string |
|
|
365 | }; |
|
|
366 | |
|
|
367 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
|
|
368 | |
|
|
369 | This function (not method) lets you add your own types to C<push_write>. |
|
|
370 | Whenever the given C<type> is used, C<push_write> will invoke the code |
|
|
371 | reference with the handle object and the remaining arguments. |
|
|
372 | |
|
|
373 | The code reference is supposed to return a single octet string that will |
|
|
374 | be appended to the write buffer. |
|
|
375 | |
|
|
376 | Note that this is a function, and all types registered this way will be |
|
|
377 | global, so try to use unique names. |
|
|
378 | |
|
|
379 | =cut |
|
|
380 | |
261 | ############################################################################# |
381 | ############################################################################# |
|
|
382 | |
|
|
383 | =back |
|
|
384 | |
|
|
385 | =head2 READ QUEUE |
|
|
386 | |
|
|
387 | AnyEvent::Handle manages two queues per handle, one for writing and one |
|
|
388 | for reading. |
|
|
389 | |
|
|
390 | The read queue is more complex than the write queue. It can be used in two |
|
|
391 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
|
|
392 | a queue. |
|
|
393 | |
|
|
394 | In the simple case, you just install an C<on_read> callback and whenever |
|
|
395 | new data arrives, it will be called. You can then remove some data (if |
|
|
396 | enough is there) from the read buffer (C<< $handle->rbuf >>) if you want |
|
|
397 | or not. |
|
|
398 | |
|
|
399 | In the more complex case, you want to queue multiple callbacks. In this |
|
|
400 | case, AnyEvent::Handle will call the first queued callback each time new |
|
|
401 | data arrives and removes it when it has done its job (see C<push_read>, |
|
|
402 | below). |
|
|
403 | |
|
|
404 | This way you can, for example, push three line-reads, followed by reading |
|
|
405 | a chunk of data, and AnyEvent::Handle will execute them in order. |
|
|
406 | |
|
|
407 | Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by |
|
|
408 | the specified number of bytes which give an XML datagram. |
|
|
409 | |
|
|
410 | # in the default state, expect some header bytes |
|
|
411 | $handle->on_read (sub { |
|
|
412 | # some data is here, now queue the length-header-read (4 octets) |
|
|
413 | shift->unshift_read_chunk (4, sub { |
|
|
414 | # header arrived, decode |
|
|
415 | my $len = unpack "N", $_[1]; |
|
|
416 | |
|
|
417 | # now read the payload |
|
|
418 | shift->unshift_read_chunk ($len, sub { |
|
|
419 | my $xml = $_[1]; |
|
|
420 | # handle xml |
|
|
421 | }); |
|
|
422 | }); |
|
|
423 | }); |
|
|
424 | |
|
|
425 | Example 2: Implement a client for a protocol that replies either with |
|
|
426 | "OK" and another line or "ERROR" for one request, and 64 bytes for the |
|
|
427 | second request. Due tot he availability of a full queue, we can just |
|
|
428 | pipeline sending both requests and manipulate the queue as necessary in |
|
|
429 | the callbacks: |
|
|
430 | |
|
|
431 | # request one |
|
|
432 | $handle->push_write ("request 1\015\012"); |
|
|
433 | |
|
|
434 | # we expect "ERROR" or "OK" as response, so push a line read |
|
|
435 | $handle->push_read_line (sub { |
|
|
436 | # if we got an "OK", we have to _prepend_ another line, |
|
|
437 | # so it will be read before the second request reads its 64 bytes |
|
|
438 | # which are already in the queue when this callback is called |
|
|
439 | # we don't do this in case we got an error |
|
|
440 | if ($_[1] eq "OK") { |
|
|
441 | $_[0]->unshift_read_line (sub { |
|
|
442 | my $response = $_[1]; |
|
|
443 | ... |
|
|
444 | }); |
|
|
445 | } |
|
|
446 | }); |
|
|
447 | |
|
|
448 | # request two |
|
|
449 | $handle->push_write ("request 2\015\012"); |
|
|
450 | |
|
|
451 | # simply read 64 bytes, always |
|
|
452 | $handle->push_read_chunk (64, sub { |
|
|
453 | my $response = $_[1]; |
|
|
454 | ... |
|
|
455 | }); |
|
|
456 | |
|
|
457 | =over 4 |
|
|
458 | |
|
|
459 | =cut |
262 | |
460 | |
263 | sub _drain_rbuf { |
461 | sub _drain_rbuf { |
264 | my ($self) = @_; |
462 | my ($self) = @_; |
265 | |
463 | |
|
|
464 | if ( |
|
|
465 | defined $self->{rbuf_max} |
|
|
466 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
467 | ) { |
|
|
468 | $! = &Errno::ENOSPC; return $self->error; |
|
|
469 | } |
|
|
470 | |
266 | return if exists $self->{in_drain}; |
471 | return if $self->{in_drain}; |
267 | local $self->{in_drain} = 1; |
472 | local $self->{in_drain} = 1; |
268 | |
473 | |
269 | while (my $len = length $self->{rbuf}) { |
474 | while (my $len = length $self->{rbuf}) { |
270 | no strict 'refs'; |
475 | no strict 'refs'; |
271 | if (@{ $self->{queue} }) { |
476 | if (my $cb = shift @{ $self->{queue} }) { |
272 | if ($self->{queue}[0]($self)) { |
477 | unless ($cb->($self)) { |
273 | shift @{ $self->{queue} }; |
|
|
274 | } elsif ($self->{eof}) { |
478 | if ($self->{eof}) { |
275 | # no progress can be made (not enough data and no data forthcoming) |
479 | # no progress can be made (not enough data and no data forthcoming) |
276 | $! = &Errno::EPIPE; return $self->error; |
480 | $! = &Errno::EPIPE; return $self->error; |
277 | } else { |
481 | } |
|
|
482 | |
|
|
483 | unshift @{ $self->{queue} }, $cb; |
278 | return; |
484 | return; |
279 | } |
485 | } |
280 | } elsif ($self->{on_read}) { |
486 | } elsif ($self->{on_read}) { |
281 | $self->{on_read}($self); |
487 | $self->{on_read}($self); |
282 | |
488 | |
… | |
… | |
296 | } |
502 | } |
297 | } |
503 | } |
298 | |
504 | |
299 | if ($self->{eof}) { |
505 | if ($self->{eof}) { |
300 | $self->_shutdown; |
506 | $self->_shutdown; |
301 | $self->{on_eof}($self); |
507 | $self->{on_eof}($self) |
|
|
508 | if $self->{on_eof}; |
302 | } |
509 | } |
303 | } |
510 | } |
304 | |
511 | |
305 | =item $handle->on_read ($cb) |
512 | =item $handle->on_read ($cb) |
306 | |
513 | |
… | |
… | |
312 | |
519 | |
313 | sub on_read { |
520 | sub on_read { |
314 | my ($self, $cb) = @_; |
521 | my ($self, $cb) = @_; |
315 | |
522 | |
316 | $self->{on_read} = $cb; |
523 | $self->{on_read} = $cb; |
317 | |
|
|
318 | unless ($self->{rw} || $self->{eof}) { |
|
|
319 | Scalar::Util::weaken $self; |
|
|
320 | |
|
|
321 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
|
|
322 | my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; |
|
|
323 | |
|
|
324 | if ($len > 0) { |
|
|
325 | if (exists $self->{rbuf_max}) { |
|
|
326 | if ($self->{rbuf_max} < length $self->{rbuf}) { |
|
|
327 | $! = &Errno::ENOSPC; return $self->error; |
|
|
328 | } |
|
|
329 | } |
|
|
330 | |
|
|
331 | } elsif (defined $len) { |
|
|
332 | $self->{eof} = 1; |
|
|
333 | delete $self->{rw}; |
|
|
334 | |
|
|
335 | } elsif ($! != EAGAIN && $! != EINTR) { |
|
|
336 | return $self->error; |
|
|
337 | } |
|
|
338 | |
|
|
339 | $self->_drain_rbuf; |
|
|
340 | }); |
|
|
341 | } |
|
|
342 | } |
524 | } |
343 | |
525 | |
344 | =item $handle->rbuf |
526 | =item $handle->rbuf |
345 | |
527 | |
346 | Returns the read buffer (as a modifiable lvalue). |
528 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
365 | Append the given callback to the end of the queue (C<push_read>) or |
547 | Append the given callback to the end of the queue (C<push_read>) or |
366 | prepend it (C<unshift_read>). |
548 | prepend it (C<unshift_read>). |
367 | |
549 | |
368 | The callback is called each time some additional read data arrives. |
550 | The callback is called each time some additional read data arrives. |
369 | |
551 | |
370 | It must check wether enough data is in the read buffer already. |
552 | It must check whether enough data is in the read buffer already. |
371 | |
553 | |
372 | If not enough data is available, it must return the empty list or a false |
554 | If not enough data is available, it must return the empty list or a false |
373 | value, in which case it will be called repeatedly until enough data is |
555 | value, in which case it will be called repeatedly until enough data is |
374 | available (or an error condition is detected). |
556 | available (or an error condition is detected). |
375 | |
557 | |
… | |
… | |
377 | interested in (which can be none at all) and return a true value. After returning |
559 | interested in (which can be none at all) and return a true value. After returning |
378 | true, it will be removed from the queue. |
560 | true, it will be removed from the queue. |
379 | |
561 | |
380 | =cut |
562 | =cut |
381 | |
563 | |
|
|
564 | our %RH; |
|
|
565 | |
|
|
566 | sub register_read_type($$) { |
|
|
567 | $RH{$_[0]} = $_[1]; |
|
|
568 | } |
|
|
569 | |
382 | sub push_read { |
570 | sub push_read { |
383 | my ($self, $cb) = @_; |
571 | my $self = shift; |
|
|
572 | my $cb = pop; |
|
|
573 | |
|
|
574 | if (@_) { |
|
|
575 | my $type = shift; |
|
|
576 | |
|
|
577 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
|
|
578 | ->($self, $cb, @_); |
|
|
579 | } |
384 | |
580 | |
385 | push @{ $self->{queue} }, $cb; |
581 | push @{ $self->{queue} }, $cb; |
386 | $self->_drain_rbuf; |
582 | $self->_drain_rbuf; |
387 | } |
583 | } |
388 | |
584 | |
389 | sub unshift_read { |
585 | sub unshift_read { |
390 | my ($self, $cb) = @_; |
586 | my $self = shift; |
|
|
587 | my $cb = pop; |
391 | |
588 | |
|
|
589 | if (@_) { |
|
|
590 | my $type = shift; |
|
|
591 | |
|
|
592 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
|
|
593 | ->($self, $cb, @_); |
|
|
594 | } |
|
|
595 | |
|
|
596 | |
392 | push @{ $self->{queue} }, $cb; |
597 | unshift @{ $self->{queue} }, $cb; |
393 | $self->_drain_rbuf; |
598 | $self->_drain_rbuf; |
394 | } |
599 | } |
395 | |
600 | |
396 | =item $handle->push_read_chunk ($len, $cb->($self, $data)) |
601 | =item $handle->push_read (type => @args, $cb) |
397 | |
602 | |
398 | =item $handle->unshift_read_chunk ($len, $cb->($self, $data)) |
603 | =item $handle->unshift_read (type => @args, $cb) |
399 | |
604 | |
400 | Append the given callback to the end of the queue (C<push_read_chunk>) or |
605 | Instead of providing a callback that parses the data itself you can chose |
401 | prepend it (C<unshift_read_chunk>). |
606 | between a number of predefined parsing formats, for chunks of data, lines |
|
|
607 | etc. |
402 | |
608 | |
403 | The callback will be called only once C<$len> bytes have been read, and |
609 | Predefined types are (if you have ideas for additional types, feel free to |
404 | these C<$len> bytes will be passed to the callback. |
610 | drop by and tell us): |
405 | |
611 | |
406 | =cut |
612 | =over 4 |
407 | |
613 | |
408 | sub _read_chunk($$) { |
614 | =item chunk => $octets, $cb->($self, $data) |
409 | my ($len, $cb) = @_; |
615 | |
|
|
616 | Invoke the callback only once C<$octets> bytes have been read. Pass the |
|
|
617 | data read to the callback. The callback will never be called with less |
|
|
618 | data. |
|
|
619 | |
|
|
620 | Example: read 2 bytes. |
|
|
621 | |
|
|
622 | $handle->push_read (chunk => 2, sub { |
|
|
623 | warn "yay ", unpack "H*", $_[1]; |
|
|
624 | }); |
|
|
625 | |
|
|
626 | =cut |
|
|
627 | |
|
|
628 | register_read_type chunk => sub { |
|
|
629 | my ($self, $cb, $len) = @_; |
410 | |
630 | |
411 | sub { |
631 | sub { |
412 | $len <= length $_[0]{rbuf} or return; |
632 | $len <= length $_[0]{rbuf} or return; |
413 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
633 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
414 | 1 |
634 | 1 |
415 | } |
635 | } |
416 | } |
636 | }; |
417 | |
637 | |
|
|
638 | # compatibility with older API |
418 | sub push_read_chunk { |
639 | sub push_read_chunk { |
419 | my ($self, $len, $cb) = @_; |
640 | $_[0]->push_read (chunk => $_[1], $_[2]); |
420 | |
|
|
421 | $self->push_read (_read_chunk $len, $cb); |
|
|
422 | } |
641 | } |
423 | |
|
|
424 | |
642 | |
425 | sub unshift_read_chunk { |
643 | sub unshift_read_chunk { |
426 | my ($self, $len, $cb) = @_; |
644 | $_[0]->unshift_read (chunk => $_[1], $_[2]); |
427 | |
|
|
428 | $self->unshift_read (_read_chunk $len, $cb); |
|
|
429 | } |
645 | } |
430 | |
646 | |
431 | =item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) |
647 | =item line => [$eol, ]$cb->($self, $line, $eol) |
432 | |
|
|
433 | =item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol)) |
|
|
434 | |
|
|
435 | Append the given callback to the end of the queue (C<push_read_line>) or |
|
|
436 | prepend it (C<unshift_read_line>). |
|
|
437 | |
648 | |
438 | The callback will be called only once a full line (including the end of |
649 | The callback will be called only once a full line (including the end of |
439 | line marker, C<$eol>) has been read. This line (excluding the end of line |
650 | line marker, C<$eol>) has been read. This line (excluding the end of line |
440 | marker) will be passed to the callback as second argument (C<$line>), and |
651 | marker) will be passed to the callback as second argument (C<$line>), and |
441 | the end of line marker as the third argument (C<$eol>). |
652 | the end of line marker as the third argument (C<$eol>). |
… | |
… | |
452 | Partial lines at the end of the stream will never be returned, as they are |
663 | Partial lines at the end of the stream will never be returned, as they are |
453 | not marked by the end of line marker. |
664 | not marked by the end of line marker. |
454 | |
665 | |
455 | =cut |
666 | =cut |
456 | |
667 | |
457 | sub _read_line($$) { |
668 | register_read_type line => sub { |
458 | my $cb = pop; |
669 | my ($self, $cb, $eol) = @_; |
459 | my $eol = @_ ? shift : qr|(\015?\012)|; |
|
|
460 | my $pos; |
|
|
461 | |
670 | |
|
|
671 | $eol = qr|(\015?\012)| if @_ < 3; |
462 | $eol = qr|(\Q$eol\E)| unless ref $eol; |
672 | $eol = quotemeta $eol unless ref $eol; |
463 | $eol = qr|^(.*?)($eol)|; |
673 | $eol = qr|^(.*?)($eol)|s; |
464 | |
674 | |
465 | sub { |
675 | sub { |
466 | $_[0]{rbuf} =~ s/$eol// or return; |
676 | $_[0]{rbuf} =~ s/$eol// or return; |
467 | |
677 | |
468 | $cb->($1, $2); |
678 | $cb->($_[0], $1, $2); |
469 | 1 |
679 | 1 |
470 | } |
680 | } |
471 | } |
681 | }; |
472 | |
682 | |
|
|
683 | # compatibility with older API |
473 | sub push_read_line { |
684 | sub push_read_line { |
474 | my $self = shift; |
685 | my $self = shift; |
475 | |
|
|
476 | $self->push_read (&_read_line); |
686 | $self->push_read (line => @_); |
477 | } |
687 | } |
478 | |
688 | |
479 | sub unshift_read_line { |
689 | sub unshift_read_line { |
480 | my $self = shift; |
690 | my $self = shift; |
481 | |
|
|
482 | $self->unshift_read (&_read_line); |
691 | $self->unshift_read (line => @_); |
483 | } |
692 | } |
|
|
693 | |
|
|
694 | =item netstring => $cb->($string) |
|
|
695 | |
|
|
696 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
697 | |
|
|
698 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
699 | |
|
|
700 | =cut |
|
|
701 | |
|
|
702 | register_read_type netstring => sub { |
|
|
703 | my ($self, $cb) = @_; |
|
|
704 | |
|
|
705 | sub { |
|
|
706 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
707 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
708 | $! = &Errno::EBADMSG; |
|
|
709 | $self->error; |
|
|
710 | } |
|
|
711 | return; |
|
|
712 | } |
|
|
713 | |
|
|
714 | my $len = $1; |
|
|
715 | |
|
|
716 | $self->unshift_read (chunk => $len, sub { |
|
|
717 | my $string = $_[1]; |
|
|
718 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
719 | if ($_[1] eq ",") { |
|
|
720 | $cb->($_[0], $string); |
|
|
721 | } else { |
|
|
722 | $! = &Errno::EBADMSG; |
|
|
723 | $self->error; |
|
|
724 | } |
|
|
725 | }); |
|
|
726 | }); |
|
|
727 | |
|
|
728 | 1 |
|
|
729 | } |
|
|
730 | }; |
484 | |
731 | |
485 | =back |
732 | =back |
486 | |
733 | |
|
|
734 | =item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) |
|
|
735 | |
|
|
736 | This function (not method) lets you add your own types to C<push_read>. |
|
|
737 | |
|
|
738 | Whenever the given C<type> is used, C<push_read> will invoke the code |
|
|
739 | reference with the handle object, the callback and the remaining |
|
|
740 | arguments. |
|
|
741 | |
|
|
742 | The code reference is supposed to return a callback (usually a closure) |
|
|
743 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
|
|
744 | |
|
|
745 | It should invoke the passed callback when it is done reading (remember to |
|
|
746 | pass C<$self> as first argument as all other callbacks do that). |
|
|
747 | |
|
|
748 | Note that this is a function, and all types registered this way will be |
|
|
749 | global, so try to use unique names. |
|
|
750 | |
|
|
751 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
|
|
752 | search for C<register_read_type>)). |
|
|
753 | |
|
|
754 | =item $handle->stop_read |
|
|
755 | |
|
|
756 | =item $handle->start_read |
|
|
757 | |
|
|
758 | In rare cases you actually do not want to read anything from the |
|
|
759 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
|
|
760 | any queued callbacks will be executed then. To start reading again, call |
|
|
761 | C<start_read>. |
|
|
762 | |
|
|
763 | =cut |
|
|
764 | |
|
|
765 | sub stop_read { |
|
|
766 | my ($self) = @_; |
|
|
767 | |
|
|
768 | delete $self->{rw}; |
|
|
769 | } |
|
|
770 | |
|
|
771 | sub start_read { |
|
|
772 | my ($self) = @_; |
|
|
773 | |
|
|
774 | unless ($self->{rw} || $self->{eof}) { |
|
|
775 | Scalar::Util::weaken $self; |
|
|
776 | |
|
|
777 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
|
|
778 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
|
|
779 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
|
|
780 | |
|
|
781 | if ($len > 0) { |
|
|
782 | $self->{filter_r} |
|
|
783 | ? $self->{filter_r}->($self, $rbuf) |
|
|
784 | : $self->_drain_rbuf; |
|
|
785 | |
|
|
786 | } elsif (defined $len) { |
|
|
787 | delete $self->{rw}; |
|
|
788 | $self->{eof} = 1; |
|
|
789 | $self->_drain_rbuf; |
|
|
790 | |
|
|
791 | } elsif ($! != EAGAIN && $! != EINTR) { |
|
|
792 | return $self->error; |
|
|
793 | } |
|
|
794 | }); |
|
|
795 | } |
|
|
796 | } |
|
|
797 | |
|
|
798 | sub _dotls { |
|
|
799 | my ($self) = @_; |
|
|
800 | |
|
|
801 | if (length $self->{tls_wbuf}) { |
|
|
802 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) { |
|
|
803 | substr $self->{tls_wbuf}, 0, $len, ""; |
|
|
804 | } |
|
|
805 | } |
|
|
806 | |
|
|
807 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
|
|
808 | $self->{wbuf} .= $buf; |
|
|
809 | $self->_drain_wbuf; |
|
|
810 | } |
|
|
811 | |
|
|
812 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
813 | $self->{rbuf} .= $buf; |
|
|
814 | $self->_drain_rbuf; |
|
|
815 | } |
|
|
816 | |
|
|
817 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
|
|
818 | |
|
|
819 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
|
|
820 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
|
|
821 | $self->error; |
|
|
822 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
|
|
823 | $! = &Errno::EIO; |
|
|
824 | $self->error; |
|
|
825 | } |
|
|
826 | |
|
|
827 | # all others are fine for our purposes |
|
|
828 | } |
|
|
829 | } |
|
|
830 | |
|
|
831 | =item $handle->starttls ($tls[, $tls_ctx]) |
|
|
832 | |
|
|
833 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
|
|
834 | object is created, you can also do that at a later time by calling |
|
|
835 | C<starttls>. |
|
|
836 | |
|
|
837 | The first argument is the same as the C<tls> constructor argument (either |
|
|
838 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
|
|
839 | |
|
|
840 | The second argument is the optional C<Net::SSLeay::CTX> object that is |
|
|
841 | used when AnyEvent::Handle has to create its own TLS connection object. |
|
|
842 | |
|
|
843 | =cut |
|
|
844 | |
|
|
845 | # TODO: maybe document... |
|
|
846 | sub starttls { |
|
|
847 | my ($self, $ssl, $ctx) = @_; |
|
|
848 | |
|
|
849 | $self->stoptls; |
|
|
850 | |
|
|
851 | if ($ssl eq "accept") { |
|
|
852 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
853 | Net::SSLeay::set_accept_state ($ssl); |
|
|
854 | } elsif ($ssl eq "connect") { |
|
|
855 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
856 | Net::SSLeay::set_connect_state ($ssl); |
|
|
857 | } |
|
|
858 | |
|
|
859 | $self->{tls} = $ssl; |
|
|
860 | |
|
|
861 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
862 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
863 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
864 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
865 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
|
|
866 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
867 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
868 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
|
|
869 | |
|
|
870 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
871 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
872 | |
|
|
873 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
|
|
874 | |
|
|
875 | $self->{filter_w} = sub { |
|
|
876 | $_[0]{tls_wbuf} .= ${$_[1]}; |
|
|
877 | &_dotls; |
|
|
878 | }; |
|
|
879 | $self->{filter_r} = sub { |
|
|
880 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
|
|
881 | &_dotls; |
|
|
882 | }; |
|
|
883 | } |
|
|
884 | |
|
|
885 | =item $handle->stoptls |
|
|
886 | |
|
|
887 | Destroys the SSL connection, if any. Partial read or write data will be |
|
|
888 | lost. |
|
|
889 | |
|
|
890 | =cut |
|
|
891 | |
|
|
892 | sub stoptls { |
|
|
893 | my ($self) = @_; |
|
|
894 | |
|
|
895 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
896 | delete $self->{tls_rbio}; |
|
|
897 | delete $self->{tls_wbio}; |
|
|
898 | delete $self->{tls_wbuf}; |
|
|
899 | delete $self->{filter_r}; |
|
|
900 | delete $self->{filter_w}; |
|
|
901 | } |
|
|
902 | |
|
|
903 | sub DESTROY { |
|
|
904 | my $self = shift; |
|
|
905 | |
|
|
906 | $self->stoptls; |
|
|
907 | } |
|
|
908 | |
|
|
909 | =item AnyEvent::Handle::TLS_CTX |
|
|
910 | |
|
|
911 | This function creates and returns the Net::SSLeay::CTX object used by |
|
|
912 | default for TLS mode. |
|
|
913 | |
|
|
914 | The context is created like this: |
|
|
915 | |
|
|
916 | Net::SSLeay::load_error_strings; |
|
|
917 | Net::SSLeay::SSLeay_add_ssl_algorithms; |
|
|
918 | Net::SSLeay::randomize; |
|
|
919 | |
|
|
920 | my $CTX = Net::SSLeay::CTX_new; |
|
|
921 | |
|
|
922 | Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL |
|
|
923 | |
|
|
924 | =cut |
|
|
925 | |
|
|
926 | our $TLS_CTX; |
|
|
927 | |
|
|
928 | sub TLS_CTX() { |
|
|
929 | $TLS_CTX || do { |
|
|
930 | require Net::SSLeay; |
|
|
931 | |
|
|
932 | Net::SSLeay::load_error_strings (); |
|
|
933 | Net::SSLeay::SSLeay_add_ssl_algorithms (); |
|
|
934 | Net::SSLeay::randomize (); |
|
|
935 | |
|
|
936 | $TLS_CTX = Net::SSLeay::CTX_new (); |
|
|
937 | |
|
|
938 | Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); |
|
|
939 | |
|
|
940 | $TLS_CTX |
|
|
941 | } |
|
|
942 | } |
|
|
943 | |
|
|
944 | =back |
|
|
945 | |
487 | =head1 AUTHOR |
946 | =head1 AUTHOR |
488 | |
947 | |
489 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
948 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
490 | |
949 | |
491 | =cut |
950 | =cut |