… | |
… | |
616 | =cut |
616 | =cut |
617 | |
617 | |
618 | sub _drain_rbuf { |
618 | sub _drain_rbuf { |
619 | my ($self) = @_; |
619 | my ($self) = @_; |
620 | |
620 | |
|
|
621 | local $self->{_in_drain} = 1; |
|
|
622 | |
621 | if ( |
623 | if ( |
622 | defined $self->{rbuf_max} |
624 | defined $self->{rbuf_max} |
623 | && $self->{rbuf_max} < length $self->{rbuf} |
625 | && $self->{rbuf_max} < length $self->{rbuf} |
624 | ) { |
626 | ) { |
625 | return $self->_error (&Errno::ENOSPC, 1); |
627 | return $self->_error (&Errno::ENOSPC, 1); |
626 | } |
628 | } |
627 | |
629 | |
628 | return if $self->{in_drain}; |
630 | while () { |
629 | local $self->{in_drain} = 1; |
|
|
630 | |
|
|
631 | while (my $len = length $self->{rbuf}) { |
|
|
632 | no strict 'refs'; |
631 | no strict 'refs'; |
|
|
632 | |
|
|
633 | my $len = length $self->{rbuf}; |
|
|
634 | |
633 | if (my $cb = shift @{ $self->{_queue} }) { |
635 | if (my $cb = shift @{ $self->{_queue} }) { |
634 | unless ($cb->($self)) { |
636 | unless ($cb->($self)) { |
635 | if ($self->{_eof}) { |
637 | if ($self->{_eof}) { |
636 | # no progress can be made (not enough data and no data forthcoming) |
638 | # no progress can be made (not enough data and no data forthcoming) |
637 | return $self->_error (&Errno::EPIPE, 1); |
639 | return $self->_error (&Errno::EPIPE, 1); |
… | |
… | |
682 | |
684 | |
683 | sub on_read { |
685 | sub on_read { |
684 | my ($self, $cb) = @_; |
686 | my ($self, $cb) = @_; |
685 | |
687 | |
686 | $self->{on_read} = $cb; |
688 | $self->{on_read} = $cb; |
687 | $self->_drain_rbuf if $cb; |
689 | $self->_drain_rbuf if $cb && !$self->{_in_drain}; |
688 | } |
690 | } |
689 | |
691 | |
690 | =item $handle->rbuf |
692 | =item $handle->rbuf |
691 | |
693 | |
692 | Returns the read buffer (as a modifiable lvalue). |
694 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
741 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
743 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
742 | ->($self, $cb, @_); |
744 | ->($self, $cb, @_); |
743 | } |
745 | } |
744 | |
746 | |
745 | push @{ $self->{_queue} }, $cb; |
747 | push @{ $self->{_queue} }, $cb; |
746 | $self->_drain_rbuf; |
748 | $self->_drain_rbuf unless $self->{_in_drain}; |
747 | } |
749 | } |
748 | |
750 | |
749 | sub unshift_read { |
751 | sub unshift_read { |
750 | my $self = shift; |
752 | my $self = shift; |
751 | my $cb = pop; |
753 | my $cb = pop; |
… | |
… | |
757 | ->($self, $cb, @_); |
759 | ->($self, $cb, @_); |
758 | } |
760 | } |
759 | |
761 | |
760 | |
762 | |
761 | unshift @{ $self->{_queue} }, $cb; |
763 | unshift @{ $self->{_queue} }, $cb; |
762 | $self->_drain_rbuf; |
764 | $self->_drain_rbuf unless $self->{_in_drain}; |
763 | } |
765 | } |
764 | |
766 | |
765 | =item $handle->push_read (type => @args, $cb) |
767 | =item $handle->push_read (type => @args, $cb) |
766 | |
768 | |
767 | =item $handle->unshift_read (type => @args, $cb) |
769 | =item $handle->unshift_read (type => @args, $cb) |
… | |
… | |
1062 | if ($len > 0) { |
1064 | if ($len > 0) { |
1063 | $self->{_activity} = AnyEvent->now; |
1065 | $self->{_activity} = AnyEvent->now; |
1064 | |
1066 | |
1065 | $self->{filter_r} |
1067 | $self->{filter_r} |
1066 | ? $self->{filter_r}($self, $rbuf) |
1068 | ? $self->{filter_r}($self, $rbuf) |
1067 | : $self->_drain_rbuf; |
1069 | : $self->{_in_drain} || $self->_drain_rbuf; |
1068 | |
1070 | |
1069 | } elsif (defined $len) { |
1071 | } elsif (defined $len) { |
1070 | delete $self->{_rw}; |
1072 | delete $self->{_rw}; |
1071 | $self->{_eof} = 1; |
1073 | $self->{_eof} = 1; |
1072 | $self->_drain_rbuf; |
1074 | $self->_drain_rbuf unless $self->{_in_drain}; |
1073 | |
1075 | |
1074 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1076 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1075 | return $self->_error ($!, 1); |
1077 | return $self->_error ($!, 1); |
1076 | } |
1078 | } |
1077 | }); |
1079 | }); |
… | |
… | |
1095 | } |
1097 | } |
1096 | |
1098 | |
1097 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
1099 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
1098 | if (length $buf) { |
1100 | if (length $buf) { |
1099 | $self->{rbuf} .= $buf; |
1101 | $self->{rbuf} .= $buf; |
1100 | $self->_drain_rbuf; |
1102 | $self->_drain_rbuf unless $self->{_in_drain}; |
1101 | } else { |
1103 | } else { |
1102 | # let's treat SSL-eof as we treat normal EOF |
1104 | # let's treat SSL-eof as we treat normal EOF |
1103 | $self->{_eof} = 1; |
1105 | $self->{_eof} = 1; |
1104 | $self->_shutdown; |
1106 | $self->_shutdown; |
1105 | return; |
1107 | return; |