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.246 by root, Sun Jun 28 09:30:37 2015 UTC

30 30
31 $cv->recv; 31 $cv->recv;
32 32
33=head1 DESCRIPTION 33=head1 DESCRIPTION
34 34
35This is a helper module to make it easier to do event-based I/O on 35This is a helper module to make it easier to do event-based I/O
36stream-based filehandles (sockets, pipes, and other stream things). 36on stream-based filehandles (sockets, pipes, and other stream
37things). Specifically, it doesn't work as expected on files, packet-based
38sockets or similar things.
37 39
38The L<AnyEvent::Intro> tutorial contains some well-documented 40The L<AnyEvent::Intro> tutorial contains some well-documented
39AnyEvent::Handle examples. 41AnyEvent::Handle examples.
40 42
41In the following, where the documentation refers to "bytes", it means 43In the following, where the documentation refers to "bytes", it means
53package AnyEvent::Handle; 55package AnyEvent::Handle;
54 56
55use Scalar::Util (); 57use Scalar::Util ();
56use List::Util (); 58use List::Util ();
57use Carp (); 59use Carp ();
58use Errno qw(EAGAIN EINTR); 60use Errno qw(EAGAIN EWOULDBLOCK EINTR);
59 61
60use AnyEvent (); BEGIN { AnyEvent::common_sense } 62use AnyEvent (); BEGIN { AnyEvent::common_sense }
61use AnyEvent::Util qw(WSAEWOULDBLOCK); 63use AnyEvent::Util qw(WSAEWOULDBLOCK);
62 64
63our $VERSION = $AnyEvent::VERSION; 65our $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
94The filehandle this L<AnyEvent::Handle> object will operate on. 96The filehandle this L<AnyEvent::Handle> object will operate on.
95NOTE: The filehandle will be set to non-blocking mode (using 97NOTE: The filehandle will be set to non-blocking mode (using
96C<AnyEvent::Util::fh_nonblocking>) by the constructor and needs to stay in 98C<AnyEvent::fh_unblock>) by the constructor and needs to stay in
97that mode. 99that 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
101Try to connect to the specified host and service (port), using 103Try to connect to the specified host and service (port), using
131 133
132The peer's numeric host and port (the socket peername) are passed as 134The 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 135parameters, together with a retry callback. At the time it is called the
134read and write queues, EOF status, TLS status and similar properties of 136read and write queues, EOF status, TLS status and similar properties of
135the handle will have been reset. 137the handle will have been reset.
136
137It is not allowed to use the read or write queues while the handle object
138is connecting.
139 138
140If, for some reason, the handle is not acceptable, calling C<$retry> will 139If, 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 140continue with the next connection target (in case of multi-homed hosts or
142SRV records there can be multiple connection endpoints). The C<$retry> 141SRV records there can be multiple connection endpoints). The C<$retry>
143callback can be invoked after the connect callback returns, i.e. one can 142callback 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
1577Makes a regex match against the regex object C<$accept> and returns 1576Makes a regex match against the regex object C<$accept> and returns
1578everything up to and including the match. 1577everything 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
1580Example: read a single line terminated by '\n'. 1580Example: 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 = ();

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines