1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::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 | |
35 | This module is a helper module to make it easier to do event-based I/O on |
35 | This module is a helper module to make it easier to do event-based I/O on |
36 | filehandles. |
36 | stream-based filehandles (sockets, pipes or other stream things). |
37 | |
37 | |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
39 | AnyEvent::Handle examples. |
39 | AnyEvent::Handle examples. |
40 | |
40 | |
41 | In the following, when the documentation refers to of "bytes" then this |
41 | In the following, when the documentation refers to of "bytes" then this |
… | |
… | |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
62 | |
62 | |
63 | our $VERSION = $AnyEvent::VERSION; |
63 | our $VERSION = $AnyEvent::VERSION; |
64 | |
64 | |
|
|
65 | sub _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 | |
71 | The constructor supports these arguments (all as C<< key => value >> pairs). |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
72 | |
87 | |
73 | =over 4 |
88 | =over 4 |
74 | |
89 | |
… | |
… | |
114 | |
129 | |
115 | The actual numeric host and port (the socket peername) are passed as |
130 | The actual numeric host and port (the socket peername) are passed as |
116 | parameters, together with a retry callback. |
131 | parameters, together with a retry callback. |
117 | |
132 | |
118 | When, for some reason, the handle is not acceptable, then calling |
133 | When, for some reason, the handle is not acceptable, then calling |
119 | C<$retry> will continue with the next conenction target (in case of |
134 | C<$retry> will continue with the next connection target (in case of |
120 | multi-homed hosts or SRV records there can be multiple connection |
135 | multi-homed hosts or SRV records there can be multiple connection |
121 | endpoints). When it is called then the read and write queues, eof status, |
136 | endpoints). At the time it is called the read and write queues, eof |
122 | tls status and similar properties of the handle are being reset. |
137 | status, tls status and similar properties of the handle will have been |
|
|
138 | reset. |
123 | |
139 | |
124 | In most cases, ignoring the C<$retry> parameter is the way to go. |
140 | In 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 | |
128 | This callback is called when the conenction could not be |
144 | This callback is called when the connection could not be |
129 | established. C<$!> will contain the relevant error code, and C<$message> a |
145 | established. C<$!> will contain the relevant error code, and C<$message> a |
130 | message describing it (usually the same as C<"$!">). |
146 | message describing it (usually the same as C<"$!">). |
131 | |
147 | |
132 | If this callback isn't specified, then C<on_error> will be called with a |
148 | If this callback isn't specified, then C<on_error> will be called with a |
133 | fatal error instead. |
149 | fatal error instead. |
… | |
… | |
176 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
192 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
177 | method or access the C<< $handle->{rbuf} >> member directly. Note that you |
193 | method or access the C<< $handle->{rbuf} >> member directly. Note that you |
178 | must not enlarge or modify the read buffer, you can only remove data at |
194 | must not enlarge or modify the read buffer, you can only remove data at |
179 | the beginning from it. |
195 | the beginning from it. |
180 | |
196 | |
|
|
197 | You can also call C<< ->push_read (...) >> or any other function that |
|
|
198 | modifies the read queue. Or do both. Or ... |
|
|
199 | |
181 | When an EOF condition is detected then AnyEvent::Handle will first try to |
200 | When an EOF condition is detected then AnyEvent::Handle will first try to |
182 | feed all the remaining data to the queued callbacks and C<on_read> before |
201 | feed all the remaining data to the queued callbacks and C<on_read> before |
183 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
202 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
184 | error will be raised (with C<$!> set to C<EPIPE>). |
203 | error will be raised (with C<$!> set to C<EPIPE>). |
185 | |
204 | |
… | |
… | |
289 | |
308 | |
290 | =item keepalive => <boolean> |
309 | =item keepalive => <boolean> |
291 | |
310 | |
292 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
311 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
293 | normally, TCP connections have no time-out once established, so TCP |
312 | normally, TCP connections have no time-out once established, so TCP |
294 | conenctions, once established, can stay alive forever even when the other |
313 | connections, once established, can stay alive forever even when the other |
295 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
314 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
296 | TCP connections whent he other side becomes unreachable. While the default |
315 | TCP connections whent he other side becomes unreachable. While the default |
297 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
316 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
298 | and, if the other side doesn't reply, take down the TCP connection some 10 |
317 | and, if the other side doesn't reply, take down the TCP connection some 10 |
299 | to 15 minutes later. |
318 | to 15 minutes later. |
… | |
… | |
312 | is enabled) gives you the most portable way of getting urgent data, by |
331 | is enabled) gives you the most portable way of getting urgent data, by |
313 | putting it into the stream. |
332 | putting it into the stream. |
314 | |
333 | |
315 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
334 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
316 | security implications, AnyEvent::Handle sets this flag automatically |
335 | security implications, AnyEvent::Handle sets this flag automatically |
317 | unless explicitly specified. |
336 | unless explicitly specified. Note that setting this flag after |
|
|
337 | establishing a connection I<may> be a bit too late (data loss could |
|
|
338 | already have occured on BSD systems), but at least it will protect you |
|
|
339 | from most attacks. |
318 | |
340 | |
319 | =item read_size => <bytes> |
341 | =item read_size => <bytes> |
320 | |
342 | |
321 | The default read block size (the amount of bytes this module will |
343 | The default read block size (the amount of bytes this module will |
322 | try to read during each loop iteration, which affects memory |
344 | try to read during each loop iteration, which affects memory |
… | |
… | |
356 | C<undef>. |
378 | C<undef>. |
357 | |
379 | |
358 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
380 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
359 | |
381 | |
360 | When this parameter is given, it enables TLS (SSL) mode, that means |
382 | When this parameter is given, it enables TLS (SSL) mode, that means |
361 | AnyEvent will start a TLS handshake as soon as the conenction has been |
383 | AnyEvent will start a TLS handshake as soon as the connection has been |
362 | established and will transparently encrypt/decrypt data afterwards. |
384 | established and will transparently encrypt/decrypt data afterwards. |
363 | |
385 | |
364 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
386 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
365 | appropriate error message. |
387 | appropriate error message. |
366 | |
388 | |
… | |
… | |
513 | } |
535 | } |
514 | |
536 | |
515 | sub _start { |
537 | sub _start { |
516 | my ($self) = @_; |
538 | my ($self) = @_; |
517 | |
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 | |
518 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
546 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
519 | |
547 | |
520 | $self->{_activity} = |
548 | $self->{_activity} = |
521 | $self->{_ractivity} = |
549 | $self->{_ractivity} = |
522 | $self->{_wactivity} = AE::now; |
550 | $self->{_wactivity} = AE::now; |
… | |
… | |
548 | $message ||= "$!"; |
576 | $message ||= "$!"; |
549 | |
577 | |
550 | if ($self->{on_error}) { |
578 | if ($self->{on_error}) { |
551 | $self->{on_error}($self, $fatal, $message); |
579 | $self->{on_error}($self, $fatal, $message); |
552 | $self->destroy if $fatal; |
580 | $self->destroy if $fatal; |
553 | } elsif ($self->{fh}) { |
581 | } elsif ($self->{fh} || $self->{connect}) { |
554 | $self->destroy; |
582 | $self->destroy; |
555 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
583 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
556 | } |
584 | } |
557 | } |
585 | } |
558 | |
586 | |
… | |
… | |
691 | |
719 | |
692 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
720 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
693 | |
721 | |
694 | =cut |
722 | =cut |
695 | |
723 | |
696 | sub on_starttls { |
724 | sub on_stoptls { |
697 | $_[0]{on_stoptls} = $_[1]; |
725 | $_[0]{on_stoptls} = $_[1]; |
698 | } |
726 | } |
699 | |
727 | |
700 | =item $handle->rbuf_max ($max_octets) |
728 | =item $handle->rbuf_max ($max_octets) |
701 | |
729 | |
… | |
… | |
813 | =item $handle->on_drain ($cb) |
841 | =item $handle->on_drain ($cb) |
814 | |
842 | |
815 | Sets the C<on_drain> callback or clears it (see the description of |
843 | Sets the C<on_drain> callback or clears it (see the description of |
816 | C<on_drain> in the constructor). |
844 | C<on_drain> in the constructor). |
817 | |
845 | |
|
|
846 | This method may invoke callbacks (and therefore the handle might be |
|
|
847 | destroyed after it returns). |
|
|
848 | |
818 | =cut |
849 | =cut |
819 | |
850 | |
820 | sub on_drain { |
851 | sub on_drain { |
821 | my ($self, $cb) = @_; |
852 | my ($self, $cb) = @_; |
822 | |
853 | |
… | |
… | |
829 | =item $handle->push_write ($data) |
860 | =item $handle->push_write ($data) |
830 | |
861 | |
831 | Queues the given scalar to be written. You can push as much data as you |
862 | Queues the given scalar to be written. You can push as much data as you |
832 | want (only limited by the available memory), as C<AnyEvent::Handle> |
863 | want (only limited by the available memory), as C<AnyEvent::Handle> |
833 | buffers it independently of the kernel. |
864 | buffers it independently of the kernel. |
|
|
865 | |
|
|
866 | This method may invoke callbacks (and therefore the handle might be |
|
|
867 | destroyed after it returns). |
834 | |
868 | |
835 | =cut |
869 | =cut |
836 | |
870 | |
837 | sub _drain_wbuf { |
871 | sub _drain_wbuf { |
838 | my ($self) = @_; |
872 | my ($self) = @_; |
… | |
… | |
868 | }; |
902 | }; |
869 | } |
903 | } |
870 | |
904 | |
871 | our %WH; |
905 | our %WH; |
872 | |
906 | |
|
|
907 | # deprecated |
873 | sub register_write_type($$) { |
908 | sub register_write_type($$) { |
874 | $WH{$_[0]} = $_[1]; |
909 | $WH{$_[0]} = $_[1]; |
875 | } |
910 | } |
876 | |
911 | |
877 | sub push_write { |
912 | sub push_write { |
878 | my $self = shift; |
913 | my $self = shift; |
879 | |
914 | |
880 | if (@_ > 1) { |
915 | if (@_ > 1) { |
881 | my $type = shift; |
916 | my $type = shift; |
882 | |
917 | |
|
|
918 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
883 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
919 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
884 | ->($self, @_); |
920 | ->($self, @_); |
885 | } |
921 | } |
886 | |
922 | |
|
|
923 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
924 | # and diagnose the problem earlier and better. |
|
|
925 | |
887 | if ($self->{tls}) { |
926 | if ($self->{tls}) { |
888 | $self->{_tls_wbuf} .= $_[0]; |
927 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
889 | &_dotls ($self) if $self->{fh}; |
928 | &_dotls ($self) if $self->{fh}; |
890 | } else { |
929 | } else { |
891 | $self->{wbuf} .= $_[0]; |
930 | utf8::downgrade $self->{wbuf} .= $_[0]; |
892 | $self->_drain_wbuf if $self->{fh}; |
931 | $self->_drain_wbuf if $self->{fh}; |
893 | } |
932 | } |
894 | } |
933 | } |
895 | |
934 | |
896 | =item $handle->push_write (type => @args) |
935 | =item $handle->push_write (type => @args) |
897 | |
936 | |
898 | Instead of formatting your data yourself, you can also let this module do |
937 | Instead of formatting your data yourself, you can also let this module |
899 | the job by specifying a type and type-specific arguments. |
938 | do the job by specifying a type and type-specific arguments. You |
|
|
939 | can also specify the (fully qualified) name of a package, in which |
|
|
940 | case AnyEvent tries to load the package and then expects to find the |
|
|
941 | C<anyevent_write_type> function inside (see "custom write types", below). |
900 | |
942 | |
901 | Predefined types are (if you have ideas for additional types, feel free to |
943 | Predefined types are (if you have ideas for additional types, feel free to |
902 | drop by and tell us): |
944 | drop by and tell us): |
903 | |
945 | |
904 | =over 4 |
946 | =over 4 |
… | |
… | |
1007 | the peer. |
1049 | the peer. |
1008 | |
1050 | |
1009 | You can rely on the normal read queue and C<on_eof> handling |
1051 | You can rely on the normal read queue and C<on_eof> handling |
1010 | afterwards. This is the cleanest way to close a connection. |
1052 | afterwards. This is the cleanest way to close a connection. |
1011 | |
1053 | |
|
|
1054 | This method may invoke callbacks (and therefore the handle might be |
|
|
1055 | destroyed after it returns). |
|
|
1056 | |
1012 | =cut |
1057 | =cut |
1013 | |
1058 | |
1014 | sub push_shutdown { |
1059 | sub push_shutdown { |
1015 | my ($self) = @_; |
1060 | my ($self) = @_; |
1016 | |
1061 | |
1017 | delete $self->{low_water_mark}; |
1062 | delete $self->{low_water_mark}; |
1018 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1063 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1019 | } |
1064 | } |
1020 | |
1065 | |
1021 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1066 | =item custom write types - Package::anyevent_write_type $handle, @args |
1022 | |
1067 | |
1023 | This function (not method) lets you add your own types to C<push_write>. |
1068 | Instead of one of the predefined types, you can also specify the name of |
|
|
1069 | a package. AnyEvent will try to load the package and then expects to find |
|
|
1070 | a function named C<anyevent_write_type> inside. If it isn't found, it |
|
|
1071 | progressively tries to load the parent package until it either finds the |
|
|
1072 | function (good) or runs out of packages (bad). |
|
|
1073 | |
1024 | Whenever the given C<type> is used, C<push_write> will invoke the code |
1074 | Whenever the given C<type> is used, C<push_write> will the function with |
1025 | reference with the handle object and the remaining arguments. |
1075 | the handle object and the remaining arguments. |
1026 | |
1076 | |
1027 | The code reference is supposed to return a single octet string that will |
1077 | The function is supposed to return a single octet string that will be |
1028 | be appended to the write buffer. |
1078 | appended to the write buffer, so you cna mentally treat this function as a |
|
|
1079 | "arguments to on-the-wire-format" converter. |
1029 | |
1080 | |
1030 | Note that this is a function, and all types registered this way will be |
1081 | Example: implement a custom write type C<join> that joins the remaining |
1031 | global, so try to use unique names. |
1082 | arguments using the first one. |
|
|
1083 | |
|
|
1084 | $handle->push_write (My::Type => " ", 1,2,3); |
|
|
1085 | |
|
|
1086 | # uses the following package, which can be defined in the "My::Type" or in |
|
|
1087 | # the "My" modules to be auto-loaded, or just about anywhere when the |
|
|
1088 | # My::Type::anyevent_write_type is defined before invoking it. |
|
|
1089 | |
|
|
1090 | package My::Type; |
|
|
1091 | |
|
|
1092 | sub anyevent_write_type { |
|
|
1093 | my ($handle, $delim, @args) = @_; |
|
|
1094 | |
|
|
1095 | join $delim, @args |
|
|
1096 | } |
1032 | |
1097 | |
1033 | =cut |
1098 | =cut |
1034 | |
1099 | |
1035 | ############################################################################# |
1100 | ############################################################################# |
1036 | |
1101 | |
… | |
… | |
1045 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
1110 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
1046 | a queue. |
1111 | a queue. |
1047 | |
1112 | |
1048 | In the simple case, you just install an C<on_read> callback and whenever |
1113 | In the simple case, you just install an C<on_read> callback and whenever |
1049 | new data arrives, it will be called. You can then remove some data (if |
1114 | new data arrives, it will be called. You can then remove some data (if |
1050 | enough is there) from the read buffer (C<< $handle->rbuf >>). Or you cna |
1115 | enough is there) from the read buffer (C<< $handle->rbuf >>). Or you can |
1051 | leave the data there if you want to accumulate more (e.g. when only a |
1116 | leave the data there if you want to accumulate more (e.g. when only a |
1052 | partial message has been received so far). |
1117 | partial message has been received so far), or change the read queue with |
|
|
1118 | e.g. C<push_read>. |
1053 | |
1119 | |
1054 | In the more complex case, you want to queue multiple callbacks. In this |
1120 | In the more complex case, you want to queue multiple callbacks. In this |
1055 | case, AnyEvent::Handle will call the first queued callback each time new |
1121 | case, AnyEvent::Handle will call the first queued callback each time new |
1056 | data arrives (also the first time it is queued) and removes it when it has |
1122 | data arrives (also the first time it is queued) and removes it when it has |
1057 | done its job (see C<push_read>, below). |
1123 | done its job (see C<push_read>, below). |
… | |
… | |
1192 | |
1258 | |
1193 | This replaces the currently set C<on_read> callback, or clears it (when |
1259 | This replaces the currently set C<on_read> callback, or clears it (when |
1194 | the new callback is C<undef>). See the description of C<on_read> in the |
1260 | the new callback is C<undef>). See the description of C<on_read> in the |
1195 | constructor. |
1261 | constructor. |
1196 | |
1262 | |
|
|
1263 | This method may invoke callbacks (and therefore the handle might be |
|
|
1264 | destroyed after it returns). |
|
|
1265 | |
1197 | =cut |
1266 | =cut |
1198 | |
1267 | |
1199 | sub on_read { |
1268 | sub on_read { |
1200 | my ($self, $cb) = @_; |
1269 | my ($self, $cb) = @_; |
1201 | |
1270 | |
… | |
… | |
1240 | |
1309 | |
1241 | If enough data was available, then the callback must remove all data it is |
1310 | If enough data was available, then the callback must remove all data it is |
1242 | interested in (which can be none at all) and return a true value. After returning |
1311 | interested in (which can be none at all) and return a true value. After returning |
1243 | true, it will be removed from the queue. |
1312 | true, it will be removed from the queue. |
1244 | |
1313 | |
|
|
1314 | These methods may invoke callbacks (and therefore the handle might be |
|
|
1315 | destroyed after it returns). |
|
|
1316 | |
1245 | =cut |
1317 | =cut |
1246 | |
1318 | |
1247 | our %RH; |
1319 | our %RH; |
1248 | |
1320 | |
1249 | sub register_read_type($$) { |
1321 | sub register_read_type($$) { |
… | |
… | |
1255 | my $cb = pop; |
1327 | my $cb = pop; |
1256 | |
1328 | |
1257 | if (@_) { |
1329 | if (@_) { |
1258 | my $type = shift; |
1330 | my $type = shift; |
1259 | |
1331 | |
|
|
1332 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1260 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
1333 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read") |
1261 | ->($self, $cb, @_); |
1334 | ->($self, $cb, @_); |
1262 | } |
1335 | } |
1263 | |
1336 | |
1264 | push @{ $self->{_queue} }, $cb; |
1337 | push @{ $self->{_queue} }, $cb; |
1265 | $self->_drain_rbuf; |
1338 | $self->_drain_rbuf; |
… | |
… | |
1284 | |
1357 | |
1285 | =item $handle->unshift_read (type => @args, $cb) |
1358 | =item $handle->unshift_read (type => @args, $cb) |
1286 | |
1359 | |
1287 | Instead of providing a callback that parses the data itself you can chose |
1360 | Instead of providing a callback that parses the data itself you can chose |
1288 | between a number of predefined parsing formats, for chunks of data, lines |
1361 | between a number of predefined parsing formats, for chunks of data, lines |
1289 | etc. |
1362 | etc. You can also specify the (fully qualified) name of a package, in |
|
|
1363 | which case AnyEvent tries to load the package and then expects to find the |
|
|
1364 | C<anyevent_read_type> function inside (see "custom read types", below). |
1290 | |
1365 | |
1291 | Predefined types are (if you have ideas for additional types, feel free to |
1366 | Predefined types are (if you have ideas for additional types, feel free to |
1292 | drop by and tell us): |
1367 | drop by and tell us): |
1293 | |
1368 | |
1294 | =over 4 |
1369 | =over 4 |
… | |
… | |
1613 | } |
1688 | } |
1614 | }; |
1689 | }; |
1615 | |
1690 | |
1616 | =back |
1691 | =back |
1617 | |
1692 | |
1618 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1693 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1619 | |
1694 | |
1620 | This function (not method) lets you add your own types to C<push_read>. |
1695 | Instead of one of the predefined types, you can also specify the name |
|
|
1696 | of a package. AnyEvent will try to load the package and then expects to |
|
|
1697 | find a function named C<anyevent_read_type> inside. If it isn't found, it |
|
|
1698 | progressively tries to load the parent package until it either finds the |
|
|
1699 | function (good) or runs out of packages (bad). |
1621 | |
1700 | |
1622 | Whenever the given C<type> is used, C<push_read> will invoke the code |
1701 | Whenever this type is used, C<push_read> will invoke the function with the |
1623 | reference with the handle object, the callback and the remaining |
1702 | handle object, the original callback and the remaining arguments. |
1624 | arguments. |
|
|
1625 | |
1703 | |
1626 | The code reference is supposed to return a callback (usually a closure) |
1704 | The function is supposed to return a callback (usually a closure) that |
1627 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
1705 | works as a plain read callback (see C<< ->push_read ($cb) >>), so you can |
|
|
1706 | mentally treat the function as a "configurable read type to read callback" |
|
|
1707 | converter. |
1628 | |
1708 | |
1629 | It should invoke the passed callback when it is done reading (remember to |
1709 | It should invoke the original callback when it is done reading (remember |
1630 | pass C<$handle> as first argument as all other callbacks do that). |
1710 | to pass C<$handle> as first argument as all other callbacks do that, |
|
|
1711 | although there is no strict requirement on this). |
1631 | |
1712 | |
1632 | Note that this is a function, and all types registered this way will be |
|
|
1633 | global, so try to use unique names. |
|
|
1634 | |
|
|
1635 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
1713 | For examples, see the source of this module (F<perldoc -m |
1636 | search for C<register_read_type>)). |
1714 | AnyEvent::Handle>, search for C<register_read_type>)). |
1637 | |
1715 | |
1638 | =item $handle->stop_read |
1716 | =item $handle->stop_read |
1639 | |
1717 | |
1640 | =item $handle->start_read |
1718 | =item $handle->start_read |
1641 | |
1719 | |
… | |
… | |
1661 | } |
1739 | } |
1662 | |
1740 | |
1663 | sub start_read { |
1741 | sub start_read { |
1664 | my ($self) = @_; |
1742 | my ($self) = @_; |
1665 | |
1743 | |
1666 | unless ($self->{_rw} || $self->{_eof}) { |
1744 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1667 | Scalar::Util::weaken $self; |
1745 | Scalar::Util::weaken $self; |
1668 | |
1746 | |
1669 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1747 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1670 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1748 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1671 | 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; |
… | |
… | |
1764 | && ($tmp != $ERROR_SYSCALL || $!); |
1842 | && ($tmp != $ERROR_SYSCALL || $!); |
1765 | |
1843 | |
1766 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1844 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1767 | $self->{wbuf} .= $tmp; |
1845 | $self->{wbuf} .= $tmp; |
1768 | $self->_drain_wbuf; |
1846 | $self->_drain_wbuf; |
|
|
1847 | $self->{tls} or return; # tls session might have gone away in callback |
1769 | } |
1848 | } |
1770 | |
1849 | |
1771 | $self->{_on_starttls} |
1850 | $self->{_on_starttls} |
1772 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1851 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1773 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1852 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1797 | when this function returns. |
1876 | when this function returns. |
1798 | |
1877 | |
1799 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1878 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1800 | handshakes on the same stream. Best do not attempt to use the stream after |
1879 | handshakes on the same stream. Best do not attempt to use the stream after |
1801 | stopping TLS. |
1880 | stopping TLS. |
|
|
1881 | |
|
|
1882 | This method may invoke callbacks (and therefore the handle might be |
|
|
1883 | destroyed after it returns). |
1802 | |
1884 | |
1803 | =cut |
1885 | =cut |
1804 | |
1886 | |
1805 | our %TLS_CACHE; #TODO not yet documented, should we? |
1887 | our %TLS_CACHE; #TODO not yet documented, should we? |
1806 | |
1888 | |
… | |
… | |
1872 | |
1954 | |
1873 | =item $handle->stoptls |
1955 | =item $handle->stoptls |
1874 | |
1956 | |
1875 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1957 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1876 | sending a close notify to the other side, but since OpenSSL doesn't |
1958 | sending a close notify to the other side, but since OpenSSL doesn't |
1877 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1959 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1878 | the stream afterwards. |
1960 | the stream afterwards. |
|
|
1961 | |
|
|
1962 | This method may invoke callbacks (and therefore the handle might be |
|
|
1963 | destroyed after it returns). |
1879 | |
1964 | |
1880 | =cut |
1965 | =cut |
1881 | |
1966 | |
1882 | sub stoptls { |
1967 | sub stoptls { |
1883 | my ($self) = @_; |
1968 | my ($self) = @_; |
1884 | |
1969 | |
1885 | if ($self->{tls}) { |
1970 | if ($self->{tls} && $self->{fh}) { |
1886 | Net::SSLeay::shutdown ($self->{tls}); |
1971 | Net::SSLeay::shutdown ($self->{tls}); |
1887 | |
1972 | |
1888 | &_dotls; |
1973 | &_dotls; |
1889 | |
1974 | |
1890 | # # 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# |
… | |
… | |
1967 | |
2052 | |
1968 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2053 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
1969 | #nop |
2054 | #nop |
1970 | } |
2055 | } |
1971 | |
2056 | |
|
|
2057 | =item $handle->destroyed |
|
|
2058 | |
|
|
2059 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2060 | ->destroy >>, true otherwise. |
|
|
2061 | |
|
|
2062 | Can be useful to decide whether the handle is still valid after some |
|
|
2063 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2064 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2065 | can 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 | |
|
|
2071 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2072 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2073 | handle being destroyed. |
|
|
2074 | |
|
|
2075 | =cut |
|
|
2076 | |
|
|
2077 | sub destroyed { 0 } |
|
|
2078 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2079 | |
1972 | =item AnyEvent::Handle::TLS_CTX |
2080 | =item AnyEvent::Handle::TLS_CTX |
1973 | |
2081 | |
1974 | This function creates and returns the AnyEvent::TLS object used by default |
2082 | This function creates and returns the AnyEvent::TLS object used by default |
1975 | for TLS mode. |
2083 | for TLS mode. |
1976 | |
2084 | |