ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/FastPing.pm
(Generate patch)

Comparing AnyEvent-FastPing/FastPing.pm (file contents):
Revision 1.6 by root, Tue Nov 17 21:37:26 2009 UTC vs.
Revision 1.9 by root, Sun Jan 16 11:48:15 2011 UTC

21 21
22=cut 22=cut
23 23
24package AnyEvent::FastPing; 24package AnyEvent::FastPing;
25 25
26use strict; 26use common::sense;
27no warnings;
28 27
29use AnyEvent; 28use AnyEvent;
30 29
31BEGIN { 30BEGIN {
32 our $VERSION = '1.11'; 31 our $VERSION = '1.14';
33 our @ISA = qw(Exporter); 32 our @ISA = qw(Exporter);
34 33
35 require Exporter; 34 require Exporter;
36 #Exporter::export_ok_tags (keys %EXPORT_TAGS); 35 #Exporter::export_ok_tags (keys %EXPORT_TAGS);
37 36
43 42
44our $THR_REQ_FH; open $THR_REQ_FH, ">&=$THR_REQ_FD" or die "FATAL: cannot fdopen"; 43our $THR_REQ_FH; open $THR_REQ_FH, ">&=$THR_REQ_FD" or die "FATAL: cannot fdopen";
45our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen"; 44our $THR_RES_FH; open $THR_RES_FH, "<&=$THR_RES_FD" or die "FATAL: cannot fdopen";
46 45
47our $THR_REQ_W; 46our $THR_REQ_W;
48our $THR_RES_W = AnyEvent->io (fh => $THR_RES_FH, poll => 'r', cb => sub { 47our $THR_RES_W = AE::io $THR_RES_FH, 0, sub {
49 my $sv = _read_res 48 my $sv = _read_res
50 or return; 49 or return;
51 50
52 $sv->(); 51 $sv->();
53}); 52};
54 53
55our $THR_REQ_BUF; 54our $THR_REQ_BUF;
56 55
57sub _send_req($) { 56sub _send_req($) {
58 $THR_REQ_BUF .= $_[0]; 57 $THR_REQ_BUF .= $_[0];
59 58
60 $THR_REQ_W ||= AnyEvent->io (fh => $THR_REQ_FH, poll => 'w', cb => sub { 59 $THR_REQ_W ||= AE::io $THR_REQ_FH, 1, sub {
61 my $len = syswrite $THR_REQ_FH, $THR_REQ_BUF; 60 my $len = syswrite $THR_REQ_FH, $THR_REQ_BUF;
62 substr $THR_REQ_BUF, 0, $len, ""; 61 substr $THR_REQ_BUF, 0, $len, "";
63 62
64 undef $THR_REQ_W unless length $THR_REQ_BUF; 63 undef $THR_REQ_W unless length $THR_REQ_BUF;
65 }); 64 };
66} 65}
67 66
68=item AnyEvent::FastPing::ipv4_supported 67=item AnyEvent::FastPing::ipv4_supported
69 68
70Returns true if IPv4 is supported in this module and on this system. 69Returns true if IPv4 is supported in this module and on this system.
145sub icmp_ping($$$&) { 144sub icmp_ping($$$&) {
146 _send_req _req_icmp_ping @_; 145 _send_req _req_icmp_ping @_;
147} 146}
148 147
149our $ICMP4_FH; 148our $ICMP4_FH;
150our $ICMP4_W = (open $ICMP4_FH, "<&=$ICMP4_FD") && AnyEvent->io (fh => $ICMP4_FH, poll => 'r', cb => \&_recv_icmp4); 149our $ICMP4_W = (open $ICMP4_FH, "<&=$ICMP4_FD") && AE::io $ICMP4_FH, 0, \&_recv_icmp4;
151our $ICMP6_FH; 150our $ICMP6_FH;
152our $ICMP6_W = (open $ICMP6_FH, "<&=$ICMP6_FD") && AnyEvent->io (fh => $ICMP6_FH, poll => 'r', cb => \&_recv_icmp6); 151our $ICMP6_W = (open $ICMP6_FH, "<&=$ICMP6_FD") && AE::io $ICMP6_FH, 0, \&_recv_icmp6;
153 152
154=item AnyEvent::FastPing::register_cb \&cb 153=item AnyEvent::FastPing::register_cb \&cb
155 154
156Register a callback that is called for every received ping reply 155Register a callback that is called for every received ping reply
157(regardless of whether a ping is still in process or not and regardless of 156(regardless of whether a ping is still in process or not and regardless of
161entry for each received packet (replies are being batched for greater 160entry for each received packet (replies are being batched for greater
162efficiency). Each packet is represented by an arrayref with three members: 161efficiency). Each packet is represented by an arrayref with three members:
163the source address (an octet string of either 4 (IPv4) or 16 (IPv6) octets 162the source address (an octet string of either 4 (IPv4) or 16 (IPv6) octets
164length), the payload as passed to C<icmp_ping> and the round trip time in 163length), the payload as passed to C<icmp_ping> and the round trip time in
165seconds. 164seconds.
165
166Example: register a callback which simply dumps the received data. Since
167the coderef is created on the fly via sub, it would be hard to unregister
168this callback again :)
169
170 AnyEvent::FastPing::register_cb sub {
171 for (@{$_[0]}) {
172 printf "%s %d %g\n",
173 (4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])),
174 $_->[2],
175 $_->[1];
176 }
177 };
166 178
167Example: a single ping reply with payload of 1 from C<::1> gets passed 179Example: a single ping reply with payload of 1 from C<::1> gets passed
168like this: 180like this:
169 181
170 [ [ 182 [ [
196 208
197=cut 209=cut
198 210
199our @CB; 211our @CB;
200 212
201sub register_cb(&) { 213sub register_cb($) {
202 push @CB, $_[0]; 214 push @CB, $_[0];
203} 215}
204 216
205sub unregister_cb($) { 217sub unregister_cb($) {
206 @CB = grep $_ != $_[0], @CB; 218 @CB = grep $_ != $_[0], @CB;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines