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.193 by root, Mon Mar 15 18:51:30 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
832=item $handle->on_drain ($cb) 832=item $handle->on_drain ($cb)
833 833
834Sets the C<on_drain> callback or clears it (see the description of 834Sets the C<on_drain> callback or clears it (see the description of
835C<on_drain> in the constructor). 835C<on_drain> in the constructor).
836 836
837This method may invoke callbacks (and therefore the handle might be
838destroyed after it returns).
839
837=cut 840=cut
838 841
839sub on_drain { 842sub on_drain {
840 my ($self, $cb) = @_; 843 my ($self, $cb) = @_;
841 844
848=item $handle->push_write ($data) 851=item $handle->push_write ($data)
849 852
850Queues the given scalar to be written. You can push as much data as you 853Queues the given scalar to be written. You can push as much data as you
851want (only limited by the available memory), as C<AnyEvent::Handle> 854want (only limited by the available memory), as C<AnyEvent::Handle>
852buffers it independently of the kernel. 855buffers it independently of the kernel.
856
857This method may invoke callbacks (and therefore the handle might be
858destroyed after it returns).
853 859
854=cut 860=cut
855 861
856sub _drain_wbuf { 862sub _drain_wbuf {
857 my ($self) = @_; 863 my ($self) = @_;
903 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" 909 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type"
904 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") 910 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write")
905 ->($self, @_); 911 ->($self, @_);
906 } 912 }
907 913
914 # we downgrade here to avoid hard-to-track-down bugs,
915 # and diagnose the problem earlier and better.
916
908 if ($self->{tls}) { 917 if ($self->{tls}) {
909 $self->{_tls_wbuf} .= $_[0]; 918 utf8::downgrade $self->{_tls_wbuf} .= $_[0];
910 &_dotls ($self) if $self->{fh}; 919 &_dotls ($self) if $self->{fh};
911 } else { 920 } else {
912 $self->{wbuf} .= $_[0]; 921 utf8::downgrade $self->{wbuf} .= $_[0];
913 $self->_drain_wbuf if $self->{fh}; 922 $self->_drain_wbuf if $self->{fh};
914 } 923 }
915} 924}
916 925
917=item $handle->push_write (type => @args) 926=item $handle->push_write (type => @args)
1030This simply shuts down the write side and signals an EOF condition to the 1039This simply shuts down the write side and signals an EOF condition to the
1031the peer. 1040the peer.
1032 1041
1033You can rely on the normal read queue and C<on_eof> handling 1042You can rely on the normal read queue and C<on_eof> handling
1034afterwards. This is the cleanest way to close a connection. 1043afterwards. This is the cleanest way to close a connection.
1044
1045This method may invoke callbacks (and therefore the handle might be
1046destroyed after it returns).
1035 1047
1036=cut 1048=cut
1037 1049
1038sub push_shutdown { 1050sub push_shutdown {
1039 my ($self) = @_; 1051 my ($self) = @_;
1236 1248
1237This replaces the currently set C<on_read> callback, or clears it (when 1249This replaces the currently set C<on_read> callback, or clears it (when
1238the new callback is C<undef>). See the description of C<on_read> in the 1250the new callback is C<undef>). See the description of C<on_read> in the
1239constructor. 1251constructor.
1240 1252
1253This method may invoke callbacks (and therefore the handle might be
1254destroyed after it returns).
1255
1241=cut 1256=cut
1242 1257
1243sub on_read { 1258sub on_read {
1244 my ($self, $cb) = @_; 1259 my ($self, $cb) = @_;
1245 1260
1283available (or an error condition is detected). 1298available (or an error condition is detected).
1284 1299
1285If enough data was available, then the callback must remove all data it is 1300If enough data was available, then the callback must remove all data it is
1286interested in (which can be none at all) and return a true value. After returning 1301interested in (which can be none at all) and return a true value. After returning
1287true, it will be removed from the queue. 1302true, it will be removed from the queue.
1303
1304These methods may invoke callbacks (and therefore the handle might be
1305destroyed after it returns).
1288 1306
1289=cut 1307=cut
1290 1308
1291our %RH; 1309our %RH;
1292 1310
1711} 1729}
1712 1730
1713sub start_read { 1731sub start_read {
1714 my ($self) = @_; 1732 my ($self) = @_;
1715 1733
1716 unless ($self->{_rw} || $self->{_eof}) { 1734 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1717 Scalar::Util::weaken $self; 1735 Scalar::Util::weaken $self;
1718 1736
1719 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1737 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1720 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1738 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1721 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1739 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1814 && ($tmp != $ERROR_SYSCALL || $!); 1832 && ($tmp != $ERROR_SYSCALL || $!);
1815 1833
1816 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1834 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1817 $self->{wbuf} .= $tmp; 1835 $self->{wbuf} .= $tmp;
1818 $self->_drain_wbuf; 1836 $self->_drain_wbuf;
1837 $self->{tls} or return; # tls session might have gone away in callback
1819 } 1838 }
1820 1839
1821 $self->{_on_starttls} 1840 $self->{_on_starttls}
1822 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () 1841 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK ()
1823 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); 1842 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established");
1847when this function returns. 1866when this function returns.
1848 1867
1849Due to bugs in OpenSSL, it might or might not be possible to do multiple 1868Due to bugs in OpenSSL, it might or might not be possible to do multiple
1850handshakes on the same stream. Best do not attempt to use the stream after 1869handshakes on the same stream. Best do not attempt to use the stream after
1851stopping TLS. 1870stopping TLS.
1871
1872This method may invoke callbacks (and therefore the handle might be
1873destroyed after it returns).
1852 1874
1853=cut 1875=cut
1854 1876
1855our %TLS_CACHE; #TODO not yet documented, should we? 1877our %TLS_CACHE; #TODO not yet documented, should we?
1856 1878
1922 1944
1923=item $handle->stoptls 1945=item $handle->stoptls
1924 1946
1925Shuts down the SSL connection - this makes a proper EOF handshake by 1947Shuts down the SSL connection - this makes a proper EOF handshake by
1926sending a close notify to the other side, but since OpenSSL doesn't 1948sending 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 1949support non-blocking shut downs, it is not guaranteed that you can re-use
1928the stream afterwards. 1950the stream afterwards.
1951
1952This method may invoke callbacks (and therefore the handle might be
1953destroyed after it returns).
1929 1954
1930=cut 1955=cut
1931 1956
1932sub stoptls { 1957sub stoptls {
1933 my ($self) = @_; 1958 my ($self) = @_;
1934 1959
1935 if ($self->{tls}) { 1960 if ($self->{tls} && $self->{fh}) {
1936 Net::SSLeay::shutdown ($self->{tls}); 1961 Net::SSLeay::shutdown ($self->{tls});
1937 1962
1938 &_dotls; 1963 &_dotls;
1939 1964
1940# # we don't give a shit. no, we do, but we can't. no...#d# 1965# # we don't give a shit. no, we do, but we can't. no...#d#
2017 2042
2018sub AnyEvent::Handle::destroyed::AUTOLOAD { 2043sub AnyEvent::Handle::destroyed::AUTOLOAD {
2019 #nop 2044 #nop
2020} 2045}
2021 2046
2047=item $handle->destroyed
2048
2049Returns false as long as the handle hasn't been destroyed by a call to C<<
2050->destroy >>, true otherwise.
2051
2052Can be useful to decide whether the handle is still valid after some
2053callback possibly destroyed the handle. For example, C<< ->push_write >>,
2054C<< ->starttls >> and other methods can call user callbacks, which in turn
2055can destroy the handle, so work can be avoided by checking sometimes:
2056
2057 $hdl->starttls ("accept");
2058 return if $hdl->destroyed;
2059 $hdl->push_write (...
2060
2061Note that the call to C<push_write> will silently be ignored if the handle
2062has been destroyed, so often you can just ignore the possibility of the
2063handle being destroyed.
2064
2065=cut
2066
2067sub destroyed { 0 }
2068sub AnyEvent::Handle::destroyed::destroyed { 1 }
2069
2022=item AnyEvent::Handle::TLS_CTX 2070=item AnyEvent::Handle::TLS_CTX
2023 2071
2024This function creates and returns the AnyEvent::TLS object used by default 2072This function creates and returns the AnyEvent::TLS object used by default
2025for TLS mode. 2073for TLS mode.
2026 2074

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines