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

Comparing AnyEvent/lib/AnyEvent/Socket.pm (file contents):
Revision 1.107 by root, Thu Aug 6 13:31:01 2009 UTC vs.
Revision 1.108 by root, Thu Aug 6 13:45:05 2009 UTC

56 inet_aton 56 inet_aton
57 tcp_server 57 tcp_server
58 tcp_connect 58 tcp_connect
59); 59);
60 60
61our $VERSION = 4.901; 61our $VERSION = 4.91;
62
63# used in cases where we may return immediately but want the
64# caller to do stuff first
65sub _postpone {
66 my ($cb, @args) = @_;
67
68 my $w; $w = AE::timer 0, 0, sub {
69 undef $w;
70 $cb->(@args);
71 };
72}
62 73
63=item $ipn = parse_ipv4 $dotted_quad 74=item $ipn = parse_ipv4 $dotted_quad
64 75
65Tries to parse the given dotted quad IPv4 address and return it in 76Tries to parse the given dotted quad IPv4 address and return it in
66octet form (or undef when it isn't in a parsable format). Supports all 77octet form (or undef when it isn't in a parsable format). Supports all
691 702
692In either case, it will create a list of target hosts (e.g. for multihomed 703In either case, it will create a list of target hosts (e.g. for multihomed
693hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to 704hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to
694each in turn. 705each in turn.
695 706
696After the connection is established (but never before the C<tcp_connect> 707After the connection is established, then the C<$connect_cb> will be
697fucntion itself returns), then the C<$connect_cb> will be invoked with
698the socket file handle (in non-blocking mode) as first and the peer host 708invoked with the socket file handle (in non-blocking mode) as first and
699(as a textual IP address) and peer port as second and third arguments, 709the peer host (as a textual IP address) and peer port as second and third
700respectively. The fourth argument is a code reference that you can call 710arguments, respectively. The fourth argument is a code reference that you
701if, for some reason, you don't like this connection, which will cause 711can call if, for some reason, you don't like this connection, which will
702C<tcp_connect> to try the next one (or call your callback without any 712cause C<tcp_connect> to try the next one (or call your callback without
703arguments if there are no more connections). In most cases, you can simply 713any arguments if there are no more connections). In most cases, you can
704ignore this argument. 714simply ignore this argument.
705 715
706 $cb->($filehandle, $host, $port, $retry) 716 $cb->($filehandle, $host, $port, $retry)
707 717
708If the connect is unsuccessful, then the C<$connect_cb> will be invoked 718If the connect is unsuccessful, then the C<$connect_cb> will be invoked
709without any arguments and C<$!> will be set appropriately (with C<ENXIO> 719without any arguments and C<$!> will be set appropriately (with C<ENXIO>
710indicating a DNS resolution failure). 720indicating a DNS resolution failure).
721
722The callback will I<never> be invoked before C<tcp_connect> returns, even
723if C<tcp_connect> was able to connect immediately (e.g. on unix domain
724sockets).
711 725
712The file handle is perfect for being plugged into L<AnyEvent::Handle>, but 726The file handle is perfect for being plugged into L<AnyEvent::Handle>, but
713can be used as a normal perl file handle as well. 727can be used as a normal perl file handle as well.
714 728
715Unless called in void context, C<tcp_connect> returns a guard object that 729Unless called in void context, C<tcp_connect> returns a guard object that
792 ... 806 ...
793 } 807 }
794 808
795=cut 809=cut
796 810
797# used in cases where we may return immediately but want the
798# caller to do stuff first
799sub _delayed_call {
800 my ($cb, @args) = @_;
801
802 my $w; $w = AE::timer 0, 0, sub {
803 undef $w;
804 $cb->(@args);
805 };
806}
807
808sub tcp_connect($$$;$) { 811sub tcp_connect($$$;$) {
809 my ($host, $port, $connect, $prepare) = @_; 812 my ($host, $port, $connect, $prepare) = @_;
810 813
811 # see http://cr.yp.to/docs/connect.html for some background 814 # see http://cr.yp.to/docs/connect.html for some background
812 # also http://advogato.org/article/672.html 815 # also http://advogato.org/article/672.html
819 822
820 $state{next} = sub { 823 $state{next} = sub {
821 return unless exists $state{fh}; 824 return unless exists $state{fh};
822 825
823 my $target = shift @target 826 my $target = shift @target
824 or return (%state = (), _delayed_call $connect); 827 or return (%state = (), _postpone $connect);
825 828
826 my ($domain, $type, $proto, $sockaddr) = @$target; 829 my ($domain, $type, $proto, $sockaddr) = @$target;
827 830
828 # socket creation 831 # socket creation
829 socket $state{fh}, $domain, $type, $proto 832 socket $state{fh}, $domain, $type, $proto

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines