… | |
… | |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | =cut |
18 | |
18 | |
19 | our $VERSION = 4.1; |
19 | our $VERSION = 4.14; |
20 | |
20 | |
21 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
22 | |
22 | |
23 | use AnyEvent; |
23 | use AnyEvent; |
24 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
… | |
… | |
105 | C<croak>. |
105 | C<croak>. |
106 | |
106 | |
107 | =item on_read => $cb->($handle) |
107 | =item on_read => $cb->($handle) |
108 | |
108 | |
109 | This sets the default read callback, which is called when data arrives |
109 | This sets the default read callback, which is called when data arrives |
110 | and no read request is in the queue. |
110 | and no read request is in the queue (unlike read queue callbacks, this |
|
|
111 | callback will only be called when at least one octet of data is in the |
|
|
112 | read buffer). |
111 | |
113 | |
112 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
114 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
113 | method or access the C<$handle->{rbuf}> member directly. |
115 | method or access the C<$handle->{rbuf}> member directly. |
114 | |
116 | |
115 | When an EOF condition is detected then AnyEvent::Handle will first try to |
117 | When an EOF condition is detected then AnyEvent::Handle will first try to |
… | |
… | |
165 | |
167 | |
166 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
168 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
167 | buffer: If the write reaches this size or gets even samller it is |
169 | buffer: If the write reaches this size or gets even samller it is |
168 | considered empty. |
170 | considered empty. |
169 | |
171 | |
|
|
172 | =item linger => <seconds> |
|
|
173 | |
|
|
174 | If non-zero (default: C<3600>), then the destructor of the |
|
|
175 | AnyEvent::Handle object will check wether there is still outstanding write |
|
|
176 | data and will install a watcher that will write out this data. No errors |
|
|
177 | will be reported (this mostly matches how the operating system treats |
|
|
178 | outstanding data at socket close time). |
|
|
179 | |
|
|
180 | This will not work for partial TLS data that could not yet been |
|
|
181 | encoded. This data will be lost. |
|
|
182 | |
170 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
183 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
171 | |
184 | |
172 | When this parameter is given, it enables TLS (SSL) mode, that means it |
185 | When this parameter is given, it enables TLS (SSL) mode, that means it |
173 | will start making tls handshake and will transparently encrypt/decrypt |
186 | will start making tls handshake and will transparently encrypt/decrypt |
174 | data. |
187 | data. |
… | |
… | |
224 | if ($self->{tls}) { |
237 | if ($self->{tls}) { |
225 | require Net::SSLeay; |
238 | require Net::SSLeay; |
226 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
239 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
227 | } |
240 | } |
228 | |
241 | |
229 | # $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop |
|
|
230 | # $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop |
|
|
231 | # $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop |
|
|
232 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
|
|
233 | |
|
|
234 | $self->{_activity} = AnyEvent->now; |
242 | $self->{_activity} = AnyEvent->now; |
235 | $self->_timeout; |
243 | $self->_timeout; |
236 | |
244 | |
237 | $self->start_read; |
245 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
238 | |
246 | |
239 | $self |
247 | $self |
240 | } |
248 | } |
241 | |
249 | |
242 | sub _shutdown { |
250 | sub _shutdown { |
… | |
… | |
339 | $self->{on_timeout}($self); |
347 | $self->{on_timeout}($self); |
340 | } else { |
348 | } else { |
341 | $self->_error (&Errno::ETIMEDOUT); |
349 | $self->_error (&Errno::ETIMEDOUT); |
342 | } |
350 | } |
343 | |
351 | |
344 | # callbakx could have changed timeout value, optimise |
352 | # callback could have changed timeout value, optimise |
345 | return unless $self->{timeout}; |
353 | return unless $self->{timeout}; |
346 | |
354 | |
347 | # calculate new after |
355 | # calculate new after |
348 | $after = $self->{timeout}; |
356 | $after = $self->{timeout}; |
349 | } |
357 | } |
350 | |
358 | |
351 | Scalar::Util::weaken $self; |
359 | Scalar::Util::weaken $self; |
|
|
360 | return unless $self; # ->error could have destroyed $self |
352 | |
361 | |
353 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
362 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
354 | delete $self->{_tw}; |
363 | delete $self->{_tw}; |
355 | $self->_timeout; |
364 | $self->_timeout; |
356 | }); |
365 | }); |
… | |
… | |
479 | my ($self, $string) = @_; |
488 | my ($self, $string) = @_; |
480 | |
489 | |
481 | sprintf "%d:%s,", (length $string), $string |
490 | sprintf "%d:%s,", (length $string), $string |
482 | }; |
491 | }; |
483 | |
492 | |
|
|
493 | =item packstring => $format, $data |
|
|
494 | |
|
|
495 | An octet string prefixed with an encoded length. The encoding C<$format> |
|
|
496 | uses the same format as a Perl C<pack> format, but must specify a single |
|
|
497 | integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an |
|
|
498 | optional C<!>, C<< < >> or C<< > >> modifier). |
|
|
499 | |
|
|
500 | =cut |
|
|
501 | |
|
|
502 | register_write_type packstring => sub { |
|
|
503 | my ($self, $format, $string) = @_; |
|
|
504 | |
|
|
505 | pack "$format/a", $string |
|
|
506 | }; |
|
|
507 | |
484 | =item json => $array_or_hashref |
508 | =item json => $array_or_hashref |
485 | |
509 | |
486 | Encodes the given hash or array reference into a JSON object. Unless you |
510 | Encodes the given hash or array reference into a JSON object. Unless you |
487 | provide your own JSON object, this means it will be encoded to JSON text |
511 | provide your own JSON object, this means it will be encoded to JSON text |
488 | in UTF-8. |
512 | in UTF-8. |
… | |
… | |
520 | |
544 | |
521 | $self->{json} ? $self->{json}->encode ($ref) |
545 | $self->{json} ? $self->{json}->encode ($ref) |
522 | : JSON::encode_json ($ref) |
546 | : JSON::encode_json ($ref) |
523 | }; |
547 | }; |
524 | |
548 | |
|
|
549 | =item storable => $reference |
|
|
550 | |
|
|
551 | Freezes the given reference using L<Storable> and writes it to the |
|
|
552 | handle. Uses the C<nfreeze> format. |
|
|
553 | |
|
|
554 | =cut |
|
|
555 | |
|
|
556 | register_write_type storable => sub { |
|
|
557 | my ($self, $ref) = @_; |
|
|
558 | |
|
|
559 | require Storable; |
|
|
560 | |
|
|
561 | pack "w/a", Storable::nfreeze ($ref) |
|
|
562 | }; |
|
|
563 | |
|
|
564 | =back |
|
|
565 | |
525 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
566 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
526 | |
567 | |
527 | This function (not method) lets you add your own types to C<push_write>. |
568 | This function (not method) lets you add your own types to C<push_write>. |
528 | Whenever the given C<type> is used, C<push_write> will invoke the code |
569 | Whenever the given C<type> is used, C<push_write> will invoke the code |
529 | reference with the handle object and the remaining arguments. |
570 | reference with the handle object and the remaining arguments. |
… | |
… | |
531 | The code reference is supposed to return a single octet string that will |
572 | The code reference is supposed to return a single octet string that will |
532 | be appended to the write buffer. |
573 | be appended to the write buffer. |
533 | |
574 | |
534 | Note that this is a function, and all types registered this way will be |
575 | Note that this is a function, and all types registered this way will be |
535 | global, so try to use unique names. |
576 | global, so try to use unique names. |
536 | |
|
|
537 | =back |
|
|
538 | |
577 | |
539 | =cut |
578 | =cut |
540 | |
579 | |
541 | ############################################################################# |
580 | ############################################################################# |
542 | |
581 | |
… | |
… | |
556 | enough is there) from the read buffer (C<< $handle->rbuf >>) if you want |
595 | enough is there) from the read buffer (C<< $handle->rbuf >>) if you want |
557 | or not. |
596 | or not. |
558 | |
597 | |
559 | In the more complex case, you want to queue multiple callbacks. In this |
598 | In the more complex case, you want to queue multiple callbacks. In this |
560 | case, AnyEvent::Handle will call the first queued callback each time new |
599 | case, AnyEvent::Handle will call the first queued callback each time new |
561 | data arrives and removes it when it has done its job (see C<push_read>, |
600 | data arrives (also the first time it is queued) and removes it when it has |
562 | below). |
601 | done its job (see C<push_read>, below). |
563 | |
602 | |
564 | This way you can, for example, push three line-reads, followed by reading |
603 | This way you can, for example, push three line-reads, followed by reading |
565 | a chunk of data, and AnyEvent::Handle will execute them in order. |
604 | a chunk of data, and AnyEvent::Handle will execute them in order. |
566 | |
605 | |
567 | Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by |
606 | Example 1: EPP protocol parser. EPP sends 4 byte length info, followed by |
… | |
… | |
619 | =cut |
658 | =cut |
620 | |
659 | |
621 | sub _drain_rbuf { |
660 | sub _drain_rbuf { |
622 | my ($self) = @_; |
661 | my ($self) = @_; |
623 | |
662 | |
|
|
663 | local $self->{_in_drain} = 1; |
|
|
664 | |
624 | if ( |
665 | if ( |
625 | defined $self->{rbuf_max} |
666 | defined $self->{rbuf_max} |
626 | && $self->{rbuf_max} < length $self->{rbuf} |
667 | && $self->{rbuf_max} < length $self->{rbuf} |
627 | ) { |
668 | ) { |
628 | return $self->_error (&Errno::ENOSPC, 1); |
669 | return $self->_error (&Errno::ENOSPC, 1); |
629 | } |
670 | } |
630 | |
671 | |
631 | return if $self->{in_drain}; |
672 | while () { |
632 | local $self->{in_drain} = 1; |
|
|
633 | |
|
|
634 | while (my $len = length $self->{rbuf}) { |
|
|
635 | no strict 'refs'; |
673 | no strict 'refs'; |
|
|
674 | |
|
|
675 | my $len = length $self->{rbuf}; |
|
|
676 | |
636 | if (my $cb = shift @{ $self->{_queue} }) { |
677 | if (my $cb = shift @{ $self->{_queue} }) { |
637 | unless ($cb->($self)) { |
678 | unless ($cb->($self)) { |
638 | if ($self->{_eof}) { |
679 | if ($self->{_eof}) { |
639 | # no progress can be made (not enough data and no data forthcoming) |
680 | # no progress can be made (not enough data and no data forthcoming) |
640 | return $self->_error (&Errno::EPIPE, 1); |
681 | $self->_error (&Errno::EPIPE, 1), last; |
641 | } |
682 | } |
642 | |
683 | |
643 | unshift @{ $self->{_queue} }, $cb; |
684 | unshift @{ $self->{_queue} }, $cb; |
644 | return; |
685 | last; |
645 | } |
686 | } |
646 | } elsif ($self->{on_read}) { |
687 | } elsif ($self->{on_read}) { |
|
|
688 | last unless $len; |
|
|
689 | |
647 | $self->{on_read}($self); |
690 | $self->{on_read}($self); |
648 | |
691 | |
649 | if ( |
692 | if ( |
650 | $self->{_eof} # if no further data will arrive |
|
|
651 | && $len == length $self->{rbuf} # and no data has been consumed |
693 | $len == length $self->{rbuf} # if no data has been consumed |
652 | && !@{ $self->{_queue} } # and the queue is still empty |
694 | && !@{ $self->{_queue} } # and the queue is still empty |
653 | && $self->{on_read} # and we still want to read data |
695 | && $self->{on_read} # but we still have on_read |
654 | ) { |
696 | ) { |
|
|
697 | # no further data will arrive |
655 | # then no progress can be made |
698 | # so no progress can be made |
656 | return $self->_error (&Errno::EPIPE, 1); |
699 | $self->_error (&Errno::EPIPE, 1), last |
|
|
700 | if $self->{_eof}; |
|
|
701 | |
|
|
702 | last; # more data might arrive |
657 | } |
703 | } |
658 | } else { |
704 | } else { |
659 | # read side becomes idle |
705 | # read side becomes idle |
660 | delete $self->{_rw}; |
706 | delete $self->{_rw}; |
661 | return; |
707 | last; |
662 | } |
708 | } |
663 | } |
709 | } |
664 | |
710 | |
665 | $self->{on_eof}($self) |
711 | $self->{on_eof}($self) |
666 | if $self->{_eof} && $self->{on_eof}; |
712 | if $self->{_eof} && $self->{on_eof}; |
|
|
713 | |
|
|
714 | # may need to restart read watcher |
|
|
715 | unless ($self->{_rw}) { |
|
|
716 | $self->start_read |
|
|
717 | if $self->{on_read} || @{ $self->{_queue} }; |
|
|
718 | } |
667 | } |
719 | } |
668 | |
720 | |
669 | =item $handle->on_read ($cb) |
721 | =item $handle->on_read ($cb) |
670 | |
722 | |
671 | This replaces the currently set C<on_read> callback, or clears it (when |
723 | This replaces the currently set C<on_read> callback, or clears it (when |
… | |
… | |
676 | |
728 | |
677 | sub on_read { |
729 | sub on_read { |
678 | my ($self, $cb) = @_; |
730 | my ($self, $cb) = @_; |
679 | |
731 | |
680 | $self->{on_read} = $cb; |
732 | $self->{on_read} = $cb; |
|
|
733 | $self->_drain_rbuf if $cb && !$self->{_in_drain}; |
681 | } |
734 | } |
682 | |
735 | |
683 | =item $handle->rbuf |
736 | =item $handle->rbuf |
684 | |
737 | |
685 | Returns the read buffer (as a modifiable lvalue). |
738 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
734 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
787 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
735 | ->($self, $cb, @_); |
788 | ->($self, $cb, @_); |
736 | } |
789 | } |
737 | |
790 | |
738 | push @{ $self->{_queue} }, $cb; |
791 | push @{ $self->{_queue} }, $cb; |
739 | $self->_drain_rbuf; |
792 | $self->_drain_rbuf unless $self->{_in_drain}; |
740 | } |
793 | } |
741 | |
794 | |
742 | sub unshift_read { |
795 | sub unshift_read { |
743 | my $self = shift; |
796 | my $self = shift; |
744 | my $cb = pop; |
797 | my $cb = pop; |
… | |
… | |
750 | ->($self, $cb, @_); |
803 | ->($self, $cb, @_); |
751 | } |
804 | } |
752 | |
805 | |
753 | |
806 | |
754 | unshift @{ $self->{_queue} }, $cb; |
807 | unshift @{ $self->{_queue} }, $cb; |
755 | $self->_drain_rbuf; |
808 | $self->_drain_rbuf unless $self->{_in_drain}; |
756 | } |
809 | } |
757 | |
810 | |
758 | =item $handle->push_read (type => @args, $cb) |
811 | =item $handle->push_read (type => @args, $cb) |
759 | |
812 | |
760 | =item $handle->unshift_read (type => @args, $cb) |
813 | =item $handle->unshift_read (type => @args, $cb) |
… | |
… | |
845 | |
898 | |
846 | sub unshift_read_line { |
899 | sub unshift_read_line { |
847 | my $self = shift; |
900 | my $self = shift; |
848 | $self->unshift_read (line => @_); |
901 | $self->unshift_read (line => @_); |
849 | } |
902 | } |
850 | |
|
|
851 | =item netstring => $cb->($handle, $string) |
|
|
852 | |
|
|
853 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
854 | |
|
|
855 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
856 | |
|
|
857 | =cut |
|
|
858 | |
|
|
859 | register_read_type netstring => sub { |
|
|
860 | my ($self, $cb) = @_; |
|
|
861 | |
|
|
862 | sub { |
|
|
863 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
864 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
865 | $self->_error (&Errno::EBADMSG); |
|
|
866 | } |
|
|
867 | return; |
|
|
868 | } |
|
|
869 | |
|
|
870 | my $len = $1; |
|
|
871 | |
|
|
872 | $self->unshift_read (chunk => $len, sub { |
|
|
873 | my $string = $_[1]; |
|
|
874 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
875 | if ($_[1] eq ",") { |
|
|
876 | $cb->($_[0], $string); |
|
|
877 | } else { |
|
|
878 | $self->_error (&Errno::EBADMSG); |
|
|
879 | } |
|
|
880 | }); |
|
|
881 | }); |
|
|
882 | |
|
|
883 | 1 |
|
|
884 | } |
|
|
885 | }; |
|
|
886 | |
903 | |
887 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
904 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
888 | |
905 | |
889 | Makes a regex match against the regex object C<$accept> and returns |
906 | Makes a regex match against the regex object C<$accept> and returns |
890 | everything up to and including the match. |
907 | everything up to and including the match. |
… | |
… | |
952 | |
969 | |
953 | () |
970 | () |
954 | } |
971 | } |
955 | }; |
972 | }; |
956 | |
973 | |
|
|
974 | =item netstring => $cb->($handle, $string) |
|
|
975 | |
|
|
976 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
977 | |
|
|
978 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
979 | |
|
|
980 | =cut |
|
|
981 | |
|
|
982 | register_read_type netstring => sub { |
|
|
983 | my ($self, $cb) = @_; |
|
|
984 | |
|
|
985 | sub { |
|
|
986 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
987 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
988 | $self->_error (&Errno::EBADMSG); |
|
|
989 | } |
|
|
990 | return; |
|
|
991 | } |
|
|
992 | |
|
|
993 | my $len = $1; |
|
|
994 | |
|
|
995 | $self->unshift_read (chunk => $len, sub { |
|
|
996 | my $string = $_[1]; |
|
|
997 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
998 | if ($_[1] eq ",") { |
|
|
999 | $cb->($_[0], $string); |
|
|
1000 | } else { |
|
|
1001 | $self->_error (&Errno::EBADMSG); |
|
|
1002 | } |
|
|
1003 | }); |
|
|
1004 | }); |
|
|
1005 | |
|
|
1006 | 1 |
|
|
1007 | } |
|
|
1008 | }; |
|
|
1009 | |
|
|
1010 | =item packstring => $format, $cb->($handle, $string) |
|
|
1011 | |
|
|
1012 | An octet string prefixed with an encoded length. The encoding C<$format> |
|
|
1013 | uses the same format as a Perl C<pack> format, but must specify a single |
|
|
1014 | integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an |
|
|
1015 | optional C<!>, C<< < >> or C<< > >> modifier). |
|
|
1016 | |
|
|
1017 | DNS over TCP uses a prefix of C<n>, EPP uses a prefix of C<N>. |
|
|
1018 | |
|
|
1019 | Example: read a block of data prefixed by its length in BER-encoded |
|
|
1020 | format (very efficient). |
|
|
1021 | |
|
|
1022 | $handle->push_read (packstring => "w", sub { |
|
|
1023 | my ($handle, $data) = @_; |
|
|
1024 | }); |
|
|
1025 | |
|
|
1026 | =cut |
|
|
1027 | |
|
|
1028 | register_read_type packstring => sub { |
|
|
1029 | my ($self, $cb, $format) = @_; |
|
|
1030 | |
|
|
1031 | sub { |
|
|
1032 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
|
|
1033 | defined (my $len = eval { unpack $format, $_[0]->{rbuf} }) |
|
|
1034 | or return; |
|
|
1035 | |
|
|
1036 | # remove prefix |
|
|
1037 | substr $_[0]->{rbuf}, 0, (length pack $format, $len), ""; |
|
|
1038 | |
|
|
1039 | # read rest |
|
|
1040 | $_[0]->unshift_read (chunk => $len, $cb); |
|
|
1041 | |
|
|
1042 | 1 |
|
|
1043 | } |
|
|
1044 | }; |
|
|
1045 | |
957 | =item json => $cb->($handle, $hash_or_arrayref) |
1046 | =item json => $cb->($handle, $hash_or_arrayref) |
958 | |
1047 | |
959 | Reads a JSON object or array, decodes it and passes it to the callback. |
1048 | Reads a JSON object or array, decodes it and passes it to the callback. |
960 | |
1049 | |
961 | If a C<json> object was passed to the constructor, then that will be used |
1050 | If a C<json> object was passed to the constructor, then that will be used |
… | |
… | |
971 | the C<json> write type description, above, for an actual example. |
1060 | the C<json> write type description, above, for an actual example. |
972 | |
1061 | |
973 | =cut |
1062 | =cut |
974 | |
1063 | |
975 | register_read_type json => sub { |
1064 | register_read_type json => sub { |
976 | my ($self, $cb, $accept, $reject, $skip) = @_; |
1065 | my ($self, $cb) = @_; |
977 | |
1066 | |
978 | require JSON; |
1067 | require JSON; |
979 | |
1068 | |
980 | my $data; |
1069 | my $data; |
981 | my $rbuf = \$self->{rbuf}; |
1070 | my $rbuf = \$self->{rbuf}; |
… | |
… | |
996 | () |
1085 | () |
997 | } |
1086 | } |
998 | } |
1087 | } |
999 | }; |
1088 | }; |
1000 | |
1089 | |
|
|
1090 | =item storable => $cb->($handle, $ref) |
|
|
1091 | |
|
|
1092 | Deserialises a L<Storable> frozen representation as written by the |
|
|
1093 | C<storable> write type (BER-encoded length prefix followed by nfreeze'd |
|
|
1094 | data). |
|
|
1095 | |
|
|
1096 | Raises C<EBADMSG> error if the data could not be decoded. |
|
|
1097 | |
|
|
1098 | =cut |
|
|
1099 | |
|
|
1100 | register_read_type storable => sub { |
|
|
1101 | my ($self, $cb) = @_; |
|
|
1102 | |
|
|
1103 | require Storable; |
|
|
1104 | |
|
|
1105 | sub { |
|
|
1106 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
|
|
1107 | defined (my $len = eval { unpack "w", $_[0]->{rbuf} }) |
|
|
1108 | or return; |
|
|
1109 | |
|
|
1110 | # remove prefix |
|
|
1111 | substr $_[0]->{rbuf}, 0, (length pack "w", $len), ""; |
|
|
1112 | |
|
|
1113 | # read rest |
|
|
1114 | $_[0]->unshift_read (chunk => $len, sub { |
|
|
1115 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
|
|
1116 | $cb->($_[0], $ref); |
|
|
1117 | } else { |
|
|
1118 | $self->_error (&Errno::EBADMSG); |
|
|
1119 | } |
|
|
1120 | }); |
|
|
1121 | } |
|
|
1122 | }; |
|
|
1123 | |
1001 | =back |
1124 | =back |
1002 | |
1125 | |
1003 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1126 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1004 | |
1127 | |
1005 | This function (not method) lets you add your own types to C<push_read>. |
1128 | This function (not method) lets you add your own types to C<push_read>. |
… | |
… | |
1023 | =item $handle->stop_read |
1146 | =item $handle->stop_read |
1024 | |
1147 | |
1025 | =item $handle->start_read |
1148 | =item $handle->start_read |
1026 | |
1149 | |
1027 | In rare cases you actually do not want to read anything from the |
1150 | In rare cases you actually do not want to read anything from the |
1028 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
1151 | socket. In this case you can call C<stop_read>. Neither C<on_read> nor |
1029 | any queued callbacks will be executed then. To start reading again, call |
1152 | any queued callbacks will be executed then. To start reading again, call |
1030 | C<start_read>. |
1153 | C<start_read>. |
|
|
1154 | |
|
|
1155 | Note that AnyEvent::Handle will automatically C<start_read> for you when |
|
|
1156 | you change the C<on_read> callback or push/unshift a read callback, and it |
|
|
1157 | will automatically C<stop_read> for you when neither C<on_read> is set nor |
|
|
1158 | there are any read requests in the queue. |
1031 | |
1159 | |
1032 | =cut |
1160 | =cut |
1033 | |
1161 | |
1034 | sub stop_read { |
1162 | sub stop_read { |
1035 | my ($self) = @_; |
1163 | my ($self) = @_; |
… | |
… | |
1050 | if ($len > 0) { |
1178 | if ($len > 0) { |
1051 | $self->{_activity} = AnyEvent->now; |
1179 | $self->{_activity} = AnyEvent->now; |
1052 | |
1180 | |
1053 | $self->{filter_r} |
1181 | $self->{filter_r} |
1054 | ? $self->{filter_r}($self, $rbuf) |
1182 | ? $self->{filter_r}($self, $rbuf) |
1055 | : $self->_drain_rbuf; |
1183 | : $self->{_in_drain} || $self->_drain_rbuf; |
1056 | |
1184 | |
1057 | } elsif (defined $len) { |
1185 | } elsif (defined $len) { |
1058 | delete $self->{_rw}; |
1186 | delete $self->{_rw}; |
1059 | $self->{_eof} = 1; |
1187 | $self->{_eof} = 1; |
1060 | $self->_drain_rbuf; |
1188 | $self->_drain_rbuf unless $self->{_in_drain}; |
1061 | |
1189 | |
1062 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1190 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1063 | return $self->_error ($!, 1); |
1191 | return $self->_error ($!, 1); |
1064 | } |
1192 | } |
1065 | }); |
1193 | }); |
… | |
… | |
1067 | } |
1195 | } |
1068 | |
1196 | |
1069 | sub _dotls { |
1197 | sub _dotls { |
1070 | my ($self) = @_; |
1198 | my ($self) = @_; |
1071 | |
1199 | |
|
|
1200 | my $buf; |
|
|
1201 | |
1072 | if (length $self->{_tls_wbuf}) { |
1202 | if (length $self->{_tls_wbuf}) { |
1073 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1203 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1074 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1204 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1075 | } |
1205 | } |
1076 | } |
1206 | } |
1077 | |
1207 | |
1078 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1208 | if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1079 | $self->{wbuf} .= $buf; |
1209 | $self->{wbuf} .= $buf; |
1080 | $self->_drain_wbuf; |
1210 | $self->_drain_wbuf; |
1081 | } |
1211 | } |
1082 | |
1212 | |
1083 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
1213 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
1214 | if (length $buf) { |
1084 | $self->{rbuf} .= $buf; |
1215 | $self->{rbuf} .= $buf; |
1085 | $self->_drain_rbuf; |
1216 | $self->_drain_rbuf unless $self->{_in_drain}; |
|
|
1217 | } else { |
|
|
1218 | # let's treat SSL-eof as we treat normal EOF |
|
|
1219 | $self->{_eof} = 1; |
|
|
1220 | $self->_shutdown; |
|
|
1221 | return; |
|
|
1222 | } |
1086 | } |
1223 | } |
1087 | |
1224 | |
1088 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1225 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1089 | |
1226 | |
1090 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
1227 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
… | |
… | |
1176 | |
1313 | |
1177 | sub DESTROY { |
1314 | sub DESTROY { |
1178 | my $self = shift; |
1315 | my $self = shift; |
1179 | |
1316 | |
1180 | $self->stoptls; |
1317 | $self->stoptls; |
|
|
1318 | |
|
|
1319 | my $linger = exists $self->{linger} ? $self->{linger} : 3600; |
|
|
1320 | |
|
|
1321 | if ($linger && length $self->{wbuf}) { |
|
|
1322 | my $fh = delete $self->{fh}; |
|
|
1323 | my $wbuf = delete $self->{wbuf}; |
|
|
1324 | |
|
|
1325 | my @linger; |
|
|
1326 | |
|
|
1327 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
|
|
1328 | my $len = syswrite $fh, $wbuf, length $wbuf; |
|
|
1329 | |
|
|
1330 | if ($len > 0) { |
|
|
1331 | substr $wbuf, 0, $len, ""; |
|
|
1332 | } else { |
|
|
1333 | @linger = (); # end |
|
|
1334 | } |
|
|
1335 | }); |
|
|
1336 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
|
|
1337 | @linger = (); |
|
|
1338 | }); |
|
|
1339 | } |
1181 | } |
1340 | } |
1182 | |
1341 | |
1183 | =item AnyEvent::Handle::TLS_CTX |
1342 | =item AnyEvent::Handle::TLS_CTX |
1184 | |
1343 | |
1185 | This function creates and returns the Net::SSLeay::CTX object used by |
1344 | This function creates and returns the Net::SSLeay::CTX object used by |