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

Comparing AnyEvent/lib/AnyEvent/Util.pm (file contents):
Revision 1.18 by root, Tue May 20 15:04:43 2008 UTC vs.
Revision 1.19 by root, Tue May 20 15:13:52 2008 UTC

22 22
23use strict; 23use strict;
24 24
25no warnings "uninitialized"; 25no warnings "uninitialized";
26 26
27use Carp ();
27use Errno (); 28use Errno ();
28use Socket (); 29use Socket ();
29use IO::Socket::INET (); 30use IO::Socket::INET ();
30 31
31use AnyEvent; 32use AnyEvent;
205 206
206sub guard(&) { 207sub guard(&) {
207 bless \(my $cb = shift), AnyEvent::Util::Guard:: 208 bless \(my $cb = shift), AnyEvent::Util::Guard::
208} 209}
209 210
211sub _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
212This function is experimental. 220This function is experimental.
213 221
214This is a convenience function that creates a tcp socket and makes a 100% 222This is a convenience function that creates a tcp socket and makes a 100%
215non-blocking connect to the given C<$host> (which can be a hostname or a 223non-blocking connect to the given C<$host> (which can be a hostname or a
216textual IP address) and C<$port>. 224textual IP address) and C<$port> (which can be a numeric port number or a
225service name).
217 226
218Unless called in void context, it returns a guard object that will 227Unless called in void context, it returns a guard object that will
219automatically abort connecting when it gets destroyed (it does not do 228automatically abort connecting when it gets destroyed (it does not do
220anything to the socket after the conenct was successful). 229anything to the socket after the connect was successful).
221 230
222If the connect is successful, then the C<$connect_cb> will be invoked with 231If the connect is successful, then the C<$connect_cb> will be invoked with
223the socket filehandle (in non-blocking mode) as first and the peer host 232the 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,
225respectively. 234respectively.
252 261
253Complex Example: connect to www.google.com on port 80 and make a simple 262Complex Example: connect to www.google.com on port 80 and make a simple
254GET request without much error handling. Also limit the connection timeout 263GET request without much error handling. Also limit the connection timeout
255to 15 seconds. 264to 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
368This function is experimental. 377This function is experimental.
369 378
370Create and bind a tcp socket to the given host (any IPv4 host if undef, 379Create and bind a tcp socket to the given host (any IPv4 host if undef,
371otherwise it must be an IPv4 or IPv6 address) and port (or an ephemeral 380otherwise it must be an IPv4 or IPv6 address) and port (service name or
372port if given as zero or undef), set the SO_REUSEADDR flag and call 381numeric port number, or an ephemeral port if given as zero or undef, so
382you cnanot bind to tcp port zero), set the SO_REUSEADDR flag and call
373C<listen>. 383C<listen>.
374 384
375For each new connection that could be C<accept>ed, call the C<$accept_cb> 385For each new connection that could be C<accept>ed, call the C<$accept_cb>
376with the filehandle (in non-blocking mode) as first and the peer host and 386with the filehandle (in non-blocking mode) as first and the peer host and
377port as second and third arguments (see C<tcp_connect> for details). 387port 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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines