lib/Remote/Perl/Protocol.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 9 totaltotal 89.7% 79 / 88 CC 9 SCAR 6.3
CC
9
Cov
100%
_drain
4.0
encode_message
2.0
encode_return
2.0
CRAP 1.9
« blib/lib/Remote/Perl/FlowControl.pm ↑ Summary lib/Remote/Perl/Bootstrap.pm »
1 134 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Remote::Perl::Protocol;
3 134 our $VERSION = '0.005';
4  
5 134 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 134 our @EXPORT_OK = qw(
8 HEADER_LEN PROTOCOL_VERSION
9 MSG_HELLO MSG_READY MSG_RUN MSG_DATA MSG_EOF
10 MSG_CREDIT MSG_MOD_REQ MSG_MOD_MISSING MSG_RETURN
11 MSG_SIGNAL MSG_SIGNAL_ACK
12 MSG_ERROR MSG_BYE
13 STREAM_CONTROL STREAM_STDIN STREAM_STDOUT STREAM_STDERR
14 TMPFILE_MASK TMPFILE_NONE TMPFILE_AUTO TMPFILE_LINUX TMPFILE_PERL TMPFILE_NAMED
15 FLAGS_WARNINGS
16 encode_message
17 encode_hello decode_hello
18 encode_credit decode_credit
19 encode_return decode_return
20 encode_run decode_run
21 );
22  
23 134 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 134 PROTOCOL_VERSION => 2,
25 HEADER_LEN => 6, # type(1) + stream(1) + length(4)
26  
27 # Flags byte layout: bits 0-2 = tmpfile strategy (0-4), bit 3+ = flags
28 TMPFILE_MASK => 0x07, # bits 0-2
29  
30 # Tmpfile strategies (carried in bits 0-2 of the flags byte)
31 TMPFILE_NONE => 0, # default: eval $source directly
32 TMPFILE_AUTO => 1, # try linux, fall back to perl
33 TMPFILE_LINUX => 2, # O_TMPFILE (anonymous inode, no directory entry)
34 TMPFILE_PERL => 3, # open('+>', undef) -- anon fd, unlinked on creation
35 TMPFILE_NAMED => 4, # File::Temp -- named file, kept until executor exits
36  
37 # Flag bits (bit 3+)
38 FLAGS_WARNINGS => 0x08, # bit 3
39  
40 # Message types
41 MSG_HELLO => 0x00,
42 MSG_READY => 0x01,
43 MSG_RUN => 0x10,
44 MSG_DATA => 0x20,
45 MSG_EOF => 0x21,
46 MSG_CREDIT => 0x30,
47 MSG_MOD_REQ => 0x40,
48 MSG_MOD_MISSING => 0x41,
49 MSG_RETURN => 0x50,
50 MSG_SIGNAL => 0x60,
51 MSG_SIGNAL_ACK => 0x61,
52 MSG_ERROR => 0xE0,
53 MSG_BYE => 0xF0,
54  
55 # Predefined stream IDs
56 STREAM_CONTROL => 0,
57 STREAM_STDIN => 1,
58 STREAM_STDOUT => 2,
59 STREAM_STDERR => 3,
60 134 };
61  
62 # encode_message($type, $stream, $body) -> bytes
63 7612 ▶sub encode_message($type, $stream, $body = '') {
Subroutine encode_message: called CC=2 SCAR=6.9
Pod undocumented
64 7612 return pack('CCN', $type, $stream, length($body)) . $body;
65 }
66  
67 # HELLO body: version(u8) + window_size(u32 BE)
68 1428 ▶sub encode_hello($version, $window_size) {
Subroutine encode_hello: called CC=1 SCAR=0.0
Pod undocumented
69 1428 return pack('CN', $version, $window_size);
70 }
71 1 ▶sub decode_hello($body) {
Subroutine decode_hello: called CC=1 SCAR=0.0
Pod undocumented
72 1 return unpack('CN', $body);
73 }
74  
75 # CREDIT body: grant(u32 BE)
76 1520 ▶sub encode_credit($n) {
Subroutine encode_credit: called CC=1 SCAR=0.0
Pod undocumented
77 1520 return pack('N', $n);
78 }
79 1 ▶sub decode_credit($body) {
Subroutine decode_credit: called CC=1 SCAR=0.0
Pod undocumented
80 1 return unpack('N', $body);
81 }
82  
83 # RETURN body: exit_code(u8) + optional message bytes
84 2 ▶sub encode_return($exit_code, $message = '') {
Subroutine encode_return: called CC=2 SCAR=6.9
Pod undocumented
85 2 return pack('C', $exit_code) . $message;
86 }
87 1460 ▶sub decode_return($body) {
Subroutine decode_return: called CC=2 SCAR=6.9
Pod undocumented
88 1460 my $exit_code = unpack('C', $body);
89 1460 ▶ my $message = length($body) > 1 ? substr($body, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
length $body > 1 ? : 543 917 1460
90 1460 return ($exit_code, $message);
91 }
92  
93 # RUN body: flags(u8) + argc(u32) + [len(u32) + bytes]... + source(rest)
94 1461 ▶sub encode_run($flags, $source, @argv) {
Subroutine encode_run: called CC=2 SCAR=6.9
Pod undocumented
95 1461 my $buf = pack('CN', $flags, scalar @argv);
96 1461 for my $arg (@argv) {
97 14 $buf .= pack('N', length($arg)) . $arg;
98 }
99 1461 return $buf . $source;
100 }
101 2 ▶sub decode_run($body) {
Subroutine decode_run: called CC=2 SCAR=6.9
Pod undocumented
102 2 my $off = 0;
103 2 my $flags = unpack('C', substr($body, $off, 1)); $off += 1;
104 2 my $argc = unpack('N', substr($body, $off, 4)); $off += 4;
105 2 my @argv;
106 2 for (1 .. $argc) {
107 2 my $len = unpack('N', substr($body, $off, 4)); $off += 4;
108 2 push @argv, substr($body, $off, $len); $off += $len;
109 }
110 2 my $source = substr($body, $off);
111 2 return ($flags, $source, @argv);
112 }
113  
114 # ------------------------------------------------------------------------------
115 # Remote::Perl::Protocol::Parser -- stateful incremental decoder
116 # ------------------------------------------------------------------------------
117 package Remote::Perl::Protocol::Parser;
118  
119 134 ▶use constant HEADER_LEN => Remote::Perl::Protocol::HEADER_LEN;
Subroutine BEGIN: called CC=1 SCAR=0.0
120  
121 1440 ▶sub new($class) {
Subroutine new: called CC=1 SCAR=0.0
122 1440 return bless { buf => '' }, $class;
123 }
124  
125 # Feed raw bytes; returns list of decoded message hashrefs:
126 # { type => $t, stream => $s, body => $b }
127 8478 ▶sub feed($self, $data) {
Subroutine feed: called CC=1 SCAR=0.0
128 8478 $self->{buf} .= $data;
129 8478 return $self->_drain;
130 }
131  
132 8478 ▶sub _drain($self) {
Subroutine _drain: called CC=4 SCAR=13.9
133 8478 my @msgs;
134 8478 ▶ while (length($self->{buf}) >= HEADER_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buf"} >= 6) 10499 8477 18976
135 10499 my ($type, $stream, $len) = unpack('CCN', $self->{buf});
136 10499 ▶ last if length($self->{buf}) < HEADER_LEN + $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"buf"} < 6 + $len 1 10498 10499
137 10498 substr($self->{buf}, 0, HEADER_LEN, '');
138 10498 ▶ my $body = $len ? substr($self->{buf}, 0, $len, '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$len ? : 4745 5753 10498
139 10498 push @msgs, { type => $type, stream => $stream, body => $body };
140 }
141 8478 return @msgs;
142 }
143  
144 # How many bytes are buffered but not yet forming a complete message
145 2 ▶sub pending_bytes($self) { length($self->{buf}) }
Subroutine pending_bytes: called CC=1 SCAR=0.0
146  
147 134 1;
148  
149 __END__
« blib/lib/Remote/Perl/FlowControl.pm ↑ Summary lib/Remote/Perl/Bootstrap.pm »