AnyEvent-Ping
view release on metacpan or search on metacpan
lib/AnyEvent/Ping.pm view on Meta::CPAN
$self->_store_result($request, 'ERROR');
}
sub _icmp_checksum {
my ($self, $msg) = @_;
my $res = 0;
foreach my $int (unpack "n*", $msg) {
$res += $int;
}
# Add possible odd byte
$res += unpack('C', substr($msg, -1, 1)) << 8
if length($msg) % 2;
# Fold high into low
$res = ($res >> 16) + ($res & 0xffff);
# Two times
$res = ($res >> 16) + ($res & 0xffff);
return ~$res;
}
1;
__END__
=head1 NAME
AnyEvent::Ping - ping hosts with AnyEvent
=head1 SYNOPSIS
use AnyEvent;
use AnyEvent::Ping;
my $host = shift || '4.2.2.2';
my $times = shift || 4;
my $package_s = shift || 56;
my $c = AnyEvent->condvar;
my $ping = AnyEvent::Ping->new;
print "PING $host $package_s(@{[$package_s+8]}) bytes of data\n";
$ping->ping($host, $times, sub {
my $results = shift;
foreach my $result (@$results) {
my $status = $result->[0];
my $time = $result->[1];
printf "%s from %s: time=%f ms\n",
$status, $host, $time * 1000;
};
$c->send;
});
$c->recv;
$ping->end;
=head1 DESCRIPTION
L<AnyEvent::Ping> is an asynchronous AnyEvent pinger.
=head1 ATTRIBUTES
L<AnyEvent::Ping> implements the following attributes.
=head2 C<interval>
my $interval = $ping->interval;
$ping->interval(1);
Interval between pings, defaults to 0.2 seconds.
=head2 C<timeout>
my $timeout = $ping->timeout;
$ping->timeout(3);
Maximum response time, defaults to 5 seconds.
=head2 C<error>
my $error = $ping->error;
Last error message.
=head1 METHODS
L<AnyEvent::Ping> implements the following methods.
=head2 C<new>
my $ping = AnyEvent::Ping->new(%options)
Constructs AnyEvent::Ping object. Following options can be passed:
=head3 C<interval>
=head3 C<timeout>
=head3 C<on_prepare>
In some cases you need to "tune" the socket before it is used to ping (for
exmaple, to bind it on a given IP address).
my $ping = AnyEvent::Ping->new(
on_prepare => sub {
my $socket = shift;
...
});
=head3 C<packet_generator>
Generates the data to be sent.
my $ping = AnyEvent::Ping->new(
packet_generator => sub {
&AnyEvent::Ping::generate_data_random($packet_size);
});
=head3 C<packet_size>
( run in 1.532 second using v1.01-cache-2.11-cpan-5837b0d9d2c )