… | |
… | |
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::TLS fh => $filehandle, key => value... |
… | |
… | |
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. |
… | |
… | |
289 | |
305 | |
290 | =item keepalive => <boolean> |
306 | =item keepalive => <boolean> |
291 | |
307 | |
292 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
308 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
293 | normally, TCP connections have no time-out once established, so TCP |
309 | normally, TCP connections have no time-out once established, so TCP |
294 | conenctions, once established, can stay alive forever even when the other |
310 | 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 |
311 | 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 |
312 | TCP connections whent he other side becomes unreachable. While the default |
297 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
313 | 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 |
314 | and, if the other side doesn't reply, take down the TCP connection some 10 |
299 | to 15 minutes later. |
315 | to 15 minutes later. |
… | |
… | |
359 | C<undef>. |
375 | C<undef>. |
360 | |
376 | |
361 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
377 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
362 | |
378 | |
363 | When this parameter is given, it enables TLS (SSL) mode, that means |
379 | When this parameter is given, it enables TLS (SSL) mode, that means |
364 | AnyEvent will start a TLS handshake as soon as the conenction has been |
380 | AnyEvent will start a TLS handshake as soon as the connection has been |
365 | established and will transparently encrypt/decrypt data afterwards. |
381 | established and will transparently encrypt/decrypt data afterwards. |
366 | |
382 | |
367 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
383 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
368 | appropriate error message. |
384 | appropriate error message. |
369 | |
385 | |
… | |
… | |
871 | }; |
887 | }; |
872 | } |
888 | } |
873 | |
889 | |
874 | our %WH; |
890 | our %WH; |
875 | |
891 | |
|
|
892 | # deprecated |
876 | sub register_write_type($$) { |
893 | sub register_write_type($$) { |
877 | $WH{$_[0]} = $_[1]; |
894 | $WH{$_[0]} = $_[1]; |
878 | } |
895 | } |
879 | |
896 | |
880 | sub push_write { |
897 | sub push_write { |
881 | my $self = shift; |
898 | my $self = shift; |
882 | |
899 | |
883 | if (@_ > 1) { |
900 | if (@_ > 1) { |
884 | my $type = shift; |
901 | my $type = shift; |
885 | |
902 | |
|
|
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
886 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
887 | ->($self, @_); |
905 | ->($self, @_); |
888 | } |
906 | } |
889 | |
907 | |
890 | if ($self->{tls}) { |
908 | if ($self->{tls}) { |
891 | $self->{_tls_wbuf} .= $_[0]; |
909 | $self->{_tls_wbuf} .= $_[0]; |
… | |
… | |
896 | } |
914 | } |
897 | } |
915 | } |
898 | |
916 | |
899 | =item $handle->push_write (type => @args) |
917 | =item $handle->push_write (type => @args) |
900 | |
918 | |
901 | Instead of formatting your data yourself, you can also let this module do |
919 | Instead of formatting your data yourself, you can also let this module |
902 | the job by specifying a type and type-specific arguments. |
920 | do the job by specifying a type and type-specific arguments. You |
|
|
921 | can also specify the (fully qualified) name of a package, in which |
|
|
922 | case AnyEvent tries to load the package and then expects to find the |
|
|
923 | C<anyevent_read_type> function inside (see "custom write types", below). |
903 | |
924 | |
904 | Predefined types are (if you have ideas for additional types, feel free to |
925 | Predefined types are (if you have ideas for additional types, feel free to |
905 | drop by and tell us): |
926 | drop by and tell us): |
906 | |
927 | |
907 | =over 4 |
928 | =over 4 |
… | |
… | |
1019 | |
1040 | |
1020 | delete $self->{low_water_mark}; |
1041 | delete $self->{low_water_mark}; |
1021 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1042 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1022 | } |
1043 | } |
1023 | |
1044 | |
1024 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1045 | =item custom write types - Package::anyevent_write_type $handle, @args |
1025 | |
1046 | |
1026 | This function (not method) lets you add your own types to C<push_write>. |
1047 | Instead of one of the predefined types, you can also specify the name of |
|
|
1048 | a package. AnyEvent will try to load the package and then expects to find |
|
|
1049 | a function named C<anyevent_write_type> inside. If it isn't found, it |
|
|
1050 | progressively tries to load the parent package until it either finds the |
|
|
1051 | function (good) or runs out of packages (bad). |
|
|
1052 | |
1027 | Whenever the given C<type> is used, C<push_write> will invoke the code |
1053 | Whenever the given C<type> is used, C<push_write> will the function with |
1028 | reference with the handle object and the remaining arguments. |
1054 | the handle object and the remaining arguments. |
1029 | |
1055 | |
1030 | The code reference is supposed to return a single octet string that will |
1056 | The function is supposed to return a single octet string that will be |
1031 | be appended to the write buffer. |
1057 | appended to the write buffer, so you cna mentally treat this function as a |
|
|
1058 | "arguments to on-the-wire-format" converter. |
1032 | |
1059 | |
1033 | Note that this is a function, and all types registered this way will be |
1060 | Example: implement a custom write type C<join> that joins the remaining |
1034 | global, so try to use unique names. |
1061 | arguments using the first one. |
|
|
1062 | |
|
|
1063 | $handle->push_write (My::Type => " ", 1,2,3); |
|
|
1064 | |
|
|
1065 | # uses the following package, which can be defined in the "My::Type" or in |
|
|
1066 | # the "My" modules to be auto-loaded, or just about anywhere when the |
|
|
1067 | # My::Type::anyevent_write_type is defined before invoking it. |
|
|
1068 | |
|
|
1069 | package My::Type; |
|
|
1070 | |
|
|
1071 | sub anyevent_write_type { |
|
|
1072 | my ($handle, $delim, @args) = @_; |
|
|
1073 | |
|
|
1074 | join $delim, @args |
|
|
1075 | } |
1035 | |
1076 | |
1036 | =cut |
1077 | =cut |
1037 | |
1078 | |
1038 | ############################################################################# |
1079 | ############################################################################# |
1039 | |
1080 | |
… | |
… | |
1258 | my $cb = pop; |
1299 | my $cb = pop; |
1259 | |
1300 | |
1260 | if (@_) { |
1301 | if (@_) { |
1261 | my $type = shift; |
1302 | my $type = shift; |
1262 | |
1303 | |
|
|
1304 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1263 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
1305 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read") |
1264 | ->($self, $cb, @_); |
1306 | ->($self, $cb, @_); |
1265 | } |
1307 | } |
1266 | |
1308 | |
1267 | push @{ $self->{_queue} }, $cb; |
1309 | push @{ $self->{_queue} }, $cb; |
1268 | $self->_drain_rbuf; |
1310 | $self->_drain_rbuf; |
… | |
… | |
1287 | |
1329 | |
1288 | =item $handle->unshift_read (type => @args, $cb) |
1330 | =item $handle->unshift_read (type => @args, $cb) |
1289 | |
1331 | |
1290 | Instead of providing a callback that parses the data itself you can chose |
1332 | Instead of providing a callback that parses the data itself you can chose |
1291 | between a number of predefined parsing formats, for chunks of data, lines |
1333 | between a number of predefined parsing formats, for chunks of data, lines |
1292 | etc. |
1334 | etc. You can also specify the (fully qualified) name of a package, in |
|
|
1335 | which case AnyEvent tries to load the package and then expects to find the |
|
|
1336 | C<anyevent_read_type> function inside (see "custom read types", below). |
1293 | |
1337 | |
1294 | Predefined types are (if you have ideas for additional types, feel free to |
1338 | Predefined types are (if you have ideas for additional types, feel free to |
1295 | drop by and tell us): |
1339 | drop by and tell us): |
1296 | |
1340 | |
1297 | =over 4 |
1341 | =over 4 |
… | |
… | |
1616 | } |
1660 | } |
1617 | }; |
1661 | }; |
1618 | |
1662 | |
1619 | =back |
1663 | =back |
1620 | |
1664 | |
1621 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1665 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1622 | |
1666 | |
1623 | This function (not method) lets you add your own types to C<push_read>. |
1667 | Instead of one of the predefined types, you can also specify the name |
|
|
1668 | of a package. AnyEvent will try to load the package and then expects to |
|
|
1669 | find a function named C<anyevent_read_type> inside. If it isn't found, it |
|
|
1670 | progressively tries to load the parent package until it either finds the |
|
|
1671 | function (good) or runs out of packages (bad). |
1624 | |
1672 | |
1625 | Whenever the given C<type> is used, C<push_read> will invoke the code |
1673 | Whenever this type is used, C<push_read> will invoke the function with the |
1626 | reference with the handle object, the callback and the remaining |
1674 | handle object, the original callback and the remaining arguments. |
1627 | arguments. |
|
|
1628 | |
1675 | |
1629 | The code reference is supposed to return a callback (usually a closure) |
1676 | The function is supposed to return a callback (usually a closure) that |
1630 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
1677 | works as a plain read callback (see C<< ->push_read ($cb) >>), so you can |
|
|
1678 | mentally treat the function as a "configurable read type to read callback" |
|
|
1679 | converter. |
1631 | |
1680 | |
1632 | It should invoke the passed callback when it is done reading (remember to |
1681 | It should invoke the original callback when it is done reading (remember |
1633 | pass C<$handle> as first argument as all other callbacks do that). |
1682 | to pass C<$handle> as first argument as all other callbacks do that, |
|
|
1683 | although there is no strict requirement on this). |
1634 | |
1684 | |
1635 | Note that this is a function, and all types registered this way will be |
|
|
1636 | global, so try to use unique names. |
|
|
1637 | |
|
|
1638 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
1685 | For examples, see the source of this module (F<perldoc -m |
1639 | search for C<register_read_type>)). |
1686 | AnyEvent::Handle>, search for C<register_read_type>)). |
1640 | |
1687 | |
1641 | =item $handle->stop_read |
1688 | =item $handle->stop_read |
1642 | |
1689 | |
1643 | =item $handle->start_read |
1690 | =item $handle->start_read |
1644 | |
1691 | |