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.30 by root, Sat May 24 23:56:26 2008 UTC vs.
Revision 1.36 by root, Mon May 26 18:26:52 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(WSAWOULDBLOCK);
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
288 289
289sub _drain_wbuf { 290sub _drain_wbuf {
290 my ($self) = @_; 291 my ($self) = @_;
291 292
292 if (!$self->{ww} && length $self->{wbuf}) { 293 if (!$self->{ww} && length $self->{wbuf}) {
294
293 Scalar::Util::weaken $self; 295 Scalar::Util::weaken $self;
296
294 my $cb = sub { 297 my $cb = sub {
295 my $len = syswrite $self->{fh}, $self->{wbuf}; 298 my $len = syswrite $self->{fh}, $self->{wbuf};
296 299
297 if ($len >= 0) { 300 if ($len >= 0) {
298 substr $self->{wbuf}, 0, $len, ""; 301 substr $self->{wbuf}, 0, $len, "";
300 $self->{on_drain}($self) 303 $self->{on_drain}($self)
301 if $self->{low_water_mark} >= length $self->{wbuf} 304 if $self->{low_water_mark} >= length $self->{wbuf}
302 && $self->{on_drain}; 305 && $self->{on_drain};
303 306
304 delete $self->{ww} unless length $self->{wbuf}; 307 delete $self->{ww} unless length $self->{wbuf};
305 } elsif ($! != EAGAIN && $! != EINTR) { 308 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) {
306 $self->error; 309 $self->error;
307 } 310 }
308 }; 311 };
309 312
313 # try to write data immediately
314 $cb->();
315
316 # if still data left in wbuf, we need to poll
310 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); 317 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb)
311 318 if length $self->{wbuf};
312 $cb->($self);
313 }; 319 };
314} 320}
315 321
316our %WH; 322our %WH;
317 323
727 733
728 1 734 1
729 } 735 }
730}; 736};
731 737
738=item regex => $accept[, $reject[, $skip], $cb->($data)
739
740Makes a regex match against the regex object C<$accept> and returns
741everything up to and including the match.
742
743Example: read a single line terminated by '\n'.
744
745 $handle->push_read (regex => qr<\n>, sub { ... });
746
747If C<$reject> is given and not undef, then it determines when the data is
748to be rejected: it is matched against the data when the C<$accept> regex
749does not match and generates an C<EBADMSG> error when it matches. This is
750useful to quickly reject wrong data (to avoid waiting for a timeout or a
751receive buffer overflow).
752
753Example: expect a single decimal number followed by whitespace, reject
754anything else (not the use of an anchor).
755
756 $handle->push_read (regex => qr<^[0-9]+\s>, qr<[^0-9]>, sub { ... });
757
758If C<$skip> is given and not C<undef>, then it will be matched against
759the receive buffer when neither C<$accept> nor C<$reject> match,
760and everything preceding and including the match will be accepted
761unconditionally. This is useful to skip large amounts of data that you
762know cannot be matched, so that the C<$accept> or C<$reject> regex do not
763have to start matching from the beginning. This is purely an optimisation
764and is usually worth only when you expect more than a few kilobytes.
765
766Example: expect a http header, which ends at C<\015\012\015\012>. Since we
767expect the header to be very large (it isn't in practise, but...), we use
768a skip regex to skip initial portions. The skip regex is tricky in that
769it only accepts something not ending in either \015 or \012, as these are
770required for the accept regex.
771
772 $handle->push_read (regex =>
773 qr<\015\012\015\012>,
774 undef, # no reject
775 qr<^.*[^\015\012]>,
776 sub { ... });
777
778=cut
779
780register_read_type regex => sub {
781 my ($self, $cb, $accept, $reject, $skip) = @_;
782
783 my $data;
784 my $rbuf = \$self->{rbuf};
785
786 sub {
787 # accept
788 if ($$rbuf =~ $accept) {
789 $data .= substr $$rbuf, 0, $+[0], "";
790 $cb->($self, $data);
791 return 1;
792 }
793
794 # reject
795 if ($reject && $$rbuf =~ $reject) {
796 $! = &Errno::EBADMSG;
797 $self->error;
798 }
799
800 # skip
801 if ($skip && $$rbuf =~ $skip) {
802 $data .= substr $$rbuf, 0, $+[0], "";
803 }
804
805 ()
806 }
807};
808
732=back 809=back
733 810
734=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) 811=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args)
735 812
736This function (not method) lets you add your own types to C<push_read>. 813This function (not method) lets you add your own types to C<push_read>.
786 } elsif (defined $len) { 863 } elsif (defined $len) {
787 delete $self->{rw}; 864 delete $self->{rw};
788 $self->{eof} = 1; 865 $self->{eof} = 1;
789 $self->_drain_rbuf; 866 $self->_drain_rbuf;
790 867
791 } elsif ($! != EAGAIN && $! != EINTR) { 868 } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAWOULDBLOCK) {
792 return $self->error; 869 return $self->error;
793 } 870 }
794 }); 871 });
795 } 872 }
796} 873}
862 # but the openssl maintainers basically said: "trust us, it just works". 939 # but the openssl maintainers basically said: "trust us, it just works".
863 # (unfortunately, we have to hardcode constants because the abysmally misdesigned 940 # (unfortunately, we have to hardcode constants because the abysmally misdesigned
864 # and mismaintained ssleay-module doesn't even offer them). 941 # and mismaintained ssleay-module doesn't even offer them).
865 # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html 942 # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html
866 Net::SSLeay::CTX_set_mode ($self->{tls}, 943 Net::SSLeay::CTX_set_mode ($self->{tls},
867 (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) 944 (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1)
868 | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); 945 | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2));
869 946
870 $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 947 $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
871 $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 948 $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
872 949
873 Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); 950 Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio});

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines