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.21 by root, Sat May 24 15:03:42 2008 UTC vs.
Revision 1.25 by root, Sat May 24 15:19:43 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
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
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
662 (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) 664 my $err = Net::SSLeay::get_error ($self->{tls}, -1);
665
663 != Net::SSLeay::ERROR_WANT_READ () 666 if ($err!= Net::SSLeay::ERROR_WANT_READ ()) {
664 ) {
665 if ($err == Net::SSLeay::ERROR_SYSCALL ()) { 667 if ($err == Net::SSLeay::ERROR_SYSCALL ()) {
666 $self->error; 668 $self->error;
667 } elsif ($err == Net::SSLeay::ERROR_SSL ()) { 669 } elsif ($err == Net::SSLeay::ERROR_SSL ()) {
668 $! = &Errno::EIO; 670 $! = &Errno::EIO;
669 $self->error; 671 $self->error;
671 673
672 # all others are fine for our purposes 674 # all others are fine for our purposes
673 } 675 }
674} 676}
675 677
678=item $handle->starttls ($tls[, $tls_ctx])
679
680Instead of starting TLS negotiation immediately when the AnyEvent::Handle
681object is created, you can also do that at a later time by calling
682C<starttls>.
683
684The first argument is the same as the C<tls> constructor argument (either
685C<"connect">, C<"accept"> or an existing Net::SSLeay object).
686
687The second argument is the optional C<Net::SSLeay::CTX> object that is
688used when AnyEvent::Handle has to create its own TLS connection object.
689
690=cut
691
676# TODO: maybe document... 692# TODO: maybe document...
677sub starttls { 693sub starttls {
678 my ($self, $ssl, $ctx) = @_; 694 my ($self, $ssl, $ctx) = @_;
695
696 $self->stoptls;
679 697
680 if ($ssl eq "accept") { 698 if ($ssl eq "accept") {
681 $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); 699 $ssl = Net::SSLeay::new ($ctx || TLS_CTX ());
682 Net::SSLeay::set_accept_state ($ssl); 700 Net::SSLeay::set_accept_state ($ssl);
683 } elsif ($ssl eq "connect") { 701 } elsif ($ssl eq "connect") {
708 Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); 726 Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]});
709 &_dotls; 727 &_dotls;
710 }; 728 };
711} 729}
712 730
731=item $handle->stoptls
732
733Destroys the SSL connection, if any. Partial read or write data will be
734lost.
735
736=cut
737
738sub stoptls {
739 my ($self) = @_;
740
741 Net::SSLeay::free (delete $self->{tls}) if $self->{tls};
742 delete $self->{tls_rbio};
743 delete $self->{tls_wbio};
744 delete $self->{tls_wbuf};
745 delete $self->{filter_r};
746 delete $self->{filter_w};
747}
748
713sub DESTROY { 749sub DESTROY {
714 my $self = shift; 750 my $self = shift;
715 751
716 Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; 752 $self->stoptls;
717} 753}
718 754
719=item AnyEvent::Handle::TLS_CTX 755=item AnyEvent::Handle::TLS_CTX
720 756
721This function creates and returns the Net::SSLeay::CTX object used by 757This function creates and returns the Net::SSLeay::CTX object used by

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines