--- AnyEvent-FastPing/FastPing.pm 2016/11/11 01:18:56 1.18 +++ AnyEvent-FastPing/FastPing.pm 2016/11/12 01:20:46 1.19 @@ -29,7 +29,7 @@ use AnyEvent; BEGIN { - our $VERSION = 2.03; + our $VERSION = 2.1; our @ISA = qw(Exporter); require Exporter; @@ -41,27 +41,13 @@ our ($THR_RES_FD, $ICMP4_FD, $ICMP6_FD); -our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; +our $THR_RES_FH; our $ICMP4_FH; our $ICMP6_FH; our @IDLE_CB; -AnyEvent::post_detect { - our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; - our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; - - our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { - sysread $THR_RES_FH, my $buf, 8; - - for my $id (unpack "S*", $buf) { - _stop_id $id; - ($IDLE_CB[$id] || sub { })->(); - } - }; -}; - =item AnyEvent::FastPing::ipv4_supported Returns true iff IPv4 is supported in this module and on this system. @@ -169,11 +155,27 @@ =cut sub new { - my ($klass) = @_; + _boot; + + our $ICMP4_W = $ICMP4_FD >= 0 && (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4; + our $ICMP6_W = $ICMP6_FD >= 0 && (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6; - AnyEvent::detect unless defined $AnyEvent::MODEL; + open $THR_RES_FH, "<&=$THR_RES_FD" or die "AnyEvent::FastPing: FATAL: cannot fdopen thread result fd"; + + our $THR_RES_W = AE::io $THR_RES_FH, 0, sub { + sysread $THR_RES_FH, my $buf, 8; + + for my $id (unpack "S*", $buf) { + _stop_id $id; + ($IDLE_CB[$id] || sub { })->(); + } + }; + + *new = sub { + _new shift, (rand 65536), (rand 65536), (rand 65536) + }; - _new $klass, (rand 65536), (rand 65536), (rand 65536) + goto &new; } sub DESTROY {