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.901; |
|
|
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 (the default |
|
|
312 | is enabled) gives you the most portable way of getting urgent data, by |
|
|
313 | putting it into the stream. |
|
|
314 | |
|
|
315 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
|
|
316 | security implications, AnyEvent::Handle sets this flag automatically |
|
|
317 | unless explicitly specified. |
|
|
318 | |
278 | =item read_size => <bytes> |
319 | =item read_size => <bytes> |
279 | |
320 | |
280 | The default read block size (the amount of bytes this module will |
321 | The default read block size (the amount of bytes this module will |
281 | try to read during each loop iteration, which affects memory |
322 | try to read during each loop iteration, which affects memory |
282 | requirements). Default: C<8192>. |
323 | requirements). Default: C<8192>. |
… | |
… | |
438 | delete $self->{_skip_drain_rbuf}; |
479 | delete $self->{_skip_drain_rbuf}; |
439 | $self->_start; |
480 | $self->_start; |
440 | |
481 | |
441 | $self->{on_connect} |
482 | $self->{on_connect} |
442 | and $self->{on_connect}($self, $host, $port, sub { |
483 | 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)}; |
484 | delete @$self{qw(fh _tw _rtw _wtw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
444 | $self->{_skip_drain_rbuf} = 1; |
485 | $self->{_skip_drain_rbuf} = 1; |
445 | &$retry; |
486 | &$retry; |
446 | }); |
487 | }); |
447 | |
488 | |
448 | } else { |
489 | } else { |
… | |
… | |
474 | sub _start { |
515 | sub _start { |
475 | my ($self) = @_; |
516 | my ($self) = @_; |
476 | |
517 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
518 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
519 | |
|
|
520 | $self->{_activity} = |
|
|
521 | $self->{_ractivity} = |
479 | $self->{_activity} = AnyEvent->now; |
522 | $self->{_wactivity} = AE::now; |
480 | $self->_timeout; |
|
|
481 | |
523 | |
|
|
524 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
525 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
|
|
526 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
|
|
527 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
528 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
|
|
529 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
483 | |
530 | |
|
|
531 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
|
|
532 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
533 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
485 | if $self->{tls}; |
534 | if $self->{tls}; |
486 | |
535 | |
487 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
536 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
488 | |
537 | |
489 | $self->start_read |
538 | $self->start_read |
490 | if $self->{on_read} || @{ $self->{_queue} }; |
539 | if $self->{on_read} || @{ $self->{_queue} }; |
491 | |
540 | |
492 | $self->_drain_wbuf; |
541 | $self->_drain_wbuf; |
493 | } |
542 | } |
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 | |
543 | |
504 | sub _error { |
544 | sub _error { |
505 | my ($self, $errno, $fatal, $message) = @_; |
545 | my ($self, $errno, $fatal, $message) = @_; |
506 | |
546 | |
507 | $! = $errno; |
547 | $! = $errno; |
… | |
… | |
544 | $_[0]{on_eof} = $_[1]; |
584 | $_[0]{on_eof} = $_[1]; |
545 | } |
585 | } |
546 | |
586 | |
547 | =item $handle->on_timeout ($cb) |
587 | =item $handle->on_timeout ($cb) |
548 | |
588 | |
549 | Replace the current C<on_timeout> callback, or disables the callback (but |
589 | =item $handle->on_rtimeout ($cb) |
550 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
551 | argument and method. |
|
|
552 | |
590 | |
553 | =cut |
591 | =item $handle->on_wtimeout ($cb) |
554 | |
592 | |
555 | sub on_timeout { |
593 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
556 | $_[0]{on_timeout} = $_[1]; |
594 | callback, or disables the callback (but not the timeout) if C<$cb> = |
557 | } |
595 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
596 | |
|
|
597 | =cut |
|
|
598 | |
|
|
599 | # see below |
558 | |
600 | |
559 | =item $handle->autocork ($boolean) |
601 | =item $handle->autocork ($boolean) |
560 | |
602 | |
561 | Enables or disables the current autocork behaviour (see C<autocork> |
603 | Enables or disables the current autocork behaviour (see C<autocork> |
562 | constructor argument). Changes will only take effect on the next write. |
604 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
577 | sub no_delay { |
619 | sub no_delay { |
578 | $_[0]{no_delay} = $_[1]; |
620 | $_[0]{no_delay} = $_[1]; |
579 | |
621 | |
580 | eval { |
622 | eval { |
581 | local $SIG{__DIE__}; |
623 | local $SIG{__DIE__}; |
582 | setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] |
624 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
583 | if $_[0]{fh}; |
625 | if $_[0]{fh}; |
584 | }; |
626 | }; |
585 | } |
627 | } |
586 | |
628 | |
|
|
629 | =item $handle->keepalive ($boolean) |
|
|
630 | |
|
|
631 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
632 | the same name for details). |
|
|
633 | |
|
|
634 | =cut |
|
|
635 | |
|
|
636 | sub keepalive { |
|
|
637 | $_[0]{keepalive} = $_[1]; |
|
|
638 | |
|
|
639 | eval { |
|
|
640 | local $SIG{__DIE__}; |
|
|
641 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
642 | if $_[0]{fh}; |
|
|
643 | }; |
|
|
644 | } |
|
|
645 | |
|
|
646 | =item $handle->oobinline ($boolean) |
|
|
647 | |
|
|
648 | Enables or disables the C<oobinline> setting (see constructor argument of |
|
|
649 | the same name for details). |
|
|
650 | |
|
|
651 | =cut |
|
|
652 | |
|
|
653 | sub oobinline { |
|
|
654 | $_[0]{oobinline} = $_[1]; |
|
|
655 | |
|
|
656 | eval { |
|
|
657 | local $SIG{__DIE__}; |
|
|
658 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
|
|
659 | if $_[0]{fh}; |
|
|
660 | }; |
|
|
661 | } |
|
|
662 | |
|
|
663 | =item $handle->keepalive ($boolean) |
|
|
664 | |
|
|
665 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
666 | the same name for details). |
|
|
667 | |
|
|
668 | =cut |
|
|
669 | |
|
|
670 | sub keepalive { |
|
|
671 | $_[0]{keepalive} = $_[1]; |
|
|
672 | |
|
|
673 | eval { |
|
|
674 | local $SIG{__DIE__}; |
|
|
675 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
676 | if $_[0]{fh}; |
|
|
677 | }; |
|
|
678 | } |
|
|
679 | |
587 | =item $handle->on_starttls ($cb) |
680 | =item $handle->on_starttls ($cb) |
588 | |
681 | |
589 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
682 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
590 | |
683 | |
591 | =cut |
684 | =cut |
… | |
… | |
616 | |
709 | |
617 | ############################################################################# |
710 | ############################################################################# |
618 | |
711 | |
619 | =item $handle->timeout ($seconds) |
712 | =item $handle->timeout ($seconds) |
620 | |
713 | |
|
|
714 | =item $handle->rtimeout ($seconds) |
|
|
715 | |
|
|
716 | =item $handle->wtimeout ($seconds) |
|
|
717 | |
621 | Configures (or disables) the inactivity timeout. |
718 | Configures (or disables) the inactivity timeout. |
622 | |
719 | |
623 | =cut |
720 | =item $handle->timeout_reset |
624 | |
721 | |
625 | sub timeout { |
722 | =item $handle->rtimeout_reset |
|
|
723 | |
|
|
724 | =item $handle->wtimeout_reset |
|
|
725 | |
|
|
726 | Reset the activity timeout, as if data was received or sent. |
|
|
727 | |
|
|
728 | These methods are cheap to call. |
|
|
729 | |
|
|
730 | =cut |
|
|
731 | |
|
|
732 | for my $dir ("", "r", "w") { |
|
|
733 | my $timeout = "${dir}timeout"; |
|
|
734 | my $tw = "_${dir}tw"; |
|
|
735 | my $on_timeout = "on_${dir}timeout"; |
|
|
736 | my $activity = "_${dir}activity"; |
|
|
737 | my $cb; |
|
|
738 | |
|
|
739 | *$on_timeout = sub { |
|
|
740 | $_[0]{$on_timeout} = $_[1]; |
|
|
741 | }; |
|
|
742 | |
|
|
743 | *$timeout = sub { |
626 | my ($self, $timeout) = @_; |
744 | my ($self, $new_value) = @_; |
627 | |
745 | |
628 | $self->{timeout} = $timeout; |
746 | $self->{$timeout} = $new_value; |
629 | $self->_timeout; |
747 | delete $self->{$tw}; &$cb; |
630 | } |
748 | }; |
631 | |
749 | |
|
|
750 | *{"${dir}timeout_reset"} = sub { |
|
|
751 | $_[0]{$activity} = AE::now; |
|
|
752 | }; |
|
|
753 | |
|
|
754 | # main workhorse: |
632 | # reset the timeout watcher, as neccessary |
755 | # reset the timeout watcher, as neccessary |
633 | # also check for time-outs |
756 | # also check for time-outs |
634 | sub _timeout { |
757 | $cb = sub { |
635 | my ($self) = @_; |
758 | my ($self) = @_; |
636 | |
759 | |
637 | if ($self->{timeout} && $self->{fh}) { |
760 | if ($self->{$timeout} && $self->{fh}) { |
638 | my $NOW = AnyEvent->now; |
761 | my $NOW = AE::now; |
639 | |
762 | |
640 | # when would the timeout trigger? |
763 | # when would the timeout trigger? |
641 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
764 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
642 | |
765 | |
643 | # now or in the past already? |
766 | # now or in the past already? |
644 | if ($after <= 0) { |
767 | if ($after <= 0) { |
645 | $self->{_activity} = $NOW; |
768 | $self->{$activity} = $NOW; |
646 | |
769 | |
647 | if ($self->{on_timeout}) { |
770 | if ($self->{$on_timeout}) { |
648 | $self->{on_timeout}($self); |
771 | $self->{$on_timeout}($self); |
649 | } else { |
772 | } else { |
650 | $self->_error (Errno::ETIMEDOUT); |
773 | $self->_error (Errno::ETIMEDOUT); |
|
|
774 | } |
|
|
775 | |
|
|
776 | # callback could have changed timeout value, optimise |
|
|
777 | return unless $self->{$timeout}; |
|
|
778 | |
|
|
779 | # calculate new after |
|
|
780 | $after = $self->{$timeout}; |
651 | } |
781 | } |
652 | |
782 | |
653 | # callback could have changed timeout value, optimise |
783 | Scalar::Util::weaken $self; |
654 | return unless $self->{timeout}; |
784 | return unless $self; # ->error could have destroyed $self |
655 | |
785 | |
656 | # calculate new after |
786 | $self->{$tw} ||= AE::timer $after, 0, sub { |
657 | $after = $self->{timeout}; |
787 | delete $self->{$tw}; |
|
|
788 | $cb->($self); |
|
|
789 | }; |
|
|
790 | } else { |
|
|
791 | delete $self->{$tw}; |
658 | } |
792 | } |
659 | |
|
|
660 | Scalar::Util::weaken $self; |
|
|
661 | return unless $self; # ->error could have destroyed $self |
|
|
662 | |
|
|
663 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
|
|
664 | delete $self->{_tw}; |
|
|
665 | $self->_timeout; |
|
|
666 | }); |
|
|
667 | } else { |
|
|
668 | delete $self->{_tw}; |
|
|
669 | } |
793 | } |
670 | } |
794 | } |
671 | |
795 | |
672 | ############################################################################# |
796 | ############################################################################# |
673 | |
797 | |
… | |
… | |
721 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
845 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
722 | |
846 | |
723 | if (defined $len) { |
847 | if (defined $len) { |
724 | substr $self->{wbuf}, 0, $len, ""; |
848 | substr $self->{wbuf}, 0, $len, ""; |
725 | |
849 | |
726 | $self->{_activity} = AnyEvent->now; |
850 | $self->{_activity} = $self->{_wactivity} = AE::now; |
727 | |
851 | |
728 | $self->{on_drain}($self) |
852 | $self->{on_drain}($self) |
729 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
853 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
730 | && $self->{on_drain}; |
854 | && $self->{on_drain}; |
731 | |
855 | |
… | |
… | |
737 | |
861 | |
738 | # try to write data immediately |
862 | # try to write data immediately |
739 | $cb->() unless $self->{autocork}; |
863 | $cb->() unless $self->{autocork}; |
740 | |
864 | |
741 | # if still data left in wbuf, we need to poll |
865 | # if still data left in wbuf, we need to poll |
742 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
866 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
743 | if length $self->{wbuf}; |
867 | if length $self->{wbuf}; |
744 | }; |
868 | }; |
745 | } |
869 | } |
746 | |
870 | |
747 | our %WH; |
871 | our %WH; |
… | |
… | |
837 | Other languages could read single lines terminated by a newline and pass |
961 | Other languages could read single lines terminated by a newline and pass |
838 | this line into their JSON decoder of choice. |
962 | this line into their JSON decoder of choice. |
839 | |
963 | |
840 | =cut |
964 | =cut |
841 | |
965 | |
|
|
966 | sub json_coder() { |
|
|
967 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
968 | || do { require JSON; JSON->new->utf8 } |
|
|
969 | } |
|
|
970 | |
842 | register_write_type json => sub { |
971 | register_write_type json => sub { |
843 | my ($self, $ref) = @_; |
972 | my ($self, $ref) = @_; |
844 | |
973 | |
845 | require JSON; |
974 | my $json = $self->{json} ||= json_coder; |
846 | |
975 | |
847 | $self->{json} ? $self->{json}->encode ($ref) |
976 | $json->encode ($ref) |
848 | : JSON::encode_json ($ref) |
|
|
849 | }; |
977 | }; |
850 | |
978 | |
851 | =item storable => $reference |
979 | =item storable => $reference |
852 | |
980 | |
853 | Freezes the given reference using L<Storable> and writes it to the |
981 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
1146 | |
1274 | |
1147 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1275 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1148 | ->($self, $cb, @_); |
1276 | ->($self, $cb, @_); |
1149 | } |
1277 | } |
1150 | |
1278 | |
1151 | |
|
|
1152 | unshift @{ $self->{_queue} }, $cb; |
1279 | unshift @{ $self->{_queue} }, $cb; |
1153 | $self->_drain_rbuf; |
1280 | $self->_drain_rbuf; |
1154 | } |
1281 | } |
1155 | |
1282 | |
1156 | =item $handle->push_read (type => @args, $cb) |
1283 | =item $handle->push_read (type => @args, $cb) |
… | |
… | |
1407 | =cut |
1534 | =cut |
1408 | |
1535 | |
1409 | register_read_type json => sub { |
1536 | register_read_type json => sub { |
1410 | my ($self, $cb) = @_; |
1537 | my ($self, $cb) = @_; |
1411 | |
1538 | |
1412 | my $json = $self->{json} ||= |
1539 | my $json = $self->{json} ||= json_coder; |
1413 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1414 | || do { require JSON; JSON->new->utf8 }; |
|
|
1415 | |
1540 | |
1416 | my $data; |
1541 | my $data; |
1417 | my $rbuf = \$self->{rbuf}; |
1542 | my $rbuf = \$self->{rbuf}; |
1418 | |
1543 | |
1419 | sub { |
1544 | sub { |
… | |
… | |
1539 | my ($self) = @_; |
1664 | my ($self) = @_; |
1540 | |
1665 | |
1541 | unless ($self->{_rw} || $self->{_eof}) { |
1666 | unless ($self->{_rw} || $self->{_eof}) { |
1542 | Scalar::Util::weaken $self; |
1667 | Scalar::Util::weaken $self; |
1543 | |
1668 | |
1544 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1669 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1545 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1670 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1546 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1671 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1547 | |
1672 | |
1548 | if ($len > 0) { |
1673 | if ($len > 0) { |
1549 | $self->{_activity} = AnyEvent->now; |
1674 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1550 | |
1675 | |
1551 | if ($self->{tls}) { |
1676 | if ($self->{tls}) { |
1552 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1677 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1553 | |
1678 | |
1554 | &_dotls ($self); |
1679 | &_dotls ($self); |
… | |
… | |
1562 | $self->_drain_rbuf; |
1687 | $self->_drain_rbuf; |
1563 | |
1688 | |
1564 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1689 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1565 | return $self->_error ($!, 1); |
1690 | return $self->_error ($!, 1); |
1566 | } |
1691 | } |
1567 | }); |
1692 | }; |
1568 | } |
1693 | } |
1569 | } |
1694 | } |
1570 | |
1695 | |
1571 | our $ERROR_SYSCALL; |
1696 | our $ERROR_SYSCALL; |
1572 | our $ERROR_WANT_READ; |
1697 | our $ERROR_WANT_READ; |
… | |
… | |
1693 | require Net::SSLeay; |
1818 | require Net::SSLeay; |
1694 | |
1819 | |
1695 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1820 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1696 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1821 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1697 | |
1822 | |
1698 | $tls = $self->{tls}; |
1823 | $tls = delete $self->{tls}; |
1699 | $ctx = $self->{tls_ctx}; |
1824 | $ctx = $self->{tls_ctx}; |
1700 | |
1825 | |
1701 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1826 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1702 | |
1827 | |
1703 | if ("HASH" eq ref $ctx) { |
1828 | if ("HASH" eq ref $ctx) { |
… | |
… | |
1790 | my $fh = delete $self->{fh}; |
1915 | my $fh = delete $self->{fh}; |
1791 | my $wbuf = delete $self->{wbuf}; |
1916 | my $wbuf = delete $self->{wbuf}; |
1792 | |
1917 | |
1793 | my @linger; |
1918 | my @linger; |
1794 | |
1919 | |
1795 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1920 | push @linger, AE::io $fh, 1, sub { |
1796 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1921 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1797 | |
1922 | |
1798 | if ($len > 0) { |
1923 | if ($len > 0) { |
1799 | substr $wbuf, 0, $len, ""; |
1924 | substr $wbuf, 0, $len, ""; |
1800 | } else { |
1925 | } else { |
1801 | @linger = (); # end |
1926 | @linger = (); # end |
1802 | } |
1927 | } |
1803 | }); |
1928 | }; |
1804 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1929 | push @linger, AE::timer $linger, 0, sub { |
1805 | @linger = (); |
1930 | @linger = (); |
1806 | }); |
1931 | }; |
1807 | } |
1932 | } |
1808 | } |
1933 | } |
1809 | |
1934 | |
1810 | =item $handle->destroy |
1935 | =item $handle->destroy |
1811 | |
1936 | |