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.198 by root, Tue Aug 31 01:03:37 2010 UTC vs.
Revision 1.203 by root, Sat Oct 16 03:22:10 2010 UTC

75 } 75 }
76 76
77 \&$func 77 \&$func
78} 78}
79 79
80sub 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...
337already have occured on BSD systems), but at least it will protect you 339already have occured on BSD systems), but at least it will protect you
338from most attacks. 340from most attacks.
339 341
340=item read_size => <bytes> 342=item read_size => <bytes>
341 343
342The default read block size (the number of bytes this module will 344The initial read block size, the number of bytes this module will try to
343try to read during each loop iteration, which affects memory 345read during each loop iteration. Each handle object will consume at least
344requirements). Default: C<8192>. 346this amount of memory for the read buffer as well, so when handling many
347connections requirements). See also C<max_read_size>. Default: C<2048>.
348
349=item max_read_size => <bytes>
350
351The maximum read buffer size used by the dynamic adjustment
352algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in
353one go it will double C<read_size> up to the maximum given by this
354option. 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
348Sets the number of bytes (default: C<0>) that make up an "empty" write 358Sets the number of bytes (default: C<0>) that make up an "empty" write
349buffer: If the buffer reaches this size or gets even samller it is 359buffer: If the buffer reaches this size or gets even samller it is
546 556
547 $self->{_activity} = 557 $self->{_activity} =
548 $self->{_ractivity} = 558 $self->{_ractivity} =
549 $self->{_wactivity} = AE::now; 559 $self->{_wactivity} = AE::now;
550 560
561 $self->{read_size} ||= 2048;
562 $self->{max_read_size} = $self->{read_size}
563 if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE);
564
551 $self->timeout (delete $self->{timeout} ) if $self->{timeout}; 565 $self->timeout (delete $self->{timeout} ) if $self->{timeout};
552 $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; 566 $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout};
553 $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; 567 $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout};
554 568
555 $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; 569 $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay};
558 $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); 572 $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1);
559 573
560 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) 574 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx})
561 if $self->{tls}; 575 if $self->{tls};
562 576
563 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 577 $self->on_drain (delete $self->{on_drain} ) if $self->{on_drain};
564 578
565 $self->start_read 579 $self->start_read
566 if $self->{on_read} || @{ $self->{_queue} }; 580 if $self->{on_read} || @{ $self->{_queue} };
567 581
568 $self->_drain_wbuf; 582 $self->_drain_wbuf;
644=cut 658=cut
645 659
646sub no_delay { 660sub no_delay {
647 $_[0]{no_delay} = $_[1]; 661 $_[0]{no_delay} = $_[1];
648 662
649 eval {
650 local $SIG{__DIE__};
651 setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] 663 setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1]
652 if $_[0]{fh}; 664 if $_[0]{fh};
653 };
654} 665}
655 666
656=item $handle->keepalive ($boolean) 667=item $handle->keepalive ($boolean)
657 668
658Enables or disables the C<keepalive> setting (see constructor argument of 669Enables or disables the C<keepalive> setting (see constructor argument of
767 $_[0]{$on_timeout} = $_[1]; 778 $_[0]{$on_timeout} = $_[1];
768 }; 779 };
769 780
770 *$timeout = sub { 781 *$timeout = sub {
771 my ($self, $new_value) = @_; 782 my ($self, $new_value) = @_;
783
784 $new_value >= 0
785 or Carp::croak "AnyEvent::Handle->$timeout called with negative timeout ($new_value), caught";
772 786
773 $self->{$timeout} = $new_value; 787 $self->{$timeout} = $new_value;
774 delete $self->{$tw}; &$cb; 788 delete $self->{$tw}; &$cb;
775 }; 789 };
776 790
1271 $self->_drain_rbuf if $cb; 1285 $self->_drain_rbuf if $cb;
1272} 1286}
1273 1287
1274=item $handle->rbuf 1288=item $handle->rbuf
1275 1289
1276Returns the read buffer (as a modifiable lvalue). 1290Returns the read buffer (as a modifiable lvalue). You can also access the
1291read buffer directly as the C<< ->{rbuf} >> member, if you want (this is
1292much faster, and no less clean).
1277 1293
1278You can access the read buffer directly as the C<< ->{rbuf} >> 1294The only operation allowed on the read buffer (apart from looking at it)
1279member, if you want. However, the only operation allowed on the 1295is removing data from its beginning. Otherwise modifying or appending to
1280read buffer (apart from looking at it) is removing data from its 1296it is not allowed and will lead to hard-to-track-down bugs.
1281beginning. Otherwise modifying or appending to it is not allowed and will
1282lead to hard-to-track-down bugs.
1283 1297
1284NOTE: The read buffer should only be used or modified if the C<on_read>, 1298NOTE: The read buffer should only be used or modified in the C<on_read>
1285C<push_read> or C<unshift_read> methods are used. The other read methods 1299callback or when C<push_read> or C<unshift_read> are used with a single
1286automatically manage the read buffer. 1300callback (i.e. untyped). Typed C<push_read> and C<unshift_read> methods
1301will manage the read buffer on their own.
1287 1302
1288=cut 1303=cut
1289 1304
1290sub rbuf : lvalue { 1305sub rbuf : lvalue {
1291 $_[0]{rbuf} 1306 $_[0]{rbuf}
1342 my $cb = pop; 1357 my $cb = pop;
1343 1358
1344 if (@_) { 1359 if (@_) {
1345 my $type = shift; 1360 my $type = shift;
1346 1361
1362 $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type"
1347 $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") 1363 or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::unshift_read")
1348 ->($self, $cb, @_); 1364 ->($self, $cb, @_);
1349 } 1365 }
1350 1366
1351 unshift @{ $self->{_queue} }, $cb; 1367 unshift @{ $self->{_queue} }, $cb;
1352 $self->_drain_rbuf; 1368 $self->_drain_rbuf;
1743 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { 1759 unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
1744 Scalar::Util::weaken $self; 1760 Scalar::Util::weaken $self;
1745 1761
1746 $self->{_rw} = AE::io $self->{fh}, 0, sub { 1762 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1747 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1763 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1748 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1764 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size}, length $$rbuf;
1749 1765
1750 if ($len > 0) { 1766 if ($len > 0) {
1751 $self->{_activity} = $self->{_ractivity} = AE::now; 1767 $self->{_activity} = $self->{_ractivity} = AE::now;
1752 1768
1753 if ($self->{tls}) { 1769 if ($self->{tls}) {
1754 Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); 1770 Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf);
1755 1771
1756 &_dotls ($self); 1772 &_dotls ($self);
1757 } else { 1773 } else {
1758 $self->_drain_rbuf; 1774 $self->_drain_rbuf;
1775 }
1776
1777 if ($len == $self->{read_size}) {
1778 $self->{read_size} *= 2;
1779 $self->{read_size} = $self->{max_read_size} || MAX_READ_SIZE
1780 if $self->{read_size} > ($self->{max_read_size} || MAX_READ_SIZE);
1759 } 1781 }
1760 1782
1761 } elsif (defined $len) { 1783 } elsif (defined $len) {
1762 delete $self->{_rw}; 1784 delete $self->{_rw};
1763 $self->{_eof} = 1; 1785 $self->{_eof} = 1;
2004 push @linger, AE::io $fh, 1, sub { 2026 push @linger, AE::io $fh, 1, sub {
2005 my $len = syswrite $fh, $wbuf, length $wbuf; 2027 my $len = syswrite $fh, $wbuf, length $wbuf;
2006 2028
2007 if ($len > 0) { 2029 if ($len > 0) {
2008 substr $wbuf, 0, $len, ""; 2030 substr $wbuf, 0, $len, "";
2009 } else { 2031 } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) {
2010 @linger = (); # end 2032 @linger = (); # end
2011 } 2033 }
2012 }; 2034 };
2013 push @linger, AE::timer $linger, 0, sub { 2035 push @linger, AE::timer $linger, 0, sub {
2014 @linger = (); 2036 @linger = ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines