… | |
… | |
10 | use Fcntl (); |
10 | use Fcntl (); |
11 | use Errno qw/EAGAIN EINTR/; |
11 | use Errno qw/EAGAIN EINTR/; |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | This module is experimental. |
17 | This 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 | |
217 | This method returns the filehandle of the L<AnyEvent::Handle> object. |
217 | This method returns the file handle of the L<AnyEvent::Handle> object. |
218 | |
218 | |
219 | =cut |
219 | =cut |
220 | |
220 | |
221 | sub fh { $_[0]->{fh} } |
221 | sub fh { $_[0]->{fh} } |
222 | |
222 | |
… | |
… | |
602 | |
602 | |
603 | =item $handle->start_read |
603 | =item $handle->start_read |
604 | |
604 | |
605 | In rare cases you actually do not want to read anything from the |
605 | In rare cases you actually do not want to read anything from the |
606 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
606 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
607 | any queued callbacks will be executed then. To start readign again, call |
607 | any queued callbacks will be executed then. To start reading again, call |
608 | C<start_read>. |
608 | C<start_read>. |
609 | |
609 | |
610 | =cut |
610 | =cut |
611 | |
611 | |
612 | sub stop_read { |
612 | sub stop_read { |
… | |
… | |
644 | |
644 | |
645 | sub _dotls { |
645 | sub _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; |
… | |
… | |
684 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
686 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
685 | Net::SSLeay::set_connect_state ($ssl); |
687 | Net::SSLeay::set_connect_state ($ssl); |
686 | } |
688 | } |
687 | |
689 | |
688 | $self->{tls} = $ssl; |
690 | $self->{tls} = $ssl; |
|
|
691 | |
|
|
692 | # basically, this is deep magic (because SSL_read should have the same issues) |
|
|
693 | # but the openssl maintainers basically said: "trust us, it just works". |
|
|
694 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
|
|
695 | # and mismaintained ssleay-module doesn't even offer them). |
|
|
696 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
|
|
697 | (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
|
|
698 | | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
689 | |
699 | |
690 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
700 | $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 ()); |
701 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
692 | |
702 | |
693 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
703 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |