… | |
… | |
712 | or die "unable to connect: $!"; |
712 | or die "unable to connect: $!"; |
713 | |
713 | |
714 | my $handle; # avoid direct assignment so on_eof has it in scope. |
714 | my $handle; # avoid direct assignment so on_eof has it in scope. |
715 | $handle = new AnyEvent::Handle |
715 | $handle = new AnyEvent::Handle |
716 | fh => $fh, |
716 | fh => $fh, |
|
|
717 | on_error => sub { |
|
|
718 | warn "error $_[2]\n"; |
|
|
719 | }, |
717 | on_eof => sub { |
720 | on_eof => sub { |
718 | undef $handle; # keep it alive till eof |
721 | $handle->destroy; # destroy handle |
719 | warn "done.\n"; |
722 | warn "done.\n"; |
720 | }; |
723 | }; |
721 | |
724 | |
722 | $handle->push_write ("GET / HTTP/1.0\015\012\015\012"); |
725 | $handle->push_write ("GET / HTTP/1.0\015\012\015\012"); |
723 | |
726 | |
… | |
… | |
780 | my $timeout = $prepare && $prepare->($state{fh}); |
783 | my $timeout = $prepare && $prepare->($state{fh}); |
781 | |
784 | |
782 | $timeout ||= 30 if AnyEvent::WIN32; |
785 | $timeout ||= 30 if AnyEvent::WIN32; |
783 | |
786 | |
784 | $state{to} = AnyEvent->timer (after => $timeout, cb => sub { |
787 | $state{to} = AnyEvent->timer (after => $timeout, cb => sub { |
785 | $! = &Errno::ETIMEDOUT; |
788 | $! = Errno::ETIMEDOUT; |
786 | $state{next}(); |
789 | $state{next}(); |
787 | }) if $timeout; |
790 | }) if $timeout; |
788 | |
791 | |
789 | # called when the connect was successful, which, |
792 | # called when the connect was successful, which, |
790 | # in theory, could be the case immediately (but never is in practise) |
793 | # in theory, could be the case immediately (but never is in practise) |
… | |
… | |
801 | $guard->cancel; |
804 | $guard->cancel; |
802 | $state{next}(); |
805 | $state{next}(); |
803 | }); |
806 | }); |
804 | } else { |
807 | } else { |
805 | # dummy read to fetch real error code |
808 | # dummy read to fetch real error code |
806 | sysread $state{fh}, my $buf, 1 if $! == &Errno::ENOTCONN; |
809 | sysread $state{fh}, my $buf, 1 if $! == Errno::ENOTCONN; |
807 | |
810 | |
808 | return if $! == &Errno::EAGAIN; # skip spurious wake-ups |
811 | return if $! == Errno::EAGAIN; # skip spurious wake-ups |
809 | |
812 | |
810 | delete $state{ww}; delete $state{to}; |
813 | delete $state{ww}; delete $state{to}; |
811 | |
814 | |
812 | $state{next}(); |
815 | $state{next}(); |
813 | } |
816 | } |
814 | }; |
817 | }; |
815 | |
818 | |
816 | # now connect |
819 | # now connect |
817 | if (connect $state{fh}, $sockaddr) { |
820 | if (connect $state{fh}, $sockaddr) { |
818 | $state{connected}->(); |
821 | $state{connected}->(); |
819 | } elsif ($! == &Errno::EINPROGRESS # POSIX |
822 | } elsif ($! == Errno::EINPROGRESS # POSIX |
820 | || $! == &Errno::EWOULDBLOCK |
823 | || $! == Errno::EWOULDBLOCK |
821 | # WSAEINPROGRESS intentionally not checked - it means something else entirely |
824 | # WSAEINPROGRESS intentionally not checked - it means something else entirely |
822 | || $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt |
825 | || $! == AnyEvent::Util::WSAEINVAL # not convinced, but doesn't hurt |
823 | || $! == AnyEvent::Util::WSAEWOULDBLOCK) { |
826 | || $! == AnyEvent::Util::WSAEWOULDBLOCK) { |
824 | $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $state{connected}); |
827 | $state{ww} = AnyEvent->io (fh => $state{fh}, poll => 'w', cb => $state{connected}); |
825 | $state{connected}->();#d# |
|
|
826 | } else { |
828 | } else { |
827 | $state{next}(); |
829 | $state{next}(); |
828 | } |
830 | } |
829 | }; |
831 | }; |
830 | |
832 | |
831 | $! = &Errno::ENXIO; |
833 | $! = Errno::ENXIO; |
832 | $state{next}(); |
834 | $state{next}(); |
833 | }; |
835 | }; |
834 | |
836 | |
835 | defined wantarray && guard { %state = () } |
837 | defined wantarray && guard { %state = () } |
836 | } |
838 | } |