… | |
… | |
22 | |
22 | |
23 | use strict; |
23 | use strict; |
24 | |
24 | |
25 | no warnings "uninitialized"; |
25 | no warnings "uninitialized"; |
26 | |
26 | |
|
|
27 | use Carp (); |
27 | use Errno (); |
28 | use Errno (); |
28 | use Socket (); |
29 | use Socket (); |
29 | use IO::Socket::INET (); |
30 | use IO::Socket::INET (); |
30 | |
31 | |
31 | use AnyEvent; |
32 | use AnyEvent; |
… | |
… | |
205 | |
206 | |
206 | sub guard(&) { |
207 | sub guard(&) { |
207 | bless \(my $cb = shift), AnyEvent::Util::Guard:: |
208 | bless \(my $cb = shift), AnyEvent::Util::Guard:: |
208 | } |
209 | } |
209 | |
210 | |
|
|
211 | sub _tcp_port($) { |
|
|
212 | $_[0] =~ /^\d*$/ and return $1*1; |
|
|
213 | |
|
|
214 | (getservbyname $_[0], "tcp")[2] |
|
|
215 | or Carp::croak "$_[0]: service unknown" |
|
|
216 | } |
|
|
217 | |
210 | =item my $guard = AnyEvent::Util::tcp_connect $host, $port, $connect_cb[, $prepare_cb] |
218 | =item my $guard = AnyEvent::Util::tcp_connect $host, $port, $connect_cb[, $prepare_cb] |
211 | |
219 | |
212 | This function is experimental. |
220 | This function is experimental. |
213 | |
221 | |
214 | This is a convenience function that creates a tcp socket and makes a 100% |
222 | This is a convenience function that creates a tcp socket and makes a 100% |
215 | non-blocking connect to the given C<$host> (which can be a hostname or a |
223 | non-blocking connect to the given C<$host> (which can be a hostname or a |
216 | textual IP address) and C<$port>. |
224 | textual IP address) and C<$port> (which can be a numeric port number or a |
|
|
225 | service name). |
217 | |
226 | |
218 | Unless called in void context, it returns a guard object that will |
227 | Unless called in void context, it returns a guard object that will |
219 | automatically abort connecting when it gets destroyed (it does not do |
228 | automatically abort connecting when it gets destroyed (it does not do |
220 | anything to the socket after the conenct was successful). |
229 | anything to the socket after the connect was successful). |
221 | |
230 | |
222 | If the connect is successful, then the C<$connect_cb> will be invoked with |
231 | If the connect is successful, then the C<$connect_cb> will be invoked with |
223 | the socket filehandle (in non-blocking mode) as first and the peer host |
232 | the socket filehandle (in non-blocking mode) as first and the peer host |
224 | (as a textual IP address) and peer port as second and third arguments, |
233 | (as a textual IP address) and peer port as second and third arguments, |
225 | respectively. |
234 | respectively. |
… | |
… | |
252 | |
261 | |
253 | Complex Example: connect to www.google.com on port 80 and make a simple |
262 | Complex Example: connect to www.google.com on port 80 and make a simple |
254 | GET request without much error handling. Also limit the connection timeout |
263 | GET request without much error handling. Also limit the connection timeout |
255 | to 15 seconds. |
264 | to 15 seconds. |
256 | |
265 | |
257 | AnyEvent::Util::tcp_connect "www.google.com", 80, |
266 | AnyEvent::Util::tcp_connect "www.google.com", "http", |
258 | sub { |
267 | sub { |
259 | my ($fh) = @_ |
268 | my ($fh) = @_ |
260 | or die "unable to connect: $!"; |
269 | or die "unable to connect: $!"; |
261 | |
270 | |
262 | my $handle; # avoid direct assignment so on_eof has it in scope. |
271 | my $handle; # avoid direct assignment so on_eof has it in scope. |
… | |
… | |
346 | $connect->(); |
355 | $connect->(); |
347 | } |
356 | } |
348 | }; |
357 | }; |
349 | |
358 | |
350 | # now connect |
359 | # now connect |
351 | if (connect $state{fh}, Socket::pack_sockaddr_in $port, $ipn) { |
360 | if (connect $state{fh}, Socket::pack_sockaddr_in _tcp_port $port, $ipn) { |
352 | $connected->(); |
361 | $connected->(); |
353 | } elsif ($! == &Errno::EINPROGRESS || $! == &Errno::EWOULDBLOCK) { # EINPROGRESS is POSIX |
362 | } elsif ($! == &Errno::EINPROGRESS || $! == &Errno::EWOULDBLOCK) { # EINPROGRESS is POSIX |
354 | $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $connected); |
363 | $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $connected); |
355 | } else { |
364 | } else { |
356 | %state = (); |
365 | %state = (); |
… | |
… | |
366 | =item $guard = AnyEvent::Util::tcp_server $host, $port, $accept_cb[, $prepare_cb] |
375 | =item $guard = AnyEvent::Util::tcp_server $host, $port, $accept_cb[, $prepare_cb] |
367 | |
376 | |
368 | This function is experimental. |
377 | This function is experimental. |
369 | |
378 | |
370 | Create and bind a tcp socket to the given host (any IPv4 host if undef, |
379 | Create and bind a tcp socket to the given host (any IPv4 host if undef, |
371 | otherwise it must be an IPv4 or IPv6 address) and port (or an ephemeral |
380 | otherwise it must be an IPv4 or IPv6 address) and port (service name or |
372 | port if given as zero or undef), set the SO_REUSEADDR flag and call |
381 | numeric port number, or an ephemeral port if given as zero or undef, so |
|
|
382 | you cnanot bind to tcp port zero), set the SO_REUSEADDR flag and call |
373 | C<listen>. |
383 | C<listen>. |
374 | |
384 | |
375 | For each new connection that could be C<accept>ed, call the C<$accept_cb> |
385 | For each new connection that could be C<accept>ed, call the C<$accept_cb> |
376 | with the filehandle (in non-blocking mode) as first and the peer host and |
386 | with the filehandle (in non-blocking mode) as first and the peer host and |
377 | port as second and third arguments (see C<tcp_connect> for details). |
387 | port as second and third arguments (see C<tcp_connect> for details). |
… | |
… | |
409 | or Carp::croak "socket: $!"; |
419 | or Carp::croak "socket: $!"; |
410 | |
420 | |
411 | setsockopt $state{fh}, &Socket::SOL_SOCKET, &Socket::SO_REUSEADDR, 1 |
421 | setsockopt $state{fh}, &Socket::SOL_SOCKET, &Socket::SO_REUSEADDR, 1 |
412 | or Carp::croak "so_reuseaddr: $!"; |
422 | or Carp::croak "so_reuseaddr: $!"; |
413 | |
423 | |
414 | bind $state{fh}, Socket::pack_sockaddr_in $port, socket_inet_aton ($host || "0.0.0.0") |
424 | bind $state{fh}, Socket::pack_sockaddr_in _tcp_port $port, socket_inet_aton ($host || "0.0.0.0") |
415 | or Carp::croak "bind: $!"; |
425 | or Carp::croak "bind: $!"; |
416 | |
426 | |
417 | fh_nonblocking $state{fh}, 1; |
427 | fh_nonblocking $state{fh}, 1; |
418 | |
428 | |
419 | my $len = ($prepare && $prepare->($state{fh})) || 128; |
429 | my $len = ($prepare && $prepare->($state{fh})) || 128; |