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.9 by root, Fri May 2 16:07:46 2008 UTC vs.
Revision 1.10 by root, Sat May 3 12:17:35 2008 UTC

70The filehandle this L<AnyEvent::Handle> object will operate on. 70The filehandle this L<AnyEvent::Handle> object will operate on.
71 71
72NOTE: The filehandle will be set to non-blocking (using 72NOTE: The filehandle will be set to non-blocking (using
73AnyEvent::Util::fh_nonblocking). 73AnyEvent::Util::fh_nonblocking).
74 74
75=item on_error => $cb->($self) [MANDATORY] 75=item on_eof => $cb->($self) [MANDATORY]
76 76
77Set the callback to be called on EOF.
78
79=item on_error => $cb->($self)
80
77This is the fatal error callback, that is called when a fatal error ocurs, 81This is the fatal error callback, that is called when, well, a fatal error
78such as not being able to resolve the hostname, failure to connect or a 82ocurs, such as not being able to resolve the hostname, failure to connect
79read error. 83or a read error.
80 84
81The object will not be in a usable state when this callback has been 85The object will not be in a usable state when this callback has been
82called. 86called.
83 87
84On callback entrance, the value of C<$!> contains the opertaing system 88On callback entrance, the value of C<$!> contains the operating system
85error (or C<ENOSPC> or C<EPIPE>). 89error (or C<ENOSPC> or C<EPIPE>).
86 90
87=item on_eof => $cb->($self) [MANDATORY] 91While not mandatory, it is I<highly> recommended to set this callback, as
88 92you will not be notified of errors otherwise. The default simply calls
89Set the callback to be called on EOF. 93die.
90 94
91=item on_read => $cb->($self) 95=item on_read => $cb->($self)
92 96
93This sets the default read callback, which is called when data arrives 97This sets the default read callback, which is called when data arrives
94and no read request is in the queue. If the read callback is C<undef> 98and no read request is in the queue.
95or has never been set, than AnyEvent::Handle will cease reading from the
96filehandle.
97 99
98To access (and remove data from) the read buffer, use the C<< ->rbuf >> 100To access (and remove data from) the read buffer, use the C<< ->rbuf >>
99method or acces sthe C<$self->{rbuf}> member directly. 101method or acces sthe C<$self->{rbuf}> member directly.
100 102
101When an EOF condition is detected then AnyEvent::Handle will first try to 103When an EOF condition is detected then AnyEvent::Handle will first try to
144 146
145 $self->{fh} or Carp::croak "mandatory argument fh is missing"; 147 $self->{fh} or Carp::croak "mandatory argument fh is missing";
146 148
147 AnyEvent::Util::fh_nonblocking $self->{fh}, 1; 149 AnyEvent::Util::fh_nonblocking $self->{fh}, 1;
148 150
149 $self->on_error ((delete $self->{on_error}) or Carp::croak "mandatory argument on_error is missing");
150 $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); 151 $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing");
151 152
153 $self->on_error (delete $self->{on_error}) if $self->{on_error};
152 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; 154 $self->on_drain (delete $self->{on_drain}) if $self->{on_drain};
153 $self->on_read (delete $self->{on_read} ) if $self->{on_read}; 155 $self->on_read (delete $self->{on_read} ) if $self->{on_read};
156
157 $self->start_read;
154 158
155 $self 159 $self
156} 160}
157 161
158sub _shutdown { 162sub _shutdown {
169 { 173 {
170 local $!; 174 local $!;
171 $self->_shutdown; 175 $self->_shutdown;
172 } 176 }
173 177
178 if ($self->{on_error}) {
174 $self->{on_error}($self); 179 $self->{on_error}($self);
180 } else {
181 die "AnyEvent::Handle uncaught fatal error: $!";
182 }
175} 183}
176 184
177=item $fh = $handle->fh 185=item $fh = $handle->fh
178 186
179This method returns the filehandle of the L<AnyEvent::Handle> object. 187This method returns the filehandle of the L<AnyEvent::Handle> object.
349 ... 357 ...
350 }); 358 });
351 359
352=over 4 360=over 4
353 361
362=cut
363
354sub _drain_rbuf { 364sub _drain_rbuf {
355 my ($self) = @_; 365 my ($self) = @_;
356 366
357 return if exists $self->{in_drain}; 367 return if exists $self->{in_drain};
358 local $self->{in_drain} = 1; 368 local $self->{in_drain} = 1;
359 369
360 while (my $len = length $self->{rbuf}) { 370 while (my $len = length $self->{rbuf}) {
361 no strict 'refs'; 371 no strict 'refs';
362 if (@{ $self->{queue} }) { 372 if (my $cb = shift @{ $self->{queue} }) {
363 if ($self->{queue}[0]($self)) { 373 if (!$cb->($self)) {
364 shift @{ $self->{queue} };
365 } elsif ($self->{eof}) { 374 if ($self->{eof}) {
366 # no progress can be made (not enough data and no data forthcoming) 375 # no progress can be made (not enough data and no data forthcoming)
367 $! = &Errno::EPIPE; return $self->error; 376 $! = &Errno::EPIPE; return $self->error;
368 } else { 377 }
378
379 unshift @{ $self->{queue} }, $cb;
369 return; 380 return;
370 } 381 }
371 } elsif ($self->{on_read}) { 382 } elsif ($self->{on_read}) {
372 $self->{on_read}($self); 383 $self->{on_read}($self);
373 384
403 414
404sub on_read { 415sub on_read {
405 my ($self, $cb) = @_; 416 my ($self, $cb) = @_;
406 417
407 $self->{on_read} = $cb; 418 $self->{on_read} = $cb;
419}
420
421=item $handle->rbuf
422
423Returns the read buffer (as a modifiable lvalue).
424
425You can access the read buffer directly as the C<< ->{rbuf} >> member, if
426you want.
427
428NOTE: The read buffer should only be used or modified if the C<on_read>,
429C<push_read> or C<unshift_read> methods are used. The other read methods
430automatically manage the read buffer.
431
432=cut
433
434sub rbuf : lvalue {
435 $_[0]{rbuf}
436}
437
438=item $handle->push_read ($cb)
439
440=item $handle->unshift_read ($cb)
441
442Append the given callback to the end of the queue (C<push_read>) or
443prepend it (C<unshift_read>).
444
445The callback is called each time some additional read data arrives.
446
447It must check wether enough data is in the read buffer already.
448
449If not enough data is available, it must return the empty list or a false
450value, in which case it will be called repeatedly until enough data is
451available (or an error condition is detected).
452
453If enough data was available, then the callback must remove all data it is
454interested in (which can be none at all) and return a true value. After returning
455true, it will be removed from the queue.
456
457=cut
458
459sub push_read {
460 my ($self, $cb) = @_;
461
462 push @{ $self->{queue} }, $cb;
463 $self->_drain_rbuf;
464}
465
466sub unshift_read {
467 my ($self, $cb) = @_;
468
469 push @{ $self->{queue} }, $cb;
470 $self->_drain_rbuf;
471}
472
473=item $handle->push_read_chunk ($len, $cb->($self, $data))
474
475=item $handle->unshift_read_chunk ($len, $cb->($self, $data))
476
477Append the given callback to the end of the queue (C<push_read_chunk>) or
478prepend it (C<unshift_read_chunk>).
479
480The callback will be called only once C<$len> bytes have been read, and
481these C<$len> bytes will be passed to the callback.
482
483=cut
484
485sub _read_chunk($$) {
486 my ($self, $len, $cb) = @_;
487
488 sub {
489 $len <= length $_[0]{rbuf} or return;
490 $cb->($self, $_[0], substr $_[0]{rbuf}, 0, $len, "");
491 1
492 }
493}
494
495sub push_read_chunk {
496 $_[0]->push_read (&_read_chunk);
497}
498
499
500sub unshift_read_chunk {
501 $_[0]->unshift_read (&_read_chunk);
502}
503
504=item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol))
505
506=item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol))
507
508Append the given callback to the end of the queue (C<push_read_line>) or
509prepend it (C<unshift_read_line>).
510
511The callback will be called only once a full line (including the end of
512line marker, C<$eol>) has been read. This line (excluding the end of line
513marker) will be passed to the callback as second argument (C<$line>), and
514the end of line marker as the third argument (C<$eol>).
515
516The end of line marker, C<$eol>, can be either a string, in which case it
517will be interpreted as a fixed record end marker, or it can be a regex
518object (e.g. created by C<qr>), in which case it is interpreted as a
519regular expression.
520
521The end of line marker argument C<$eol> is optional, if it is missing (NOT
522undef), then C<qr|\015?\012|> is used (which is good for most internet
523protocols).
524
525Partial lines at the end of the stream will never be returned, as they are
526not marked by the end of line marker.
527
528=cut
529
530sub _read_line($$) {
531 my $self = shift;
532 my $cb = pop;
533 my $eol = @_ ? shift : qr|(\015?\012)|;
534 my $pos;
535
536 $eol = qr|(\Q$eol\E)| unless ref $eol;
537 $eol = qr|^(.*?)($eol)|;
538
539 sub {
540 $_[0]{rbuf} =~ s/$eol// or return;
541
542 $cb->($self, $1, $2);
543 1
544 }
545}
546
547sub push_read_line {
548 $_[0]->push_read (&_read_line);
549}
550
551sub unshift_read_line {
552 $_[0]->unshift_read (&_read_line);
553}
554
555=item $handle->stop_read
556
557=item $handle->start_read
558
559In rare cases you actually do not want to read anything form the
560socket. In this case you can call C<stop_read>. Neither C<on_read> no
561any queued callbacks will be executed then. To start readign again, call
562C<start_read>.
563
564=cut
565
566sub stop_read {
567 my ($self) = @_;
568
569 delete $self->{rw};
570}
571
572sub start_read {
573 my ($self) = @_;
408 574
409 unless ($self->{rw} || $self->{eof}) { 575 unless ($self->{rw} || $self->{eof}) {
410 Scalar::Util::weaken $self; 576 Scalar::Util::weaken $self;
411 577
412 $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { 578 $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub {
430 $self->_drain_rbuf; 596 $self->_drain_rbuf;
431 }); 597 });
432 } 598 }
433} 599}
434 600
435=item $handle->rbuf
436
437Returns the read buffer (as a modifiable lvalue).
438
439You can access the read buffer directly as the C<< ->{rbuf} >> member, if
440you want.
441
442NOTE: The read buffer should only be used or modified if the C<on_read>,
443C<push_read> or C<unshift_read> methods are used. The other read methods
444automatically manage the read buffer.
445
446=cut
447
448sub rbuf : lvalue {
449 $_[0]{rbuf}
450}
451
452=item $handle->push_read ($cb)
453
454=item $handle->unshift_read ($cb)
455
456Append the given callback to the end of the queue (C<push_read>) or
457prepend it (C<unshift_read>).
458
459The callback is called each time some additional read data arrives.
460
461It must check wether enough data is in the read buffer already.
462
463If not enough data is available, it must return the empty list or a false
464value, in which case it will be called repeatedly until enough data is
465available (or an error condition is detected).
466
467If enough data was available, then the callback must remove all data it is
468interested in (which can be none at all) and return a true value. After returning
469true, it will be removed from the queue.
470
471=cut
472
473sub push_read {
474 my ($self, $cb) = @_;
475
476 push @{ $self->{queue} }, $cb;
477 $self->_drain_rbuf;
478}
479
480sub unshift_read {
481 my ($self, $cb) = @_;
482
483 push @{ $self->{queue} }, $cb;
484 $self->_drain_rbuf;
485}
486
487=item $handle->push_read_chunk ($len, $cb->($self, $data))
488
489=item $handle->unshift_read_chunk ($len, $cb->($self, $data))
490
491Append the given callback to the end of the queue (C<push_read_chunk>) or
492prepend it (C<unshift_read_chunk>).
493
494The callback will be called only once C<$len> bytes have been read, and
495these C<$len> bytes will be passed to the callback.
496
497=cut
498
499sub _read_chunk($$) {
500 my ($len, $cb) = @_;
501
502 sub {
503 $len <= length $_[0]{rbuf} or return;
504 $cb->($_[0], substr $_[0]{rbuf}, 0, $len, "");
505 1
506 }
507}
508
509sub push_read_chunk {
510 my ($self, $len, $cb) = @_;
511
512 $self->push_read (_read_chunk $len, $cb);
513}
514
515
516sub unshift_read_chunk {
517 my ($self, $len, $cb) = @_;
518
519 $self->unshift_read (_read_chunk $len, $cb);
520}
521
522=item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol))
523
524=item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol))
525
526Append the given callback to the end of the queue (C<push_read_line>) or
527prepend it (C<unshift_read_line>).
528
529The callback will be called only once a full line (including the end of
530line marker, C<$eol>) has been read. This line (excluding the end of line
531marker) will be passed to the callback as second argument (C<$line>), and
532the end of line marker as the third argument (C<$eol>).
533
534The end of line marker, C<$eol>, can be either a string, in which case it
535will be interpreted as a fixed record end marker, or it can be a regex
536object (e.g. created by C<qr>), in which case it is interpreted as a
537regular expression.
538
539The end of line marker argument C<$eol> is optional, if it is missing (NOT
540undef), then C<qr|\015?\012|> is used (which is good for most internet
541protocols).
542
543Partial lines at the end of the stream will never be returned, as they are
544not marked by the end of line marker.
545
546=cut
547
548sub _read_line($$) {
549 my $cb = pop;
550 my $eol = @_ ? shift : qr|(\015?\012)|;
551 my $pos;
552
553 $eol = qr|(\Q$eol\E)| unless ref $eol;
554 $eol = qr|^(.*?)($eol)|;
555
556 sub {
557 $_[0]{rbuf} =~ s/$eol// or return;
558
559 $cb->($1, $2);
560 1
561 }
562}
563
564sub push_read_line {
565 my $self = shift;
566
567 $self->push_read (&_read_line);
568}
569
570sub unshift_read_line {
571 my $self = shift;
572
573 $self->unshift_read (&_read_line);
574}
575
576=back 601=back
577 602
578=head1 AUTHOR 603=head1 AUTHOR
579 604
580Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. 605Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines