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.43 by root, Wed May 28 23:57:38 2008 UTC vs.
Revision 1.45 by root, Thu May 29 00:20:39 2008 UTC

7use AnyEvent::Util qw(WSAEWOULDBLOCK); 7use AnyEvent::Util qw(WSAEWOULDBLOCK);
8use Scalar::Util (); 8use Scalar::Util ();
9use Carp (); 9use Carp ();
10use Fcntl (); 10use Fcntl ();
11use Errno qw(EAGAIN EINTR); 11use Errno qw(EAGAIN EINTR);
12use Time::HiRes qw(time);
13 12
14=head1 NAME 13=head1 NAME
15 14
16AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
17 16
126=item timeout => $fractional_seconds 125=item timeout => $fractional_seconds
127 126
128If non-zero, then this enables an "inactivity" timeout: whenever this many 127If non-zero, then this enables an "inactivity" timeout: whenever this many
129seconds pass without a successful read or write on the underlying file 128seconds pass without a successful read or write on the underlying file
130handle, the C<on_timeout> callback will be invoked (and if that one is 129handle, the C<on_timeout> callback will be invoked (and if that one is
131missing, an C<ETIMEDOUT> errror will be raised). 130missing, an C<ETIMEDOUT> error will be raised).
132 131
133Note that timeout processing is also active when you currently do not have 132Note that timeout processing is also active when you currently do not have
134any outstanding read or write requests: If you plan to keep the connection 133any outstanding read or write requests: If you plan to keep the connection
135idle then you should disable the timout temporarily or ignore the timeout 134idle then you should disable the timout temporarily or ignore the timeout
136in the C<on_timeout> callback. 135in the C<on_timeout> callback.
228# $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop 227# $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop
229# $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop 228# $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop
230# $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop 229# $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop
231 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 230 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
232 231
233 $self->{_activity} = time; 232 $self->{_activity} = AnyEvent->now;
234 $self->_timeout; 233 $self->_timeout;
235 234
236 $self->start_read; 235 $self->start_read;
237 236
238 $self 237 $self
319# also check for time-outs 318# also check for time-outs
320sub _timeout { 319sub _timeout {
321 my ($self) = @_; 320 my ($self) = @_;
322 321
323 if ($self->{timeout}) { 322 if ($self->{timeout}) {
324 my $NOW = time; 323 my $NOW = AnyEvent->now;
325 324
326 # when would the timeout trigger? 325 # when would the timeout trigger?
327 my $after = $self->{_activity} + $self->{timeout} - $NOW; 326 my $after = $self->{_activity} + $self->{timeout} - $NOW;
328
329 warn "next to in $after\n";#d#
330 327
331 # now or in the past already? 328 # now or in the past already?
332 if ($after <= 0) { 329 if ($after <= 0) {
333 $self->{_activity} = $NOW; 330 $self->{_activity} = $NOW;
334 331
346 $after = $self->{timeout}; 343 $after = $self->{timeout};
347 } 344 }
348 345
349 Scalar::Util::weaken $self; 346 Scalar::Util::weaken $self;
350 347
351 warn "after $after\n";#d#
352 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { 348 $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub {
353 delete $self->{_tw}; 349 delete $self->{_tw};
354 $self->_timeout; 350 $self->_timeout;
355 }); 351 });
356 } else { 352 } else {
410 my $len = syswrite $self->{fh}, $self->{wbuf}; 406 my $len = syswrite $self->{fh}, $self->{wbuf};
411 407
412 if ($len >= 0) { 408 if ($len >= 0) {
413 substr $self->{wbuf}, 0, $len, ""; 409 substr $self->{wbuf}, 0, $len, "";
414 410
415 $self->{_activity} = time; 411 $self->{_activity} = AnyEvent->now;
416 412
417 $self->{on_drain}($self) 413 $self->{on_drain}($self)
418 if $self->{low_water_mark} >= length $self->{wbuf} 414 if $self->{low_water_mark} >= length $self->{wbuf}
419 && $self->{on_drain}; 415 && $self->{on_drain};
420 416
1056 $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { 1052 $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub {
1057 my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; 1053 my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf};
1058 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; 1054 my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf;
1059 1055
1060 if ($len > 0) { 1056 if ($len > 0) {
1061 $self->{_activity} = time; 1057 $self->{_activity} = AnyEvent->now;
1062 1058
1063 $self->{filter_r} 1059 $self->{filter_r}
1064 ? $self->{filter_r}->($self, $rbuf) 1060 ? $self->{filter_r}->($self, $rbuf)
1065 : $self->_drain_rbuf; 1061 : $self->_drain_rbuf;
1066 1062

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines