… | |
… | |
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 |
… | |
… | |
491 | AnyEvent::Socket::tcp_connect ( |
501 | AnyEvent::Socket::tcp_connect ( |
492 | $self->{connect}[0], |
502 | $self->{connect}[0], |
493 | $self->{connect}[1], |
503 | $self->{connect}[1], |
494 | sub { |
504 | sub { |
495 | my ($fh, $host, $port, $retry) = @_; |
505 | my ($fh, $host, $port, $retry) = @_; |
|
|
506 | |
|
|
507 | delete $self->{_connect}; |
496 | |
508 | |
497 | if ($fh) { |
509 | if ($fh) { |
498 | $self->{fh} = $fh; |
510 | $self->{fh} = $fh; |
499 | |
511 | |
500 | delete $self->{_skip_drain_rbuf}; |
512 | delete $self->{_skip_drain_rbuf}; |
… | |
… | |
546 | |
558 | |
547 | $self->{_activity} = |
559 | $self->{_activity} = |
548 | $self->{_ractivity} = |
560 | $self->{_ractivity} = |
549 | $self->{_wactivity} = AE::now; |
561 | $self->{_wactivity} = AE::now; |
550 | |
562 | |
|
|
563 | $self->{read_size} ||= 2048; |
|
|
564 | $self->{max_read_size} = $self->{read_size} |
|
|
565 | if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE); |
|
|
566 | |
551 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
567 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
552 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
568 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
553 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
569 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
554 | |
570 | |
555 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
571 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
… | |
… | |
644 | =cut |
660 | =cut |
645 | |
661 | |
646 | sub no_delay { |
662 | sub no_delay { |
647 | $_[0]{no_delay} = $_[1]; |
663 | $_[0]{no_delay} = $_[1]; |
648 | |
664 | |
649 | eval { |
|
|
650 | local $SIG{__DIE__}; |
|
|
651 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
665 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
652 | if $_[0]{fh}; |
666 | if $_[0]{fh}; |
653 | }; |
|
|
654 | } |
667 | } |
655 | |
668 | |
656 | =item $handle->keepalive ($boolean) |
669 | =item $handle->keepalive ($boolean) |
657 | |
670 | |
658 | Enables or disables the C<keepalive> setting (see constructor argument of |
671 | Enables or disables the C<keepalive> setting (see constructor argument of |
… | |
… | |
767 | $_[0]{$on_timeout} = $_[1]; |
780 | $_[0]{$on_timeout} = $_[1]; |
768 | }; |
781 | }; |
769 | |
782 | |
770 | *$timeout = sub { |
783 | *$timeout = sub { |
771 | my ($self, $new_value) = @_; |
784 | my ($self, $new_value) = @_; |
|
|
785 | |
|
|
786 | $new_value >= 0 |
|
|
787 | or Carp::croak "AnyEvent::Handle->$timeout called with negative timeout ($new_value), caught"; |
772 | |
788 | |
773 | $self->{$timeout} = $new_value; |
789 | $self->{$timeout} = $new_value; |
774 | delete $self->{$tw}; &$cb; |
790 | delete $self->{$tw}; &$cb; |
775 | }; |
791 | }; |
776 | |
792 | |
… | |
… | |
1745 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1761 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1746 | Scalar::Util::weaken $self; |
1762 | Scalar::Util::weaken $self; |
1747 | |
1763 | |
1748 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1764 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1749 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1765 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1750 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1766 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size}, length $$rbuf; |
1751 | |
1767 | |
1752 | if ($len > 0) { |
1768 | if ($len > 0) { |
1753 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1769 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1754 | |
1770 | |
1755 | if ($self->{tls}) { |
1771 | if ($self->{tls}) { |
1756 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1772 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1757 | |
1773 | |
1758 | &_dotls ($self); |
1774 | &_dotls ($self); |
1759 | } else { |
1775 | } else { |
1760 | $self->_drain_rbuf; |
1776 | $self->_drain_rbuf; |
|
|
1777 | } |
|
|
1778 | |
|
|
1779 | if ($len == $self->{read_size}) { |
|
|
1780 | $self->{read_size} *= 2; |
|
|
1781 | $self->{read_size} = $self->{max_read_size} || MAX_READ_SIZE |
|
|
1782 | if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE); |
1761 | } |
1783 | } |
1762 | |
1784 | |
1763 | } elsif (defined $len) { |
1785 | } elsif (defined $len) { |
1764 | delete $self->{_rw}; |
1786 | delete $self->{_rw}; |
1765 | $self->{_eof} = 1; |
1787 | $self->{_eof} = 1; |
… | |
… | |
2006 | push @linger, AE::io $fh, 1, sub { |
2028 | push @linger, AE::io $fh, 1, sub { |
2007 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2029 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2008 | |
2030 | |
2009 | if ($len > 0) { |
2031 | if ($len > 0) { |
2010 | substr $wbuf, 0, $len, ""; |
2032 | substr $wbuf, 0, $len, ""; |
2011 | } else { |
2033 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) { |
2012 | @linger = (); # end |
2034 | @linger = (); # end |
2013 | } |
2035 | } |
2014 | }; |
2036 | }; |
2015 | push @linger, AE::timer $linger, 0, sub { |
2037 | push @linger, AE::timer $linger, 0, sub { |
2016 | @linger = (); |
2038 | @linger = (); |