… | |
… | |
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 | |
75 | =item on_eof => $cb->($self) [MANDATORY] |
77 | =item on_eof => $cb->($self) |
76 | |
78 | |
77 | Set the callback to be called on EOF. |
79 | Set the callback to be called on EOF. |
78 | |
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 | |
79 | =item on_error => $cb->($self) |
85 | =item on_error => $cb->($self) |
80 | |
86 | |
81 | This is the fatal error callback, that is called when, well, a fatal error |
87 | This is the fatal error callback, that is called when, well, a fatal error |
82 | ocurs, such as not being able to resolve the hostname, failure to connect |
88 | occurs, such as not being able to resolve the hostname, failure to connect |
83 | or a read error. |
89 | or a read error. |
84 | |
90 | |
85 | 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 |
86 | called. |
92 | called. |
87 | |
93 | |
… | |
… | |
96 | |
102 | |
97 | 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 |
98 | and no read request is in the queue. |
104 | and no read request is in the queue. |
99 | |
105 | |
100 | 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 >> |
101 | method or acces sthe C<$self->{rbuf}> member directly. |
107 | method or access the C<$self->{rbuf}> member directly. |
102 | |
108 | |
103 | 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 |
104 | 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 |
105 | 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 |
106 | error will be raised (with C<$!> set to C<EPIPE>). |
112 | error will be raised (with C<$!> set to C<EPIPE>). |
… | |
… | |
133 | |
139 | |
134 | 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 |
135 | 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 |
136 | considered empty. |
142 | considered empty. |
137 | |
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 | For the TLS server side, use C<accept>, and for the TLS client side of a |
|
|
151 | connection, use C<connect> mode. |
|
|
152 | |
|
|
153 | You can also provide your own TLS connection object, but you have |
|
|
154 | to make sure that you call either C<Net::SSLeay::set_connect_state> |
|
|
155 | or C<Net::SSLeay::set_accept_state> on it before you pass it to |
|
|
156 | AnyEvent::Handle. |
|
|
157 | |
|
|
158 | =item tls_ctx => $ssl_ctx |
|
|
159 | |
|
|
160 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
|
|
161 | (unless a connection object was specified directly). If this parameter is |
|
|
162 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
|
|
163 | |
138 | =back |
164 | =back |
139 | |
165 | |
140 | =cut |
166 | =cut |
141 | |
167 | |
142 | sub new { |
168 | sub new { |
… | |
… | |
146 | |
172 | |
147 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
173 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
148 | |
174 | |
149 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
175 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
150 | |
176 | |
151 | $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); |
177 | if ($self->{tls}) { |
|
|
178 | require Net::SSLeay; |
|
|
179 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
|
|
180 | } |
152 | |
181 | |
|
|
182 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
153 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
183 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
154 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
184 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
155 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
185 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
156 | |
186 | |
157 | $self->start_read; |
187 | $self->start_read; |
… | |
… | |
182 | } |
212 | } |
183 | } |
213 | } |
184 | |
214 | |
185 | =item $fh = $handle->fh |
215 | =item $fh = $handle->fh |
186 | |
216 | |
187 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
217 | This method returns the file handle of the L<AnyEvent::Handle> object. |
188 | |
218 | |
189 | =cut |
219 | =cut |
190 | |
220 | |
191 | sub fh { $_[0]->{fh} } |
221 | sub fh { $_[0]->{fh} } |
192 | |
222 | |
… | |
… | |
220 | for reading. |
250 | for reading. |
221 | |
251 | |
222 | The write queue is very simple: you can add data to its end, and |
252 | The write queue is very simple: you can add data to its end, and |
223 | AnyEvent::Handle will automatically try to get rid of it for you. |
253 | AnyEvent::Handle will automatically try to get rid of it for you. |
224 | |
254 | |
225 | When data could be writtena nd the write buffer is shorter then the low |
255 | When data could be written and the write buffer is shorter then the low |
226 | water mark, the C<on_drain> callback will be invoked. |
256 | water mark, the C<on_drain> callback will be invoked. |
227 | |
257 | |
228 | =over 4 |
258 | =over 4 |
229 | |
259 | |
230 | =item $handle->on_drain ($cb) |
260 | =item $handle->on_drain ($cb) |
… | |
… | |
249 | want (only limited by the available memory), as C<AnyEvent::Handle> |
279 | want (only limited by the available memory), as C<AnyEvent::Handle> |
250 | buffers it independently of the kernel. |
280 | buffers it independently of the kernel. |
251 | |
281 | |
252 | =cut |
282 | =cut |
253 | |
283 | |
254 | sub push_write { |
284 | sub _drain_wbuf { |
255 | my ($self, $data) = @_; |
285 | my ($self) = @_; |
256 | |
|
|
257 | $self->{wbuf} .= $data; |
|
|
258 | |
286 | |
259 | unless ($self->{ww}) { |
287 | unless ($self->{ww}) { |
260 | Scalar::Util::weaken $self; |
288 | Scalar::Util::weaken $self; |
261 | my $cb = sub { |
289 | my $cb = sub { |
262 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
290 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
263 | |
291 | |
264 | if ($len > 0) { |
292 | if ($len > 0) { |
265 | substr $self->{wbuf}, 0, $len, ""; |
293 | substr $self->{wbuf}, 0, $len, ""; |
266 | |
|
|
267 | |
294 | |
268 | $self->{on_drain}($self) |
295 | $self->{on_drain}($self) |
269 | if $self->{low_water_mark} >= length $self->{wbuf} |
296 | if $self->{low_water_mark} >= length $self->{wbuf} |
270 | && $self->{on_drain}; |
297 | && $self->{on_drain}; |
271 | |
298 | |
… | |
… | |
277 | |
304 | |
278 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); |
305 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); |
279 | |
306 | |
280 | $cb->($self); |
307 | $cb->($self); |
281 | }; |
308 | }; |
|
|
309 | } |
|
|
310 | |
|
|
311 | sub push_write { |
|
|
312 | my $self = shift; |
|
|
313 | |
|
|
314 | if ($self->{filter_w}) { |
|
|
315 | $self->{filter_w}->($self, \$_[0]); |
|
|
316 | } else { |
|
|
317 | $self->{wbuf} .= $_[0]; |
|
|
318 | $self->_drain_wbuf; |
|
|
319 | } |
282 | } |
320 | } |
283 | |
321 | |
284 | ############################################################################# |
322 | ############################################################################# |
285 | |
323 | |
286 | =back |
324 | =back |
… | |
… | |
361 | |
399 | |
362 | =cut |
400 | =cut |
363 | |
401 | |
364 | sub _drain_rbuf { |
402 | sub _drain_rbuf { |
365 | my ($self) = @_; |
403 | my ($self) = @_; |
|
|
404 | |
|
|
405 | if ( |
|
|
406 | defined $self->{rbuf_max} |
|
|
407 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
408 | ) { |
|
|
409 | $! = &Errno::ENOSPC; return $self->error; |
|
|
410 | } |
366 | |
411 | |
367 | return if $self->{in_drain}; |
412 | return if $self->{in_drain}; |
368 | local $self->{in_drain} = 1; |
413 | local $self->{in_drain} = 1; |
369 | |
414 | |
370 | while (my $len = length $self->{rbuf}) { |
415 | while (my $len = length $self->{rbuf}) { |
… | |
… | |
398 | } |
443 | } |
399 | } |
444 | } |
400 | |
445 | |
401 | if ($self->{eof}) { |
446 | if ($self->{eof}) { |
402 | $self->_shutdown; |
447 | $self->_shutdown; |
403 | $self->{on_eof}($self); |
448 | $self->{on_eof}($self) |
|
|
449 | if $self->{on_eof}; |
404 | } |
450 | } |
405 | } |
451 | } |
406 | |
452 | |
407 | =item $handle->on_read ($cb) |
453 | =item $handle->on_read ($cb) |
408 | |
454 | |
… | |
… | |
442 | Append the given callback to the end of the queue (C<push_read>) or |
488 | Append the given callback to the end of the queue (C<push_read>) or |
443 | prepend it (C<unshift_read>). |
489 | prepend it (C<unshift_read>). |
444 | |
490 | |
445 | The callback is called each time some additional read data arrives. |
491 | The callback is called each time some additional read data arrives. |
446 | |
492 | |
447 | It must check wether enough data is in the read buffer already. |
493 | It must check whether enough data is in the read buffer already. |
448 | |
494 | |
449 | If not enough data is available, it must return the empty list or a false |
495 | If not enough data is available, it must return the empty list or a false |
450 | value, in which case it will be called repeatedly until enough data is |
496 | value, in which case it will be called repeatedly until enough data is |
451 | available (or an error condition is detected). |
497 | available (or an error condition is detected). |
452 | |
498 | |
… | |
… | |
531 | my $self = shift; |
577 | my $self = shift; |
532 | my $cb = pop; |
578 | my $cb = pop; |
533 | my $eol = @_ ? shift : qr|(\015?\012)|; |
579 | my $eol = @_ ? shift : qr|(\015?\012)|; |
534 | my $pos; |
580 | my $pos; |
535 | |
581 | |
536 | $eol = qr|(\Q$eol\E)| unless ref $eol; |
582 | $eol = quotemeta $eol unless ref $eol; |
537 | $eol = qr|^(.*?)($eol)|; |
583 | $eol = qr|^(.*?)($eol)|s; |
538 | |
584 | |
539 | sub { |
585 | sub { |
540 | $_[0]{rbuf} =~ s/$eol// or return; |
586 | $_[0]{rbuf} =~ s/$eol// or return; |
541 | |
587 | |
542 | $cb->($_[0], $1, $2); |
588 | $cb->($_[0], $1, $2); |
… | |
… | |
554 | |
600 | |
555 | =item $handle->stop_read |
601 | =item $handle->stop_read |
556 | |
602 | |
557 | =item $handle->start_read |
603 | =item $handle->start_read |
558 | |
604 | |
559 | In rare cases you actually do not want to read anything form the |
605 | In rare cases you actually do not want to read anything from the |
560 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
606 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
561 | any queued callbacks will be executed then. To start readign again, call |
607 | any queued callbacks will be executed then. To start reading again, call |
562 | C<start_read>. |
608 | C<start_read>. |
563 | |
609 | |
564 | =cut |
610 | =cut |
565 | |
611 | |
566 | sub stop_read { |
612 | sub stop_read { |
… | |
… | |
574 | |
620 | |
575 | unless ($self->{rw} || $self->{eof}) { |
621 | unless ($self->{rw} || $self->{eof}) { |
576 | Scalar::Util::weaken $self; |
622 | Scalar::Util::weaken $self; |
577 | |
623 | |
578 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
624 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
|
|
625 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
579 | my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; |
626 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
580 | |
627 | |
581 | if ($len > 0) { |
628 | if ($len > 0) { |
582 | if (defined $self->{rbuf_max}) { |
629 | $self->{filter_r} |
583 | if ($self->{rbuf_max} < length $self->{rbuf}) { |
630 | ? $self->{filter_r}->($self, $rbuf) |
584 | $! = &Errno::ENOSPC; return $self->error; |
631 | : $self->_drain_rbuf; |
585 | } |
|
|
586 | } |
|
|
587 | |
632 | |
588 | } elsif (defined $len) { |
633 | } elsif (defined $len) { |
|
|
634 | delete $self->{rw}; |
589 | $self->{eof} = 1; |
635 | $self->{eof} = 1; |
590 | delete $self->{rw}; |
636 | $self->_drain_rbuf; |
591 | |
637 | |
592 | } elsif ($! != EAGAIN && $! != EINTR) { |
638 | } elsif ($! != EAGAIN && $! != EINTR) { |
593 | return $self->error; |
639 | return $self->error; |
594 | } |
640 | } |
595 | |
|
|
596 | $self->_drain_rbuf; |
|
|
597 | }); |
641 | }); |
598 | } |
642 | } |
599 | } |
643 | } |
600 | |
644 | |
|
|
645 | sub _dotls { |
|
|
646 | my ($self) = @_; |
|
|
647 | |
|
|
648 | if (length $self->{tls_wbuf}) { |
|
|
649 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) { |
|
|
650 | substr $self->{tls_wbuf}, 0, $len, ""; |
|
|
651 | } |
|
|
652 | } |
|
|
653 | |
|
|
654 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
|
|
655 | $self->{wbuf} .= $buf; |
|
|
656 | $self->_drain_wbuf; |
|
|
657 | } |
|
|
658 | |
|
|
659 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
660 | $self->{rbuf} .= $buf; |
|
|
661 | $self->_drain_rbuf; |
|
|
662 | } |
|
|
663 | |
|
|
664 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
|
|
665 | |
|
|
666 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
|
|
667 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
|
|
668 | $self->error; |
|
|
669 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
|
|
670 | $! = &Errno::EIO; |
|
|
671 | $self->error; |
|
|
672 | } |
|
|
673 | |
|
|
674 | # all others are fine for our purposes |
|
|
675 | } |
|
|
676 | } |
|
|
677 | |
|
|
678 | # TODO: maybe document... |
|
|
679 | sub starttls { |
|
|
680 | my ($self, $ssl, $ctx) = @_; |
|
|
681 | |
|
|
682 | if ($ssl eq "accept") { |
|
|
683 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
684 | Net::SSLeay::set_accept_state ($ssl); |
|
|
685 | } elsif ($ssl eq "connect") { |
|
|
686 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
|
|
687 | Net::SSLeay::set_connect_state ($ssl); |
|
|
688 | } |
|
|
689 | |
|
|
690 | $self->{tls} = $ssl; |
|
|
691 | |
|
|
692 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
693 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
694 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
695 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
696 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
697 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
698 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
|
|
699 | |
|
|
700 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
701 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
|
|
702 | |
|
|
703 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
|
|
704 | |
|
|
705 | $self->{filter_w} = sub { |
|
|
706 | $_[0]{tls_wbuf} .= ${$_[1]}; |
|
|
707 | &_dotls; |
|
|
708 | }; |
|
|
709 | $self->{filter_r} = sub { |
|
|
710 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
|
|
711 | &_dotls; |
|
|
712 | }; |
|
|
713 | } |
|
|
714 | |
|
|
715 | sub DESTROY { |
|
|
716 | my $self = shift; |
|
|
717 | |
|
|
718 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
719 | } |
|
|
720 | |
|
|
721 | =item AnyEvent::Handle::TLS_CTX |
|
|
722 | |
|
|
723 | This function creates and returns the Net::SSLeay::CTX object used by |
|
|
724 | default for TLS mode. |
|
|
725 | |
|
|
726 | The context is created like this: |
|
|
727 | |
|
|
728 | Net::SSLeay::load_error_strings; |
|
|
729 | Net::SSLeay::SSLeay_add_ssl_algorithms; |
|
|
730 | Net::SSLeay::randomize; |
|
|
731 | |
|
|
732 | my $CTX = Net::SSLeay::CTX_new; |
|
|
733 | |
|
|
734 | Net::SSLeay::CTX_set_options $CTX, Net::SSLeay::OP_ALL |
|
|
735 | |
|
|
736 | =cut |
|
|
737 | |
|
|
738 | our $TLS_CTX; |
|
|
739 | |
|
|
740 | sub TLS_CTX() { |
|
|
741 | $TLS_CTX || do { |
|
|
742 | require Net::SSLeay; |
|
|
743 | |
|
|
744 | Net::SSLeay::load_error_strings (); |
|
|
745 | Net::SSLeay::SSLeay_add_ssl_algorithms (); |
|
|
746 | Net::SSLeay::randomize (); |
|
|
747 | |
|
|
748 | $TLS_CTX = Net::SSLeay::CTX_new (); |
|
|
749 | |
|
|
750 | Net::SSLeay::CTX_set_options ($TLS_CTX, Net::SSLeay::OP_ALL ()); |
|
|
751 | |
|
|
752 | $TLS_CTX |
|
|
753 | } |
|
|
754 | } |
|
|
755 | |
601 | =back |
756 | =back |
602 | |
757 | |
603 | =head1 AUTHOR |
758 | =head1 AUTHOR |
604 | |
759 | |
605 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
760 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |