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.188 by root, Thu Sep 17 08:20:14 2009 UTC vs.
Revision 1.197 by root, Tue Aug 31 00:59:55 2010 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 3AnyEvent::Handle - non-blocking I/O on streaming handles via AnyEvent
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use AnyEvent; 7 use AnyEvent;
8 use AnyEvent::Handle; 8 use AnyEvent::Handle;
31 $cv->recv; 31 $cv->recv;
32 32
33=head1 DESCRIPTION 33=head1 DESCRIPTION
34 34
35This module is a helper module to make it easier to do event-based I/O on 35This module is a helper module to make it easier to do event-based I/O on
36filehandles. 36stream-based filehandles (sockets, pipes or other stream things).
37 37
38The L<AnyEvent::Intro> tutorial contains some well-documented 38The L<AnyEvent::Intro> tutorial contains some well-documented
39AnyEvent::Handle examples. 39AnyEvent::Handle examples.
40 40
41In the following, when the documentation refers to of "bytes" then this 41In the following, when the documentation refers to of "bytes" then this
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
191 191
192To access (and remove data from) the read buffer, use the C<< ->rbuf >> 192To access (and remove data from) the read buffer, use the C<< ->rbuf >>
193method or access the C<< $handle->{rbuf} >> member directly. Note that you 193method or access the C<< $handle->{rbuf} >> member directly. Note that you
194must not enlarge or modify the read buffer, you can only remove data at 194must not enlarge or modify the read buffer, you can only remove data at
195the beginning from it. 195the beginning from it.
196
197You can also call C<< ->push_read (...) >> or any other function that
198modifies the read queue. Or do both. Or ...
196 199
197When an EOF condition is detected then AnyEvent::Handle will first try to 200When an EOF condition is detected then AnyEvent::Handle will first try to
198feed all the remaining data to the queued callbacks and C<on_read> before 201feed all the remaining data to the queued callbacks and C<on_read> before
199calling the C<on_eof> callback. If no progress can be made, then a fatal 202calling the C<on_eof> callback. If no progress can be made, then a fatal
200error will be raised (with C<$!> set to C<EPIPE>). 203error will be raised (with C<$!> set to C<EPIPE>).
532} 535}
533 536
534sub _start { 537sub _start {
535 my ($self) = @_; 538 my ($self) = @_;
536 539
540 # too many clueless people try to use udp and similar sockets
541 # with AnyEvent::Handle, do them a favour.
542 my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE ();
543 Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!"
544 if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type;
545
537 AnyEvent::Util::fh_nonblocking $self->{fh}, 1; 546 AnyEvent::Util::fh_nonblocking $self->{fh}, 1;
538 547
539 $self->{_activity} = 548 $self->{_activity} =
540 $self->{_ractivity} = 549 $self->{_ractivity} =
541 $self->{_wactivity} = AE::now; 550 $self->{_wactivity} = AE::now;
710 719
711Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). 720Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument).
712 721
713=cut 722=cut
714 723
715sub on_starttls { 724sub on_stoptls {
716 $_[0]{on_stoptls} = $_[1]; 725 $_[0]{on_stoptls} = $_[1];
717} 726}
718 727
719=item $handle->rbuf_max ($max_octets) 728=item $handle->rbuf_max ($max_octets)
720 729
832=item $handle->on_drain ($cb) 841=item $handle->on_drain ($cb)
833 842
834Sets the C<on_drain> callback or clears it (see the description of 843Sets the C<on_drain> callback or clears it (see the description of
835C<on_drain> in the constructor). 844C<on_drain> in the constructor).
836 845
846This method may invoke callbacks (and therefore the handle might be
847destroyed after it returns).
848
837=cut 849=cut
838 850
839sub on_drain { 851sub on_drain {
840 my ($self, $cb) = @_; 852 my ($self, $cb) = @_;
841 853
848=item $handle->push_write ($data) 860=item $handle->push_write ($data)
849 861
850Queues the given scalar to be written. You can push as much data as you 862Queues 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> 863want (only limited by the available memory), as C<AnyEvent::Handle>
852buffers it independently of the kernel. 864buffers it independently of the kernel.
865
866This method may invoke callbacks (and therefore the handle might be
867destroyed after it returns).
853 868
854=cut 869=cut
855 870
856sub _drain_wbuf { 871sub _drain_wbuf {
857 my ($self) = @_; 872 my ($self) = @_;
903 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" 918 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type"
904 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") 919 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write")
905 ->($self, @_); 920 ->($self, @_);
906 } 921 }
907 922
923 # we downgrade here to avoid hard-to-track-down bugs,
924 # and diagnose the problem earlier and better.
925
908 if ($self->{tls}) { 926 if ($self->{tls}) {
909 $self->{_tls_wbuf} .= $_[0]; 927 utf8::downgrade $self->{_tls_wbuf} .= $_[0];
910 &_dotls ($self) if $self->{fh}; 928 &_dotls ($self) if $self->{fh};
911 } else { 929 } else {
912 $self->{wbuf} .= $_[0]; 930 utf8::downgrade $self->{wbuf} .= $_[0];
913 $self->_drain_wbuf if $self->{fh}; 931 $self->_drain_wbuf if $self->{fh};
914 } 932 }
915} 933}
916 934
917=item $handle->push_write (type => @args) 935=item $handle->push_write (type => @args)
918 936
919Instead of formatting your data yourself, you can also let this module 937Instead of formatting your data yourself, you can also let this module
920do the job by specifying a type and type-specific arguments. You 938do the job by specifying a type and type-specific arguments. You
921can also specify the (fully qualified) name of a package, in which 939can also specify the (fully qualified) name of a package, in which
922case AnyEvent tries to load the package and then expects to find the 940case AnyEvent tries to load the package and then expects to find the
923C<anyevent_read_type> function inside (see "custom write types", below). 941C<anyevent_write_type> function inside (see "custom write types", below).
924 942
925Predefined types are (if you have ideas for additional types, feel free to 943Predefined types are (if you have ideas for additional types, feel free to
926drop by and tell us): 944drop by and tell us):
927 945
928=over 4 946=over 4
1031the peer. 1049the peer.
1032 1050
1033You can rely on the normal read queue and C<on_eof> handling 1051You can rely on the normal read queue and C<on_eof> handling
1034afterwards. This is the cleanest way to close a connection. 1052afterwards. This is the cleanest way to close a connection.
1035 1053
1054This method may invoke callbacks (and therefore the handle might be
1055destroyed after it returns).
1056
1036=cut 1057=cut
1037 1058
1038sub push_shutdown { 1059sub push_shutdown {
1039 my ($self) = @_; 1060 my ($self) = @_;
1040 1061
1089ways, the "simple" way, using only C<on_read> and the "complex" way, using 1110ways, the "simple" way, using only C<on_read> and the "complex" way, using
1090a queue. 1111a queue.
1091 1112
1092In the simple case, you just install an C<on_read> callback and whenever 1113In the simple case, you just install an C<on_read> callback and whenever
1093new data arrives, it will be called. You can then remove some data (if 1114new data arrives, it will be called. You can then remove some data (if
1094enough is there) from the read buffer (C<< $handle->rbuf >>). Or you cna 1115enough is there) from the read buffer (C<< $handle->rbuf >>). Or you can
1095leave the data there if you want to accumulate more (e.g. when only a 1116leave the data there if you want to accumulate more (e.g. when only a
1096partial message has been received so far). 1117partial message has been received so far), or change the read queue with
1118e.g. C<push_read>.
1097 1119
1098In the more complex case, you want to queue multiple callbacks. In this 1120In the more complex case, you want to queue multiple callbacks. In this
1099case, AnyEvent::Handle will call the first queued callback each time new 1121case, AnyEvent::Handle will call the first queued callback each time new
1100data arrives (also the first time it is queued) and removes it when it has 1122data arrives (also the first time it is queued) and removes it when it has
1101done its job (see C<push_read>, below). 1123done its job (see C<push_read>, below).
1236 1258
1237This replaces the currently set C<on_read> callback, or clears it (when 1259This 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 1260the new callback is C<undef>). See the description of C<on_read> in the
1239constructor. 1261constructor.
1240 1262
1263This method may invoke callbacks (and therefore the handle might be
1264destroyed after it returns).
1265
1241=cut 1266=cut
1242 1267
1243sub on_read { 1268sub on_read {
1244 my ($self, $cb) = @_; 1269 my ($self, $cb) = @_;
1245 1270
1283available (or an error condition is detected). 1308available (or an error condition is detected).
1284 1309
1285If enough data was available, then the callback must remove all data it is 1310If 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 1311interested in (which can be none at all) and return a true value. After returning
1287true, it will be removed from the queue. 1312true, it will be removed from the queue.
1313
1314These methods may invoke callbacks (and therefore the handle might be
1315destroyed after it returns).
1288 1316
1289=cut 1317=cut
1290 1318
1291our %RH; 1319our %RH;
1292 1320
1711} 1739}
1712 1740
1713sub start_read { 1741sub start_read {
1714 my ($self) = @_; 1742 my ($self) = @_;
1715 1743
1716 unless ($self->{_rw} || $self->{_eof}) { 1744 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1717 Scalar::Util::weaken $self; 1745 Scalar::Util::weaken $self;
1718 1746
1719 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1747 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1720 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1748 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1721 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1749 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1814 && ($tmp != $ERROR_SYSCALL || $!); 1842 && ($tmp != $ERROR_SYSCALL || $!);
1815 1843
1816 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1844 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1817 $self->{wbuf} .= $tmp; 1845 $self->{wbuf} .= $tmp;
1818 $self->_drain_wbuf; 1846 $self->_drain_wbuf;
1847 $self->{tls} or return; # tls session might have gone away in callback
1819 } 1848 }
1820 1849
1821 $self->{_on_starttls} 1850 $self->{_on_starttls}
1822 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () 1851 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK ()
1823 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); 1852 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established");
1847when this function returns. 1876when this function returns.
1848 1877
1849Due to bugs in OpenSSL, it might or might not be possible to do multiple 1878Due 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 1879handshakes on the same stream. Best do not attempt to use the stream after
1851stopping TLS. 1880stopping TLS.
1881
1882This method may invoke callbacks (and therefore the handle might be
1883destroyed after it returns).
1852 1884
1853=cut 1885=cut
1854 1886
1855our %TLS_CACHE; #TODO not yet documented, should we? 1887our %TLS_CACHE; #TODO not yet documented, should we?
1856 1888
1922 1954
1923=item $handle->stoptls 1955=item $handle->stoptls
1924 1956
1925Shuts down the SSL connection - this makes a proper EOF handshake by 1957Shuts down the SSL connection - this makes a proper EOF handshake by
1926sending a close notify to the other side, but since OpenSSL doesn't 1958sending 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 1959support non-blocking shut downs, it is not guaranteed that you can re-use
1928the stream afterwards. 1960the stream afterwards.
1961
1962This method may invoke callbacks (and therefore the handle might be
1963destroyed after it returns).
1929 1964
1930=cut 1965=cut
1931 1966
1932sub stoptls { 1967sub stoptls {
1933 my ($self) = @_; 1968 my ($self) = @_;
1934 1969
1935 if ($self->{tls}) { 1970 if ($self->{tls} && $self->{fh}) {
1936 Net::SSLeay::shutdown ($self->{tls}); 1971 Net::SSLeay::shutdown ($self->{tls});
1937 1972
1938 &_dotls; 1973 &_dotls;
1939 1974
1940# # we don't give a shit. no, we do, but we can't. no...#d# 1975# # we don't give a shit. no, we do, but we can't. no...#d#
2017 2052
2018sub AnyEvent::Handle::destroyed::AUTOLOAD { 2053sub AnyEvent::Handle::destroyed::AUTOLOAD {
2019 #nop 2054 #nop
2020} 2055}
2021 2056
2057=item $handle->destroyed
2058
2059Returns false as long as the handle hasn't been destroyed by a call to C<<
2060->destroy >>, true otherwise.
2061
2062Can be useful to decide whether the handle is still valid after some
2063callback possibly destroyed the handle. For example, C<< ->push_write >>,
2064C<< ->starttls >> and other methods can call user callbacks, which in turn
2065can destroy the handle, so work can be avoided by checking sometimes:
2066
2067 $hdl->starttls ("accept");
2068 return if $hdl->destroyed;
2069 $hdl->push_write (...
2070
2071Note that the call to C<push_write> will silently be ignored if the handle
2072has been destroyed, so often you can just ignore the possibility of the
2073handle being destroyed.
2074
2075=cut
2076
2077sub destroyed { 0 }
2078sub AnyEvent::Handle::destroyed::destroyed { 1 }
2079
2022=item AnyEvent::Handle::TLS_CTX 2080=item AnyEvent::Handle::TLS_CTX
2023 2081
2024This function creates and returns the AnyEvent::TLS object used by default 2082This function creates and returns the AnyEvent::TLS object used by default
2025for TLS mode. 2083for TLS mode.
2026 2084

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines