… | |
… | |
56 | inet_aton |
56 | inet_aton |
57 | tcp_server |
57 | tcp_server |
58 | tcp_connect |
58 | tcp_connect |
59 | ); |
59 | ); |
60 | |
60 | |
61 | our $VERSION = 4.901; |
61 | our $VERSION = $AnyEvent::VERSION; |
|
|
62 | |
|
|
63 | # used in cases where we may return immediately but want the |
|
|
64 | # caller to do stuff first |
|
|
65 | sub _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 | |
65 | Tries to parse the given dotted quad IPv4 address and return it in |
76 | Tries to parse the given dotted quad IPv4 address and return it in |
66 | octet form (or undef when it isn't in a parsable format). Supports all |
77 | octet form (or undef when it isn't in a parsable format). Supports all |
… | |
… | |
691 | |
702 | |
692 | In either case, it will create a list of target hosts (e.g. for multihomed |
703 | In either case, it will create a list of target hosts (e.g. for multihomed |
693 | hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to |
704 | hosts or hosts with both IPv4 and IPv6 addresses) and try to connect to |
694 | each in turn. |
705 | each in turn. |
695 | |
706 | |
696 | After the connection is established (but never before the C<tcp_connect> |
707 | After the connection is established, then the C<$connect_cb> will be |
697 | fucntion itself returns), then the C<$connect_cb> will be invoked with |
|
|
698 | the socket file handle (in non-blocking mode) as first and the peer host |
708 | invoked 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, |
709 | the peer host (as a textual IP address) and peer port as second and third |
700 | respectively. The fourth argument is a code reference that you can call |
710 | arguments, respectively. The fourth argument is a code reference that you |
701 | if, for some reason, you don't like this connection, which will cause |
711 | can call if, for some reason, you don't like this connection, which will |
702 | C<tcp_connect> to try the next one (or call your callback without any |
712 | cause C<tcp_connect> to try the next one (or call your callback without |
703 | arguments if there are no more connections). In most cases, you can simply |
713 | any arguments if there are no more connections). In most cases, you can |
704 | ignore this argument. |
714 | simply ignore this argument. |
705 | |
715 | |
706 | $cb->($filehandle, $host, $port, $retry) |
716 | $cb->($filehandle, $host, $port, $retry) |
707 | |
717 | |
708 | If the connect is unsuccessful, then the C<$connect_cb> will be invoked |
718 | If the connect is unsuccessful, then the C<$connect_cb> will be invoked |
709 | without any arguments and C<$!> will be set appropriately (with C<ENXIO> |
719 | without any arguments and C<$!> will be set appropriately (with C<ENXIO> |
710 | indicating a DNS resolution failure). |
720 | indicating a DNS resolution failure). |
|
|
721 | |
|
|
722 | The callback will I<never> be invoked before C<tcp_connect> returns, even |
|
|
723 | if C<tcp_connect> was able to connect immediately (e.g. on unix domain |
|
|
724 | sockets). |
711 | |
725 | |
712 | The file handle is perfect for being plugged into L<AnyEvent::Handle>, but |
726 | The file handle is perfect for being plugged into L<AnyEvent::Handle>, but |
713 | can be used as a normal perl file handle as well. |
727 | can be used as a normal perl file handle as well. |
714 | |
728 | |
715 | Unless called in void context, C<tcp_connect> returns a guard object that |
729 | Unless called in void context, C<tcp_connect> returns a guard object that |
… | |
… | |
765 | warn "done.\n"; |
779 | warn "done.\n"; |
766 | }; |
780 | }; |
767 | |
781 | |
768 | $handle->push_write ("GET / HTTP/1.0\015\012\015\012"); |
782 | $handle->push_write ("GET / HTTP/1.0\015\012\015\012"); |
769 | |
783 | |
770 | $handle->push_read_line ("\015\012\015\012", sub { |
784 | $handle->push_read (line => "\015\012\015\012", sub { |
771 | my ($handle, $line) = @_; |
785 | my ($handle, $line) = @_; |
772 | |
786 | |
773 | # print response header |
787 | # print response header |
774 | print "HEADER\n$line\n\nBODY\n"; |
788 | print "HEADER\n$line\n\nBODY\n"; |
775 | |
789 | |
… | |
… | |
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 |
|
|
799 | sub _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 | |
|
|
808 | sub tcp_connect($$$;$) { |
811 | sub 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 |