… | |
… | |
30 | |
30 | |
31 | $cv->recv; |
31 | $cv->recv; |
32 | |
32 | |
33 | =head1 DESCRIPTION |
33 | =head1 DESCRIPTION |
34 | |
34 | |
35 | This is a helper module to make it easier to do event-based I/O on |
35 | This is a helper module to make it easier to do event-based I/O |
36 | stream-based filehandles (sockets, pipes, and other stream things). |
36 | on stream-based filehandles (sockets, pipes, and other stream |
|
|
37 | things). Specifically, it doesn't work as expected on files, packet-based |
|
|
38 | sockets or similar things. |
37 | |
39 | |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
40 | The L<AnyEvent::Intro> tutorial contains some well-documented |
39 | AnyEvent::Handle examples. |
41 | AnyEvent::Handle examples. |
40 | |
42 | |
41 | In the following, where the documentation refers to "bytes", it means |
43 | In the following, where the documentation refers to "bytes", it means |
… | |
… | |
53 | package AnyEvent::Handle; |
55 | package AnyEvent::Handle; |
54 | |
56 | |
55 | use Scalar::Util (); |
57 | use Scalar::Util (); |
56 | use List::Util (); |
58 | use List::Util (); |
57 | use Carp (); |
59 | use Carp (); |
58 | use Errno qw(EAGAIN EINTR); |
60 | use Errno qw(EAGAIN EWOULDBLOCK EINTR); |
59 | |
61 | |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
62 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
63 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
62 | |
64 | |
63 | our $VERSION = $AnyEvent::VERSION; |
65 | our $VERSION = $AnyEvent::VERSION; |
… | |
… | |
91 | |
93 | |
92 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
94 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
93 | |
95 | |
94 | The filehandle this L<AnyEvent::Handle> object will operate on. |
96 | The filehandle this L<AnyEvent::Handle> object will operate on. |
95 | NOTE: The filehandle will be set to non-blocking mode (using |
97 | NOTE: The filehandle will be set to non-blocking mode (using |
96 | C<AnyEvent::Util::fh_nonblocking>) by the constructor and needs to stay in |
98 | C<AnyEvent::fh_unblock>) by the constructor and needs to stay in |
97 | that mode. |
99 | that mode. |
98 | |
100 | |
99 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
101 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
100 | |
102 | |
101 | Try to connect to the specified host and service (port), using |
103 | Try to connect to the specified host and service (port), using |
… | |
… | |
131 | |
133 | |
132 | The peer's numeric host and port (the socket peername) are passed as |
134 | The peer's numeric host and port (the socket peername) are passed as |
133 | parameters, together with a retry callback. At the time it is called the |
135 | parameters, together with a retry callback. At the time it is called the |
134 | read and write queues, EOF status, TLS status and similar properties of |
136 | read and write queues, EOF status, TLS status and similar properties of |
135 | the handle will have been reset. |
137 | the handle will have been reset. |
136 | |
|
|
137 | It is not allowed to use the read or write queues while the handle object |
|
|
138 | is connecting. |
|
|
139 | |
138 | |
140 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
139 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
141 | continue with the next connection target (in case of multi-homed hosts or |
140 | continue with the next connection target (in case of multi-homed hosts or |
142 | SRV records there can be multiple connection endpoints). The C<$retry> |
141 | SRV records there can be multiple connection endpoints). The C<$retry> |
143 | callback can be invoked after the connect callback returns, i.e. one can |
142 | callback can be invoked after the connect callback returns, i.e. one can |
… | |
… | |
614 | # with AnyEvent::Handle, do them a favour. |
613 | # with AnyEvent::Handle, do them a favour. |
615 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
614 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
616 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
615 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
617 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
616 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
618 | |
617 | |
619 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
618 | AnyEvent::fh_unblock $self->{fh}; |
620 | |
619 | |
621 | $self->{_activity} = |
620 | $self->{_activity} = |
622 | $self->{_ractivity} = |
621 | $self->{_ractivity} = |
623 | $self->{_wactivity} = AE::now; |
622 | $self->{_wactivity} = AE::now; |
624 | |
623 | |
… | |
… | |
974 | $self->{on_drain}($self) |
973 | $self->{on_drain}($self) |
975 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
974 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
976 | && $self->{on_drain}; |
975 | && $self->{on_drain}; |
977 | |
976 | |
978 | delete $self->{_ww} unless length $self->{wbuf}; |
977 | delete $self->{_ww} unless length $self->{wbuf}; |
979 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
978 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
980 | $self->_error ($!, 1); |
979 | $self->_error ($!, 1); |
981 | } |
980 | } |
982 | }; |
981 | }; |
983 | |
982 | |
984 | # try to write data immediately |
983 | # try to write data immediately |
… | |
… | |
1573 | }; |
1572 | }; |
1574 | |
1573 | |
1575 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1574 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1576 | |
1575 | |
1577 | Makes a regex match against the regex object C<$accept> and returns |
1576 | Makes a regex match against the regex object C<$accept> and returns |
1578 | everything up to and including the match. |
1577 | everything up to and including the match. All the usual regex variables |
|
|
1578 | ($1, %+ etc.) from the regex match are available in the callback. |
1579 | |
1579 | |
1580 | Example: read a single line terminated by '\n'. |
1580 | Example: read a single line terminated by '\n'. |
1581 | |
1581 | |
1582 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1582 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1583 | |
1583 | |
… | |
… | |
2041 | } elsif (defined $len) { |
2041 | } elsif (defined $len) { |
2042 | delete $self->{_rw}; |
2042 | delete $self->{_rw}; |
2043 | $self->{_eof} = 1; |
2043 | $self->{_eof} = 1; |
2044 | $self->_drain_rbuf; |
2044 | $self->_drain_rbuf; |
2045 | |
2045 | |
2046 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
2046 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
2047 | return $self->_error ($!, 1); |
2047 | return $self->_error ($!, 1); |
2048 | } |
2048 | } |
2049 | }; |
2049 | }; |
2050 | } |
2050 | } |
2051 | } |
2051 | } |
… | |
… | |
2307 | push @linger, AE::io $fh, 1, sub { |
2307 | push @linger, AE::io $fh, 1, sub { |
2308 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2308 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2309 | |
2309 | |
2310 | if ($len > 0) { |
2310 | if ($len > 0) { |
2311 | substr $wbuf, 0, $len, ""; |
2311 | substr $wbuf, 0, $len, ""; |
2312 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) { |
2312 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK)) { |
2313 | @linger = (); # end |
2313 | @linger = (); # end |
2314 | } |
2314 | } |
2315 | }; |
2315 | }; |
2316 | push @linger, AE::timer $linger, 0, sub { |
2316 | push @linger, AE::timer $linger, 0, sub { |
2317 | @linger = (); |
2317 | @linger = (); |