| 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
| |||||||||||||||||||||||||||||
| 19 | 4841 | my $i = sysread($file, $_[1], $len - $n, $n); | |||||||||||||||||||||||||||
| 20 | 4841 | ▶ | if (defined $i) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 21 | 4841 | ▶ | if ($i == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 0 | my $i = syswrite($file, $buf, $len - $n, $n); | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | if (defined($i)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 0 | $n += $i; | |||||||||||||||||||||||||||
| 46 | } elsif ($!{EINTR}) { | ||||||||||||||||||||||||||||
| 47 | 0 | redo; | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return $n ? $n : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 3775 | ▶ | if ($c ne ':') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2709 | ▶ | die "bad netstring: $c" unless ($c =~ /\d+/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2709 | $n .= $c; | |||||||||||||||||||||||||||
| 85 | } else { | ||||||||||||||||||||||||||||
| 86 | 1066 | ▶ | die "bad netstring: $c" if ($n eq ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 1066 | last; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 2709 | $res = sysreadfull($s,$c,1); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1181 | ▶ | if ($res && ($res = sysreadfull($s,$b,$n))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$res and $res = sysreadfull($s, $b, $n)
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)
| |||||||||||||||||||||||||||||
| 92 | 1066 | ▶ | die "bad netstring: $b" if ($res != $n); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 1066 | ▶ | if ($res = sysreadfull($s,$c,1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1066 | ▶ | die "bad netstring: $c" unless ($c eq ','); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 1066 | return $b; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 115 | ▶ | die "EINTR" if $!{EINTR}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 115 | ▶ | die "netstring read error: $!" unless defined $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 115 | return ''; # EOF | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 22 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||