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.86; |
|
|
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 |
… | |
… | |
438 | delete $self->{_skip_drain_rbuf}; |
450 | delete $self->{_skip_drain_rbuf}; |
439 | $self->_start; |
451 | $self->_start; |
440 | |
452 | |
441 | $self->{on_connect} |
453 | $self->{on_connect} |
442 | and $self->{on_connect}($self, $host, $port, sub { |
454 | 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)}; |
455 | delete @$self{qw(fh _tw _rtw _wtw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
444 | $self->{_skip_drain_rbuf} = 1; |
456 | $self->{_skip_drain_rbuf} = 1; |
445 | &$retry; |
457 | &$retry; |
446 | }); |
458 | }); |
447 | |
459 | |
448 | } else { |
460 | } else { |
… | |
… | |
474 | sub _start { |
486 | sub _start { |
475 | my ($self) = @_; |
487 | my ($self) = @_; |
476 | |
488 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
489 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
490 | |
|
|
491 | $self->{_activity} = |
|
|
492 | $self->{_ractivity} = |
479 | $self->{_activity} = AnyEvent->now; |
493 | $self->{_wactivity} = AE::now; |
480 | $self->_timeout; |
494 | |
|
|
495 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
496 | $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; |
|
|
497 | $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; |
481 | |
498 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
499 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
483 | |
500 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
501 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
485 | if $self->{tls}; |
502 | if $self->{tls}; |
… | |
… | |
489 | $self->start_read |
506 | $self->start_read |
490 | if $self->{on_read} || @{ $self->{_queue} }; |
507 | if $self->{on_read} || @{ $self->{_queue} }; |
491 | |
508 | |
492 | $self->_drain_wbuf; |
509 | $self->_drain_wbuf; |
493 | } |
510 | } |
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 | |
511 | |
504 | sub _error { |
512 | sub _error { |
505 | my ($self, $errno, $fatal, $message) = @_; |
513 | my ($self, $errno, $fatal, $message) = @_; |
506 | |
514 | |
507 | $! = $errno; |
515 | $! = $errno; |
… | |
… | |
544 | $_[0]{on_eof} = $_[1]; |
552 | $_[0]{on_eof} = $_[1]; |
545 | } |
553 | } |
546 | |
554 | |
547 | =item $handle->on_timeout ($cb) |
555 | =item $handle->on_timeout ($cb) |
548 | |
556 | |
549 | Replace the current C<on_timeout> callback, or disables the callback (but |
557 | =item $handle->on_rtimeout ($cb) |
550 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
551 | argument and method. |
|
|
552 | |
558 | |
553 | =cut |
559 | =item $handle->on_wtimeout ($cb) |
554 | |
560 | |
555 | sub on_timeout { |
561 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
556 | $_[0]{on_timeout} = $_[1]; |
562 | callback, or disables the callback (but not the timeout) if C<$cb> = |
557 | } |
563 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
564 | |
|
|
565 | =cut |
|
|
566 | |
|
|
567 | # see below |
558 | |
568 | |
559 | =item $handle->autocork ($boolean) |
569 | =item $handle->autocork ($boolean) |
560 | |
570 | |
561 | Enables or disables the current autocork behaviour (see C<autocork> |
571 | Enables or disables the current autocork behaviour (see C<autocork> |
562 | constructor argument). Changes will only take effect on the next write. |
572 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
602 | |
612 | |
603 | sub on_starttls { |
613 | sub on_starttls { |
604 | $_[0]{on_stoptls} = $_[1]; |
614 | $_[0]{on_stoptls} = $_[1]; |
605 | } |
615 | } |
606 | |
616 | |
|
|
617 | =item $handle->rbuf_max ($max_octets) |
|
|
618 | |
|
|
619 | Configures the C<rbuf_max> setting (C<undef> disables it). |
|
|
620 | |
|
|
621 | =cut |
|
|
622 | |
|
|
623 | sub rbuf_max { |
|
|
624 | $_[0]{rbuf_max} = $_[1]; |
|
|
625 | } |
|
|
626 | |
607 | ############################################################################# |
627 | ############################################################################# |
608 | |
628 | |
609 | =item $handle->timeout ($seconds) |
629 | =item $handle->timeout ($seconds) |
610 | |
630 | |
|
|
631 | =item $handle->rtimeout ($seconds) |
|
|
632 | |
|
|
633 | =item $handle->wtimeout ($seconds) |
|
|
634 | |
611 | Configures (or disables) the inactivity timeout. |
635 | Configures (or disables) the inactivity timeout. |
612 | |
636 | |
613 | =cut |
637 | =item $handle->timeout_reset |
614 | |
638 | |
615 | sub timeout { |
639 | =item $handle->rtimeout_reset |
|
|
640 | |
|
|
641 | =item $handle->wtimeout_reset |
|
|
642 | |
|
|
643 | Reset the activity timeout, as if data was received or sent. |
|
|
644 | |
|
|
645 | These methods are cheap to call. |
|
|
646 | |
|
|
647 | =cut |
|
|
648 | |
|
|
649 | for my $dir ("", "r", "w") { |
|
|
650 | my $timeout = "${dir}timeout"; |
|
|
651 | my $tw = "_${dir}tw"; |
|
|
652 | my $on_timeout = "on_${dir}timeout"; |
|
|
653 | my $activity = "_${dir}activity"; |
|
|
654 | my $cb; |
|
|
655 | |
|
|
656 | *$on_timeout = sub { |
|
|
657 | $_[0]{$on_timeout} = $_[1]; |
|
|
658 | }; |
|
|
659 | |
|
|
660 | *$timeout = sub { |
616 | my ($self, $timeout) = @_; |
661 | my ($self, $new_value) = @_; |
617 | |
662 | |
618 | $self->{timeout} = $timeout; |
663 | $self->{$timeout} = $new_value; |
619 | $self->_timeout; |
664 | delete $self->{$tw}; &$cb; |
620 | } |
665 | }; |
621 | |
666 | |
|
|
667 | *{"${dir}timeout_reset"} = sub { |
|
|
668 | $_[0]{$activity} = AE::now; |
|
|
669 | }; |
|
|
670 | |
|
|
671 | # main workhorse: |
622 | # reset the timeout watcher, as neccessary |
672 | # reset the timeout watcher, as neccessary |
623 | # also check for time-outs |
673 | # also check for time-outs |
624 | sub _timeout { |
674 | $cb = sub { |
625 | my ($self) = @_; |
675 | my ($self) = @_; |
626 | |
676 | |
627 | if ($self->{timeout} && $self->{fh}) { |
677 | if ($self->{$timeout} && $self->{fh}) { |
628 | my $NOW = AnyEvent->now; |
678 | my $NOW = AE::now; |
629 | |
679 | |
630 | # when would the timeout trigger? |
680 | # when would the timeout trigger? |
631 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
681 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
632 | |
682 | |
633 | # now or in the past already? |
683 | # now or in the past already? |
634 | if ($after <= 0) { |
684 | if ($after <= 0) { |
635 | $self->{_activity} = $NOW; |
685 | $self->{$activity} = $NOW; |
636 | |
686 | |
637 | if ($self->{on_timeout}) { |
687 | if ($self->{$on_timeout}) { |
638 | $self->{on_timeout}($self); |
688 | $self->{$on_timeout}($self); |
639 | } else { |
689 | } else { |
640 | $self->_error (Errno::ETIMEDOUT); |
690 | $self->_error (Errno::ETIMEDOUT); |
|
|
691 | } |
|
|
692 | |
|
|
693 | # callback could have changed timeout value, optimise |
|
|
694 | return unless $self->{$timeout}; |
|
|
695 | |
|
|
696 | # calculate new after |
|
|
697 | $after = $self->{$timeout}; |
641 | } |
698 | } |
642 | |
699 | |
643 | # callback could have changed timeout value, optimise |
700 | Scalar::Util::weaken $self; |
644 | return unless $self->{timeout}; |
701 | return unless $self; # ->error could have destroyed $self |
645 | |
702 | |
646 | # calculate new after |
703 | $self->{$tw} ||= AE::timer $after, 0, sub { |
647 | $after = $self->{timeout}; |
704 | delete $self->{$tw}; |
|
|
705 | $cb->($self); |
|
|
706 | }; |
|
|
707 | } else { |
|
|
708 | delete $self->{$tw}; |
648 | } |
709 | } |
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 | } |
710 | } |
660 | } |
711 | } |
661 | |
712 | |
662 | ############################################################################# |
713 | ############################################################################# |
663 | |
714 | |
… | |
… | |
711 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
762 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
712 | |
763 | |
713 | if (defined $len) { |
764 | if (defined $len) { |
714 | substr $self->{wbuf}, 0, $len, ""; |
765 | substr $self->{wbuf}, 0, $len, ""; |
715 | |
766 | |
716 | $self->{_activity} = AnyEvent->now; |
767 | $self->{_activity} = $self->{_wactivity} = AE::now; |
717 | |
768 | |
718 | $self->{on_drain}($self) |
769 | $self->{on_drain}($self) |
719 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
770 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
720 | && $self->{on_drain}; |
771 | && $self->{on_drain}; |
721 | |
772 | |
… | |
… | |
727 | |
778 | |
728 | # try to write data immediately |
779 | # try to write data immediately |
729 | $cb->() unless $self->{autocork}; |
780 | $cb->() unless $self->{autocork}; |
730 | |
781 | |
731 | # if still data left in wbuf, we need to poll |
782 | # if still data left in wbuf, we need to poll |
732 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
783 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
733 | if length $self->{wbuf}; |
784 | if length $self->{wbuf}; |
734 | }; |
785 | }; |
735 | } |
786 | } |
736 | |
787 | |
737 | our %WH; |
788 | our %WH; |
… | |
… | |
827 | Other languages could read single lines terminated by a newline and pass |
878 | Other languages could read single lines terminated by a newline and pass |
828 | this line into their JSON decoder of choice. |
879 | this line into their JSON decoder of choice. |
829 | |
880 | |
830 | =cut |
881 | =cut |
831 | |
882 | |
|
|
883 | sub json_coder() { |
|
|
884 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
885 | || do { require JSON; JSON->new->utf8 } |
|
|
886 | } |
|
|
887 | |
832 | register_write_type json => sub { |
888 | register_write_type json => sub { |
833 | my ($self, $ref) = @_; |
889 | my ($self, $ref) = @_; |
834 | |
890 | |
835 | require JSON; |
891 | my $json = $self->{json} ||= json_coder; |
836 | |
892 | |
837 | $self->{json} ? $self->{json}->encode ($ref) |
893 | $json->encode ($ref) |
838 | : JSON::encode_json ($ref) |
|
|
839 | }; |
894 | }; |
840 | |
895 | |
841 | =item storable => $reference |
896 | =item storable => $reference |
842 | |
897 | |
843 | Freezes the given reference using L<Storable> and writes it to the |
898 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
981 | |
1036 | |
982 | sub _drain_rbuf { |
1037 | sub _drain_rbuf { |
983 | my ($self) = @_; |
1038 | my ($self) = @_; |
984 | |
1039 | |
985 | # avoid recursion |
1040 | # avoid recursion |
986 | return if exists $self->{_skip_drain_rbuf}; |
1041 | return if $self->{_skip_drain_rbuf}; |
987 | local $self->{_skip_drain_rbuf} = 1; |
1042 | 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 | |
1043 | |
996 | while () { |
1044 | while () { |
997 | # we need to use a separate tls read buffer, as we must not receive data while |
1045 | # 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. |
1046 | # we are draining the buffer, and this can only happen with TLS. |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} if exists $self->{_tls_rbuf}; |
1047 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
|
|
1048 | if exists $self->{_tls_rbuf}; |
1000 | |
1049 | |
1001 | my $len = length $self->{rbuf}; |
1050 | my $len = length $self->{rbuf}; |
1002 | |
1051 | |
1003 | if (my $cb = shift @{ $self->{_queue} }) { |
1052 | if (my $cb = shift @{ $self->{_queue} }) { |
1004 | unless ($cb->($self)) { |
1053 | unless ($cb->($self)) { |
1005 | if ($self->{_eof}) { |
1054 | # no progress can be made |
1006 | # no progress can be made (not enough data and no data forthcoming) |
1055 | # (not enough data and no data forthcoming) |
1007 | $self->_error (Errno::EPIPE, 1), return; |
1056 | $self->_error (Errno::EPIPE, 1), return |
1008 | } |
1057 | if $self->{_eof}; |
1009 | |
1058 | |
1010 | unshift @{ $self->{_queue} }, $cb; |
1059 | unshift @{ $self->{_queue} }, $cb; |
1011 | last; |
1060 | last; |
1012 | } |
1061 | } |
1013 | } elsif ($self->{on_read}) { |
1062 | } elsif ($self->{on_read}) { |
… | |
… | |
1033 | last; |
1082 | last; |
1034 | } |
1083 | } |
1035 | } |
1084 | } |
1036 | |
1085 | |
1037 | if ($self->{_eof}) { |
1086 | if ($self->{_eof}) { |
1038 | if ($self->{on_eof}) { |
1087 | $self->{on_eof} |
1039 | $self->{on_eof}($self) |
1088 | ? $self->{on_eof}($self) |
1040 | } else { |
|
|
1041 | $self->_error (0, 1, "Unexpected end-of-file"); |
1089 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1042 | } |
1090 | |
|
|
1091 | return; |
|
|
1092 | } |
|
|
1093 | |
|
|
1094 | if ( |
|
|
1095 | defined $self->{rbuf_max} |
|
|
1096 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1097 | ) { |
|
|
1098 | $self->_error (Errno::ENOSPC, 1), return; |
1043 | } |
1099 | } |
1044 | |
1100 | |
1045 | # may need to restart read watcher |
1101 | # may need to restart read watcher |
1046 | unless ($self->{_rw}) { |
1102 | unless ($self->{_rw}) { |
1047 | $self->start_read |
1103 | $self->start_read |
… | |
… | |
1396 | =cut |
1452 | =cut |
1397 | |
1453 | |
1398 | register_read_type json => sub { |
1454 | register_read_type json => sub { |
1399 | my ($self, $cb) = @_; |
1455 | my ($self, $cb) = @_; |
1400 | |
1456 | |
1401 | my $json = $self->{json} ||= |
1457 | my $json = $self->{json} ||= json_coder; |
1402 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1403 | || do { require JSON; JSON->new->utf8 }; |
|
|
1404 | |
1458 | |
1405 | my $data; |
1459 | my $data; |
1406 | my $rbuf = \$self->{rbuf}; |
1460 | my $rbuf = \$self->{rbuf}; |
1407 | |
1461 | |
1408 | sub { |
1462 | sub { |
… | |
… | |
1528 | my ($self) = @_; |
1582 | my ($self) = @_; |
1529 | |
1583 | |
1530 | unless ($self->{_rw} || $self->{_eof}) { |
1584 | unless ($self->{_rw} || $self->{_eof}) { |
1531 | Scalar::Util::weaken $self; |
1585 | Scalar::Util::weaken $self; |
1532 | |
1586 | |
1533 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1587 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1534 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1588 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1535 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1589 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1536 | |
1590 | |
1537 | if ($len > 0) { |
1591 | if ($len > 0) { |
1538 | $self->{_activity} = AnyEvent->now; |
1592 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1539 | |
1593 | |
1540 | if ($self->{tls}) { |
1594 | if ($self->{tls}) { |
1541 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1595 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1542 | |
1596 | |
1543 | &_dotls ($self); |
1597 | &_dotls ($self); |
… | |
… | |
1551 | $self->_drain_rbuf; |
1605 | $self->_drain_rbuf; |
1552 | |
1606 | |
1553 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1607 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1554 | return $self->_error ($!, 1); |
1608 | return $self->_error ($!, 1); |
1555 | } |
1609 | } |
1556 | }); |
1610 | }; |
1557 | } |
1611 | } |
1558 | } |
1612 | } |
1559 | |
1613 | |
1560 | our $ERROR_SYSCALL; |
1614 | our $ERROR_SYSCALL; |
1561 | our $ERROR_WANT_READ; |
1615 | our $ERROR_WANT_READ; |
… | |
… | |
1682 | require Net::SSLeay; |
1736 | require Net::SSLeay; |
1683 | |
1737 | |
1684 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1738 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1685 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1739 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1686 | |
1740 | |
1687 | $tls = $self->{tls}; |
1741 | $tls = delete $self->{tls}; |
1688 | $ctx = $self->{tls_ctx}; |
1742 | $ctx = $self->{tls_ctx}; |
1689 | |
1743 | |
1690 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1744 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1691 | |
1745 | |
1692 | if ("HASH" eq ref $ctx) { |
1746 | if ("HASH" eq ref $ctx) { |
… | |
… | |
1721 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1775 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1722 | |
1776 | |
1723 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1777 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1724 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1778 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1725 | |
1779 | |
|
|
1780 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
|
|
1781 | |
1726 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1782 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1727 | |
1783 | |
1728 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1784 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1729 | if $self->{on_starttls}; |
1785 | if $self->{on_starttls}; |
1730 | |
1786 | |
… | |
… | |
1759 | my ($self) = @_; |
1815 | my ($self) = @_; |
1760 | |
1816 | |
1761 | return unless $self->{tls}; |
1817 | return unless $self->{tls}; |
1762 | |
1818 | |
1763 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1819 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1764 | if ref $self->{tls}; |
1820 | if $self->{tls} > 0; |
1765 | |
1821 | |
1766 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1822 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1767 | } |
1823 | } |
1768 | |
1824 | |
1769 | sub DESTROY { |
1825 | sub DESTROY { |
… | |
… | |
1777 | my $fh = delete $self->{fh}; |
1833 | my $fh = delete $self->{fh}; |
1778 | my $wbuf = delete $self->{wbuf}; |
1834 | my $wbuf = delete $self->{wbuf}; |
1779 | |
1835 | |
1780 | my @linger; |
1836 | my @linger; |
1781 | |
1837 | |
1782 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1838 | push @linger, AE::io $fh, 1, sub { |
1783 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1839 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1784 | |
1840 | |
1785 | if ($len > 0) { |
1841 | if ($len > 0) { |
1786 | substr $wbuf, 0, $len, ""; |
1842 | substr $wbuf, 0, $len, ""; |
1787 | } else { |
1843 | } else { |
1788 | @linger = (); # end |
1844 | @linger = (); # end |
1789 | } |
1845 | } |
1790 | }); |
1846 | }; |
1791 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1847 | push @linger, AE::timer $linger, 0, sub { |
1792 | @linger = (); |
1848 | @linger = (); |
1793 | }); |
1849 | }; |
1794 | } |
1850 | } |
1795 | } |
1851 | } |
1796 | |
1852 | |
1797 | =item $handle->destroy |
1853 | =item $handle->destroy |
1798 | |
1854 | |
1799 | Shuts down the handle object as much as possible - this call ensures that |
1855 | Shuts down the handle object as much as possible - this call ensures that |
1800 | no further callbacks will be invoked and as many resources as possible |
1856 | no further callbacks will be invoked and as many resources as possible |
1801 | will be freed. You must not call any methods on the object afterwards. |
1857 | will be freed. Any method you will call on the handle object after |
|
|
1858 | destroying it in this way will be silently ignored (and it will return the |
|
|
1859 | empty list). |
1802 | |
1860 | |
1803 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1861 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1804 | object and it will simply shut down. This works in fatal error and EOF |
1862 | object and it will simply shut down. This works in fatal error and EOF |
1805 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1863 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1806 | callback, so when you want to destroy the AnyEvent::Handle object from |
1864 | callback, so when you want to destroy the AnyEvent::Handle object from |
… | |
… | |
1820 | sub destroy { |
1878 | sub destroy { |
1821 | my ($self) = @_; |
1879 | my ($self) = @_; |
1822 | |
1880 | |
1823 | $self->DESTROY; |
1881 | $self->DESTROY; |
1824 | %$self = (); |
1882 | %$self = (); |
|
|
1883 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
1884 | } |
|
|
1885 | |
|
|
1886 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
|
|
1887 | #nop |
1825 | } |
1888 | } |
1826 | |
1889 | |
1827 | =item AnyEvent::Handle::TLS_CTX |
1890 | =item AnyEvent::Handle::TLS_CTX |
1828 | |
1891 | |
1829 | This function creates and returns the AnyEvent::TLS object used by default |
1892 | This function creates and returns the AnyEvent::TLS object used by default |