ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.241 by root, Fri Sep 5 22:17:26 2014 UTC vs.
Revision 1.245 by root, Tue Jun 16 03:22:20 2015 UTC

53package AnyEvent::Handle; 53package AnyEvent::Handle;
54 54
55use Scalar::Util (); 55use Scalar::Util ();
56use List::Util (); 56use List::Util ();
57use Carp (); 57use Carp ();
58use Errno qw(EAGAIN EINTR); 58use Errno qw(EAGAIN EWOULDBLOCK EINTR);
59 59
60use AnyEvent (); BEGIN { AnyEvent::common_sense } 60use AnyEvent (); BEGIN { AnyEvent::common_sense }
61use AnyEvent::Util qw(WSAEWOULDBLOCK); 61use AnyEvent::Util qw(WSAEWOULDBLOCK);
62 62
63our $VERSION = $AnyEvent::VERSION; 63our $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
94The filehandle this L<AnyEvent::Handle> object will operate on. 94The filehandle this L<AnyEvent::Handle> object will operate on.
95NOTE: The filehandle will be set to non-blocking mode (using 95NOTE: The filehandle will be set to non-blocking mode (using
96C<AnyEvent::Util::fh_nonblocking>) by the constructor and needs to stay in 96C<AnyEvent::fh_unblock>) by the constructor and needs to stay in
97that mode. 97that 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
101Try to connect to the specified host and service (port), using 101Try to connect to the specified host and service (port), using
131 131
132The peer's numeric host and port (the socket peername) are passed as 132The peer's numeric host and port (the socket peername) are passed as
133parameters, together with a retry callback. At the time it is called the 133parameters, together with a retry callback. At the time it is called the
134read and write queues, EOF status, TLS status and similar properties of 134read and write queues, EOF status, TLS status and similar properties of
135the handle will have been reset. 135the handle will have been reset.
136
137It is not allowed to use the read or write queues while the handle object
138is connecting.
139 136
140If, for some reason, the handle is not acceptable, calling C<$retry> will 137If, for some reason, the handle is not acceptable, calling C<$retry> will
141continue with the next connection target (in case of multi-homed hosts or 138continue with the next connection target (in case of multi-homed hosts or
142SRV records there can be multiple connection endpoints). The C<$retry> 139SRV records there can be multiple connection endpoints). The C<$retry>
143callback can be invoked after the connect callback returns, i.e. one can 140callback 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
1577Makes a regex match against the regex object C<$accept> and returns 1574Makes a regex match against the regex object C<$accept> and returns
1578everything up to and including the match. 1575everything 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
1580Example: read a single line terminated by '\n'. 1578Example: 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 = ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines