… | |
… | |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on filehandles via AnyEvent |
16 | |
16 | |
17 | =cut |
17 | This module is experimental. |
18 | |
18 | |
|
|
19 | =cut |
|
|
20 | |
19 | our $VERSION = '0.02'; |
21 | our $VERSION = '0.04'; |
20 | |
22 | |
21 | =head1 SYNOPSIS |
23 | =head1 SYNOPSIS |
22 | |
24 | |
23 | use AnyEvent; |
25 | use AnyEvent; |
24 | use AnyEvent::Handle; |
26 | use AnyEvent::Handle; |
… | |
… | |
43 | $cv->wait; |
45 | $cv->wait; |
44 | |
46 | |
45 | =head1 DESCRIPTION |
47 | =head1 DESCRIPTION |
46 | |
48 | |
47 | This module is a helper module to make it easier to do event-based I/O on |
49 | This module is a helper module to make it easier to do event-based I/O on |
48 | filehandles (and sockets, see L<AnyEvent::Socket> for an easy way to make |
50 | filehandles. For utility functions for doing non-blocking connects and accepts |
49 | non-blocking resolves and connects). |
51 | on sockets see L<AnyEvent::Util>. |
50 | |
52 | |
51 | In the following, when the documentation refers to of "bytes" then this |
53 | In the following, when the documentation refers to of "bytes" then this |
52 | means characters. As sysread and syswrite are used for all I/O, their |
54 | means characters. As sysread and syswrite are used for all I/O, their |
53 | treatment of characters applies to this module as well. |
55 | treatment of characters applies to this module as well. |
54 | |
56 | |
… | |
… | |
362 | =cut |
364 | =cut |
363 | |
365 | |
364 | sub _drain_rbuf { |
366 | sub _drain_rbuf { |
365 | my ($self) = @_; |
367 | my ($self) = @_; |
366 | |
368 | |
367 | return if exists $self->{in_drain}; |
369 | return if $self->{in_drain}; |
368 | local $self->{in_drain} = 1; |
370 | local $self->{in_drain} = 1; |
369 | |
371 | |
370 | while (my $len = length $self->{rbuf}) { |
372 | while (my $len = length $self->{rbuf}) { |
371 | no strict 'refs'; |
373 | no strict 'refs'; |
372 | if (my $cb = shift @{ $self->{queue} }) { |
374 | if (my $cb = shift @{ $self->{queue} }) { |
… | |
… | |
485 | sub _read_chunk($$) { |
487 | sub _read_chunk($$) { |
486 | my ($self, $len, $cb) = @_; |
488 | my ($self, $len, $cb) = @_; |
487 | |
489 | |
488 | sub { |
490 | sub { |
489 | $len <= length $_[0]{rbuf} or return; |
491 | $len <= length $_[0]{rbuf} or return; |
490 | $cb->($self, $_[0], substr $_[0]{rbuf}, 0, $len, ""); |
492 | $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); |
491 | 1 |
493 | 1 |
492 | } |
494 | } |
493 | } |
495 | } |
494 | |
496 | |
495 | sub push_read_chunk { |
497 | sub push_read_chunk { |
… | |
… | |
531 | my $self = shift; |
533 | my $self = shift; |
532 | my $cb = pop; |
534 | my $cb = pop; |
533 | my $eol = @_ ? shift : qr|(\015?\012)|; |
535 | my $eol = @_ ? shift : qr|(\015?\012)|; |
534 | my $pos; |
536 | my $pos; |
535 | |
537 | |
536 | $eol = qr|(\Q$eol\E)| unless ref $eol; |
538 | $eol = quotemeta $eol unless ref $eol; |
537 | $eol = qr|^(.*?)($eol)|; |
539 | $eol = qr|^(.*?)($eol)|s; |
538 | |
540 | |
539 | sub { |
541 | sub { |
540 | $_[0]{rbuf} =~ s/$eol// or return; |
542 | $_[0]{rbuf} =~ s/$eol// or return; |
541 | |
543 | |
542 | $cb->($self, $1, $2); |
544 | $cb->($_[0], $1, $2); |
543 | 1 |
545 | 1 |
544 | } |
546 | } |
545 | } |
547 | } |
546 | |
548 | |
547 | sub push_read_line { |
549 | sub push_read_line { |
… | |
… | |
577 | |
579 | |
578 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
580 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
579 | my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; |
581 | my $len = sysread $self->{fh}, $self->{rbuf}, $self->{read_size} || 8192, length $self->{rbuf}; |
580 | |
582 | |
581 | if ($len > 0) { |
583 | if ($len > 0) { |
582 | if (exists $self->{rbuf_max}) { |
584 | if (defined $self->{rbuf_max}) { |
583 | if ($self->{rbuf_max} < length $self->{rbuf}) { |
585 | if ($self->{rbuf_max} < length $self->{rbuf}) { |
584 | $! = &Errno::ENOSPC; return $self->error; |
586 | $! = &Errno::ENOSPC; return $self->error; |
585 | } |
587 | } |
586 | } |
588 | } |
587 | |
589 | |