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.19 by root, Sat May 24 05:57:11 2008 UTC vs.
Revision 1.23 by root, Sat May 24 15:11:22 2008 UTC

10use Fcntl (); 10use Fcntl ();
11use Errno qw/EAGAIN EINTR/; 11use Errno qw/EAGAIN EINTR/;
12 12
13=head1 NAME 13=head1 NAME
14 14
15AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent 15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
16 16
17This module is experimental. 17This module is experimental.
18 18
19=cut 19=cut
20 20
83waiting for data. 83waiting for data.
84 84
85=item on_error => $cb->($self) 85=item on_error => $cb->($self)
86 86
87This is the fatal error callback, that is called when, well, a fatal error 87This is the fatal error callback, that is called when, well, a fatal error
88ocurs, such as not being able to resolve the hostname, failure to connect 88occurs, such as not being able to resolve the hostname, failure to connect
89or a read error. 89or a read error.
90 90
91The object will not be in a usable state when this callback has been 91The object will not be in a usable state when this callback has been
92called. 92called.
93 93
102 102
103This sets the default read callback, which is called when data arrives 103This sets the default read callback, which is called when data arrives
104and no read request is in the queue. 104and no read request is in the queue.
105 105
106To access (and remove data from) the read buffer, use the C<< ->rbuf >> 106To access (and remove data from) the read buffer, use the C<< ->rbuf >>
107method or acces sthe C<$self->{rbuf}> member directly. 107method or access the C<$self->{rbuf}> member directly.
108 108
109When an EOF condition is detected then AnyEvent::Handle will first try to 109When an EOF condition is detected then AnyEvent::Handle will first try to
110feed all the remaining data to the queued callbacks and C<on_read> before 110feed all the remaining data to the queued callbacks and C<on_read> before
111calling the C<on_eof> callback. If no progress can be made, then a fatal 111calling the C<on_eof> callback. If no progress can be made, then a fatal
112error will be raised (with C<$!> set to C<EPIPE>). 112error will be raised (with C<$!> set to C<EPIPE>).
212 } 212 }
213} 213}
214 214
215=item $fh = $handle->fh 215=item $fh = $handle->fh
216 216
217This method returns the filehandle of the L<AnyEvent::Handle> object. 217This method returns the file handle of the L<AnyEvent::Handle> object.
218 218
219=cut 219=cut
220 220
221sub fh { $_[0]->{fh} } 221sub fh { $_[0]->{fh} }
222 222
250for reading. 250for reading.
251 251
252The write queue is very simple: you can add data to its end, and 252The write queue is very simple: you can add data to its end, and
253AnyEvent::Handle will automatically try to get rid of it for you. 253AnyEvent::Handle will automatically try to get rid of it for you.
254 254
255When data could be writtena nd the write buffer is shorter then the low 255When data could be written and the write buffer is shorter then the low
256water mark, the C<on_drain> callback will be invoked. 256water mark, the C<on_drain> callback will be invoked.
257 257
258=over 4 258=over 4
259 259
260=item $handle->on_drain ($cb) 260=item $handle->on_drain ($cb)
488Append the given callback to the end of the queue (C<push_read>) or 488Append the given callback to the end of the queue (C<push_read>) or
489prepend it (C<unshift_read>). 489prepend it (C<unshift_read>).
490 490
491The callback is called each time some additional read data arrives. 491The callback is called each time some additional read data arrives.
492 492
493It must check wether enough data is in the read buffer already. 493It must check whether enough data is in the read buffer already.
494 494
495If not enough data is available, it must return the empty list or a false 495If not enough data is available, it must return the empty list or a false
496value, in which case it will be called repeatedly until enough data is 496value, in which case it will be called repeatedly until enough data is
497available (or an error condition is detected). 497available (or an error condition is detected).
498 498
602 602
603=item $handle->start_read 603=item $handle->start_read
604 604
605In rare cases you actually do not want to read anything from the 605In rare cases you actually do not want to read anything from the
606socket. In this case you can call C<stop_read>. Neither C<on_read> no 606socket. In this case you can call C<stop_read>. Neither C<on_read> no
607any queued callbacks will be executed then. To start readign again, call 607any queued callbacks will be executed then. To start reading again, call
608C<start_read>. 608C<start_read>.
609 609
610=cut 610=cut
611 611
612sub stop_read { 612sub stop_read {
644 644
645sub _dotls { 645sub _dotls {
646 my ($self) = @_; 646 my ($self) = @_;
647 647
648 if (length $self->{tls_wbuf}) { 648 if (length $self->{tls_wbuf}) {
649 my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf}); 649 while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) {
650 substr $self->{tls_wbuf}, 0, $len, "" if $len > 0; 650 substr $self->{tls_wbuf}, 0, $len, "";
651 }
651 } 652 }
652 653
653 if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { 654 if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) {
654 $self->{wbuf} .= $buf; 655 $self->{wbuf} .= $buf;
655 $self->_drain_wbuf; 656 $self->_drain_wbuf;
656 } 657 }
657 658
658 if (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { 659 while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) {
659 $self->{rbuf} .= $buf; 660 $self->{rbuf} .= $buf;
660 $self->_drain_rbuf; 661 $self->_drain_rbuf;
661 } elsif ( 662 }
663
664 if (
662 (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) 665 (my $err = Net::SSLeay::get_error ($self->{tls}, -1))
663 != Net::SSLeay::ERROR_WANT_READ () 666 != Net::SSLeay::ERROR_WANT_READ ()
664 ) { 667 ) {
665 if ($err == Net::SSLeay::ERROR_SYSCALL ()) { 668 if ($err == Net::SSLeay::ERROR_SYSCALL ()) {
666 $self->error; 669 $self->error;
685 Net::SSLeay::set_connect_state ($ssl); 688 Net::SSLeay::set_connect_state ($ssl);
686 } 689 }
687 690
688 $self->{tls} = $ssl; 691 $self->{tls} = $ssl;
689 692
693 # basically, this is deep magic (because SSL_read should have the same issues)
694 # but the openssl maintainers basically said: "trust us, it just works".
695 # (unfortunately, we have to hardcode constants because the abysmally misdesigned
696 # and mismaintained ssleay-module doesn't even offer them).
697 Net::SSLeay::CTX_set_mode ($self->{tls},
698 (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1)
699 | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2));
700
690 $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 701 $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
691 $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 702 $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
692 703
693 Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); 704 Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio});
694 705

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines