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.28 by root, Sat May 24 22:27:11 2008 UTC vs.
Revision 1.32 by root, Sun May 25 01:10:54 2008 UTC

2 2
3no warnings; 3no warnings;
4use strict; 4use strict;
5 5
6use AnyEvent (); 6use AnyEvent ();
7use AnyEvent::Util (); 7use AnyEvent::Util qw(WSAEAGAIN);
8use Scalar::Util (); 8use Scalar::Util ();
9use Carp (); 9use Carp ();
10use Fcntl (); 10use Fcntl ();
11use Errno qw/EAGAIN EINTR/; 11use Errno qw/EAGAIN EINTR/;
12 12
13=head1 NAME 13=head1 NAME
14 14
15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent 15AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent
16 16
17This module is experimental.
18
19=cut 17=cut
20 18
21our $VERSION = '0.04'; 19our $VERSION = '0.04';
22 20
23=head1 SYNOPSIS 21=head1 SYNOPSIS
25 use AnyEvent; 23 use AnyEvent;
26 use AnyEvent::Handle; 24 use AnyEvent::Handle;
27 25
28 my $cv = AnyEvent->condvar; 26 my $cv = AnyEvent->condvar;
29 27
30 my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); 28 my $handle =
31
32 #TODO
33
34 # or use the constructor to pass the callback:
35
36 my $ae_fh2 =
37 AnyEvent::Handle->new ( 29 AnyEvent::Handle->new (
38 fh => \*STDIN, 30 fh => \*STDIN,
39 on_eof => sub { 31 on_eof => sub {
40 $cv->broadcast; 32 $cv->broadcast;
41 }, 33 },
42 #TODO
43 ); 34 );
44 35
45 $cv->wait; 36 # send some request line
37 $handle->push_write ("getinfo\015\012");
38
39 # read the response line
40 $handle->push_read (line => sub {
41 my ($handle, $line) = @_;
42 warn "read line <$line>\n";
43 $cv->send;
44 });
45
46 $cv->recv;
46 47
47=head1 DESCRIPTION 48=head1 DESCRIPTION
48 49
49This module is a helper module to make it easier to do event-based I/O on 50This module is a helper module to make it easier to do event-based I/O on
50filehandles. For utility functions for doing non-blocking connects and accepts 51filehandles. For utility functions for doing non-blocking connects and accepts
90 91
91The object will not be in a usable state when this callback has been 92The object will not be in a usable state when this callback has been
92called. 93called.
93 94
94On callback entrance, the value of C<$!> contains the operating system 95On callback entrance, the value of C<$!> contains the operating system
95error (or C<ENOSPC> or C<EPIPE>). 96error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>).
96 97
97While not mandatory, it is I<highly> recommended to set this callback, as 98While not mandatory, it is I<highly> recommended to set this callback, as
98you will not be notified of errors otherwise. The default simply calls 99you will not be notified of errors otherwise. The default simply calls
99die. 100die.
100 101
168 169
169=back 170=back
170 171
171=cut 172=cut
172 173
173our (%RH, %WH);
174
175sub register_read_type($$) {
176 $RH{$_[0]} = $_[1];
177}
178
179sub register_write_type($$) {
180 $WH{$_[0]} = $_[1];
181}
182
183sub new { 174sub new {
184 my $class = shift; 175 my $class = shift;
185 176
186 my $self = bless { @_ }, $class; 177 my $self = bless { @_ }, $class;
187 178
221 } 212 }
222 213
223 if ($self->{on_error}) { 214 if ($self->{on_error}) {
224 $self->{on_error}($self); 215 $self->{on_error}($self);
225 } else { 216 } else {
226 die "AnyEvent::Handle uncaught fatal error: $!"; 217 Carp::croak "AnyEvent::Handle uncaught fatal error: $!";
227 } 218 }
228} 219}
229 220
230=item $fh = $handle->fh 221=item $fh = $handle->fh
231 222
297=cut 288=cut
298 289
299sub _drain_wbuf { 290sub _drain_wbuf {
300 my ($self) = @_; 291 my ($self) = @_;
301 292
302 unless ($self->{ww}) { 293 if (!$self->{ww} && length $self->{wbuf}) {
303 Scalar::Util::weaken $self; 294 Scalar::Util::weaken $self;
304 my $cb = sub { 295 my $cb = sub {
305 my $len = syswrite $self->{fh}, $self->{wbuf}; 296 my $len = syswrite $self->{fh}, $self->{wbuf};
306 297
307 if ($len > 0) { 298 if ($len >= 0) {
308 substr $self->{wbuf}, 0, $len, ""; 299 substr $self->{wbuf}, 0, $len, "";
309 300
310 $self->{on_drain}($self) 301 $self->{on_drain}($self)
311 if $self->{low_water_mark} >= length $self->{wbuf} 302 if $self->{low_water_mark} >= length $self->{wbuf}
312 && $self->{on_drain}; 303 && $self->{on_drain};
313 304
314 delete $self->{ww} unless length $self->{wbuf}; 305 delete $self->{ww} unless length $self->{wbuf};
315 } elsif ($! != EAGAIN && $! != EINTR) { 306 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEAGAIN) {
316 $self->error; 307 $self->error;
317 } 308 }
318 }; 309 };
319 310
320 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); 311 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb);
321 312
322 $cb->($self); 313 $cb->($self);
323 }; 314 };
324} 315}
325 316
317our %WH;
318
319sub register_write_type($$) {
320 $WH{$_[0]} = $_[1];
321}
322
326sub push_write { 323sub push_write {
327 my $self = shift; 324 my $self = shift;
325
326 if (@_ > 1) {
327 my $type = shift;
328
329 @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write")
330 ->($self, @_);
331 }
328 332
329 if ($self->{filter_w}) { 333 if ($self->{filter_w}) {
330 $self->{filter_w}->($self, \$_[0]); 334 $self->{filter_w}->($self, \$_[0]);
331 } else { 335 } else {
332 $self->{wbuf} .= $_[0]; 336 $self->{wbuf} .= $_[0];
333 $self->_drain_wbuf; 337 $self->_drain_wbuf;
334 } 338 }
335} 339}
340
341=item $handle->push_write (type => @args)
342
343=item $handle->unshift_write (type => @args)
344
345Instead of formatting your data yourself, you can also let this module do
346the job by specifying a type and type-specific arguments.
347
348Predefined types are (if you have ideas for additional types, feel free to
349drop by and tell us):
350
351=over 4
352
353=item netstring => $string
354
355Formats the given value as netstring
356(http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them).
357
358=back
359
360=cut
361
362register_write_type netstring => sub {
363 my ($self, $string) = @_;
364
365 sprintf "%d:%s,", (length $string), $string
366};
367
368=item AnyEvent::Handle::register_write_type type => $coderef->($self, @args)
369
370This function (not method) lets you add your own types to C<push_write>.
371Whenever the given C<type> is used, C<push_write> will invoke the code
372reference with the handle object and the remaining arguments.
373
374The code reference is supposed to return a single octet string that will
375be appended to the write buffer.
376
377Note that this is a function, and all types registered this way will be
378global, so try to use unique names.
379
380=cut
336 381
337############################################################################# 382#############################################################################
338 383
339=back 384=back
340 385
428 local $self->{in_drain} = 1; 473 local $self->{in_drain} = 1;
429 474
430 while (my $len = length $self->{rbuf}) { 475 while (my $len = length $self->{rbuf}) {
431 no strict 'refs'; 476 no strict 'refs';
432 if (my $cb = shift @{ $self->{queue} }) { 477 if (my $cb = shift @{ $self->{queue} }) {
433 if (!$cb->($self)) { 478 unless ($cb->($self)) {
434 if ($self->{eof}) { 479 if ($self->{eof}) {
435 # no progress can be made (not enough data and no data forthcoming) 480 # no progress can be made (not enough data and no data forthcoming)
436 $! = &Errno::EPIPE; return $self->error; 481 $! = &Errno::EPIPE; return $self->error;
437 } 482 }
438 483
515interested in (which can be none at all) and return a true value. After returning 560interested in (which can be none at all) and return a true value. After returning
516true, it will be removed from the queue. 561true, it will be removed from the queue.
517 562
518=cut 563=cut
519 564
565our %RH;
566
567sub register_read_type($$) {
568 $RH{$_[0]} = $_[1];
569}
570
520sub push_read { 571sub push_read {
521 my $self = shift; 572 my $self = shift;
522 my $cb = pop; 573 my $cb = pop;
523 574
524 if (@_) { 575 if (@_) {
554 605
555Instead of providing a callback that parses the data itself you can chose 606Instead of providing a callback that parses the data itself you can chose
556between a number of predefined parsing formats, for chunks of data, lines 607between a number of predefined parsing formats, for chunks of data, lines
557etc. 608etc.
558 609
559The types currently supported are: 610Predefined types are (if you have ideas for additional types, feel free to
611drop by and tell us):
560 612
561=over 4 613=over 4
562 614
563=item chunk => $octets, $cb->($self, $data) 615=item chunk => $octets, $cb->($self, $data)
564 616
638sub unshift_read_line { 690sub unshift_read_line {
639 my $self = shift; 691 my $self = shift;
640 $self->unshift_read (line => @_); 692 $self->unshift_read (line => @_);
641} 693}
642 694
695=item netstring => $cb->($string)
696
697A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement).
698
699Throws an error with C<$!> set to EBADMSG on format violations.
700
701=cut
702
703register_read_type netstring => sub {
704 my ($self, $cb) = @_;
705
706 sub {
707 unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) {
708 if ($_[0]{rbuf} =~ /[^0-9]/) {
709 $! = &Errno::EBADMSG;
710 $self->error;
711 }
712 return;
713 }
714
715 my $len = $1;
716
717 $self->unshift_read (chunk => $len, sub {
718 my $string = $_[1];
719 $_[0]->unshift_read (chunk => 1, sub {
720 if ($_[1] eq ",") {
721 $cb->($_[0], $string);
722 } else {
723 $! = &Errno::EBADMSG;
724 $self->error;
725 }
726 });
727 });
728
729 1
730 }
731};
732
643=back 733=back
734
735=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args)
736
737This function (not method) lets you add your own types to C<push_read>.
738
739Whenever the given C<type> is used, C<push_read> will invoke the code
740reference with the handle object, the callback and the remaining
741arguments.
742
743The code reference is supposed to return a callback (usually a closure)
744that works as a plain read callback (see C<< ->push_read ($cb) >>).
745
746It should invoke the passed callback when it is done reading (remember to
747pass C<$self> as first argument as all other callbacks do that).
748
749Note that this is a function, and all types registered this way will be
750global, so try to use unique names.
751
752For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>,
753search for C<register_read_type>)).
644 754
645=item $handle->stop_read 755=item $handle->stop_read
646 756
647=item $handle->start_read 757=item $handle->start_read
648 758
677 } elsif (defined $len) { 787 } elsif (defined $len) {
678 delete $self->{rw}; 788 delete $self->{rw};
679 $self->{eof} = 1; 789 $self->{eof} = 1;
680 $self->_drain_rbuf; 790 $self->_drain_rbuf;
681 791
682 } elsif ($! != EAGAIN && $! != EINTR) { 792 } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAEAGAIN) {
683 return $self->error; 793 return $self->error;
684 } 794 }
685 }); 795 });
686 } 796 }
687} 797}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines