blib/lib/AnyEvent/PacketReader.pm

filter: Statementstmt 27.6% 39 / 141 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 47.0% 8 / 17 Podpod 33.3% 1 / 3 totaltotal 18.0% 48 / 266 CC 47 SCAR 56.7
CC
47
Cov
16%
_read
462.0
packet_reader
420.0
DESTROY
12.0
CRAP 289.3
↑ Summary
1 package AnyEvent::PacketReader;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10 1 our @ISA = qw(Exporter);
11 1 our @EXPORT = qw(packet_reader);
12  
13 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Errno qw(EPIPE EBADMSG EMSGSIZE EINTR EAGAIN EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $MAX_TOTAL_LENGTH = 1e6;
18  
19 1 our $debug;
20  
21 1 my %header_length = ( n => 2,
22 v => 2,
23 N => 4,
24 V => 4,
25 W => 1,
26 S => 2,
27 L => 4,
28 Q => 8 );
29  
30 1 for my $dir (qw(> <)) {
31 2 for my $t (qw(S L Q)) {
32 6 $header_length{"$t$dir"} = $header_length{$t};
33 }
34 }
35  
36 1 my %short_templ = map { $_ => $_ } keys %header_length;
37 1 my %load_offset = %header_length;
38 1 my $good_packers = join '', keys %header_length;
39  
40 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0 ▶$SIG{INT} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 print Data::Dumper->Dump([\%short_templ, \%header_length, \%load_offset], [qw(%short_templ %header_length %load_offset)]);
43 0 exit 1;
44 1 };
45  
46 0 ▶sub packet_reader {
Subroutine packet_reader: not called CC=20 SCAR=60.4
Pod documented
47 0 my $cb = pop;
48 0 my ($fh, $templ, $max_total_length) = @_;
49 0 ▶ croak 'Usage: packet_reader($fh, [$templ, [$max_total_length,]] $callback)'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh and defined $cb 0 0 0
Condition0%0/3
defined $fh and defined $cb
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fh and defined $cb
defined $fh defined $cb
Truth table0%0/3
A and B
A= defined $fhB= defined $cb
ABresult
0X0
100
111
50 unless defined $fh and defined $cb;
51  
52 0 ▶ $max_total_length ||= $MAX_TOTAL_LENGTH;
Condition0%0/3
$max_total_length ||= $AnyEvent::PacketReader::MAX_TOTAL_LENGTH
l!l&&r!l&&!r
000
MC/DC0%0/2
$max_total_length ||= $AnyEvent::PacketReader::MAX_TOTAL_LENGTH
$max_total_length $AnyEvent::PacketReader::MAX_TOTAL_LENGTH
Truth table0%0/3
A ||= B
A= $max_total_lengthB= $AnyEvent::PacketReader::MAX_TOTAL_LENGTH
ABresult
1X1
011
000
53 0 my ($header_length, $load_offset, $short_templ);
54  
55 0 ▶ if (defined $templ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $templ) { } 0 0 0
56 0 ▶ unless (defined($short_templ = $short_templ{$templ})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($short_templ = $short_templ{$templ})) 0 0 0
57 0 ▶ $debug and warn "PR: examining template '$templ'\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: examining template '${templ}'\n" 0 0 0
58 0 my $load_offset;
59 0 ▶ if ($templ =~ /^(x+)(\d*)/g) {
Branch: 2 branches
BranchTrueFalseTotal
if ($templ =~ /^(x+)(\d*)/g) { } 0 0 0
elsif ($templ =~ /^\@!(\d*)/g) { } 0 0 0
60 0 ▶ $header_length = length($1) + (length $2 ? $2 - 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
length $2 ? : 0 0 0
61 }
62 elsif ($templ =~ /^\@!(\d*)/g) {
63 0 ▶ $header_length = (length $1 ? $1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 0 0 0
64 }
65 else {
66 0 $header_length = 0;
67 }
68  
69 0 ▶ $templ =~ /\G([$good_packers][<>]?)/go
Branch: 1 branch
BranchTrueFalseTotal
$templ =~ /\G([$good_packers][<>]?)/go or croak("bad header template '${templ}'") 0 0 0
70 or croak "bad header template '$templ'";
71  
72 0 ▶ $header_length += ($header_length{$1} // die "Internal error: \$header_length{$1} is not defined");
Condition0%0/2
$header_length{$1} // die("Internal error: \$header_length{$1} is not defined")
l!l
00
MC/DC0%0/1
$header_length{$1} // die("Internal error: \$header_length{$1} is not defined")
$header_length{$1}
Truth table0%0/2
A
A= $header_length{$1}
Aresult
11
00
73  
74 0 $short_templ = substr $templ, 0, pos($templ);
75  
76  
77 0 ▶ if ($templ =~ /\G\@!(\d*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($templ =~ /\G\@!(\d*)/g) { } 0 0 0
78 0 ▶ $load_offset = (length $1 ? $1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 0 0 0
79 }
80 else {
81 0 $load_offset = $header_length;
82 0 ▶ if ($templ =~ /\G(x+)(\d*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($templ =~ /\G(x+)(\d*)/g) 0 0 0
83 0 ▶ $load_offset += length $1 + (length $2 ? $2 - 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
length $2 ? : 0 0 0
84 }
85 }
86  
87 0 ▶ $templ =~ /\G$/g or croak "bad header template '$templ'";
Branch: 1 branch
BranchTrueFalseTotal
$templ =~ /\G$/g or croak("bad header template '${templ}'") 0 0 0
88  
89 0 $short_templ{$templ} = $short_templ;
90 0 $header_length{$templ} = $header_length;
91 0 $load_offset{$templ} = $load_offset;
92 0 ▶ $debug and warn "PR: template '$templ' examined, header_length: $header_length, load_offset: $load_offset\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: template '${templ}' examined, header_length: $header_length, load_offset: $load_offset\n" 0 0 0
93 }
94  
95 0 $header_length = $header_length{$templ};
96 0 $load_offset = $load_offset{$templ};
97  
98 }
99 else {
100 0 ▶ $debug and warn "PR: defaulting to template 'N'\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: defaulting to template 'N'\n" 0 0 0
101 0 $templ = 'N';
102 0 $header_length = 4;
103 0 $load_offset = 4;
104 }
105  
106 # data is: 0:buffer, 1:fh, 2:watcher, 3:header_length, 4:total_length, 5:short_templ, 6:max_total_length, 7:cb, 8:load_offset
107 0 my $data = ['' , $fh , undef , $header_length , undef , $short_templ , $max_total_length , $cb , $load_offset ];
108 0 my $obj = \$data;
109 0 bless $obj;
110 0 $obj->resume;
111 0 $obj;
112 }
113  
114 0 ▶sub pause {
Subroutine pause: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my $data = ${shift()};
116 0 $data->[2] = undef;
117 }
118  
119 0 ▶sub resume {
Subroutine resume: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my $data = ${shift()};
121 0 ▶ if (defined(my $fh = $data->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fh = $data->[1])) 0 0 0
122 0 ▶ $data->[2] = AE::io $fh, 0, sub { _read($data) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 }
124 }
125  
126 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
127 0 my $obj = shift;
128 0 ▶ $debug and warn "PR: watcher is gone, aborting read\n" if ${$obj}->[3];
Branch: 1 branch
BranchTrueFalseTotal
if ${$obj;}->[3] 0 0 0
Condition0%0/5
${$obj;}->[3] && ($AnyEvent::PacketReader::debug && warn("PR: watcher is gone, aborting read\n"))
!ll&&!rl&&r
000
$AnyEvent::PacketReader::debug and warn "PR: watcher is gone, aborting read\n"
!ll
00
MC/DC0%0/2
${$obj;}->[3] && ($AnyEvent::PacketReader::debug && warn("PR: watcher is gone, aborting read\n"))
${$obj;}->[3] $AnyEvent::PacketReader::debug
Truth table0%0/3
A && B
A= ${$obj;}->[3]B= $AnyEvent::PacketReader::debug
ABresult
0X0
100
111
129 0 @{$$obj} = ();
130 }
131  
132 0 ▶sub _hexdump {
Subroutine _hexdump: not called CC=3 SCAR=20.8
133 0 local ($!, $@);
134 1 ▶ no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
135 0 ▶ while ($_[0] =~ /(.{1,32})/smg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /(.{1,32})/gms) 0 0 0
136 0 my $line = $1;
137 0 my @c= (( map { sprintf "%02x",$_ } unpack('C*', $line)),
138 ((" ") x 32))[0..31];
139 0 ▶ $line=~s/(.)/ my $c=$1; unpack("c",$c)>=32 ? $c : '.' /egms;
Branch: 1 branch
BranchTrueFalseTotal
unpack('c', $c) >= 32 ? : 0 0 0
140 0 print STDERR "$_[1] ", join(" ", @c, '|', $line), "\n";
141 }
142 0 print STDERR "\n";
143  
144 }
145  
146 0 ▶sub _read {
Subroutine _read: not called CC=21 SCAR=61.4
147 0 my $data = shift;
148 0 ▶ my $length = $data->[4] || $data->[3];
Condition0%0/3
$data->[4] || $data->[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->[4] || $data->[3]
$data->[4] $data->[3]
Truth table0%0/3
A || B
A= $data->[4]B= $data->[3]
ABresult
1X1
011
000
149 0 my $offset = length $data->[0];
150 0 my $remaining = $length - $offset;
151 0 my $bytes = sysread($data->[1], $data->[0], $remaining, $offset);
152 0 ▶ if ($bytes) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bytes) { } 0 0 0
elsif (defined $bytes) { } 0 0 0
153 0 ▶ $debug and warn "PR: $bytes bytes read\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: $bytes bytes read\n" 0 0 0
154 0 ▶ if (length $data->[0] == $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data->[0] == $length) 0 0 0
155 0 ▶ unless (defined $data->[4]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->[4]) 0 0 0
156 0 my $load_length = unpack $data->[5], $data->[0];
157 0 ▶ unless (defined $load_length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $load_length) 0 0 0
158 0 ▶ $debug and warn "PR: unable to extract size field from header\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: unable to extract size field from header\n" 0 0 0
159 0 return _fatal($data, EBADMSG);
160 }
161 0 my $total_length = $data->[8] + $load_length;
162 0 ▶ $debug and warn "PR: reading full packet ".
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn 'PR: reading full packet ' . "(load length: $load_length, total: $total_length, current: $length)\n" 0 0 0
163 "(load length: $load_length, total: $total_length, current: $length)\n";
164  
165 0 ▶ if ($total_length > $data->[6]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_length > $data->[6]) 0 0 0
166 0 ▶ $debug and warn "PR: received packet is too long\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: received packet is too long\n" 0 0 0
167 0 return _fatal($data, EMSGSIZE)
168 }
169 0 ▶ if ($length < $total_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < $total_length) 0 0 0
170 0 $data->[4] = $total_length;
171 0 return;
172 }
173 # else, the packet is done
174 0 ▶ if ($length > $total_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > $total_length) 0 0 0
175 0 ▶ $debug and warn "PR: header length ($length) > total length ($total_length)\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: header length ($length) > total length ($total_length)\n" 0 0 0
176 0 return _fatal($data, EBADMSG);
177 }
178 }
179  
180 0 ▶ $debug and warn "PR: packet read, invoking callback\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: packet read, invoking callback\n" 0 0 0
181 0 $data->[7]->($data->[0]);
182 # somebody may have taken a reference to the buffer so we start clean:
183 0 @$data = ('', @$data[1..3], undef, @$data[5..$#$data]);
184 0 ▶ $debug and warn "PR: waiting for a new packet\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: waiting for a new packet\n" 0 0 0
185 }
186 }
187 elsif (defined $bytes) {
188 0 ▶ $debug and warn "PR: EOF!\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: EOF!\n" 0 0 0
189 0 return _fatal($data, EPIPE);
190 }
191 else {
192 0 ▶ $debug and warn "PR: sysread failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$AnyEvent::PacketReader::debug and warn "PR: sysread failed: $!\n" 0 0 0
193 0 ▶ $! == $_ and return for (EINTR, EAGAIN, EWOULDBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
$! == $_ and return 0 0 0
194 0 return _fatal($data);
195 }
196 }
197  
198 0 ▶sub _fatal {
Subroutine _fatal: not called CC=3 SCAR=24.8
199 0 my $data = shift;
200 0 ▶ local $! = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
201 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AnyEvent::PacketReader::debug) 0 0 0
202 0 warn "PR: fatal error: $!\n";
203 0 _hexdump($data->[0], 'pkt:');
204 }
205 0 $data->[7]->();
206 0 @$data = (); # release watcher;
207 }
208  
209 1 1;
210 __END__
↑ Summary