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.56 by root, Wed Jun 4 09:55:16 2008 UTC vs.
Revision 1.60 by root, Thu Jun 5 18:30:08 2008 UTC

14 14
15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
16 16
17=cut 17=cut
18 18
19our $VERSION = 4.12; 19our $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;
224 if ($self->{tls}) { 224 if ($self->{tls}) {
225 require Net::SSLeay; 225 require Net::SSLeay;
226 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); 226 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx});
227 } 227 }
228 228
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; 229 $self->{_activity} = AnyEvent->now;
235 $self->_timeout; 230 $self->_timeout;
236 231
237 $self->start_read; 232 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
233 $self->on_read (delete $self->{on_read} ) if $self->{on_read};
238 234
239 $self 235 $self
240} 236}
241 237
242sub _shutdown { 238sub _shutdown {
620=cut 616=cut
621 617
622sub _drain_rbuf { 618sub _drain_rbuf {
623 my ($self) = @_; 619 my ($self) = @_;
624 620
621 local $self->{_in_drain} = 1;
622
625 if ( 623 if (
626 defined $self->{rbuf_max} 624 defined $self->{rbuf_max}
627 && $self->{rbuf_max} < length $self->{rbuf} 625 && $self->{rbuf_max} < length $self->{rbuf}
628 ) { 626 ) {
629 return $self->_error (&Errno::ENOSPC, 1); 627 return $self->_error (&Errno::ENOSPC, 1);
630 } 628 }
631 629
632 return if $self->{in_drain}; 630 while () {
633 local $self->{in_drain} = 1;
634
635 while (my $len = length $self->{rbuf}) {
636 no strict 'refs'; 631 no strict 'refs';
632
633 my $len = length $self->{rbuf};
634
637 if (my $cb = shift @{ $self->{_queue} }) { 635 if (my $cb = shift @{ $self->{_queue} }) {
638 unless ($cb->($self)) { 636 unless ($cb->($self)) {
639 if ($self->{_eof}) { 637 if ($self->{_eof}) {
640 # 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)
641 return $self->_error (&Errno::EPIPE, 1); 639 return $self->_error (&Errno::EPIPE, 1);
686 684
687sub on_read { 685sub on_read {
688 my ($self, $cb) = @_; 686 my ($self, $cb) = @_;
689 687
690 $self->{on_read} = $cb; 688 $self->{on_read} = $cb;
689 $self->_drain_rbuf if $cb && !$self->{_in_drain};
691} 690}
692 691
693=item $handle->rbuf 692=item $handle->rbuf
694 693
695Returns the read buffer (as a modifiable lvalue). 694Returns the read buffer (as a modifiable lvalue).
744 $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")
745 ->($self, $cb, @_); 744 ->($self, $cb, @_);
746 } 745 }
747 746
748 push @{ $self->{_queue} }, $cb; 747 push @{ $self->{_queue} }, $cb;
749 $self->_drain_rbuf; 748 $self->_drain_rbuf unless $self->{_in_drain};
750} 749}
751 750
752sub unshift_read { 751sub unshift_read {
753 my $self = shift; 752 my $self = shift;
754 my $cb = pop; 753 my $cb = pop;
760 ->($self, $cb, @_); 759 ->($self, $cb, @_);
761 } 760 }
762 761
763 762
764 unshift @{ $self->{_queue} }, $cb; 763 unshift @{ $self->{_queue} }, $cb;
765 $self->_drain_rbuf; 764 $self->_drain_rbuf unless $self->{_in_drain};
766} 765}
767 766
768=item $handle->push_read (type => @args, $cb) 767=item $handle->push_read (type => @args, $cb)
769 768
770=item $handle->unshift_read (type => @args, $cb) 769=item $handle->unshift_read (type => @args, $cb)
1033=item $handle->stop_read 1032=item $handle->stop_read
1034 1033
1035=item $handle->start_read 1034=item $handle->start_read
1036 1035
1037In rare cases you actually do not want to read anything from the 1036In rare cases you actually do not want to read anything from the
1038socket. In this case you can call C<stop_read>. Neither C<on_read> no 1037socket. In this case you can call C<stop_read>. Neither C<on_read> nor
1039any queued callbacks will be executed then. To start reading again, call 1038any queued callbacks will be executed then. To start reading again, call
1040C<start_read>. 1039C<start_read>.
1041 1040
1042Note that AnyEvent::Handle will automatically C<start_read> for you when 1041Note 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 1042you change the C<on_read> callback or push/unshift a read callback, and it
1065 if ($len > 0) { 1064 if ($len > 0) {
1066 $self->{_activity} = AnyEvent->now; 1065 $self->{_activity} = AnyEvent->now;
1067 1066
1068 $self->{filter_r} 1067 $self->{filter_r}
1069 ? $self->{filter_r}($self, $rbuf) 1068 ? $self->{filter_r}($self, $rbuf)
1070 : $self->_drain_rbuf; 1069 : $self->{_in_drain} || $self->_drain_rbuf;
1071 1070
1072 } elsif (defined $len) { 1071 } elsif (defined $len) {
1073 delete $self->{_rw}; 1072 delete $self->{_rw};
1074 $self->{_eof} = 1; 1073 $self->{_eof} = 1;
1075 $self->_drain_rbuf; 1074 $self->_drain_rbuf unless $self->{_in_drain};
1076 1075
1077 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { 1076 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) {
1078 return $self->_error ($!, 1); 1077 return $self->_error ($!, 1);
1079 } 1078 }
1080 }); 1079 });
1098 } 1097 }
1099 1098
1100 while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { 1099 while (defined ($buf = Net::SSLeay::read ($self->{tls}))) {
1101 if (length $buf) { 1100 if (length $buf) {
1102 $self->{rbuf} .= $buf; 1101 $self->{rbuf} .= $buf;
1103 $self->_drain_rbuf; 1102 $self->_drain_rbuf unless $self->{_in_drain};
1104 } else { 1103 } else {
1105 # let's treat SSL-eof as we treat normal EOF 1104 # let's treat SSL-eof as we treat normal EOF
1106 $self->{_eof} = 1; 1105 $self->{_eof} = 1;
1107 $self->_shutdown; 1106 $self->_shutdown;
1108 return; 1107 return;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines