|  line  | 
 stmt  | 
 bran  | 
 cond  | 
 sub  | 
 pod  | 
 time  | 
 code  | 
| 
1
 | 
  
 
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 # Netstring messages for RPC::Switch::Client::Tiny  | 
| 
2
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 #  | 
| 
3
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 package RPC::Switch::Client::Tiny::Netstring;  | 
| 
4
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
5
 | 
21
 | 
 
 | 
 
 | 
  
21
  
 | 
 
 | 
76850
 | 
 use strict;  | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
53
 | 
    | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
591
 | 
    | 
| 
6
 | 
21
 | 
 
 | 
 
 | 
  
21
  
 | 
 
 | 
87
 | 
 use warnings;  | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
118
 | 
    | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
507
 | 
    | 
| 
7
 | 
21
 | 
 
 | 
 
 | 
  
21
  
 | 
 
 | 
105
 | 
 use Exporter 'import';  | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
61
 | 
    | 
| 
 
 | 
21
 | 
 
 | 
 
 | 
 
 | 
 
 | 
12412
 | 
    | 
| 
8
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 our @EXPORT = qw(netstring_read netstring_write);  | 
| 
9
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
10
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 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
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub sysreadfull {  | 
| 
16
 | 
4816
 | 
 
 | 
 
 | 
  
4816
  
 | 
  
0
  
 | 
10116
 | 
 	my ($file, $len) = ($_[0], $_[2]); # need ref on $buf here  | 
| 
17
 | 
4816
 | 
 
 | 
 
 | 
 
 | 
 
 | 
8143
 | 
 	my $n = 0;  | 
| 
18
 | 
4816
 | 
 
 | 
 
 | 
 
 | 
 
 | 
10875
 | 
 	while ($len - $n) {  | 
| 
19
 | 
4816
 | 
 
 | 
 
 | 
 
 | 
 
 | 
42828
 | 
 		my $i = sysread($file, $_[1], $len - $n, $n);  | 
| 
20
 | 
4816
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
12925
 | 
 		if (defined $i) {  | 
| 
 
 | 
 
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
21
 | 
4816
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
8609
 | 
 			if ($i == 0) {  | 
| 
22
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
 				return $n;  | 
| 
23
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 			} else {  | 
| 
24
 | 
4816
 | 
 
 | 
 
 | 
 
 | 
 
 | 
12482
 | 
 				$n += $i;  | 
| 
25
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 			}  | 
| 
26
 | 
1
 | 
 
 | 
 
 | 
  
1
  
 | 
 
 | 
432
 | 
 		} elsif ($!{EINTR}) {  | 
| 
 
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1326
 | 
    | 
| 
 
 | 
1
 | 
 
 | 
 
 | 
 
 | 
 
 | 
9
 | 
    | 
| 
27
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
 			redo;  | 
| 
28
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		} else {  | 
| 
29
 | 
  
0
  
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
0
 | 
 			return $n ? $n : undef;  | 
| 
30
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		}  | 
| 
31
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	}  | 
| 
32
 | 
4816
 | 
 
 | 
 
 | 
 
 | 
 
 | 
14902
 | 
 	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
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub syswritefull {  | 
| 
39
 | 
  
0
  
 | 
 
 | 
 
 | 
  
0
  
 | 
  
0
  
 | 
0
 | 
         my ($file, $buf) = @_;  | 
| 
40
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         my $len = length($buf);  | 
| 
41
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         my $n = 0;  | 
| 
42
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         while ($len - $n) {  | 
| 
43
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
                 my $i = syswrite($file, $buf, $len - $n, $n);  | 
| 
44
 | 
  
0
  
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
0
 | 
                 if (defined($i)) {  | 
| 
 
 | 
 
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
45
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
                         $n += $i;  | 
| 
46
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
                 } elsif ($!{EINTR}) {  | 
| 
47
 | 
  
0
  
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
                         redo;  | 
| 
48
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
                 } else {  | 
| 
49
 | 
  
0
  
 | 
  
  0
  
 | 
 
 | 
 
 | 
 
 | 
0
 | 
                         return $n ? $n : undef;  | 
| 
50
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
                 }  | 
| 
51
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
         }  | 
| 
52
 | 
0
 | 
 
 | 
 
 | 
 
 | 
 
 | 
0
 | 
         return $n;  | 
| 
53
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
54
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
55
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 # netstring proto: http://cr.yp.to/proto/netstrings.txt  | 
| 
56
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 #  | 
| 
57
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub netstring_write {  | 
| 
58
 | 
1100
 | 
 
 | 
 
 | 
  
1100
  
 | 
  
0
  
 | 
33395
 | 
 	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
 | 
1100
 | 
 
 | 
 
 | 
 
 | 
 
 | 
62423
 | 
 	my $res = print $s '' . length($str) . ':' . $str . ',';  | 
| 
66
 | 
1100
 | 
 
 | 
 
 | 
 
 | 
 
 | 
14616
 | 
 	$s->flush();  | 
| 
67
 | 
1100
 | 
 
 | 
 
 | 
 
 | 
 
 | 
17321
 | 
 	return $res;  | 
| 
68
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
69
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
70
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 # returns received netstring, or empty string on EOF  | 
| 
71
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 # dies on error -> use eval {..}  | 
| 
72
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 #  | 
| 
73
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 sub netstring_read {  | 
| 
74
 | 
1175
 | 
 
 | 
 
 | 
  
1175
  
 | 
  
0
  
 | 
28399
 | 
 	my ($s) = @_;  | 
| 
75
 | 
1175
 | 
 
 | 
 
 | 
 
 | 
 
 | 
4265
 | 
 	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
 | 
1175
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1020606
 | 
 	my $res = sysread($s,$c,1);  | 
| 
81
 | 
1175
 | 
 
 | 
 
 | 
 
 | 
 
 | 
6229
 | 
 	while ($res) {  | 
| 
82
 | 
3756
 | 
  
100
  
 | 
 
 | 
 
 | 
 
 | 
8200
 | 
 		if ($c ne ':') {  | 
| 
83
 | 
2696
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
15414
 | 
 			die "bad netstring: $c" unless ($c =~ /\d+/);  | 
| 
84
 | 
2696
 | 
 
 | 
 
 | 
 
 | 
 
 | 
5600
 | 
 			$n .= $c;  | 
| 
85
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		} else {  | 
| 
86
 | 
1060
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
2773
 | 
 			die "bad netstring: $c" if ($n eq '');  | 
| 
87
 | 
1060
 | 
 
 | 
 
 | 
 
 | 
 
 | 
2178
 | 
 			last;  | 
| 
88
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		}  | 
| 
89
 | 
2696
 | 
 
 | 
 
 | 
 
 | 
 
 | 
7571
 | 
 		$res = sysreadfull($s,$c,1);  | 
| 
90
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	}  | 
| 
91
 | 
1175
 | 
  
100
  
 | 
  
 66
  
 | 
 
 | 
 
 | 
5188
 | 
 	if ($res && ($res = sysreadfull($s,$b,$n))) {  | 
| 
92
 | 
1060
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
3332
 | 
 		die "bad netstring: $b" if ($res != $n);  | 
| 
93
 | 
1060
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
2167
 | 
 		if ($res = sysreadfull($s,$c,1)) {  | 
| 
94
 | 
1060
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
3058
 | 
 			die "bad netstring: $c" unless ($c eq ',');  | 
| 
95
 | 
1060
 | 
 
 | 
 
 | 
 
 | 
 
 | 
6535
 | 
 			return $b;  | 
| 
96
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 		}  | 
| 
97
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 	}  | 
| 
98
 | 
115
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
3419
 | 
 	die "EINTR" if $!{EINTR};  | 
| 
99
 | 
115
 | 
  
 50
  
 | 
 
 | 
 
 | 
 
 | 
2889
 | 
 	die "netstring read error: $!" unless defined $res;  | 
| 
100
 | 
115
 | 
 
 | 
 
 | 
 
 | 
 
 | 
1027
 | 
 	return ''; # EOF  | 
| 
101
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 }  | 
| 
102
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
103
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 1;  | 
| 
104
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
    | 
| 
105
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 
 | 
 __END__  |