1 | package AnyEvent::Handle; |
|
|
2 | |
|
|
3 | use Scalar::Util (); |
|
|
4 | use Carp (); |
|
|
5 | use Errno qw(EAGAIN EINTR); |
|
|
6 | |
|
|
7 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
8 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
9 | |
|
|
10 | =head1 NAME |
1 | =head1 NAME |
11 | |
2 | |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
|
|
14 | =cut |
|
|
15 | |
|
|
16 | our $VERSION = 4.87; |
|
|
17 | |
4 | |
18 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
19 | |
6 | |
20 | use AnyEvent; |
7 | use AnyEvent; |
21 | use AnyEvent::Handle; |
8 | use AnyEvent::Handle; |
… | |
… | |
59 | C<on_error> callback. |
46 | C<on_error> callback. |
60 | |
47 | |
61 | All callbacks will be invoked with the handle object as their first |
48 | All callbacks will be invoked with the handle object as their first |
62 | argument. |
49 | argument. |
63 | |
50 | |
|
|
51 | =cut |
|
|
52 | |
|
|
53 | package AnyEvent::Handle; |
|
|
54 | |
|
|
55 | use Scalar::Util (); |
|
|
56 | use List::Util (); |
|
|
57 | use Carp (); |
|
|
58 | use Errno qw(EAGAIN EINTR); |
|
|
59 | |
|
|
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
62 | |
|
|
63 | our $VERSION = $AnyEvent::VERSION; |
|
|
64 | |
64 | =head1 METHODS |
65 | =head1 METHODS |
65 | |
66 | |
66 | =over 4 |
67 | =over 4 |
67 | |
68 | |
68 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
69 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
… | |
… | |
216 | memory and push it into the queue, but instead only read more data from |
217 | memory and push it into the queue, but instead only read more data from |
217 | the file when the write queue becomes empty. |
218 | the file when the write queue becomes empty. |
218 | |
219 | |
219 | =item timeout => $fractional_seconds |
220 | =item timeout => $fractional_seconds |
220 | |
221 | |
|
|
222 | =item rtimeout => $fractional_seconds |
|
|
223 | |
|
|
224 | =item wtimeout => $fractional_seconds |
|
|
225 | |
221 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
226 | If non-zero, then these enables an "inactivity" timeout: whenever this |
222 | seconds pass without a successful read or write on the underlying file |
227 | many seconds pass without a successful read or write on the underlying |
223 | handle, the C<on_timeout> callback will be invoked (and if that one is |
228 | file handle (or a call to C<timeout_reset>), the C<on_timeout> callback |
224 | missing, a non-fatal C<ETIMEDOUT> error will be raised). |
229 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
|
|
230 | error will be raised). |
|
|
231 | |
|
|
232 | There are three variants of the timeouts that work fully independent |
|
|
233 | of each other, for both read and write, just read, and just write: |
|
|
234 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
|
|
235 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
|
|
236 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
225 | |
237 | |
226 | Note that timeout processing is also active when you currently do not have |
238 | Note that timeout processing is also active when you currently do not have |
227 | any outstanding read or write requests: If you plan to keep the connection |
239 | any outstanding read or write requests: If you plan to keep the connection |
228 | idle then you should disable the timout temporarily or ignore the timeout |
240 | idle then you should disable the timout temporarily or ignore the timeout |
229 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
241 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
… | |
… | |
273 | accomplishd by setting this option to a true value. |
285 | accomplishd by setting this option to a true value. |
274 | |
286 | |
275 | The default is your opertaing system's default behaviour (most likely |
287 | The default is your opertaing system's default behaviour (most likely |
276 | enabled), this option explicitly enables or disables it, if possible. |
288 | enabled), this option explicitly enables or disables it, if possible. |
277 | |
289 | |
|
|
290 | =item keepalive => <boolean> |
|
|
291 | |
|
|
292 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
|
|
293 | normally, TCP connections have no time-out once established, so TCP |
|
|
294 | conenctions, once established, can stay alive forever even when the other |
|
|
295 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
|
|
296 | TCP connections whent he other side becomes unreachable. While the default |
|
|
297 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
|
|
298 | and, if the other side doesn't reply, take down the TCP connection some 10 |
|
|
299 | to 15 minutes later. |
|
|
300 | |
|
|
301 | It is harmless to specify this option for file handles that do not support |
|
|
302 | keepalives, and enabling it on connections that are potentially long-lived |
|
|
303 | is usually a good idea. |
|
|
304 | |
|
|
305 | =item oobinline => <boolean> |
|
|
306 | |
|
|
307 | BSD majorly fucked up the implementation of TCP urgent data. The result |
|
|
308 | is that almost no OS implements TCP according to the specs, and every OS |
|
|
309 | implements it slightly differently. |
|
|
310 | |
|
|
311 | If you want to handle TCP urgent data, then setting this flag gives you |
|
|
312 | the most portable way of getting urgent data, by putting it into the |
|
|
313 | stream. |
|
|
314 | |
278 | =item read_size => <bytes> |
315 | =item read_size => <bytes> |
279 | |
316 | |
280 | The default read block size (the amount of bytes this module will |
317 | The default read block size (the amount of bytes this module will |
281 | try to read during each loop iteration, which affects memory |
318 | try to read during each loop iteration, which affects memory |
282 | requirements). Default: C<8192>. |
319 | requirements). Default: C<8192>. |
… | |
… | |
438 | delete $self->{_skip_drain_rbuf}; |
475 | delete $self->{_skip_drain_rbuf}; |
439 | $self->_start; |
476 | $self->_start; |
440 | |
477 | |
441 | $self->{on_connect} |
478 | $self->{on_connect} |
442 | and $self->{on_connect}($self, $host, $port, sub { |
479 | and $self->{on_connect}($self, $host, $port, sub { |
443 | delete @$self{qw(fh _tw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
480 | delete @$self{qw(fh _tw _rtw _wtw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
444 | $self->{_skip_drain_rbuf} = 1; |
481 | $self->{_skip_drain_rbuf} = 1; |
445 | &$retry; |
482 | &$retry; |
446 | }); |
483 | }); |
447 | |
484 | |
448 | } else { |
485 | } else { |
… | |
… | |
474 | sub _start { |
511 | sub _start { |
475 | my ($self) = @_; |
512 | my ($self) = @_; |
476 | |
513 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
514 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
515 | |
|
|
516 | $self->{_activity} = |
|
|
517 | $self->{_ractivity} = |
479 | $self->{_activity} = AnyEvent->now; |
518 | $self->{_wactivity} = AE::now; |
480 | $self->_timeout; |
|
|
481 | |
519 | |
|
|
520 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
521 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
|
|
522 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
|
|
523 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
524 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay}; |
|
|
525 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive}; |
|
|
526 | $self->oobinline (delete $self->{oobinline}) if exists $self->{oobinline}; |
483 | |
527 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
528 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
485 | if $self->{tls}; |
529 | if $self->{tls}; |
486 | |
530 | |
487 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
531 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
488 | |
532 | |
489 | $self->start_read |
533 | $self->start_read |
490 | if $self->{on_read} || @{ $self->{_queue} }; |
534 | if $self->{on_read} || @{ $self->{_queue} }; |
491 | |
535 | |
492 | $self->_drain_wbuf; |
536 | $self->_drain_wbuf; |
493 | } |
537 | } |
494 | |
|
|
495 | #sub _shutdown { |
|
|
496 | # my ($self) = @_; |
|
|
497 | # |
|
|
498 | # delete @$self{qw(_tw _rw _ww fh wbuf on_read _queue)}; |
|
|
499 | # $self->{_eof} = 1; # tell starttls et. al to stop trying |
|
|
500 | # |
|
|
501 | # &_freetls; |
|
|
502 | #} |
|
|
503 | |
538 | |
504 | sub _error { |
539 | sub _error { |
505 | my ($self, $errno, $fatal, $message) = @_; |
540 | my ($self, $errno, $fatal, $message) = @_; |
506 | |
541 | |
507 | $! = $errno; |
542 | $! = $errno; |
… | |
… | |
544 | $_[0]{on_eof} = $_[1]; |
579 | $_[0]{on_eof} = $_[1]; |
545 | } |
580 | } |
546 | |
581 | |
547 | =item $handle->on_timeout ($cb) |
582 | =item $handle->on_timeout ($cb) |
548 | |
583 | |
549 | Replace the current C<on_timeout> callback, or disables the callback (but |
584 | =item $handle->on_rtimeout ($cb) |
550 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
551 | argument and method. |
|
|
552 | |
585 | |
553 | =cut |
586 | =item $handle->on_wtimeout ($cb) |
554 | |
587 | |
555 | sub on_timeout { |
588 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
556 | $_[0]{on_timeout} = $_[1]; |
589 | callback, or disables the callback (but not the timeout) if C<$cb> = |
557 | } |
590 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
591 | |
|
|
592 | =cut |
|
|
593 | |
|
|
594 | # see below |
558 | |
595 | |
559 | =item $handle->autocork ($boolean) |
596 | =item $handle->autocork ($boolean) |
560 | |
597 | |
561 | Enables or disables the current autocork behaviour (see C<autocork> |
598 | Enables or disables the current autocork behaviour (see C<autocork> |
562 | constructor argument). Changes will only take effect on the next write. |
599 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
577 | sub no_delay { |
614 | sub no_delay { |
578 | $_[0]{no_delay} = $_[1]; |
615 | $_[0]{no_delay} = $_[1]; |
579 | |
616 | |
580 | eval { |
617 | eval { |
581 | local $SIG{__DIE__}; |
618 | local $SIG{__DIE__}; |
582 | setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] |
619 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
583 | if $_[0]{fh}; |
620 | if $_[0]{fh}; |
584 | }; |
621 | }; |
585 | } |
622 | } |
586 | |
623 | |
|
|
624 | =item $handle->keepalive ($boolean) |
|
|
625 | |
|
|
626 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
627 | the same name for details). |
|
|
628 | |
|
|
629 | =cut |
|
|
630 | |
|
|
631 | sub keepalive { |
|
|
632 | $_[0]{keepalive} = $_[1]; |
|
|
633 | |
|
|
634 | eval { |
|
|
635 | local $SIG{__DIE__}; |
|
|
636 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
637 | if $_[0]{fh}; |
|
|
638 | }; |
|
|
639 | } |
|
|
640 | |
|
|
641 | =item $handle->oobinline ($boolean) |
|
|
642 | |
|
|
643 | Enables or disables the C<oobinline> setting (see constructor argument of |
|
|
644 | the same name for details). |
|
|
645 | |
|
|
646 | =cut |
|
|
647 | |
|
|
648 | sub oobinline { |
|
|
649 | $_[0]{oobinline} = $_[1]; |
|
|
650 | |
|
|
651 | eval { |
|
|
652 | local $SIG{__DIE__}; |
|
|
653 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
|
|
654 | if $_[0]{fh}; |
|
|
655 | }; |
|
|
656 | } |
|
|
657 | |
|
|
658 | =item $handle->keepalive ($boolean) |
|
|
659 | |
|
|
660 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
661 | the same name for details). |
|
|
662 | |
|
|
663 | =cut |
|
|
664 | |
|
|
665 | sub keepalive { |
|
|
666 | $_[0]{keepalive} = $_[1]; |
|
|
667 | |
|
|
668 | eval { |
|
|
669 | local $SIG{__DIE__}; |
|
|
670 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
671 | if $_[0]{fh}; |
|
|
672 | }; |
|
|
673 | } |
|
|
674 | |
587 | =item $handle->on_starttls ($cb) |
675 | =item $handle->on_starttls ($cb) |
588 | |
676 | |
589 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
677 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
590 | |
678 | |
591 | =cut |
679 | =cut |
… | |
… | |
602 | |
690 | |
603 | sub on_starttls { |
691 | sub on_starttls { |
604 | $_[0]{on_stoptls} = $_[1]; |
692 | $_[0]{on_stoptls} = $_[1]; |
605 | } |
693 | } |
606 | |
694 | |
|
|
695 | =item $handle->rbuf_max ($max_octets) |
|
|
696 | |
|
|
697 | Configures the C<rbuf_max> setting (C<undef> disables it). |
|
|
698 | |
|
|
699 | =cut |
|
|
700 | |
|
|
701 | sub rbuf_max { |
|
|
702 | $_[0]{rbuf_max} = $_[1]; |
|
|
703 | } |
|
|
704 | |
607 | ############################################################################# |
705 | ############################################################################# |
608 | |
706 | |
609 | =item $handle->timeout ($seconds) |
707 | =item $handle->timeout ($seconds) |
610 | |
708 | |
|
|
709 | =item $handle->rtimeout ($seconds) |
|
|
710 | |
|
|
711 | =item $handle->wtimeout ($seconds) |
|
|
712 | |
611 | Configures (or disables) the inactivity timeout. |
713 | Configures (or disables) the inactivity timeout. |
612 | |
714 | |
613 | =cut |
715 | =item $handle->timeout_reset |
614 | |
716 | |
615 | sub timeout { |
717 | =item $handle->rtimeout_reset |
|
|
718 | |
|
|
719 | =item $handle->wtimeout_reset |
|
|
720 | |
|
|
721 | Reset the activity timeout, as if data was received or sent. |
|
|
722 | |
|
|
723 | These methods are cheap to call. |
|
|
724 | |
|
|
725 | =cut |
|
|
726 | |
|
|
727 | for my $dir ("", "r", "w") { |
|
|
728 | my $timeout = "${dir}timeout"; |
|
|
729 | my $tw = "_${dir}tw"; |
|
|
730 | my $on_timeout = "on_${dir}timeout"; |
|
|
731 | my $activity = "_${dir}activity"; |
|
|
732 | my $cb; |
|
|
733 | |
|
|
734 | *$on_timeout = sub { |
|
|
735 | $_[0]{$on_timeout} = $_[1]; |
|
|
736 | }; |
|
|
737 | |
|
|
738 | *$timeout = sub { |
616 | my ($self, $timeout) = @_; |
739 | my ($self, $new_value) = @_; |
617 | |
740 | |
618 | $self->{timeout} = $timeout; |
741 | $self->{$timeout} = $new_value; |
619 | $self->_timeout; |
742 | delete $self->{$tw}; &$cb; |
620 | } |
743 | }; |
621 | |
744 | |
|
|
745 | *{"${dir}timeout_reset"} = sub { |
|
|
746 | $_[0]{$activity} = AE::now; |
|
|
747 | }; |
|
|
748 | |
|
|
749 | # main workhorse: |
622 | # reset the timeout watcher, as neccessary |
750 | # reset the timeout watcher, as neccessary |
623 | # also check for time-outs |
751 | # also check for time-outs |
624 | sub _timeout { |
752 | $cb = sub { |
625 | my ($self) = @_; |
753 | my ($self) = @_; |
626 | |
754 | |
627 | if ($self->{timeout} && $self->{fh}) { |
755 | if ($self->{$timeout} && $self->{fh}) { |
628 | my $NOW = AnyEvent->now; |
756 | my $NOW = AE::now; |
629 | |
757 | |
630 | # when would the timeout trigger? |
758 | # when would the timeout trigger? |
631 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
759 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
632 | |
760 | |
633 | # now or in the past already? |
761 | # now or in the past already? |
634 | if ($after <= 0) { |
762 | if ($after <= 0) { |
635 | $self->{_activity} = $NOW; |
763 | $self->{$activity} = $NOW; |
636 | |
764 | |
637 | if ($self->{on_timeout}) { |
765 | if ($self->{$on_timeout}) { |
638 | $self->{on_timeout}($self); |
766 | $self->{$on_timeout}($self); |
639 | } else { |
767 | } else { |
640 | $self->_error (Errno::ETIMEDOUT); |
768 | $self->_error (Errno::ETIMEDOUT); |
|
|
769 | } |
|
|
770 | |
|
|
771 | # callback could have changed timeout value, optimise |
|
|
772 | return unless $self->{$timeout}; |
|
|
773 | |
|
|
774 | # calculate new after |
|
|
775 | $after = $self->{$timeout}; |
641 | } |
776 | } |
642 | |
777 | |
643 | # callback could have changed timeout value, optimise |
778 | Scalar::Util::weaken $self; |
644 | return unless $self->{timeout}; |
779 | return unless $self; # ->error could have destroyed $self |
645 | |
780 | |
646 | # calculate new after |
781 | $self->{$tw} ||= AE::timer $after, 0, sub { |
647 | $after = $self->{timeout}; |
782 | delete $self->{$tw}; |
|
|
783 | $cb->($self); |
|
|
784 | }; |
|
|
785 | } else { |
|
|
786 | delete $self->{$tw}; |
648 | } |
787 | } |
649 | |
|
|
650 | Scalar::Util::weaken $self; |
|
|
651 | return unless $self; # ->error could have destroyed $self |
|
|
652 | |
|
|
653 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
|
|
654 | delete $self->{_tw}; |
|
|
655 | $self->_timeout; |
|
|
656 | }); |
|
|
657 | } else { |
|
|
658 | delete $self->{_tw}; |
|
|
659 | } |
788 | } |
660 | } |
789 | } |
661 | |
790 | |
662 | ############################################################################# |
791 | ############################################################################# |
663 | |
792 | |
… | |
… | |
711 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
840 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
712 | |
841 | |
713 | if (defined $len) { |
842 | if (defined $len) { |
714 | substr $self->{wbuf}, 0, $len, ""; |
843 | substr $self->{wbuf}, 0, $len, ""; |
715 | |
844 | |
716 | $self->{_activity} = AnyEvent->now; |
845 | $self->{_activity} = $self->{_wactivity} = AE::now; |
717 | |
846 | |
718 | $self->{on_drain}($self) |
847 | $self->{on_drain}($self) |
719 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
848 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
720 | && $self->{on_drain}; |
849 | && $self->{on_drain}; |
721 | |
850 | |
… | |
… | |
727 | |
856 | |
728 | # try to write data immediately |
857 | # try to write data immediately |
729 | $cb->() unless $self->{autocork}; |
858 | $cb->() unless $self->{autocork}; |
730 | |
859 | |
731 | # if still data left in wbuf, we need to poll |
860 | # if still data left in wbuf, we need to poll |
732 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
861 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
733 | if length $self->{wbuf}; |
862 | if length $self->{wbuf}; |
734 | }; |
863 | }; |
735 | } |
864 | } |
736 | |
865 | |
737 | our %WH; |
866 | our %WH; |
… | |
… | |
827 | Other languages could read single lines terminated by a newline and pass |
956 | Other languages could read single lines terminated by a newline and pass |
828 | this line into their JSON decoder of choice. |
957 | this line into their JSON decoder of choice. |
829 | |
958 | |
830 | =cut |
959 | =cut |
831 | |
960 | |
|
|
961 | sub json_coder() { |
|
|
962 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
963 | || do { require JSON; JSON->new->utf8 } |
|
|
964 | } |
|
|
965 | |
832 | register_write_type json => sub { |
966 | register_write_type json => sub { |
833 | my ($self, $ref) = @_; |
967 | my ($self, $ref) = @_; |
834 | |
968 | |
835 | require JSON; |
969 | my $json = $self->{json} ||= json_coder; |
836 | |
970 | |
837 | $self->{json} ? $self->{json}->encode ($ref) |
971 | $json->encode ($ref) |
838 | : JSON::encode_json ($ref) |
|
|
839 | }; |
972 | }; |
840 | |
973 | |
841 | =item storable => $reference |
974 | =item storable => $reference |
842 | |
975 | |
843 | Freezes the given reference using L<Storable> and writes it to the |
976 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
981 | |
1114 | |
982 | sub _drain_rbuf { |
1115 | sub _drain_rbuf { |
983 | my ($self) = @_; |
1116 | my ($self) = @_; |
984 | |
1117 | |
985 | # avoid recursion |
1118 | # avoid recursion |
986 | return if exists $self->{_skip_drain_rbuf}; |
1119 | return if $self->{_skip_drain_rbuf}; |
987 | local $self->{_skip_drain_rbuf} = 1; |
1120 | local $self->{_skip_drain_rbuf} = 1; |
988 | |
|
|
989 | if ( |
|
|
990 | defined $self->{rbuf_max} |
|
|
991 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
992 | ) { |
|
|
993 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
994 | } |
|
|
995 | |
1121 | |
996 | while () { |
1122 | while () { |
997 | # we need to use a separate tls read buffer, as we must not receive data while |
1123 | # we need to use a separate tls read buffer, as we must not receive data while |
998 | # we are draining the buffer, and this can only happen with TLS. |
1124 | # we are draining the buffer, and this can only happen with TLS. |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
1125 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
… | |
… | |
1041 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1167 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1042 | |
1168 | |
1043 | return; |
1169 | return; |
1044 | } |
1170 | } |
1045 | |
1171 | |
|
|
1172 | if ( |
|
|
1173 | defined $self->{rbuf_max} |
|
|
1174 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1175 | ) { |
|
|
1176 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
1177 | } |
|
|
1178 | |
1046 | # may need to restart read watcher |
1179 | # may need to restart read watcher |
1047 | unless ($self->{_rw}) { |
1180 | unless ($self->{_rw}) { |
1048 | $self->start_read |
1181 | $self->start_read |
1049 | if $self->{on_read} || @{ $self->{_queue} }; |
1182 | if $self->{on_read} || @{ $self->{_queue} }; |
1050 | } |
1183 | } |
… | |
… | |
1135 | my $type = shift; |
1268 | my $type = shift; |
1136 | |
1269 | |
1137 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1270 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1138 | ->($self, $cb, @_); |
1271 | ->($self, $cb, @_); |
1139 | } |
1272 | } |
1140 | |
|
|
1141 | |
1273 | |
1142 | unshift @{ $self->{_queue} }, $cb; |
1274 | unshift @{ $self->{_queue} }, $cb; |
1143 | $self->_drain_rbuf; |
1275 | $self->_drain_rbuf; |
1144 | } |
1276 | } |
1145 | |
1277 | |
… | |
… | |
1397 | =cut |
1529 | =cut |
1398 | |
1530 | |
1399 | register_read_type json => sub { |
1531 | register_read_type json => sub { |
1400 | my ($self, $cb) = @_; |
1532 | my ($self, $cb) = @_; |
1401 | |
1533 | |
1402 | my $json = $self->{json} ||= |
1534 | my $json = $self->{json} ||= json_coder; |
1403 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1404 | || do { require JSON; JSON->new->utf8 }; |
|
|
1405 | |
1535 | |
1406 | my $data; |
1536 | my $data; |
1407 | my $rbuf = \$self->{rbuf}; |
1537 | my $rbuf = \$self->{rbuf}; |
1408 | |
1538 | |
1409 | sub { |
1539 | sub { |
… | |
… | |
1529 | my ($self) = @_; |
1659 | my ($self) = @_; |
1530 | |
1660 | |
1531 | unless ($self->{_rw} || $self->{_eof}) { |
1661 | unless ($self->{_rw} || $self->{_eof}) { |
1532 | Scalar::Util::weaken $self; |
1662 | Scalar::Util::weaken $self; |
1533 | |
1663 | |
1534 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1664 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1535 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1665 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1536 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1666 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1537 | |
1667 | |
1538 | if ($len > 0) { |
1668 | if ($len > 0) { |
1539 | $self->{_activity} = AnyEvent->now; |
1669 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1540 | |
1670 | |
1541 | if ($self->{tls}) { |
1671 | if ($self->{tls}) { |
1542 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1672 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1543 | |
1673 | |
1544 | &_dotls ($self); |
1674 | &_dotls ($self); |
… | |
… | |
1552 | $self->_drain_rbuf; |
1682 | $self->_drain_rbuf; |
1553 | |
1683 | |
1554 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1684 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1555 | return $self->_error ($!, 1); |
1685 | return $self->_error ($!, 1); |
1556 | } |
1686 | } |
1557 | }); |
1687 | }; |
1558 | } |
1688 | } |
1559 | } |
1689 | } |
1560 | |
1690 | |
1561 | our $ERROR_SYSCALL; |
1691 | our $ERROR_SYSCALL; |
1562 | our $ERROR_WANT_READ; |
1692 | our $ERROR_WANT_READ; |
… | |
… | |
1683 | require Net::SSLeay; |
1813 | require Net::SSLeay; |
1684 | |
1814 | |
1685 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1815 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1686 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1816 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1687 | |
1817 | |
1688 | $tls = $self->{tls}; |
1818 | $tls = delete $self->{tls}; |
1689 | $ctx = $self->{tls_ctx}; |
1819 | $ctx = $self->{tls_ctx}; |
1690 | |
1820 | |
1691 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1821 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1692 | |
1822 | |
1693 | if ("HASH" eq ref $ctx) { |
1823 | if ("HASH" eq ref $ctx) { |
… | |
… | |
1722 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1852 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1723 | |
1853 | |
1724 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1854 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1725 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1855 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1726 | |
1856 | |
|
|
1857 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
|
|
1858 | |
1727 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1859 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1728 | |
1860 | |
1729 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1861 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1730 | if $self->{on_starttls}; |
1862 | if $self->{on_starttls}; |
1731 | |
1863 | |
… | |
… | |
1760 | my ($self) = @_; |
1892 | my ($self) = @_; |
1761 | |
1893 | |
1762 | return unless $self->{tls}; |
1894 | return unless $self->{tls}; |
1763 | |
1895 | |
1764 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1896 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1765 | if ref $self->{tls}; |
1897 | if $self->{tls} > 0; |
1766 | |
1898 | |
1767 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1899 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1768 | } |
1900 | } |
1769 | |
1901 | |
1770 | sub DESTROY { |
1902 | sub DESTROY { |
… | |
… | |
1778 | my $fh = delete $self->{fh}; |
1910 | my $fh = delete $self->{fh}; |
1779 | my $wbuf = delete $self->{wbuf}; |
1911 | my $wbuf = delete $self->{wbuf}; |
1780 | |
1912 | |
1781 | my @linger; |
1913 | my @linger; |
1782 | |
1914 | |
1783 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1915 | push @linger, AE::io $fh, 1, sub { |
1784 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1916 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1785 | |
1917 | |
1786 | if ($len > 0) { |
1918 | if ($len > 0) { |
1787 | substr $wbuf, 0, $len, ""; |
1919 | substr $wbuf, 0, $len, ""; |
1788 | } else { |
1920 | } else { |
1789 | @linger = (); # end |
1921 | @linger = (); # end |
1790 | } |
1922 | } |
1791 | }); |
1923 | }; |
1792 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1924 | push @linger, AE::timer $linger, 0, sub { |
1793 | @linger = (); |
1925 | @linger = (); |
1794 | }); |
1926 | }; |
1795 | } |
1927 | } |
1796 | } |
1928 | } |
1797 | |
1929 | |
1798 | =item $handle->destroy |
1930 | =item $handle->destroy |
1799 | |
1931 | |
1800 | Shuts down the handle object as much as possible - this call ensures that |
1932 | Shuts down the handle object as much as possible - this call ensures that |
1801 | no further callbacks will be invoked and as many resources as possible |
1933 | no further callbacks will be invoked and as many resources as possible |
1802 | will be freed. You must not call any methods on the object afterwards. |
1934 | will be freed. Any method you will call on the handle object after |
|
|
1935 | destroying it in this way will be silently ignored (and it will return the |
|
|
1936 | empty list). |
1803 | |
1937 | |
1804 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1938 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1805 | object and it will simply shut down. This works in fatal error and EOF |
1939 | object and it will simply shut down. This works in fatal error and EOF |
1806 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1940 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1807 | callback, so when you want to destroy the AnyEvent::Handle object from |
1941 | callback, so when you want to destroy the AnyEvent::Handle object from |
… | |
… | |
1821 | sub destroy { |
1955 | sub destroy { |
1822 | my ($self) = @_; |
1956 | my ($self) = @_; |
1823 | |
1957 | |
1824 | $self->DESTROY; |
1958 | $self->DESTROY; |
1825 | %$self = (); |
1959 | %$self = (); |
|
|
1960 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
1961 | } |
|
|
1962 | |
|
|
1963 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
|
|
1964 | #nop |
1826 | } |
1965 | } |
1827 | |
1966 | |
1828 | =item AnyEvent::Handle::TLS_CTX |
1967 | =item AnyEvent::Handle::TLS_CTX |
1829 | |
1968 | |
1830 | This function creates and returns the AnyEvent::TLS object used by default |
1969 | This function creates and returns the AnyEvent::TLS object used by default |