Tiny/Netstring.pm

filter: Statementstmt 77.5% 45 / 58 Branchbran 44.7% 17 / 38 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 4 totaltotal 63.7% 72 / 113 CC 21 SCAR 25.5
CC
21
Cov
65%
syswritefull
30.0
netstring_read
13.1
sysreadfull
7.9
CRAP 12.8
« Tiny/Async.pm ↑ Summary Tiny/SessionCache.pm »
1 # Netstring messages for RPC::Switch::Client::Tiny
2 #
3 package RPC::Switch::Client::Tiny::Netstring;
4  
5 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 our @EXPORT = qw(netstring_read netstring_write);
9  
10 22 our $VERSION = 1.14;
11  
12 # Returns data in $buf and number of bytes read, or undef on EOF
13 # (see: https://www.perlmonks.org/?node_id=1173814)
14 #
15 4841 ▶sub sysreadfull {
Subroutine sysreadfull: called CC=6 SCAR=20.7
Pod undocumented
16 4841 my ($file, $len) = ($_[0], $_[2]); # need ref on $buf here
17 4841 my $n = 0;
18 4841 ▶ while ($len - $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len - $n) 4841 4841 9682
19 4841 my $i = sysread($file, $_[1], $len - $n, $n);
20 4841 ▶ if (defined $i) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $i) { } 4841 0 4841
elsif ($!{'EINTR'}) { } 0 0 0
21 4841 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 0 4841 4841
22 0 return $n;
23 } else {
24 4841 $n += $i;
25 }
26 1 ▶ } elsif ($!{EINTR}) {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 redo;
28 } else {
29 0 ▶ return $n ? $n : undef;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 0 0 0
30 }
31 }
32 4841 return $n;
33 }
34  
35 # Returns number of bytes written. Catches partial writes and
36 # interrupts but returns on file-errors like the print call.
37 #
38 0 ▶sub syswritefull {
Subroutine syswritefull: not called CC=5 SCAR=34.0
Pod undocumented
39 0 my ($file, $buf) = @_;
40 0 my $len = length($buf);
41 0 my $n = 0;
42 0 ▶ while ($len - $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len - $n) 0 0 0
43 0 my $i = syswrite($file, $buf, $len - $n, $n);
44 0 ▶ if (defined($i)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $i) { } 0 0 0
elsif ($!{'EINTR'}) { } 0 0 0
45 0 $n += $i;
46 } elsif ($!{EINTR}) {
47 0 redo;
48 } else {
49 0 ▶ return $n ? $n : undef;
Branch: 1 branch
BranchTrueFalseTotal
$n ? : 0 0 0
50 }
51 }
52 0 return $n;
53 }
54  
55 # netstring proto: http://cr.yp.to/proto/netstrings.txt
56 #
57 1105 ▶sub netstring_write {
Subroutine netstring_write: called CC=1 SCAR=0.0
Pod undocumented
58 1105 my ($s, $str) = @_;
59  
60 # A print call catches partial writes and interrupts,
61 # but it will return on file-errors like 'Broken Pipe'.
62 #
63 # TODO: $client->stop() does not interrupt a blocking print
64 #
65 1105 my $res = print $s '' . length($str) . ':' . $str . ',';
66 1105 $s->flush();
67 1105 return $res;
68 }
69  
70 # returns received netstring, or empty string on EOF
71 # dies on error -> use eval {..}
72 #
73 1181 ▶sub netstring_read {
Subroutine netstring_read: called CC=12 SCAR=25.7
Pod undocumented
74 1181 my ($s) = @_;
75 1181 my ($c, $b, $n) = ('', '', '');
76  
77 # Break on EINTR only before start of message,
78 # so that a partial message is never discarded.
79 #
80 1181 my $res = sysread($s,$c,1);
81 1181 ▶ while ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 3775 115 3890
82 3775 ▶ if ($c ne ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne ':') { } 2709 1066 3775
83 2709 ▶ die "bad netstring: $c" unless ($c =~ /\d+/);
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /\d+/ 0 2709 2709
84 2709 $n .= $c;
85 } else {
86 1066 ▶ die "bad netstring: $c" if ($n eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "" 0 1066 1066
87 1066 last;
88 }
89 2709 $res = sysreadfull($s,$c,1);
90 }
91 1181 ▶ if ($res && ($res = sysreadfull($s,$b,$n))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res and $res = sysreadfull($s, $b, $n)) 1066 115 1181
Condition66%2/3
$res and $res = sysreadfull($s, $b, $n)
!ll&&!rl&&r
11501066
MC/DC50%1/2
$res and $res = sysreadfull($s, $b, $n)
$res $res = sysreadfull($s, $b, $n)
Truth table66%2/3
A and B
A= $resB= $res = sysreadfull($s, $b, $n)
ABresult
0X0
100
111
92 1066 ▶ die "bad netstring: $b" if ($res != $n);
Branch: 1 branch
BranchTrueFalseTotal
if $res != $n 0 1066 1066
93 1066 ▶ if ($res = sysreadfull($s,$c,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = sysreadfull($s, $c, 1)) 1066 0 1066
94 1066 ▶ die "bad netstring: $c" unless ($c eq ',');
Branch: 1 branch
BranchTrueFalseTotal
unless $c eq "," 0 1066 1066
95 1066 return $b;
96 }
97 }
98 115 ▶ die "EINTR" if $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if $!{"EINTR"} 0 115 115
99 115 ▶ die "netstring read error: $!" unless defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 115 115
100 115 return ''; # EOF
101 }
102  
103 22 1;
104  
105 __END__
« Tiny/Async.pm ↑ Summary Tiny/SessionCache.pm »