… | |
… | |
75 | } |
75 | } |
76 | |
76 | |
77 | \&$func |
77 | \&$func |
78 | } |
78 | } |
79 | |
79 | |
|
|
80 | sub MAX_READ_SIZE() { 131072 } |
|
|
81 | |
80 | =head1 METHODS |
82 | =head1 METHODS |
81 | |
83 | |
82 | =over 4 |
84 | =over 4 |
83 | |
85 | |
84 | =item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value... |
86 | =item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value... |
… | |
… | |
157 | |
159 | |
158 | Some errors are fatal (which is indicated by C<$fatal> being true). On |
160 | Some errors are fatal (which is indicated by C<$fatal> being true). On |
159 | fatal errors the handle object will be destroyed (by a call to C<< -> |
161 | fatal errors the handle object will be destroyed (by a call to C<< -> |
160 | destroy >>) after invoking the error callback (which means you are free to |
162 | destroy >>) after invoking the error callback (which means you are free to |
161 | examine the handle object). Examples of fatal errors are an EOF condition |
163 | examine the handle object). Examples of fatal errors are an EOF condition |
162 | with active (but unsatisifable) read watchers (C<EPIPE>) or I/O errors. In |
164 | with active (but unsatisfiable) read watchers (C<EPIPE>) or I/O errors. In |
163 | cases where the other side can close the connection at will, it is |
165 | cases where the other side can close the connection at will, it is |
164 | often easiest to not report C<EPIPE> errors in this callback. |
166 | often easiest to not report C<EPIPE> errors in this callback. |
165 | |
167 | |
166 | AnyEvent::Handle tries to find an appropriate error code for you to check |
168 | AnyEvent::Handle tries to find an appropriate error code for you to check |
167 | against, but in some cases (TLS errors), this does not work well. It is |
169 | against, but in some cases (TLS errors), this does not work well. It is |
… | |
… | |
337 | already have occured on BSD systems), but at least it will protect you |
339 | already have occured on BSD systems), but at least it will protect you |
338 | from most attacks. |
340 | from most attacks. |
339 | |
341 | |
340 | =item read_size => <bytes> |
342 | =item read_size => <bytes> |
341 | |
343 | |
342 | The default read block size (the number of bytes this module will |
344 | The initial read block size, the number of bytes this module will try to |
343 | try to read during each loop iteration, which affects memory |
345 | read during each loop iteration. Each handle object will consume at least |
344 | requirements). Default: C<8192>. |
346 | this amount of memory for the read buffer as well, so when handling many |
|
|
347 | connections requirements). See also C<max_read_size>. Default: C<2048>. |
|
|
348 | |
|
|
349 | =item max_read_size => <bytes> |
|
|
350 | |
|
|
351 | The maximum read buffer size used by the dynamic adjustment |
|
|
352 | algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in |
|
|
353 | one go it will double C<read_size> up to the maximum given by this |
|
|
354 | option. Default: C<131072> or C<read_size>, whichever is higher. |
345 | |
355 | |
346 | =item low_water_mark => <bytes> |
356 | =item low_water_mark => <bytes> |
347 | |
357 | |
348 | Sets the number of bytes (default: C<0>) that make up an "empty" write |
358 | Sets the number of bytes (default: C<0>) that make up an "empty" write |
349 | buffer: If the buffer reaches this size or gets even samller it is |
359 | buffer: If the buffer reaches this size or gets even samller it is |
… | |
… | |
412 | Use the C<< ->starttls >> method if you need to start TLS negotiation later. |
422 | Use the C<< ->starttls >> method if you need to start TLS negotiation later. |
413 | |
423 | |
414 | =item tls_ctx => $anyevent_tls |
424 | =item tls_ctx => $anyevent_tls |
415 | |
425 | |
416 | Use the given C<AnyEvent::TLS> object to create the new TLS connection |
426 | Use the given C<AnyEvent::TLS> object to create the new TLS connection |
417 | (unless a connection object was specified directly). If this parameter is |
427 | (unless a connection object was specified directly). If this |
418 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
428 | parameter is missing (or C<undef>), then AnyEvent::Handle will use |
|
|
429 | C<AnyEvent::Handle::TLS_CTX>. |
419 | |
430 | |
420 | Instead of an object, you can also specify a hash reference with C<< key |
431 | Instead of an object, you can also specify a hash reference with C<< key |
421 | => value >> pairs. Those will be passed to L<AnyEvent::TLS> to create a |
432 | => value >> pairs. Those will be passed to L<AnyEvent::TLS> to create a |
422 | new TLS context object. |
433 | new TLS context object. |
423 | |
434 | |
… | |
… | |
491 | AnyEvent::Socket::tcp_connect ( |
502 | AnyEvent::Socket::tcp_connect ( |
492 | $self->{connect}[0], |
503 | $self->{connect}[0], |
493 | $self->{connect}[1], |
504 | $self->{connect}[1], |
494 | sub { |
505 | sub { |
495 | my ($fh, $host, $port, $retry) = @_; |
506 | my ($fh, $host, $port, $retry) = @_; |
|
|
507 | |
|
|
508 | delete $self->{_connect}; # no longer needed |
496 | |
509 | |
497 | if ($fh) { |
510 | if ($fh) { |
498 | $self->{fh} = $fh; |
511 | $self->{fh} = $fh; |
499 | |
512 | |
500 | delete $self->{_skip_drain_rbuf}; |
513 | delete $self->{_skip_drain_rbuf}; |
… | |
… | |
545 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
558 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
546 | |
559 | |
547 | $self->{_activity} = |
560 | $self->{_activity} = |
548 | $self->{_ractivity} = |
561 | $self->{_ractivity} = |
549 | $self->{_wactivity} = AE::now; |
562 | $self->{_wactivity} = AE::now; |
|
|
563 | |
|
|
564 | $self->{read_size} ||= 2048; |
|
|
565 | $self->{max_read_size} = $self->{read_size} |
|
|
566 | if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE); |
550 | |
567 | |
551 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
568 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
552 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
569 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
553 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
570 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
554 | |
571 | |
… | |
… | |
1745 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1762 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1746 | Scalar::Util::weaken $self; |
1763 | Scalar::Util::weaken $self; |
1747 | |
1764 | |
1748 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1765 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1749 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1766 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1750 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1767 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size}, length $$rbuf; |
1751 | |
1768 | |
1752 | if ($len > 0) { |
1769 | if ($len > 0) { |
1753 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1770 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1754 | |
1771 | |
1755 | if ($self->{tls}) { |
1772 | if ($self->{tls}) { |
1756 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1773 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1757 | |
1774 | |
1758 | &_dotls ($self); |
1775 | &_dotls ($self); |
1759 | } else { |
1776 | } else { |
1760 | $self->_drain_rbuf; |
1777 | $self->_drain_rbuf; |
|
|
1778 | } |
|
|
1779 | |
|
|
1780 | if ($len == $self->{read_size}) { |
|
|
1781 | $self->{read_size} *= 2; |
|
|
1782 | $self->{read_size} = $self->{max_read_size} || MAX_READ_SIZE |
|
|
1783 | if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE); |
1761 | } |
1784 | } |
1762 | |
1785 | |
1763 | } elsif (defined $len) { |
1786 | } elsif (defined $len) { |
1764 | delete $self->{_rw}; |
1787 | delete $self->{_rw}; |
1765 | $self->{_eof} = 1; |
1788 | $self->{_eof} = 1; |
… | |
… | |
2006 | push @linger, AE::io $fh, 1, sub { |
2029 | push @linger, AE::io $fh, 1, sub { |
2007 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2030 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2008 | |
2031 | |
2009 | if ($len > 0) { |
2032 | if ($len > 0) { |
2010 | substr $wbuf, 0, $len, ""; |
2033 | substr $wbuf, 0, $len, ""; |
2011 | } else { |
2034 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) { |
2012 | @linger = (); # end |
2035 | @linger = (); # end |
2013 | } |
2036 | } |
2014 | }; |
2037 | }; |
2015 | push @linger, AE::timer $linger, 0, sub { |
2038 | push @linger, AE::timer $linger, 0, sub { |
2016 | @linger = (); |
2039 | @linger = (); |
… | |
… | |
2112 | |
2135 | |
2113 | It is only safe to "forget" the reference inside EOF or error callbacks, |
2136 | It is only safe to "forget" the reference inside EOF or error callbacks, |
2114 | from within all other callbacks, you need to explicitly call the C<< |
2137 | from within all other callbacks, you need to explicitly call the C<< |
2115 | ->destroy >> method. |
2138 | ->destroy >> method. |
2116 | |
2139 | |
|
|
2140 | =item Why is my C<on_eof> callback never called? |
|
|
2141 | |
|
|
2142 | Probably because your C<on_error> callback is being called instead: When |
|
|
2143 | you have outstanding requests in your read queue, then an EOF is |
|
|
2144 | considered an error as you clearly expected some data. |
|
|
2145 | |
|
|
2146 | To avoid this, make sure you have an empty read queue whenever your handle |
|
|
2147 | is supposed to be "idle" (i.e. connection closes are O.K.). You cna set |
|
|
2148 | an C<on_read> handler that simply pushes the first read requests in the |
|
|
2149 | queue. |
|
|
2150 | |
|
|
2151 | See also the next question, which explains this in a bit more detail. |
|
|
2152 | |
|
|
2153 | =item How can I serve requests in a loop? |
|
|
2154 | |
|
|
2155 | Most protocols consist of some setup phase (authentication for example) |
|
|
2156 | followed by a request handling phase, where the server waits for requests |
|
|
2157 | and handles them, in a loop. |
|
|
2158 | |
|
|
2159 | There are two important variants: The first (traditional, better) variant |
|
|
2160 | handles requests until the server gets some QUIT command, causing it to |
|
|
2161 | close the connection first (highly desirable for a busy TCP server). A |
|
|
2162 | client dropping the connection is an error, which means this variant can |
|
|
2163 | detect an unexpected detection close. |
|
|
2164 | |
|
|
2165 | To handle this case, always make sure you have a on-empty read queue, by |
|
|
2166 | pushing the "read request start" handler on it: |
|
|
2167 | |
|
|
2168 | # we assume a request starts with a single line |
|
|
2169 | my @start_request; @start_request = (line => sub { |
|
|
2170 | my ($hdl, $line) = @_; |
|
|
2171 | |
|
|
2172 | ... handle request |
|
|
2173 | |
|
|
2174 | # push next request read, possibly from a nested callback |
|
|
2175 | $hdl->push_read (@start_request); |
|
|
2176 | }); |
|
|
2177 | |
|
|
2178 | # auth done, now go into request handling loop |
|
|
2179 | # now push the first @start_request |
|
|
2180 | $hdl->push_read (@start_request); |
|
|
2181 | |
|
|
2182 | By always having an outstanding C<push_read>, the handle always expects |
|
|
2183 | some data and raises the C<EPIPE> error when the connction is dropped |
|
|
2184 | unexpectedly. |
|
|
2185 | |
|
|
2186 | The second variant is a protocol where the client can drop the connection |
|
|
2187 | at any time. For TCP, this means that the server machine may run out of |
|
|
2188 | sockets easier, and in general, it means you cnanot distinguish a protocl |
|
|
2189 | failure/client crash from a normal connection close. Nevertheless, these |
|
|
2190 | kinds of protocols are common (and sometimes even the best solution to the |
|
|
2191 | problem). |
|
|
2192 | |
|
|
2193 | Having an outstanding read request at all times is possible if you ignore |
|
|
2194 | C<EPIPE> errors, but this doesn't help with when the client drops the |
|
|
2195 | connection during a request, which would still be an error. |
|
|
2196 | |
|
|
2197 | A better solution is to push the initial request read in an C<on_read> |
|
|
2198 | callback. This avoids an error, as when the server doesn't expect data |
|
|
2199 | (i.e. is idly waiting for the next request, an EOF will not raise an |
|
|
2200 | error, but simply result in an C<on_eof> callback. It is also a bit slower |
|
|
2201 | and simpler: |
|
|
2202 | |
|
|
2203 | # auth done, now go into request handling loop |
|
|
2204 | $hdl->on_read (sub { |
|
|
2205 | my ($hdl) = @_; |
|
|
2206 | |
|
|
2207 | # called each time we receive data but the read queue is empty |
|
|
2208 | # simply start read the request |
|
|
2209 | |
|
|
2210 | $hdl->push_read (line => sub { |
|
|
2211 | my ($hdl, $line) = @_; |
|
|
2212 | |
|
|
2213 | ... handle request |
|
|
2214 | |
|
|
2215 | # do nothing special when the request has been handled, just |
|
|
2216 | # let the request queue go empty. |
|
|
2217 | }); |
|
|
2218 | }); |
|
|
2219 | |
2117 | =item I get different callback invocations in TLS mode/Why can't I pause |
2220 | =item I get different callback invocations in TLS mode/Why can't I pause |
2118 | reading? |
2221 | reading? |
2119 | |
2222 | |
2120 | Unlike, say, TCP, TLS connections do not consist of two independent |
2223 | Unlike, say, TCP, TLS connections do not consist of two independent |
2121 | communication channels, one for each direction. Or put differently, the |
2224 | communication channels, one for each direction. Or put differently, the |