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.185 by root, Thu Sep 3 19:48:27 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
129 129
130The actual numeric host and port (the socket peername) are passed as 130The actual numeric host and port (the socket peername) are passed as
131parameters, together with a retry callback. 131parameters, together with a retry callback.
132 132
133When, for some reason, the handle is not acceptable, then calling 133When, for some reason, the handle is not acceptable, then calling
134C<$retry> will continue with the next conenction target (in case of 134C<$retry> will continue with the next connection target (in case of
135multi-homed hosts or SRV records there can be multiple connection 135multi-homed hosts or SRV records there can be multiple connection
136endpoints). When it is called then the read and write queues, eof status, 136endpoints). At the time it is called the read and write queues, eof
137tls status and similar properties of the handle are being reset. 137status, tls status and similar properties of the handle will have been
138reset.
138 139
139In most cases, ignoring the C<$retry> parameter is the way to go. 140In most cases, ignoring the C<$retry> parameter is the way to go.
140 141
141=item on_connect_error => $cb->($handle, $message) 142=item on_connect_error => $cb->($handle, $message)
142 143
143This callback is called when the conenction could not be 144This callback is called when the connection could not be
144established. C<$!> will contain the relevant error code, and C<$message> a 145established. C<$!> will contain the relevant error code, and C<$message> a
145message describing it (usually the same as C<"$!">). 146message describing it (usually the same as C<"$!">).
146 147
147If this callback isn't specified, then C<on_error> will be called with a 148If this callback isn't specified, then C<on_error> will be called with a
148fatal error instead. 149fatal error instead.
304 305
305=item keepalive => <boolean> 306=item keepalive => <boolean>
306 307
307Enables (default disable) the SO_KEEPALIVE option on the stream socket: 308Enables (default disable) the SO_KEEPALIVE option on the stream socket:
308normally, TCP connections have no time-out once established, so TCP 309normally, TCP connections have no time-out once established, so TCP
309conenctions, once established, can stay alive forever even when the other 310connections, once established, can stay alive forever even when the other
310side has long gone. TCP keepalives are a cheap way to take down long-lived 311side has long gone. TCP keepalives are a cheap way to take down long-lived
311TCP connections whent he other side becomes unreachable. While the default 312TCP connections whent he other side becomes unreachable. While the default
312is OS-dependent, TCP keepalives usually kick in after around two hours, 313is OS-dependent, TCP keepalives usually kick in after around two hours,
313and, if the other side doesn't reply, take down the TCP connection some 10 314and, if the other side doesn't reply, take down the TCP connection some 10
314to 15 minutes later. 315to 15 minutes later.
374C<undef>. 375C<undef>.
375 376
376=item tls => "accept" | "connect" | Net::SSLeay::SSL object 377=item tls => "accept" | "connect" | Net::SSLeay::SSL object
377 378
378When this parameter is given, it enables TLS (SSL) mode, that means 379When this parameter is given, it enables TLS (SSL) mode, that means
379AnyEvent will start a TLS handshake as soon as the conenction has been 380AnyEvent will start a TLS handshake as soon as the connection has been
380established and will transparently encrypt/decrypt data afterwards. 381established and will transparently encrypt/decrypt data afterwards.
381 382
382All TLS protocol errors will be signalled as C<EPROTO>, with an 383All TLS protocol errors will be signalled as C<EPROTO>, with an
383appropriate error message. 384appropriate error message.
384 385
566 $message ||= "$!"; 567 $message ||= "$!";
567 568
568 if ($self->{on_error}) { 569 if ($self->{on_error}) {
569 $self->{on_error}($self, $fatal, $message); 570 $self->{on_error}($self, $fatal, $message);
570 $self->destroy if $fatal; 571 $self->destroy if $fatal;
571 } elsif ($self->{fh}) { 572 } elsif ($self->{fh} || $self->{connect}) {
572 $self->destroy; 573 $self->destroy;
573 Carp::croak "AnyEvent::Handle uncaught error: $message"; 574 Carp::croak "AnyEvent::Handle uncaught error: $message";
574 } 575 }
575} 576}
576 577
709 710
710Replace 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).
711 712
712=cut 713=cut
713 714
714sub on_starttls { 715sub on_stoptls {
715 $_[0]{on_stoptls} = $_[1]; 716 $_[0]{on_stoptls} = $_[1];
716} 717}
717 718
718=item $handle->rbuf_max ($max_octets) 719=item $handle->rbuf_max ($max_octets)
719 720
831=item $handle->on_drain ($cb) 832=item $handle->on_drain ($cb)
832 833
833Sets 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
834C<on_drain> in the constructor). 835C<on_drain> in the constructor).
835 836
837This method may invoke callbacks (and therefore the handle might be
838destroyed after it returns).
839
836=cut 840=cut
837 841
838sub on_drain { 842sub on_drain {
839 my ($self, $cb) = @_; 843 my ($self, $cb) = @_;
840 844
847=item $handle->push_write ($data) 851=item $handle->push_write ($data)
848 852
849Queues 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
850want (only limited by the available memory), as C<AnyEvent::Handle> 854want (only limited by the available memory), as C<AnyEvent::Handle>
851buffers 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).
852 859
853=cut 860=cut
854 861
855sub _drain_wbuf { 862sub _drain_wbuf {
856 my ($self) = @_; 863 my ($self) = @_;
902 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" 909 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type"
903 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")
904 ->($self, @_); 911 ->($self, @_);
905 } 912 }
906 913
914 # we downgrade here to avoid hard-to-track-down bugs,
915 # and diagnose the problem earlier and better.
916
907 if ($self->{tls}) { 917 if ($self->{tls}) {
908 $self->{_tls_wbuf} .= $_[0]; 918 utf8::downgrade $self->{_tls_wbuf} .= $_[0];
909 &_dotls ($self) if $self->{fh}; 919 &_dotls ($self) if $self->{fh};
910 } else { 920 } else {
911 $self->{wbuf} .= $_[0]; 921 utf8::downgrade $self->{wbuf} .= $_[0];
912 $self->_drain_wbuf if $self->{fh}; 922 $self->_drain_wbuf if $self->{fh};
913 } 923 }
914} 924}
915 925
916=item $handle->push_write (type => @args) 926=item $handle->push_write (type => @args)
1029This 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
1030the peer. 1040the peer.
1031 1041
1032You 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
1033afterwards. 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).
1034 1047
1035=cut 1048=cut
1036 1049
1037sub push_shutdown { 1050sub push_shutdown {
1038 my ($self) = @_; 1051 my ($self) = @_;
1235 1248
1236This 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
1237the 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
1238constructor. 1251constructor.
1239 1252
1253This method may invoke callbacks (and therefore the handle might be
1254destroyed after it returns).
1255
1240=cut 1256=cut
1241 1257
1242sub on_read { 1258sub on_read {
1243 my ($self, $cb) = @_; 1259 my ($self, $cb) = @_;
1244 1260
1282available (or an error condition is detected). 1298available (or an error condition is detected).
1283 1299
1284If 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
1285interested 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
1286true, 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).
1287 1306
1288=cut 1307=cut
1289 1308
1290our %RH; 1309our %RH;
1291 1310
1710} 1729}
1711 1730
1712sub start_read { 1731sub start_read {
1713 my ($self) = @_; 1732 my ($self) = @_;
1714 1733
1715 unless ($self->{_rw} || $self->{_eof}) { 1734 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1716 Scalar::Util::weaken $self; 1735 Scalar::Util::weaken $self;
1717 1736
1718 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1737 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1719 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1738 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1720 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;
1813 && ($tmp != $ERROR_SYSCALL || $!); 1832 && ($tmp != $ERROR_SYSCALL || $!);
1814 1833
1815 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1834 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1816 $self->{wbuf} .= $tmp; 1835 $self->{wbuf} .= $tmp;
1817 $self->_drain_wbuf; 1836 $self->_drain_wbuf;
1837 $self->{tls} or return; # tls session might have gone away in callback
1818 } 1838 }
1819 1839
1820 $self->{_on_starttls} 1840 $self->{_on_starttls}
1821 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () 1841 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK ()
1822 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); 1842 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established");
1846when this function returns. 1866when this function returns.
1847 1867
1848Due 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
1849handshakes 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
1850stopping TLS. 1870stopping TLS.
1871
1872This method may invoke callbacks (and therefore the handle might be
1873destroyed after it returns).
1851 1874
1852=cut 1875=cut
1853 1876
1854our %TLS_CACHE; #TODO not yet documented, should we? 1877our %TLS_CACHE; #TODO not yet documented, should we?
1855 1878
1921 1944
1922=item $handle->stoptls 1945=item $handle->stoptls
1923 1946
1924Shuts down the SSL connection - this makes a proper EOF handshake by 1947Shuts down the SSL connection - this makes a proper EOF handshake by
1925sending 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
1926support 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
1927the stream afterwards. 1950the stream afterwards.
1951
1952This method may invoke callbacks (and therefore the handle might be
1953destroyed after it returns).
1928 1954
1929=cut 1955=cut
1930 1956
1931sub stoptls { 1957sub stoptls {
1932 my ($self) = @_; 1958 my ($self) = @_;
1933 1959
1934 if ($self->{tls}) { 1960 if ($self->{tls} && $self->{fh}) {
1935 Net::SSLeay::shutdown ($self->{tls}); 1961 Net::SSLeay::shutdown ($self->{tls});
1936 1962
1937 &_dotls; 1963 &_dotls;
1938 1964
1939# # 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#
2016 2042
2017sub AnyEvent::Handle::destroyed::AUTOLOAD { 2043sub AnyEvent::Handle::destroyed::AUTOLOAD {
2018 #nop 2044 #nop
2019} 2045}
2020 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
2021=item AnyEvent::Handle::TLS_CTX 2070=item AnyEvent::Handle::TLS_CTX
2022 2071
2023This function creates and returns the AnyEvent::TLS object used by default 2072This function creates and returns the AnyEvent::TLS object used by default
2024for TLS mode. 2073for TLS mode.
2025 2074

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines