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.31 by root, Sun May 25 00:08:49 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
289 289
290sub _drain_wbuf { 290sub _drain_wbuf {
291 my ($self) = @_; 291 my ($self) = @_;
292 292
293 if (!$self->{ww} && length $self->{wbuf}) { 293 if (!$self->{ww} && length $self->{wbuf}) {
294
294 Scalar::Util::weaken $self; 295 Scalar::Util::weaken $self;
296
295 my $cb = sub { 297 my $cb = sub {
296 my $len = syswrite $self->{fh}, $self->{wbuf}; 298 my $len = syswrite $self->{fh}, $self->{wbuf};
297 299
298 if ($len >= 0) { 300 if ($len >= 0) {
299 substr $self->{wbuf}, 0, $len, ""; 301 substr $self->{wbuf}, 0, $len, "";
301 $self->{on_drain}($self) 303 $self->{on_drain}($self)
302 if $self->{low_water_mark} >= length $self->{wbuf} 304 if $self->{low_water_mark} >= length $self->{wbuf}
303 && $self->{on_drain}; 305 && $self->{on_drain};
304 306
305 delete $self->{ww} unless length $self->{wbuf}; 307 delete $self->{ww} unless length $self->{wbuf};
306 } elsif ($! != EAGAIN && $! != EINTR) { 308 } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) {
307 $self->error; 309 $self->error;
308 } 310 }
309 }; 311 };
310 312
313 # try to write data immediately
314 $cb->();
315
316 # if still data left in wbuf, we need to poll
311 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); 317 $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb)
312 318 if length $self->{wbuf};
313 $cb->($self);
314 }; 319 };
315} 320}
316 321
317our %WH; 322our %WH;
318 323
728 733
729 1 734 1
730 } 735 }
731}; 736};
732 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
733=back 809=back
734 810
735=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) 811=item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args)
736 812
737This 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>.
787 } elsif (defined $len) { 863 } elsif (defined $len) {
788 delete $self->{rw}; 864 delete $self->{rw};
789 $self->{eof} = 1; 865 $self->{eof} = 1;
790 $self->_drain_rbuf; 866 $self->_drain_rbuf;
791 867
792 } elsif ($! != EAGAIN && $! != EINTR) { 868 } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAWOULDBLOCK) {
793 return $self->error; 869 return $self->error;
794 } 870 }
795 }); 871 });
796 } 872 }
797} 873}
863 # but the openssl maintainers basically said: "trust us, it just works". 939 # but the openssl maintainers basically said: "trust us, it just works".
864 # (unfortunately, we have to hardcode constants because the abysmally misdesigned 940 # (unfortunately, we have to hardcode constants because the abysmally misdesigned
865 # and mismaintained ssleay-module doesn't even offer them). 941 # and mismaintained ssleay-module doesn't even offer them).
866 # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html 942 # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html
867 Net::SSLeay::CTX_set_mode ($self->{tls}, 943 Net::SSLeay::CTX_set_mode ($self->{tls},
868 (eval { Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) 944 (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1)
869 | (eval { Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); 945 | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2));
870 946
871 $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 ());
872 $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 ());
873 949
874 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