… | |
… | |
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 | |
… | |
… | |
70 | The filehandle this L<AnyEvent::Handle> object will operate on. |
72 | The filehandle this L<AnyEvent::Handle> object will operate on. |
71 | |
73 | |
72 | NOTE: The filehandle will be set to non-blocking (using |
74 | NOTE: The filehandle will be set to non-blocking (using |
73 | AnyEvent::Util::fh_nonblocking). |
75 | AnyEvent::Util::fh_nonblocking). |
74 | |
76 | |
75 | =item on_eof => $cb->($self) [MANDATORY] |
77 | =item on_eof => $cb->($self) |
76 | |
78 | |
77 | Set the callback to be called on EOF. |
79 | Set the callback to be called on EOF. |
|
|
80 | |
|
|
81 | While not mandatory, it is highly recommended to set an eof callback, |
|
|
82 | otherwise you might end up with a closed socket while you are still |
|
|
83 | waiting for data. |
78 | |
84 | |
79 | =item on_error => $cb->($self) |
85 | =item on_error => $cb->($self) |
80 | |
86 | |
81 | This is the fatal error callback, that is called when, well, a fatal error |
87 | This is the fatal error callback, that is called when, well, a fatal error |
82 | ocurs, such as not being able to resolve the hostname, failure to connect |
88 | ocurs, such as not being able to resolve the hostname, failure to connect |
… | |
… | |
146 | |
152 | |
147 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
153 | $self->{fh} or Carp::croak "mandatory argument fh is missing"; |
148 | |
154 | |
149 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
155 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
150 | |
156 | |
151 | $self->on_eof ((delete $self->{on_eof} ) or Carp::croak "mandatory argument on_eof is missing"); |
157 | $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; |
152 | |
|
|
153 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
158 | $self->on_error (delete $self->{on_error}) if $self->{on_error}; |
154 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
159 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
155 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
160 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
156 | |
161 | |
157 | $self->start_read; |
162 | $self->start_read; |
… | |
… | |
398 | } |
403 | } |
399 | } |
404 | } |
400 | |
405 | |
401 | if ($self->{eof}) { |
406 | if ($self->{eof}) { |
402 | $self->_shutdown; |
407 | $self->_shutdown; |
403 | $self->{on_eof}($self); |
408 | $self->{on_eof}($self) |
|
|
409 | if $self->{on_eof}; |
404 | } |
410 | } |
405 | } |
411 | } |
406 | |
412 | |
407 | =item $handle->on_read ($cb) |
413 | =item $handle->on_read ($cb) |
408 | |
414 | |
… | |
… | |
531 | my $self = shift; |
537 | my $self = shift; |
532 | my $cb = pop; |
538 | my $cb = pop; |
533 | my $eol = @_ ? shift : qr|(\015?\012)|; |
539 | my $eol = @_ ? shift : qr|(\015?\012)|; |
534 | my $pos; |
540 | my $pos; |
535 | |
541 | |
536 | $eol = qr|(\Q$eol\E)| unless ref $eol; |
542 | $eol = quotemeta $eol unless ref $eol; |
537 | $eol = qr|^(.*?)($eol)|; |
543 | $eol = qr|^(.*?)($eol)|s; |
538 | |
544 | |
539 | sub { |
545 | sub { |
540 | $_[0]{rbuf} =~ s/$eol// or return; |
546 | $_[0]{rbuf} =~ s/$eol// or return; |
541 | |
547 | |
542 | $cb->($_[0], $1, $2); |
548 | $cb->($_[0], $1, $2); |