Skip to content

Instantly share code, notes, and snippets.

@Logioniz
Created September 14, 2015 16:09
Show Gist options
  • Save Logioniz/c2bec75ae71e22830e04 to your computer and use it in GitHub Desktop.
Save Logioniz/c2bec75ae71e22830e04 to your computer and use it in GitHub Desktop.
ipv4, ipv6 connect async ev
#!/use/bin/perl
use Mojo::Base -strict;
use Socket qw/:DEFAULT SOCK_NONBLOCK getaddrinfo/;
use EV;
my $host = 'localhost';
my $port = '27017';
my ($tcp_name, $tcp_alias, $tcp_proto) = getprotobyname('tcp');
my ($err, @hosts) = getaddrinfo($host, $port, {protocol => $tcp_proto});
my ($ws, $w);
my ($socks, $sock);
for $host (@hosts) {
if ($host->{family} eq AF_INET) {
socket($sock, PF_INET, SOCK_STREAM | SOCK_NONBLOCK, getprotobyname('tcp'));
} else {
socket($sock, PF_INET6, SOCK_STREAM | SOCK_NONBLOCK, getprotobyname('tcp'));
}
push @$socks, $sock;
if (!connect($sock, $host->{addr})) {
if ($!{EINPROGRESS} || $!{EWOULDBLOCK}) {
$! = unpack("L", getsockopt($sock, SOL_SOCKET, SO_ERROR));
warn $! and next if $!;
$w = EV::io $sock, EV::WRITE, sub {
my ($w, $revents) = @_;
exit 0;
};
push @$ws, $w;
} else {
}
}
}
EV::run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment