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.181 by root, Tue Sep 1 10:40:05 2009 UTC vs.
Revision 1.196 by root, Tue Jun 8 10:04:17 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;
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
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
60use AnyEvent (); BEGIN { AnyEvent::common_sense } 60use AnyEvent (); BEGIN { AnyEvent::common_sense }
61use AnyEvent::Util qw(WSAEWOULDBLOCK); 61use AnyEvent::Util qw(WSAEWOULDBLOCK);
62 62
63our $VERSION = $AnyEvent::VERSION; 63our $VERSION = $AnyEvent::VERSION;
64 64
65sub _load_func($) {
66 my $func = $_[0];
67
68 unless (defined &$func) {
69 my $pkg = $func;
70 do {
71 $pkg =~ s/::[^:]+$//
72 or return;
73 eval "require $pkg";
74 } until defined &$func;
75 }
76
77 \&$func
78}
79
65=head1 METHODS 80=head1 METHODS
66 81
67=over 4 82=over 4
68 83
69=item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... 84=item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value...
70 85
71The constructor supports these arguments (all as C<< key => value >> pairs). 86The constructor supports these arguments (all as C<< key => value >> pairs).
72 87
73=over 4 88=over 4
74 89
114 129
115The actual numeric host and port (the socket peername) are passed as 130The actual numeric host and port (the socket peername) are passed as
116parameters, together with a retry callback. 131parameters, together with a retry callback.
117 132
118When, for some reason, the handle is not acceptable, then calling 133When, for some reason, the handle is not acceptable, then calling
119C<$retry> will continue with the next conenction target (in case of 134C<$retry> will continue with the next connection target (in case of
120multi-homed hosts or SRV records there can be multiple connection 135multi-homed hosts or SRV records there can be multiple connection
121endpoints). 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
122tls status and similar properties of the handle are being reset. 137status, tls status and similar properties of the handle will have been
138reset.
123 139
124In 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.
125 141
126=item on_connect_error => $cb->($handle, $message) 142=item on_connect_error => $cb->($handle, $message)
127 143
128This callback is called when the conenction could not be 144This callback is called when the connection could not be
129established. C<$!> will contain the relevant error code, and C<$message> a 145established. C<$!> will contain the relevant error code, and C<$message> a
130message describing it (usually the same as C<"$!">). 146message describing it (usually the same as C<"$!">).
131 147
132If 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
133fatal error instead. 149fatal error instead.
285accomplishd by setting this option to a true value. 301accomplishd by setting this option to a true value.
286 302
287The default is your opertaing system's default behaviour (most likely 303The default is your opertaing system's default behaviour (most likely
288enabled), this option explicitly enables or disables it, if possible. 304enabled), this option explicitly enables or disables it, if possible.
289 305
306=item keepalive => <boolean>
307
308Enables (default disable) the SO_KEEPALIVE option on the stream socket:
309normally, TCP connections have no time-out once established, so TCP
310connections, once established, can stay alive forever even when the other
311side has long gone. TCP keepalives are a cheap way to take down long-lived
312TCP connections whent he other side becomes unreachable. While the default
313is OS-dependent, TCP keepalives usually kick in after around two hours,
314and, if the other side doesn't reply, take down the TCP connection some 10
315to 15 minutes later.
316
317It is harmless to specify this option for file handles that do not support
318keepalives, and enabling it on connections that are potentially long-lived
319is usually a good idea.
320
321=item oobinline => <boolean>
322
323BSD majorly fucked up the implementation of TCP urgent data. The result
324is that almost no OS implements TCP according to the specs, and every OS
325implements it slightly differently.
326
327If you want to handle TCP urgent data, then setting this flag (the default
328is enabled) gives you the most portable way of getting urgent data, by
329putting it into the stream.
330
331Since BSD emulation of OOB data on top of TCP's urgent data can have
332security implications, AnyEvent::Handle sets this flag automatically
333unless explicitly specified. Note that setting this flag after
334establishing a connection I<may> be a bit too late (data loss could
335already have occured on BSD systems), but at least it will protect you
336from most attacks.
337
290=item read_size => <bytes> 338=item read_size => <bytes>
291 339
292The default read block size (the amount of bytes this module will 340The default read block size (the amount of bytes this module will
293try to read during each loop iteration, which affects memory 341try to read during each loop iteration, which affects memory
294requirements). Default: C<8192>. 342requirements). Default: C<8192>.
327C<undef>. 375C<undef>.
328 376
329=item tls => "accept" | "connect" | Net::SSLeay::SSL object 377=item tls => "accept" | "connect" | Net::SSLeay::SSL object
330 378
331When this parameter is given, it enables TLS (SSL) mode, that means 379When this parameter is given, it enables TLS (SSL) mode, that means
332AnyEvent 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
333established and will transparently encrypt/decrypt data afterwards. 381established and will transparently encrypt/decrypt data afterwards.
334 382
335All TLS protocol errors will be signalled as C<EPROTO>, with an 383All TLS protocol errors will be signalled as C<EPROTO>, with an
336appropriate error message. 384appropriate error message.
337 385
484} 532}
485 533
486sub _start { 534sub _start {
487 my ($self) = @_; 535 my ($self) = @_;
488 536
537 # too many clueless people try to use udp and similar sockets
538 # with AnyEvent::Handle, do them a favour.
539 my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE ();
540 Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!"
541 if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type;
542
489 AnyEvent::Util::fh_nonblocking $self->{fh}, 1; 543 AnyEvent::Util::fh_nonblocking $self->{fh}, 1;
490 544
491 $self->{_activity} = 545 $self->{_activity} =
492 $self->{_ractivity} = 546 $self->{_ractivity} =
493 $self->{_wactivity} = AE::now; 547 $self->{_wactivity} = AE::now;
494 548
495 $self->timeout (delete $self->{timeout} ) if $self->{timeout}; 549 $self->timeout (delete $self->{timeout} ) if $self->{timeout};
496 $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; 550 $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout};
497 $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; 551 $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout};
498 552
499 $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; 553 $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay};
554 $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive};
500 555
556 $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1);
557
501 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) 558 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx})
502 if $self->{tls}; 559 if $self->{tls};
503 560
504 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 561 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
505 562
506 $self->start_read 563 $self->start_read
507 if $self->{on_read} || @{ $self->{_queue} }; 564 if $self->{on_read} || @{ $self->{_queue} };
508 565
509 $self->_drain_wbuf; 566 $self->_drain_wbuf;
516 $message ||= "$!"; 573 $message ||= "$!";
517 574
518 if ($self->{on_error}) { 575 if ($self->{on_error}) {
519 $self->{on_error}($self, $fatal, $message); 576 $self->{on_error}($self, $fatal, $message);
520 $self->destroy if $fatal; 577 $self->destroy if $fatal;
521 } elsif ($self->{fh}) { 578 } elsif ($self->{fh} || $self->{connect}) {
522 $self->destroy; 579 $self->destroy;
523 Carp::croak "AnyEvent::Handle uncaught error: $message"; 580 Carp::croak "AnyEvent::Handle uncaught error: $message";
524 } 581 }
525} 582}
526 583
587sub no_delay { 644sub no_delay {
588 $_[0]{no_delay} = $_[1]; 645 $_[0]{no_delay} = $_[1];
589 646
590 eval { 647 eval {
591 local $SIG{__DIE__}; 648 local $SIG{__DIE__};
592 setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] 649 setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1]
593 if $_[0]{fh}; 650 if $_[0]{fh};
594 }; 651 };
595} 652}
596 653
654=item $handle->keepalive ($boolean)
655
656Enables or disables the C<keepalive> setting (see constructor argument of
657the same name for details).
658
659=cut
660
661sub keepalive {
662 $_[0]{keepalive} = $_[1];
663
664 eval {
665 local $SIG{__DIE__};
666 setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1]
667 if $_[0]{fh};
668 };
669}
670
671=item $handle->oobinline ($boolean)
672
673Enables or disables the C<oobinline> setting (see constructor argument of
674the same name for details).
675
676=cut
677
678sub oobinline {
679 $_[0]{oobinline} = $_[1];
680
681 eval {
682 local $SIG{__DIE__};
683 setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1]
684 if $_[0]{fh};
685 };
686}
687
688=item $handle->keepalive ($boolean)
689
690Enables or disables the C<keepalive> setting (see constructor argument of
691the same name for details).
692
693=cut
694
695sub keepalive {
696 $_[0]{keepalive} = $_[1];
697
698 eval {
699 local $SIG{__DIE__};
700 setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1]
701 if $_[0]{fh};
702 };
703}
704
597=item $handle->on_starttls ($cb) 705=item $handle->on_starttls ($cb)
598 706
599Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). 707Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument).
600 708
601=cut 709=cut
608 716
609Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). 717Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument).
610 718
611=cut 719=cut
612 720
613sub on_starttls { 721sub on_stoptls {
614 $_[0]{on_stoptls} = $_[1]; 722 $_[0]{on_stoptls} = $_[1];
615} 723}
616 724
617=item $handle->rbuf_max ($max_octets) 725=item $handle->rbuf_max ($max_octets)
618 726
730=item $handle->on_drain ($cb) 838=item $handle->on_drain ($cb)
731 839
732Sets the C<on_drain> callback or clears it (see the description of 840Sets the C<on_drain> callback or clears it (see the description of
733C<on_drain> in the constructor). 841C<on_drain> in the constructor).
734 842
843This method may invoke callbacks (and therefore the handle might be
844destroyed after it returns).
845
735=cut 846=cut
736 847
737sub on_drain { 848sub on_drain {
738 my ($self, $cb) = @_; 849 my ($self, $cb) = @_;
739 850
746=item $handle->push_write ($data) 857=item $handle->push_write ($data)
747 858
748Queues the given scalar to be written. You can push as much data as you 859Queues the given scalar to be written. You can push as much data as you
749want (only limited by the available memory), as C<AnyEvent::Handle> 860want (only limited by the available memory), as C<AnyEvent::Handle>
750buffers it independently of the kernel. 861buffers it independently of the kernel.
862
863This method may invoke callbacks (and therefore the handle might be
864destroyed after it returns).
751 865
752=cut 866=cut
753 867
754sub _drain_wbuf { 868sub _drain_wbuf {
755 my ($self) = @_; 869 my ($self) = @_;
785 }; 899 };
786} 900}
787 901
788our %WH; 902our %WH;
789 903
904# deprecated
790sub register_write_type($$) { 905sub register_write_type($$) {
791 $WH{$_[0]} = $_[1]; 906 $WH{$_[0]} = $_[1];
792} 907}
793 908
794sub push_write { 909sub push_write {
795 my $self = shift; 910 my $self = shift;
796 911
797 if (@_ > 1) { 912 if (@_ > 1) {
798 my $type = shift; 913 my $type = shift;
799 914
915 @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type"
800 @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") 916 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write")
801 ->($self, @_); 917 ->($self, @_);
802 } 918 }
803 919
920 # we downgrade here to avoid hard-to-track-down bugs,
921 # and diagnose the problem earlier and better.
922
804 if ($self->{tls}) { 923 if ($self->{tls}) {
805 $self->{_tls_wbuf} .= $_[0]; 924 utf8::downgrade $self->{_tls_wbuf} .= $_[0];
806 &_dotls ($self) if $self->{fh}; 925 &_dotls ($self) if $self->{fh};
807 } else { 926 } else {
808 $self->{wbuf} .= $_[0]; 927 utf8::downgrade $self->{wbuf} .= $_[0];
809 $self->_drain_wbuf if $self->{fh}; 928 $self->_drain_wbuf if $self->{fh};
810 } 929 }
811} 930}
812 931
813=item $handle->push_write (type => @args) 932=item $handle->push_write (type => @args)
814 933
815Instead of formatting your data yourself, you can also let this module do 934Instead of formatting your data yourself, you can also let this module
816the job by specifying a type and type-specific arguments. 935do the job by specifying a type and type-specific arguments. You
936can also specify the (fully qualified) name of a package, in which
937case AnyEvent tries to load the package and then expects to find the
938C<anyevent_read_type> function inside (see "custom write types", below).
817 939
818Predefined types are (if you have ideas for additional types, feel free to 940Predefined types are (if you have ideas for additional types, feel free to
819drop by and tell us): 941drop by and tell us):
820 942
821=over 4 943=over 4
924the peer. 1046the peer.
925 1047
926You can rely on the normal read queue and C<on_eof> handling 1048You can rely on the normal read queue and C<on_eof> handling
927afterwards. This is the cleanest way to close a connection. 1049afterwards. This is the cleanest way to close a connection.
928 1050
1051This method may invoke callbacks (and therefore the handle might be
1052destroyed after it returns).
1053
929=cut 1054=cut
930 1055
931sub push_shutdown { 1056sub push_shutdown {
932 my ($self) = @_; 1057 my ($self) = @_;
933 1058
934 delete $self->{low_water_mark}; 1059 delete $self->{low_water_mark};
935 $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); 1060 $self->on_drain (sub { shutdown $_[0]{fh}, 1 });
936} 1061}
937 1062
938=item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) 1063=item custom write types - Package::anyevent_write_type $handle, @args
939 1064
940This function (not method) lets you add your own types to C<push_write>. 1065Instead of one of the predefined types, you can also specify the name of
1066a package. AnyEvent will try to load the package and then expects to find
1067a function named C<anyevent_write_type> inside. If it isn't found, it
1068progressively tries to load the parent package until it either finds the
1069function (good) or runs out of packages (bad).
1070
941Whenever the given C<type> is used, C<push_write> will invoke the code 1071Whenever the given C<type> is used, C<push_write> will the function with
942reference with the handle object and the remaining arguments. 1072the handle object and the remaining arguments.
943 1073
944The code reference is supposed to return a single octet string that will 1074The function is supposed to return a single octet string that will be
945be appended to the write buffer. 1075appended to the write buffer, so you cna mentally treat this function as a
1076"arguments to on-the-wire-format" converter.
946 1077
947Note that this is a function, and all types registered this way will be 1078Example: implement a custom write type C<join> that joins the remaining
948global, so try to use unique names. 1079arguments using the first one.
1080
1081 $handle->push_write (My::Type => " ", 1,2,3);
1082
1083 # uses the following package, which can be defined in the "My::Type" or in
1084 # the "My" modules to be auto-loaded, or just about anywhere when the
1085 # My::Type::anyevent_write_type is defined before invoking it.
1086
1087 package My::Type;
1088
1089 sub anyevent_write_type {
1090 my ($handle, $delim, @args) = @_;
1091
1092 join $delim, @args
1093 }
949 1094
950=cut 1095=cut
951 1096
952############################################################################# 1097#############################################################################
953 1098
1109 1254
1110This replaces the currently set C<on_read> callback, or clears it (when 1255This replaces the currently set C<on_read> callback, or clears it (when
1111the new callback is C<undef>). See the description of C<on_read> in the 1256the new callback is C<undef>). See the description of C<on_read> in the
1112constructor. 1257constructor.
1113 1258
1259This method may invoke callbacks (and therefore the handle might be
1260destroyed after it returns).
1261
1114=cut 1262=cut
1115 1263
1116sub on_read { 1264sub on_read {
1117 my ($self, $cb) = @_; 1265 my ($self, $cb) = @_;
1118 1266
1157 1305
1158If enough data was available, then the callback must remove all data it is 1306If enough data was available, then the callback must remove all data it is
1159interested in (which can be none at all) and return a true value. After returning 1307interested in (which can be none at all) and return a true value. After returning
1160true, it will be removed from the queue. 1308true, it will be removed from the queue.
1161 1309
1310These methods may invoke callbacks (and therefore the handle might be
1311destroyed after it returns).
1312
1162=cut 1313=cut
1163 1314
1164our %RH; 1315our %RH;
1165 1316
1166sub register_read_type($$) { 1317sub register_read_type($$) {
1172 my $cb = pop; 1323 my $cb = pop;
1173 1324
1174 if (@_) { 1325 if (@_) {
1175 my $type = shift; 1326 my $type = shift;
1176 1327
1328 $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type"
1177 $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") 1329 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read")
1178 ->($self, $cb, @_); 1330 ->($self, $cb, @_);
1179 } 1331 }
1180 1332
1181 push @{ $self->{_queue} }, $cb; 1333 push @{ $self->{_queue} }, $cb;
1182 $self->_drain_rbuf; 1334 $self->_drain_rbuf;
1201 1353
1202=item $handle->unshift_read (type => @args, $cb) 1354=item $handle->unshift_read (type => @args, $cb)
1203 1355
1204Instead of providing a callback that parses the data itself you can chose 1356Instead of providing a callback that parses the data itself you can chose
1205between a number of predefined parsing formats, for chunks of data, lines 1357between a number of predefined parsing formats, for chunks of data, lines
1206etc. 1358etc. You can also specify the (fully qualified) name of a package, in
1359which case AnyEvent tries to load the package and then expects to find the
1360C<anyevent_read_type> function inside (see "custom read types", below).
1207 1361
1208Predefined types are (if you have ideas for additional types, feel free to 1362Predefined types are (if you have ideas for additional types, feel free to
1209drop by and tell us): 1363drop by and tell us):
1210 1364
1211=over 4 1365=over 4
1530 } 1684 }
1531}; 1685};
1532 1686
1533=back 1687=back
1534 1688
1535=item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) 1689=item custom read types - Package::anyevent_read_type $handle, $cb, @args
1536 1690
1537This function (not method) lets you add your own types to C<push_read>. 1691Instead of one of the predefined types, you can also specify the name
1692of a package. AnyEvent will try to load the package and then expects to
1693find a function named C<anyevent_read_type> inside. If it isn't found, it
1694progressively tries to load the parent package until it either finds the
1695function (good) or runs out of packages (bad).
1538 1696
1539Whenever the given C<type> is used, C<push_read> will invoke the code 1697Whenever this type is used, C<push_read> will invoke the function with the
1540reference with the handle object, the callback and the remaining 1698handle object, the original callback and the remaining arguments.
1541arguments.
1542 1699
1543The code reference is supposed to return a callback (usually a closure) 1700The function is supposed to return a callback (usually a closure) that
1544that works as a plain read callback (see C<< ->push_read ($cb) >>). 1701works as a plain read callback (see C<< ->push_read ($cb) >>), so you can
1702mentally treat the function as a "configurable read type to read callback"
1703converter.
1545 1704
1546It should invoke the passed callback when it is done reading (remember to 1705It should invoke the original callback when it is done reading (remember
1547pass C<$handle> as first argument as all other callbacks do that). 1706to pass C<$handle> as first argument as all other callbacks do that,
1707although there is no strict requirement on this).
1548 1708
1549Note that this is a function, and all types registered this way will be
1550global, so try to use unique names.
1551
1552For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, 1709For examples, see the source of this module (F<perldoc -m
1553search for C<register_read_type>)). 1710AnyEvent::Handle>, search for C<register_read_type>)).
1554 1711
1555=item $handle->stop_read 1712=item $handle->stop_read
1556 1713
1557=item $handle->start_read 1714=item $handle->start_read
1558 1715
1578} 1735}
1579 1736
1580sub start_read { 1737sub start_read {
1581 my ($self) = @_; 1738 my ($self) = @_;
1582 1739
1583 unless ($self->{_rw} || $self->{_eof}) { 1740 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1584 Scalar::Util::weaken $self; 1741 Scalar::Util::weaken $self;
1585 1742
1586 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1743 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1587 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1744 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1588 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1745 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1681 && ($tmp != $ERROR_SYSCALL || $!); 1838 && ($tmp != $ERROR_SYSCALL || $!);
1682 1839
1683 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1840 while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1684 $self->{wbuf} .= $tmp; 1841 $self->{wbuf} .= $tmp;
1685 $self->_drain_wbuf; 1842 $self->_drain_wbuf;
1843 $self->{tls} or return; # tls session might have gone away in callback
1686 } 1844 }
1687 1845
1688 $self->{_on_starttls} 1846 $self->{_on_starttls}
1689 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () 1847 and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK ()
1690 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); 1848 and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established");
1714when this function returns. 1872when this function returns.
1715 1873
1716Due to bugs in OpenSSL, it might or might not be possible to do multiple 1874Due to bugs in OpenSSL, it might or might not be possible to do multiple
1717handshakes on the same stream. Best do not attempt to use the stream after 1875handshakes on the same stream. Best do not attempt to use the stream after
1718stopping TLS. 1876stopping TLS.
1877
1878This method may invoke callbacks (and therefore the handle might be
1879destroyed after it returns).
1719 1880
1720=cut 1881=cut
1721 1882
1722our %TLS_CACHE; #TODO not yet documented, should we? 1883our %TLS_CACHE; #TODO not yet documented, should we?
1723 1884
1789 1950
1790=item $handle->stoptls 1951=item $handle->stoptls
1791 1952
1792Shuts down the SSL connection - this makes a proper EOF handshake by 1953Shuts down the SSL connection - this makes a proper EOF handshake by
1793sending a close notify to the other side, but since OpenSSL doesn't 1954sending a close notify to the other side, but since OpenSSL doesn't
1794support non-blocking shut downs, it is not guarenteed that you can re-use 1955support non-blocking shut downs, it is not guaranteed that you can re-use
1795the stream afterwards. 1956the stream afterwards.
1957
1958This method may invoke callbacks (and therefore the handle might be
1959destroyed after it returns).
1796 1960
1797=cut 1961=cut
1798 1962
1799sub stoptls { 1963sub stoptls {
1800 my ($self) = @_; 1964 my ($self) = @_;
1801 1965
1802 if ($self->{tls}) { 1966 if ($self->{tls} && $self->{fh}) {
1803 Net::SSLeay::shutdown ($self->{tls}); 1967 Net::SSLeay::shutdown ($self->{tls});
1804 1968
1805 &_dotls; 1969 &_dotls;
1806 1970
1807# # we don't give a shit. no, we do, but we can't. no...#d# 1971# # we don't give a shit. no, we do, but we can't. no...#d#
1884 2048
1885sub AnyEvent::Handle::destroyed::AUTOLOAD { 2049sub AnyEvent::Handle::destroyed::AUTOLOAD {
1886 #nop 2050 #nop
1887} 2051}
1888 2052
2053=item $handle->destroyed
2054
2055Returns false as long as the handle hasn't been destroyed by a call to C<<
2056->destroy >>, true otherwise.
2057
2058Can be useful to decide whether the handle is still valid after some
2059callback possibly destroyed the handle. For example, C<< ->push_write >>,
2060C<< ->starttls >> and other methods can call user callbacks, which in turn
2061can destroy the handle, so work can be avoided by checking sometimes:
2062
2063 $hdl->starttls ("accept");
2064 return if $hdl->destroyed;
2065 $hdl->push_write (...
2066
2067Note that the call to C<push_write> will silently be ignored if the handle
2068has been destroyed, so often you can just ignore the possibility of the
2069handle being destroyed.
2070
2071=cut
2072
2073sub destroyed { 0 }
2074sub AnyEvent::Handle::destroyed::destroyed { 1 }
2075
1889=item AnyEvent::Handle::TLS_CTX 2076=item AnyEvent::Handle::TLS_CTX
1890 2077
1891This function creates and returns the AnyEvent::TLS object used by default 2078This function creates and returns the AnyEvent::TLS object used by default
1892for TLS mode. 2079for TLS mode.
1893 2080

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines