ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.54 by root, Tue Jun 3 09:02:46 2008 UTC vs.
Revision 1.56 by root, Wed Jun 4 09:55:16 2008 UTC

339 $self->{on_timeout}($self); 339 $self->{on_timeout}($self);
340 } else { 340 } else {
341 $self->_error (&Errno::ETIMEDOUT); 341 $self->_error (&Errno::ETIMEDOUT);
342 } 342 }
343 343
344 # callbakx could have changed timeout value, optimise 344 # callback could have changed timeout value, optimise
345 return unless $self->{timeout}; 345 return unless $self->{timeout};
346 346
347 # calculate new after 347 # calculate new after
348 $after = $self->{timeout}; 348 $after = $self->{timeout};
349 } 349 }
350 350
351 Scalar::Util::weaken $self; 351 Scalar::Util::weaken $self;
352 return unless $self; # ->error could have destroyed $self
352 353
353 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { 354 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub {
354 delete $self->{_tw}; 355 delete $self->{_tw};
355 $self->_timeout; 356 $self->_timeout;
356 }); 357 });
639 # no progress can be made (not enough data and no data forthcoming) 640 # no progress can be made (not enough data and no data forthcoming)
640 return $self->_error (&Errno::EPIPE, 1); 641 return $self->_error (&Errno::EPIPE, 1);
641 } 642 }
642 643
643 unshift @{ $self->{_queue} }, $cb; 644 unshift @{ $self->{_queue} }, $cb;
644 return; 645 last;
645 } 646 }
646 } elsif ($self->{on_read}) { 647 } elsif ($self->{on_read}) {
647 $self->{on_read}($self); 648 $self->{on_read}($self);
648 649
649 if ( 650 if (
650 $self->{_eof} # if no further data will arrive
651 && $len == length $self->{rbuf} # and no data has been consumed 651 $len == length $self->{rbuf} # if no data has been consumed
652 && !@{ $self->{_queue} } # and the queue is still empty 652 && !@{ $self->{_queue} } # and the queue is still empty
653 && $self->{on_read} # and we still want to read data 653 && $self->{on_read} # but we still have on_read
654 ) { 654 ) {
655 # no further data will arrive
655 # then no progress can be made 656 # so no progress can be made
656 return $self->_error (&Errno::EPIPE, 1); 657 return $self->_error (&Errno::EPIPE, 1)
658 if $self->{_eof};
659
660 last; # more data might arrive
657 } 661 }
658 } else { 662 } else {
659 # read side becomes idle 663 # read side becomes idle
660 delete $self->{_rw}; 664 delete $self->{_rw};
661 return; 665 last;
662 } 666 }
663 } 667 }
664 668
665 $self->{on_eof}($self) 669 $self->{on_eof}($self)
666 if $self->{_eof} && $self->{on_eof}; 670 if $self->{_eof} && $self->{on_eof};
671
672 # may need to restart read watcher
673 unless ($self->{_rw}) {
674 $self->start_read
675 if $self->{on_read} || @{ $self->{_queue} };
676 }
667} 677}
668 678
669=item $handle->on_read ($cb) 679=item $handle->on_read ($cb)
670 680
671This replaces the currently set C<on_read> callback, or clears it (when 681This replaces the currently set C<on_read> callback, or clears it (when
1027In rare cases you actually do not want to read anything from the 1037In rare cases you actually do not want to read anything from the
1028socket. In this case you can call C<stop_read>. Neither C<on_read> no 1038socket. In this case you can call C<stop_read>. Neither C<on_read> no
1029any queued callbacks will be executed then. To start reading again, call 1039any queued callbacks will be executed then. To start reading again, call
1030C<start_read>. 1040C<start_read>.
1031 1041
1042Note that AnyEvent::Handle will automatically C<start_read> for you when
1043you change the C<on_read> callback or push/unshift a read callback, and it
1044will automatically C<stop_read> for you when neither C<on_read> is set nor
1045there are any read requests in the queue.
1046
1032=cut 1047=cut
1033 1048
1034sub stop_read { 1049sub stop_read {
1035 my ($self) = @_; 1050 my ($self) = @_;
1036 1051
1067} 1082}
1068 1083
1069sub _dotls { 1084sub _dotls {
1070 my ($self) = @_; 1085 my ($self) = @_;
1071 1086
1087 my $buf;
1088
1072 if (length $self->{_tls_wbuf}) { 1089 if (length $self->{_tls_wbuf}) {
1073 while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { 1090 while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) {
1074 substr $self->{_tls_wbuf}, 0, $len, ""; 1091 substr $self->{_tls_wbuf}, 0, $len, "";
1075 } 1092 }
1076 } 1093 }
1077 1094
1078 if (defined (my $buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1095 if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1079 $self->{wbuf} .= $buf; 1096 $self->{wbuf} .= $buf;
1080 $self->_drain_wbuf; 1097 $self->_drain_wbuf;
1081 } 1098 }
1082 1099
1083 while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { 1100 while (defined ($buf = Net::SSLeay::read ($self->{tls}))) {
1101 if (length $buf) {
1084 $self->{rbuf} .= $buf; 1102 $self->{rbuf} .= $buf;
1085 $self->_drain_rbuf; 1103 $self->_drain_rbuf;
1104 } else {
1105 # let's treat SSL-eof as we treat normal EOF
1106 $self->{_eof} = 1;
1107 $self->_shutdown;
1108 return;
1109 }
1086 } 1110 }
1087 1111
1088 my $err = Net::SSLeay::get_error ($self->{tls}, -1); 1112 my $err = Net::SSLeay::get_error ($self->{tls}, -1);
1089 1113
1090 if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { 1114 if ($err!= Net::SSLeay::ERROR_WANT_READ ()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines