… | |
… | |
289 | |
289 | |
290 | sub _drain_wbuf { |
290 | sub _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, ""; |
… | |
… | |
306 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) { |
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 | |
317 | our %WH; |
322 | our %WH; |
318 | |
323 | |
… | |
… | |
728 | |
733 | |
729 | 1 |
734 | 1 |
730 | } |
735 | } |
731 | }; |
736 | }; |
732 | |
737 | |
|
|
738 | =item regex => $accept[, $reject[, $skip], $cb->($data) |
|
|
739 | |
|
|
740 | Makes a regex match against the regex object C<$accept> and returns |
|
|
741 | everything up to and including the match. |
|
|
742 | |
|
|
743 | Example: read a single line terminated by '\n'. |
|
|
744 | |
|
|
745 | $handle->push_read (regex => qr<\n>, sub { ... }); |
|
|
746 | |
|
|
747 | If C<$reject> is given and not undef, then it determines when the data is |
|
|
748 | to be rejected: it is matched against the data when the C<$accept> regex |
|
|
749 | does not match and generates an C<EBADMSG> error when it matches. This is |
|
|
750 | useful to quickly reject wrong data (to avoid waiting for a timeout or a |
|
|
751 | receive buffer overflow). |
|
|
752 | |
|
|
753 | Example: expect a single decimal number followed by whitespace, reject |
|
|
754 | anything else (not the use of an anchor). |
|
|
755 | |
|
|
756 | $handle->push_read (regex => qr<^[0-9]+\s>, qr<[^0-9]>, sub { ... }); |
|
|
757 | |
|
|
758 | If C<$skip> is given and not C<undef>, then it will be matched against |
|
|
759 | the receive buffer when neither C<$accept> nor C<$reject> match, |
|
|
760 | and everything preceding and including the match will be accepted |
|
|
761 | unconditionally. This is useful to skip large amounts of data that you |
|
|
762 | know cannot be matched, so that the C<$accept> or C<$reject> regex do not |
|
|
763 | have to start matching from the beginning. This is purely an optimisation |
|
|
764 | and is usually worth only when you expect more than a few kilobytes. |
|
|
765 | |
|
|
766 | Example: expect a http header, which ends at C<\015\012\015\012>. Since we |
|
|
767 | expect the header to be very large (it isn't in practise, but...), we use |
|
|
768 | a skip regex to skip initial portions. The skip regex is tricky in that |
|
|
769 | it only accepts something not ending in either \015 or \012, as these are |
|
|
770 | required 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 | |
|
|
780 | register_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 | |
737 | This function (not method) lets you add your own types to C<push_read>. |
813 | This function (not method) lets you add your own types to C<push_read>. |