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.29 by root, Sat May 24 23:10:18 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
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
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 };
315}
316
317our %WH;
318
319sub register_write_type($$) {
320 $WH{$_[0]} = $_[1];
324} 321}
325 322
326sub push_write { 323sub push_write {
327 my $self = shift; 324 my $self = shift;
328 325
346=item $handle->unshift_write (type => @args) 343=item $handle->unshift_write (type => @args)
347 344
348Instead of formatting your data yourself, you can also let this module do 345Instead of formatting your data yourself, you can also let this module do
349the job by specifying a type and type-specific arguments. 346the job by specifying a type and type-specific arguments.
350 347
351Predefined types are: 348Predefined types are (if you have ideas for additional types, feel free to
349drop by and tell us):
352 350
353=over 4 351=over 4
354 352
355=item netstring => $string 353=item netstring => $string
356 354
357Formats the given value as netstring 355Formats the given value as netstring
358(http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). 356(http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them).
359 357
358=back
359
360=cut 360=cut
361 361
362register_write_type netstring => sub { 362register_write_type netstring => sub {
363 my ($self, $string) = @_; 363 my ($self, $string) = @_;
364 364
365 sprintf "%d:%s,", (length $string), $string 365 sprintf "%d:%s,", (length $string), $string
366}; 366};
367 367
368=back 368=item AnyEvent::Handle::register_write_type type => $coderef->($self, @args)
369 369
370=cut 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.
371 373
374The code reference is supposed to return a single octet string that will
375be appended to the write buffer.
372 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
373 381
374############################################################################# 382#############################################################################
375 383
376=back 384=back
377 385
552interested 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
553true, it will be removed from the queue. 561true, it will be removed from the queue.
554 562
555=cut 563=cut
556 564
565our %RH;
566
567sub register_read_type($$) {
568 $RH{$_[0]} = $_[1];
569}
570
557sub push_read { 571sub push_read {
558 my $self = shift; 572 my $self = shift;
559 my $cb = pop; 573 my $cb = pop;
560 574
561 if (@_) { 575 if (@_) {
591 605
592Instead 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
593between a number of predefined parsing formats, for chunks of data, lines 607between a number of predefined parsing formats, for chunks of data, lines
594etc. 608etc.
595 609
596The types currently supported are: 610Predefined types are (if you have ideas for additional types, feel free to
611drop by and tell us):
597 612
598=over 4 613=over 4
599 614
600=item chunk => $octets, $cb->($self, $data) 615=item chunk => $octets, $cb->($self, $data)
601 616
715 } 730 }
716}; 731};
717 732
718=back 733=back
719 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>)).
754
720=item $handle->stop_read 755=item $handle->stop_read
721 756
722=item $handle->start_read 757=item $handle->start_read
723 758
724In rare cases you actually do not want to read anything from the 759In rare cases you actually do not want to read anything from the
752 } elsif (defined $len) { 787 } elsif (defined $len) {
753 delete $self->{rw}; 788 delete $self->{rw};
754 $self->{eof} = 1; 789 $self->{eof} = 1;
755 $self->_drain_rbuf; 790 $self->_drain_rbuf;
756 791
757 } elsif ($! != EAGAIN && $! != EINTR) { 792 } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAEAGAIN) {
758 return $self->error; 793 return $self->error;
759 } 794 }
760 }); 795 });
761 } 796 }
762} 797}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines