Stream/Codec.pm

filter: Statementstmt 57.1% 16 / 28 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 44.2% 23 / 52 CC 7 SCAR 30.3
CC
7
Cov
39%
validate
42.0
make_codec
6.0
BEGIN
1.0
CRAP 20.7
« Stream.pm ↑ Summary Stream/Codec/U32BE.pm »
1 package Linux::Event::Stream::Codec;
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '0.002';
7  
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Linux::Event::Stream::Codec - Framing/encoding contract for Linux::Event::Stream
14  
15 =head1 SYNOPSIS
16  
17 use v5.36;
18 use Linux::Event;
19 use Linux::Event::Stream;
20  
21 # A codec is a (blessed) hashref with two coderefs:
22 my $codec = Linux::Event::Stream::Codec::make_codec(
23 name => 'MyApp::Codec::Nul',
24  
25 decode => sub ($self, $inref, $outref) {
26 while (1) {
27 my $i = index($$inref, "\0");
28 last if $i < 0;
29  
30 my $msg = substr($$inref, 0, $i, '');
31 substr($$inref, 0, 1, ''); # drop the NUL
32 push @$outref, $msg;
33 }
34 return 1;
35 },
36  
37 encode => sub ($self, $msg) {
38 return $msg . "\0";
39 },
40 );
41  
42 my $s = Linux::Event::Stream->new(
43 loop => $loop,
44 fh => $fh,
45 codec => $codec,
46 on_message => sub ($stream, $msg, $data) {
47 ...
48 },
49 );
50  
51 =head1 DESCRIPTION
52  
53 This module documents the codec interface used by L<Linux::Event::Stream> when
54 operating in framed/message mode (C<codec> + C<on_message>). A codec answers
55 exactly one question:
56  
57 "Where does one message end and the next begin?"
58  
59 In other words: this is framing + encoding/decoding, not an application
60 protocol.
61  
62 Codecs are intentionally lightweight: they are (blessed) hashrefs containing
63 two coderefs. L<Linux::Event::Stream> validates these once at construction time
64 and then calls the coderefs directly in the hot path (no per-message method
65 dispatch required).
66  
67 =head1 CODEC CONTRACT
68  
69 =head2 Structure
70  
71 A codec is a hashref (or blessed hashref) with the following keys:
72  
73 decode => CODE
74 encode => CODE
75  
76 =head2 decode
77  
78 my ($ok, $err) = $codec->{decode}->($codec, \$inbuf, \@out);
79  
80 The C<decode> callback:
81  
82 =over 4
83  
84 =item * receives the codec object, a reference to the input buffer string, and
85 an arrayref to push decoded messages into
86  
87 =item * consumes complete messages from C<$$inbuf> (modify it in-place)
88  
89 =item * pushes 0..N decoded messages onto C<@out>
90  
91 =item * returns C<(1)> on success
92  
93 =item * returns C<(0, $error_string)> on failure
94  
95 =back
96  
97 If a message is incomplete, leave the remaining bytes in C<$$inbuf> and return
98 C<(1)>. L<Linux::Event::Stream> will append more bytes and call C<decode> again
99 later.
100  
101 =head2 encode
102  
103 my $bytes = $codec->{encode}->($codec, $msg);
104  
105 The C<encode> callback returns the bytes to be written to the stream for the
106 given message.
107  
108 =head1 HELPERS
109  
110 This module provides small helpers that are useful when writing your own codec.
111 They are optional; L<Linux::Event::Stream> does not require you to use them.
112  
113 =head2 validate
114  
115 Linux::Event::Stream::Codec::validate($codec);
116  
117 Croaks unless C<$codec> is a hashref (or blessed hashref) containing CODE refs
118 at C<decode> and C<encode>.
119  
120 =head2 make_codec
121  
122 my $codec = Linux::Event::Stream::Codec::make_codec(
123 name => 'My::Codec',
124 decode => sub (...) { ...; return 1 },
125 encode => sub (...) { ... },
126 );
127  
128 Creates and returns a blessed codec hashref. The C<name> is optional; if
129 provided, it is used as the blessing package.
130  
131 =head1 SECURITY AND LIMITS
132  
133 When framing untrusted input (e.g. TCP servers), always enforce reasonable
134 limits.
135  
136 =over 4
137  
138 =item * Cap buffered undecoded bytes (Stream's C<max_inbuf>)
139  
140 =item * Cap per-frame size (codec option like C<max_frame> / C<max_line>)
141  
142 =back
143  
144 Never allow a peer to grow memory without bound by sending an unterminated
145 frame.
146  
147 =cut
148  
149 ▶sub validate ($codec) {
Subroutine validate: not called CC=6 SCAR=37.4
Pod documented
150 0 ▶ croak 'codec is required' if !defined $codec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codec 0 0 0
151  
152 0 my $rt = reftype($codec);
153 0 ▶ croak 'codec must be a hashref (or blessed hashref)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rt or $rt ne "HASH" 0 0 0
Condition0%0/3
not defined $rt or $rt ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $rt or $rt ne "HASH"
not defined $rt $rt ne "HASH"
Truth table0%0/3
not(A) or B
A= not defined $rtB= $rt ne "HASH"
ABresult
1X1
011
000
154 if !defined($rt) || $rt ne 'HASH';
155  
156 0 my $dec = $codec->{decode};
157 0 my $enc = $codec->{encode};
158  
159 0 ▶ croak 'codec->{decode} must be a CODE ref' if ref($dec) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $dec ne "CODE" 0 0 0
160 0 ▶ croak 'codec->{encode} must be a CODE ref' if ref($enc) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $enc ne "CODE" 0 0 0
161  
162 0 return 1;
163 }
164  
165 ▶sub make_codec (%args) {
Subroutine make_codec: not called CC=2 SCAR=17.9
Pod documented
166 0 my $name = delete $args{name};
167 0 my $codec = { %args };
168  
169 0 validate($codec);
170  
171 # Blessing is optional; bless only when the caller provides a name.
172 0 ▶ return $name ? bless($codec, $name) : $codec;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
173 }
174  
175 1 1;
« Stream.pm ↑ Summary Stream/Codec/U32BE.pm »