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.172 by root, Wed Aug 5 20:50:27 2009 UTC vs.
Revision 1.177 by root, Sun Aug 9 00:24:35 2009 UTC

1package AnyEvent::Handle;
2
3use Scalar::Util ();
4use Carp ();
5use Errno qw(EAGAIN EINTR);
6
7use AnyEvent (); BEGIN { AnyEvent::common_sense }
8use AnyEvent::Util qw(WSAEWOULDBLOCK);
9
10=head1 NAME 1=head1 NAME
11 2
12AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 3AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
13
14=cut
15
16our $VERSION = 4.901;
17 4
18=head1 SYNOPSIS 5=head1 SYNOPSIS
19 6
20 use AnyEvent; 7 use AnyEvent;
21 use AnyEvent::Handle; 8 use AnyEvent::Handle;
59C<on_error> callback. 46C<on_error> callback.
60 47
61All callbacks will be invoked with the handle object as their first 48All callbacks will be invoked with the handle object as their first
62argument. 49argument.
63 50
51=cut
52
53package AnyEvent::Handle;
54
55use Scalar::Util ();
56use List::Util ();
57use Carp ();
58use Errno qw(EAGAIN EINTR);
59
60use AnyEvent (); BEGIN { AnyEvent::common_sense }
61use AnyEvent::Util qw(WSAEWOULDBLOCK);
62
63our $VERSION = $AnyEvent::VERSION;
64
64=head1 METHODS 65=head1 METHODS
65 66
66=over 4 67=over 4
67 68
68=item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... 69=item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value...
216memory and push it into the queue, but instead only read more data from 217memory and push it into the queue, but instead only read more data from
217the file when the write queue becomes empty. 218the file when the write queue becomes empty.
218 219
219=item timeout => $fractional_seconds 220=item timeout => $fractional_seconds
220 221
222=item rtimeout => $fractional_seconds
223
224=item wtimeout => $fractional_seconds
225
221If non-zero, then this enables an "inactivity" timeout: whenever this many 226If non-zero, then these enables an "inactivity" timeout: whenever this
222seconds pass without a successful read or write on the underlying file 227many seconds pass without a successful read or write on the underlying
223handle, the C<on_timeout> callback will be invoked (and if that one is 228file handle (or a call to C<timeout_reset>), the C<on_timeout> callback
224missing, a non-fatal C<ETIMEDOUT> error will be raised). 229will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT>
230error will be raised).
231
232There are three variants of the timeouts that work fully independent
233of each other, for both read and write, just read, and just write:
234C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks
235C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions
236C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>.
225 237
226Note that timeout processing is also active when you currently do not have 238Note that timeout processing is also active when you currently do not have
227any outstanding read or write requests: If you plan to keep the connection 239any outstanding read or write requests: If you plan to keep the connection
228idle then you should disable the timout temporarily or ignore the timeout 240idle then you should disable the timout temporarily or ignore the timeout
229in the C<on_timeout> callback, in which case AnyEvent::Handle will simply 241in the C<on_timeout> callback, in which case AnyEvent::Handle will simply
474sub _start { 486sub _start {
475 my ($self) = @_; 487 my ($self) = @_;
476 488
477 AnyEvent::Util::fh_nonblocking $self->{fh}, 1; 489 AnyEvent::Util::fh_nonblocking $self->{fh}, 1;
478 490
491 $self->{_activity} =
492 $self->{_ractivity} =
479 $self->{_activity} = AnyEvent->now; 493 $self->{_wactivity} = AE::now;
480 $self->_timeout; 494
495 $self->timeout (delete $self->{timeout} ) if $self->{timeout};
496 $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout};
497 $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout};
481 498
482 $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; 499 $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay};
483 500
484 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) 501 $self->starttls (delete $self->{tls}, delete $self->{tls_ctx})
485 if $self->{tls}; 502 if $self->{tls};
544 $_[0]{on_eof} = $_[1]; 561 $_[0]{on_eof} = $_[1];
545} 562}
546 563
547=item $handle->on_timeout ($cb) 564=item $handle->on_timeout ($cb)
548 565
549Replace the current C<on_timeout> callback, or disables the callback (but 566=item $handle->on_rtimeout ($cb)
550not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor
551argument and method.
552 567
553=cut 568=item $handle->on_wtimeout ($cb)
554 569
555sub on_timeout { 570Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout>
556 $_[0]{on_timeout} = $_[1]; 571callback, or disables the callback (but not the timeout) if C<$cb> =
557} 572C<undef>. See the C<timeout> constructor argument and method.
573
574=cut
575
576# see below
558 577
559=item $handle->autocork ($boolean) 578=item $handle->autocork ($boolean)
560 579
561Enables or disables the current autocork behaviour (see C<autocork> 580Enables or disables the current autocork behaviour (see C<autocork>
562constructor argument). Changes will only take effect on the next write. 581constructor argument). Changes will only take effect on the next write.
616 635
617############################################################################# 636#############################################################################
618 637
619=item $handle->timeout ($seconds) 638=item $handle->timeout ($seconds)
620 639
640=item $handle->rtimeout ($seconds)
641
642=item $handle->wtimeout ($seconds)
643
621Configures (or disables) the inactivity timeout. 644Configures (or disables) the inactivity timeout.
622 645
623=cut 646=item $handle->timeout_reset
624 647
625sub timeout { 648=item $handle->rtimeout_reset
649
650=item $handle->wtimeout_reset
651
652Reset the activity timeout, as if data was received or sent.
653
654These methods are cheap to call.
655
656=cut
657
658for my $dir ("", "r", "w") {
659 my $timeout = "${dir}timeout";
660 my $tw = "_${dir}tw";
661 my $on_timeout = "on_${dir}timeout";
662 my $activity = "_${dir}activity";
663 my $cb;
664
665 *$on_timeout = sub {
666 $_[0]{$on_timeout} = $_[1];
667 };
668
669 *$timeout = sub {
626 my ($self, $timeout) = @_; 670 my ($self, $new_value) = @_;
627 671
628 $self->{timeout} = $timeout; 672 $self->{$timeout} = $new_value;
629 $self->_timeout; 673 delete $self->{$tw}; &$cb;
630} 674 };
631 675
676 *{"${dir}timeout_reset"} = sub {
677 $_[0]{$activity} = AE::now;
678 };
679
680 # main workhorse:
632# reset the timeout watcher, as neccessary 681 # reset the timeout watcher, as neccessary
633# also check for time-outs 682 # also check for time-outs
634sub _timeout { 683 $cb = sub {
635 my ($self) = @_; 684 my ($self) = @_;
636 685
637 if ($self->{timeout} && $self->{fh}) { 686 if ($self->{$timeout} && $self->{fh}) {
638 my $NOW = AnyEvent->now; 687 my $NOW = AE::now;
639 688
640 # when would the timeout trigger? 689 # when would the timeout trigger?
641 my $after = $self->{_activity} + $self->{timeout} - $NOW; 690 my $after = $self->{$activity} + $self->{$timeout} - $NOW;
642 691
643 # now or in the past already? 692 # now or in the past already?
644 if ($after <= 0) { 693 if ($after <= 0) {
645 $self->{_activity} = $NOW; 694 $self->{$activity} = $NOW;
646 695
647 if ($self->{on_timeout}) { 696 if ($self->{$on_timeout}) {
648 $self->{on_timeout}($self); 697 $self->{$on_timeout}($self);
649 } else { 698 } else {
650 $self->_error (Errno::ETIMEDOUT); 699 $self->_error (Errno::ETIMEDOUT);
700 }
701
702 # callback could have changed timeout value, optimise
703 return unless $self->{$timeout};
704
705 # calculate new after
706 $after = $self->{$timeout};
651 } 707 }
652 708
653 # callback could have changed timeout value, optimise 709 Scalar::Util::weaken $self;
654 return unless $self->{timeout}; 710 return unless $self; # ->error could have destroyed $self
655 711
656 # calculate new after 712 $self->{$tw} ||= AE::timer $after, 0, sub {
657 $after = $self->{timeout}; 713 delete $self->{$tw};
714 $cb->($self);
715 };
716 } else {
717 delete $self->{$tw};
658 } 718 }
659
660 Scalar::Util::weaken $self;
661 return unless $self; # ->error could have destroyed $self
662
663 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub {
664 delete $self->{_tw};
665 $self->_timeout;
666 });
667 } else {
668 delete $self->{_tw};
669 } 719 }
670} 720}
671 721
672############################################################################# 722#############################################################################
673 723
721 my $len = syswrite $self->{fh}, $self->{wbuf}; 771 my $len = syswrite $self->{fh}, $self->{wbuf};
722 772
723 if (defined $len) { 773 if (defined $len) {
724 substr $self->{wbuf}, 0, $len, ""; 774 substr $self->{wbuf}, 0, $len, "";
725 775
726 $self->{_activity} = AnyEvent->now; 776 $self->{_activity} = $self->{_wactivity} = AE::now;
727 777
728 $self->{on_drain}($self) 778 $self->{on_drain}($self)
729 if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) 779 if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf})
730 && $self->{on_drain}; 780 && $self->{on_drain};
731 781
737 787
738 # try to write data immediately 788 # try to write data immediately
739 $cb->() unless $self->{autocork}; 789 $cb->() unless $self->{autocork};
740 790
741 # if still data left in wbuf, we need to poll 791 # if still data left in wbuf, we need to poll
742 $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) 792 $self->{_ww} = AE::io $self->{fh}, 1, $cb
743 if length $self->{wbuf}; 793 if length $self->{wbuf};
744 }; 794 };
745} 795}
746 796
747our %WH; 797our %WH;
1539 my ($self) = @_; 1589 my ($self) = @_;
1540 1590
1541 unless ($self->{_rw} || $self->{_eof}) { 1591 unless ($self->{_rw} || $self->{_eof}) {
1542 Scalar::Util::weaken $self; 1592 Scalar::Util::weaken $self;
1543 1593
1544 $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { 1594 $self->{_rw} = AE::io $self->{fh}, 0, sub {
1545 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); 1595 my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
1546 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1596 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1547 1597
1548 if ($len > 0) { 1598 if ($len > 0) {
1549 $self->{_activity} = AnyEvent->now; 1599 $self->{_activity} = $self->{_ractivity} = AE::now;
1550 1600
1551 if ($self->{tls}) { 1601 if ($self->{tls}) {
1552 Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); 1602 Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf);
1553 1603
1554 &_dotls ($self); 1604 &_dotls ($self);
1562 $self->_drain_rbuf; 1612 $self->_drain_rbuf;
1563 1613
1564 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { 1614 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) {
1565 return $self->_error ($!, 1); 1615 return $self->_error ($!, 1);
1566 } 1616 }
1567 }); 1617 };
1568 } 1618 }
1569} 1619}
1570 1620
1571our $ERROR_SYSCALL; 1621our $ERROR_SYSCALL;
1572our $ERROR_WANT_READ; 1622our $ERROR_WANT_READ;
1790 my $fh = delete $self->{fh}; 1840 my $fh = delete $self->{fh};
1791 my $wbuf = delete $self->{wbuf}; 1841 my $wbuf = delete $self->{wbuf};
1792 1842
1793 my @linger; 1843 my @linger;
1794 1844
1795 push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { 1845 push @linger, AE::io $fh, 1, sub {
1796 my $len = syswrite $fh, $wbuf, length $wbuf; 1846 my $len = syswrite $fh, $wbuf, length $wbuf;
1797 1847
1798 if ($len > 0) { 1848 if ($len > 0) {
1799 substr $wbuf, 0, $len, ""; 1849 substr $wbuf, 0, $len, "";
1800 } else { 1850 } else {
1801 @linger = (); # end 1851 @linger = (); # end
1802 } 1852 }
1803 }); 1853 };
1804 push @linger, AnyEvent->timer (after => $linger, cb => sub { 1854 push @linger, AE::timer $linger, 0, sub {
1805 @linger = (); 1855 @linger = ();
1806 }); 1856 };
1807 } 1857 }
1808} 1858}
1809 1859
1810=item $handle->destroy 1860=item $handle->destroy
1811 1861

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines