… | |
… | |
53 | package AnyEvent::Handle; |
53 | package AnyEvent::Handle; |
54 | |
54 | |
55 | use Scalar::Util (); |
55 | use Scalar::Util (); |
56 | use List::Util (); |
56 | use List::Util (); |
57 | use Carp (); |
57 | use Carp (); |
58 | use Errno qw(EAGAIN EINTR); |
58 | use Errno qw(EAGAIN EWOULDBLOCK EINTR); |
59 | |
59 | |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
62 | |
62 | |
63 | our $VERSION = $AnyEvent::VERSION; |
63 | our $VERSION = $AnyEvent::VERSION; |
… | |
… | |
91 | |
91 | |
92 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
92 | =item fh => $filehandle [C<fh> or C<connect> MANDATORY] |
93 | |
93 | |
94 | The filehandle this L<AnyEvent::Handle> object will operate on. |
94 | The filehandle this L<AnyEvent::Handle> object will operate on. |
95 | NOTE: The filehandle will be set to non-blocking mode (using |
95 | 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 |
96 | C<AnyEvent::fh_unblock>) by the constructor and needs to stay in |
97 | that mode. |
97 | that mode. |
98 | |
98 | |
99 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
99 | =item connect => [$host, $service] [C<fh> or C<connect> MANDATORY] |
100 | |
100 | |
101 | Try to connect to the specified host and service (port), using |
101 | Try to connect to the specified host and service (port), using |
… | |
… | |
131 | |
131 | |
132 | The peer's numeric host and port (the socket peername) are passed as |
132 | 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 |
133 | 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 |
134 | read and write queues, EOF status, TLS status and similar properties of |
135 | the handle will have been reset. |
135 | 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 | |
136 | |
140 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
137 | 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 |
138 | 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> |
139 | 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 |
140 | callback can be invoked after the connect callback returns, i.e. one can |
… | |
… | |
614 | # with AnyEvent::Handle, do them a favour. |
611 | # with AnyEvent::Handle, do them a favour. |
615 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
612 | 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!" |
613 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
617 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
614 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
618 | |
615 | |
619 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
616 | AnyEvent::fh_unblock $self->{fh}; |
620 | |
617 | |
621 | $self->{_activity} = |
618 | $self->{_activity} = |
622 | $self->{_ractivity} = |
619 | $self->{_ractivity} = |
623 | $self->{_wactivity} = AE::now; |
620 | $self->{_wactivity} = AE::now; |
624 | |
621 | |
… | |
… | |
974 | $self->{on_drain}($self) |
971 | $self->{on_drain}($self) |
975 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
972 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
976 | && $self->{on_drain}; |
973 | && $self->{on_drain}; |
977 | |
974 | |
978 | delete $self->{_ww} unless length $self->{wbuf}; |
975 | delete $self->{_ww} unless length $self->{wbuf}; |
979 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
976 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
980 | $self->_error ($!, 1); |
977 | $self->_error ($!, 1); |
981 | } |
978 | } |
982 | }; |
979 | }; |
983 | |
980 | |
984 | # try to write data immediately |
981 | # try to write data immediately |
… | |
… | |
1573 | }; |
1570 | }; |
1574 | |
1571 | |
1575 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1572 | =item regex => $accept[, $reject[, $skip], $cb->($handle, $data) |
1576 | |
1573 | |
1577 | Makes a regex match against the regex object C<$accept> and returns |
1574 | Makes a regex match against the regex object C<$accept> and returns |
1578 | everything up to and including the match. |
1575 | everything up to and including the match. All the usual regex variables |
|
|
1576 | ($1, %+ etc.) from the regex match are available in the callback. |
1579 | |
1577 | |
1580 | Example: read a single line terminated by '\n'. |
1578 | Example: read a single line terminated by '\n'. |
1581 | |
1579 | |
1582 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1580 | $handle->push_read (regex => qr<\n>, sub { ... }); |
1583 | |
1581 | |
… | |
… | |
2041 | } elsif (defined $len) { |
2039 | } elsif (defined $len) { |
2042 | delete $self->{_rw}; |
2040 | delete $self->{_rw}; |
2043 | $self->{_eof} = 1; |
2041 | $self->{_eof} = 1; |
2044 | $self->_drain_rbuf; |
2042 | $self->_drain_rbuf; |
2045 | |
2043 | |
2046 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
2044 | } elsif ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK) { |
2047 | return $self->_error ($!, 1); |
2045 | return $self->_error ($!, 1); |
2048 | } |
2046 | } |
2049 | }; |
2047 | }; |
2050 | } |
2048 | } |
2051 | } |
2049 | } |
… | |
… | |
2307 | push @linger, AE::io $fh, 1, sub { |
2305 | push @linger, AE::io $fh, 1, sub { |
2308 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2306 | my $len = syswrite $fh, $wbuf, length $wbuf; |
2309 | |
2307 | |
2310 | if ($len > 0) { |
2308 | if ($len > 0) { |
2311 | substr $wbuf, 0, $len, ""; |
2309 | substr $wbuf, 0, $len, ""; |
2312 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK)) { |
2310 | } elsif (defined $len || ($! != EAGAIN && $! != EINTR && $! != EWOULDBLOCK && $! != WSAEWOULDBLOCK)) { |
2313 | @linger = (); # end |
2311 | @linger = (); # end |
2314 | } |
2312 | } |
2315 | }; |
2313 | }; |
2316 | push @linger, AE::timer $linger, 0, sub { |
2314 | push @linger, AE::timer $linger, 0, sub { |
2317 | @linger = (); |
2315 | @linger = (); |