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.187 by root, Tue Sep 8 00:01:12 2009 UTC vs.
Revision 1.192 by root, Fri Mar 12 23:22:14 2010 UTC

14 on_error => sub { 14 on_error => sub {
15 my ($hdl, $fatal, $msg) = @_; 15 my ($hdl, $fatal, $msg) = @_;
16 warn "got error $msg\n"; 16 warn "got error $msg\n";
17 $hdl->destroy; 17 $hdl->destroy;
18 $cv->send; 18 $cv->send;
19 ); 19 };
20 20
21 # send some request line 21 # send some request line
22 $hdl->push_write ("getinfo\015\012"); 22 $hdl->push_write ("getinfo\015\012");
23 23
24 # read the response line 24 # read the response line
79 79
80=head1 METHODS 80=head1 METHODS
81 81
82=over 4 82=over 4
83 83
84=item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... 84=item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value...
85 85
86The constructor supports these arguments (all as C<< key => value >> pairs). 86The constructor supports these arguments (all as C<< key => value >> pairs).
87 87
88=over 4 88=over 4
89 89
710 710
711Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). 711Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument).
712 712
713=cut 713=cut
714 714
715sub on_starttls { 715sub on_stoptls {
716 $_[0]{on_stoptls} = $_[1]; 716 $_[0]{on_stoptls} = $_[1];
717} 717}
718 718
719=item $handle->rbuf_max ($max_octets) 719=item $handle->rbuf_max ($max_octets)
720 720
903 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" 903 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type"
904 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") 904 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write")
905 ->($self, @_); 905 ->($self, @_);
906 } 906 }
907 907
908 # we downgrade here to avoid hard-to-track-down bugs,
909 # and diagnose the problem earlier and better.
910
908 if ($self->{tls}) { 911 if ($self->{tls}) {
909 $self->{_tls_wbuf} .= $_[0]; 912 utf8::downgrade $self->{_tls_wbuf} .= $_[0];
910 &_dotls ($self) if $self->{fh}; 913 &_dotls ($self) if $self->{fh};
911 } else { 914 } else {
912 $self->{wbuf} .= $_[0]; 915 utf8::downgrade $self->{wbuf} .= $_[0];
913 $self->_drain_wbuf if $self->{fh}; 916 $self->_drain_wbuf if $self->{fh};
914 } 917 }
915} 918}
916 919
917=item $handle->push_write (type => @args) 920=item $handle->push_write (type => @args)
1711} 1714}
1712 1715
1713sub start_read { 1716sub start_read {
1714 my ($self) = @_; 1717 my ($self) = @_;
1715 1718
1716 unless ($self->{_rw} || $self->{_eof}) { 1719 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1717 Scalar::Util::weaken $self; 1720 Scalar::Util::weaken $self;
1718 1721
1719 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1722 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1720 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1723 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1721 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1724 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1814 && ($tmp != $ERROR_SYSCALL || $!); 1817 && ($tmp != $ERROR_SYSCALL || $!);
1815 1818
1816 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1819 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1817 $self->{wbuf} .= $tmp; 1820 $self->{wbuf} .= $tmp;
1818 $self->_drain_wbuf; 1821 $self->_drain_wbuf;
1822 $self->{tls} or return; # tls session might have gone away in callback
1819 } 1823 }
1820 1824
1821 $self->{_on_starttls} 1825 $self->{_on_starttls}
1822 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () 1826 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK ()
1823 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); 1827 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established");
1922 1926
1923=item $handle->stoptls 1927=item $handle->stoptls
1924 1928
1925Shuts down the SSL connection - this makes a proper EOF handshake by 1929Shuts down the SSL connection - this makes a proper EOF handshake by
1926sending a close notify to the other side, but since OpenSSL doesn't 1930sending a close notify to the other side, but since OpenSSL doesn't
1927support non-blocking shut downs, it is not guarenteed that you can re-use 1931support non-blocking shut downs, it is not guaranteed that you can re-use
1928the stream afterwards. 1932the stream afterwards.
1929 1933
1930=cut 1934=cut
1931 1935
1932sub stoptls { 1936sub stoptls {
1933 my ($self) = @_; 1937 my ($self) = @_;
1934 1938
1935 if ($self->{tls}) { 1939 if ($self->{tls} && $self->{fh}) {
1936 Net::SSLeay::shutdown ($self->{tls}); 1940 Net::SSLeay::shutdown ($self->{tls});
1937 1941
1938 &_dotls; 1942 &_dotls;
1939 1943
1940# # we don't give a shit. no, we do, but we can't. no...#d# 1944# # we don't give a shit. no, we do, but we can't. no...#d#
2017 2021
2018sub AnyEvent::Handle::destroyed::AUTOLOAD { 2022sub AnyEvent::Handle::destroyed::AUTOLOAD {
2019 #nop 2023 #nop
2020} 2024}
2021 2025
2026=item $handle->destroyed
2027
2028Returns false as long as the handle hasn't been destroyed by a call to C<<
2029->destroy >>, true otherwise.
2030
2031Can be useful to decide whether the handle is still valid after some
2032callback possibly destroyed the handle. For example, C<< ->push_write >>,
2033C<< ->starttls >> and other methods can call user callbacks, which in turn
2034can destroy the handle, so work can be avoided by checking sometimes:
2035
2036 $hdl->starttls ("accept");
2037 return if $hdl->destroyed;
2038 $hdl->push_write (...
2039
2040Note that the call to C<push_write> will silently be ignored if the handle
2041has been destroyed, so often you can just ignore the possibility of the
2042handle being destroyed.
2043
2044=cut
2045
2046sub destroyed { 0 }
2047sub AnyEvent::Handle::destroyed::destroyed { 1 }
2048
2022=item AnyEvent::Handle::TLS_CTX 2049=item AnyEvent::Handle::TLS_CTX
2023 2050
2024This function creates and returns the AnyEvent::TLS object used by default 2051This function creates and returns the AnyEvent::TLS object used by default
2025for TLS mode. 2052for TLS mode.
2026 2053

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines