Stream.pm

filter: Statementstmt 80.0% 188 / 235 Branchbran 51.4% 69 / 134 Conditioncond 45.3% 49 / 108 MC/DCmcdc 11.2% 7 / 62 Subroutinesub 84.3% 27 / 32 Podpod 100% 13 / 13 totaltotal 60.4% 353 / 584 CC 108 SCAR 35.7
CC
108
Cov
64%
new
84.2
_on_read_ready
45.7
write
26.1
CRAP 35.5
« Stream/Codec/Netstring.pm ↑ Summary Stream/Codec.pm »
1 package Linux::Event::Stream;
2 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.002';
7  
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use Linux::Event::Stream::Codec::Line ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Linux::Event::Stream::Codec::Netstring ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Linux::Event::Stream::Codec::U32BE ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Watcher callbacks: ($loop, $fh, $watcher)
17 # The Stream instance is stored in $watcher->data.
18 3 ▶sub _watch_read_cb ($loop, $fh, $watcher) { my $self = $watcher->data or return; _on_read_ready($self, $watcher, $fh) }
Subroutine _watch_read_cb: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = $watcher->data or return 0 3 3
19 90 ▶sub _watch_write_cb ($loop, $fh, $watcher) { my $self = $watcher->data or return; _on_write_ready($self, $watcher, $fh) }
Subroutine _watch_write_cb: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
my $self = $watcher->data or return 0 90 90
20 0 ▶sub _watch_error_cb ($loop, $fh, $watcher) { my $self = $watcher->data or return; _on_error_ready($self, $watcher, $fh) }
Subroutine _watch_error_cb: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
my $self = $watcher->data or return 0 0 0
21  
22 ▶sub new ($class, %opt) {
Subroutine new: called CC=38 SCAR=44.3
Pod documented
23 6 ▶ my $loop = delete $opt{loop} // croak 'new(): missing loop';
Condition33%1/3
delete $opt{"loop"} // croak("new(): missing loop")
l!l&&r!l&&!r
600
MC/DC0%0/2
delete $opt{"loop"} // croak("new(): missing loop")
delete $opt{"loop"} croak("new(): missing loop")
Truth table33%1/3
A // B
A= delete $opt{"loop"}B= croak("new(): missing loop")
ABresult
1X1
011
000
24 6 ▶ my $fh = delete $opt{fh} // croak 'new(): missing fh';
Condition33%1/3
delete $opt{"fh"} // croak("new(): missing fh")
l!l&&r!l&&!r
600
MC/DC0%0/2
delete $opt{"fh"} // croak("new(): missing fh")
delete $opt{"fh"} croak("new(): missing fh")
Truth table33%1/3
A // B
A= delete $opt{"fh"}B= croak("new(): missing fh")
ABresult
1X1
011
000
25  
26 6 my $on_read = delete $opt{on_read};
27 6 my $on_message = delete $opt{on_message};
28 6 my $on_error = delete $opt{on_error};
29 6 my $on_close = delete $opt{on_close};
30  
31 6 ▶ croak 'on_read must be a coderef' if defined $on_read && ref($on_read) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_read and ref $on_read ne "CODE" 0 6 6
Condition33%1/3
defined $on_read and ref $on_read ne "CODE"
!ll&&!rl&&r
600
MC/DC0%0/2
defined $on_read and ref $on_read ne "CODE"
defined $on_read ref $on_read ne "CODE"
Truth table33%1/3
A and B
A= defined $on_readB= ref $on_read ne "CODE"
ABresult
0X0
100
111
32 6 ▶ croak 'on_message must be a coderef' if defined $on_message && ref($on_message) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_message and ref $on_message ne "CODE" 0 6 6
Condition66%2/3
defined $on_message and ref $on_message ne "CODE"
!ll&&!rl&&r
330
MC/DC0%0/2
defined $on_message and ref $on_message ne "CODE"
defined $on_message ref $on_message ne "CODE"
Truth table66%2/3
A and B
A= defined $on_messageB= ref $on_message ne "CODE"
ABresult
0X0
100
111
33 6 ▶ croak 'on_error must be a coderef' if defined $on_error && ref($on_error) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_error and ref $on_error ne "CODE" 0 6 6
Condition66%2/3
defined $on_error and ref $on_error ne "CODE"
!ll&&!rl&&r
420
MC/DC0%0/2
defined $on_error and ref $on_error ne "CODE"
defined $on_error ref $on_error ne "CODE"
Truth table66%2/3
A and B
A= defined $on_errorB= ref $on_error ne "CODE"
ABresult
0X0
100
111
34 6 ▶ croak 'on_close must be a coderef' if defined $on_close && ref($on_close) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_close and ref $on_close ne "CODE" 0 6 6
Condition66%2/3
defined $on_close and ref $on_close ne "CODE"
!ll&&!rl&&r
240
MC/DC0%0/2
defined $on_close and ref $on_close ne "CODE"
defined $on_close ref $on_close ne "CODE"
Truth table66%2/3
A and B
A= defined $on_closeB= ref $on_close ne "CODE"
ABresult
0X0
100
111
35  
36 6 my $codec = delete $opt{codec};
37 6 my $decoder = delete $opt{decoder};
38 6 my $encoder = delete $opt{encoder};
39  
40 6 ▶ croak 'use either codec or decoder/encoder, not both'
Branch: 1 branch
BranchTrueFalseTotal
if defined $codec and defined $decoder || defined $encoder 0 6 6
Condition50%3/6
defined $codec and defined $decoder || defined $encoder
!ll&&!rl&&r
330
defined $decoder or defined $encoder
l!l&&r!l&&!r
003
MC/DC0%0/3
defined $codec and defined $decoder || defined $encoder
defined $codec defined $decoder defined $encoder
Truth table50%2/4
A and B or C
A= defined $codecB= defined $decoderC= defined $encoder
ABCresult
0XX0
1000
11X1
1011
41 if defined($codec) && (defined($decoder) || defined($encoder));
42  
43 # Framed/message mode is enabled by providing on_message.
44 6 ▶ my $framed = defined($on_message) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $on_message ? : 3 3 6
45 6 ▶ croak 'on_read is not used when on_message is provided; use on_message + codec'
Branch: 1 branch
BranchTrueFalseTotal
if $framed and defined $on_read 0 6 6
Condition66%2/3
$framed and defined $on_read
!ll&&!rl&&r
330
MC/DC0%0/2
$framed and defined $on_read
$framed defined $on_read
Truth table66%2/3
A and B
A= $framedB= defined $on_read
ABresult
0X0
100
111
46 if $framed && defined $on_read;
47  
48 6 my $data = delete $opt{data};
49  
50 6 ▶ my $high = delete $opt{high_watermark} // 1_048_576;
Condition100%2/2
delete $opt{"high_watermark"} // 1048576
l!l
15
MC/DC100%1/1
delete $opt{"high_watermark"} // 1048576
delete $opt{"high_watermark"}
Truth table100%2/2
A
A= delete $opt{"high_watermark"}
Aresult
11
00
51 6 ▶ my $low = delete $opt{low_watermark} // 262_144;
Condition100%2/2
delete $opt{"low_watermark"} // 262144
l!l
15
MC/DC100%1/1
delete $opt{"low_watermark"} // 262144
delete $opt{"low_watermark"}
Truth table100%2/2
A
A= delete $opt{"low_watermark"}
Aresult
11
00
52 6 ▶ $low = $high if $low > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $low > $high 0 6 6
53  
54 6 ▶ my $read_size = delete $opt{read_size} // 8192;
Condition50%1/2
delete $opt{"read_size"} // 8192
l!l
06
MC/DC0%0/1
delete $opt{"read_size"} // 8192
delete $opt{"read_size"}
Truth table50%1/2
A
A= delete $opt{"read_size"}
Aresult
11
00
55 6 ▶ my $max_read_per_tick = delete $opt{max_read_per_tick} // 0;
Condition50%1/2
delete $opt{"max_read_per_tick"} // 0
l!l
06
MC/DC0%0/1
delete $opt{"max_read_per_tick"} // 0
delete $opt{"max_read_per_tick"}
Truth table50%1/2
A
A= delete $opt{"max_read_per_tick"}
Aresult
11
00
56  
57 6 my $max_inbuf = delete $opt{max_inbuf};
58 6 ▶ $max_inbuf //= 1_048_576 if $framed;
Branch: 1 branch
BranchTrueFalseTotal
if $framed 3 3 6
Condition100%2/2
$max_inbuf //= 1048576
l!l
12
MC/DC100%1/1
$max_inbuf //= 1048576
$max_inbuf
Truth table100%2/2
A
A= $max_inbuf
Aresult
11
00
59  
60 6 ▶ my $close_fh = delete $opt{close_fh} // 0;
Condition50%1/2
delete $opt{"close_fh"} // 0
l!l
06
MC/DC0%0/1
delete $opt{"close_fh"} // 0
delete $opt{"close_fh"}
Truth table50%1/2
A
A= delete $opt{"close_fh"}
Aresult
11
00
61  
62 6 ▶ if ($framed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($framed) 3 3 6
63 3 ▶ $codec = $decoder // $encoder // $codec;
Condition33%2/6
($decoder // $encoder) // $codec
l!l&&r!l&&!r
030
$decoder // $encoder
l!l&&r!l&&!r
003
MC/DC0%0/3
($decoder // $encoder) // $codec
$decoder $encoder $codec
Truth table25%1/4
A // B // C
A= $decoderB= $encoderC= $codec
ABCresult
1XX1
01X1
0011
0000
64 3 ▶ croak 'new(): missing codec (required with on_message)' if !defined $codec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codec 0 3 3
65  
66 3 ▶ if (!ref($codec)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $codec) 3 0 3
67 3 ▶ $codec =
Branch: 3 branches
BranchTrueFalseTotal
$codec eq 'line' ? : 3 0 3
$codec eq 'netstring' ? : 0 0 0
$codec eq 'u32be' ? : 0 0 0
68 $codec eq 'line' ? Linux::Event::Stream::Codec::Line->new :
69 $codec eq 'netstring' ? Linux::Event::Stream::Codec::Netstring->new :
70 $codec eq 'u32be' ? Linux::Event::Stream::Codec::U32BE->new :
71 croak "unknown codec alias: $codec";
72 }
73  
74 3 ▶ croak 'codec must be a hashref (or blessed hashref)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined builtin::reftype($codec) or builtin::reftype($codec) ne "HASH" 0 3 3
Condition33%1/3
not defined builtin::reftype($codec) or builtin::reftype($codec) ne "HASH"
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined builtin::reftype($codec) or builtin::reftype($codec) ne "HASH"
not defined builtin::reftype($codec) builtin::reftype($codec) ne "HASH"
Truth table33%1/3
not(A) or B
A= not defined builtin::reftype($codec)B= builtin::reftype($codec) ne "HASH"
ABresult
1X1
011
000
75 if !defined(reftype($codec)) || reftype($codec) ne 'HASH';
76 3 ▶ croak 'codec->{decode} must be a coderef' if ref($codec->{decode}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $codec->{"decode"} ne "CODE" 0 3 3
77 3 ▶ croak 'codec->{encode} must be a coderef' if ref($codec->{encode}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $codec->{"encode"} ne "CODE" 0 3 3
78 }
79  
80 6 ▶ croak 'unknown options: ' . join(', ', sort keys %opt) if %opt;
Branch: 1 branch
BranchTrueFalseTotal
if %opt 0 6 6
81  
82 6 _set_nonblocking($fh);
83  
84 6 my $self = bless {
85 loop => $loop,
86 fh => $fh,
87  
88 # raw bytes mode
89 on_read => $on_read,
90  
91 # framed/message mode
92 on_message => $on_message,
93 codec => $codec,
94 rbuf => '',
95 max_inbuf => $max_inbuf,
96  
97 on_error => $on_error,
98 on_close => $on_close,
99 data => $data,
100  
101 last_error => undef,
102  
103 # write buffering
104 wbuf => '',
105 woff => 0,
106  
107 high_watermark => $high,
108 low_watermark => $low,
109  
110 read_size => $read_size,
111 max_read_per_tick => $max_read_per_tick,
112  
113 # state
114 closed => 0,
115 closing => 0,
116 read_paused => 0,
117 write_blocked => 0,
118 close_fh => $close_fh,
119 close_cb_fired => 0,
120  
121 watcher => undef,
122 }, $class;
123  
124 6 my $w = $loop->watch($fh,
125 read => \&_watch_read_cb,
126 write => \&_watch_write_cb,
127 error => \&_watch_error_cb,
128 data => $self,
129 );
130  
131 6 $self->{watcher} = $w;
132  
133 # Do not arm write notifications until there is buffered data.
134 6 $w->disable_write;
135  
136 6 return $self;
137 }
138  
139 0 ▶sub fh ($self) { $self->{fh} }
Subroutine fh: not called CC=1 SCAR=6.9
Pod documented
140  
141 1 ▶sub is_closed ($self) { !!$self->{closed} }
Subroutine is_closed: called CC=1 SCAR=0.0
Pod documented
142 0 ▶sub is_closing ($self) { !!$self->{closing} }
Subroutine is_closing: not called CC=1 SCAR=6.9
Pod documented
143  
144 ▶sub buffered_bytes ($self) {
Subroutine buffered_bytes: called CC=4 SCAR=15.2
Pod documented
145 5 ▶ my $len = length($self->{wbuf} // '');
Condition50%1/2
$self->{"wbuf"} // ""
l!l
50
MC/DC0%0/1
$self->{"wbuf"} // ""
$self->{"wbuf"}
Truth table50%1/2
A
A= $self->{"wbuf"}
Aresult
11
00
146 5 ▶ my $off = $self->{woff} // 0;
Condition50%1/2
$self->{"woff"} // 0
l!l
50
MC/DC0%0/1
$self->{"woff"} // 0
$self->{"woff"}
Truth table50%1/2
A
A= $self->{"woff"}
Aresult
11
00
147 5 ▶ return $len > $off ? ($len - $off) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$len > $off ? : 0 5 5
148 }
149  
150 31 ▶sub is_write_blocked ($self) { !!$self->{write_blocked} }
Subroutine is_write_blocked: called CC=1 SCAR=0.0
Pod documented
151  
152 1 ▶sub last_error ($self) { $self->{last_error} }
Subroutine last_error: called CC=1 SCAR=0.0
Pod documented
153  
154 ▶sub write ($self, $bytes) {
Subroutine write: called CC=13 SCAR=32.6
Pod documented
155 4 ▶ return 0 if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 4 4
156 4 ▶ return 1 if !defined($bytes) || $bytes eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or $bytes eq "" 0 4 4
Condition33%1/3
not defined $bytes or $bytes eq ""
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $bytes or $bytes eq ""
not defined $bytes $bytes eq ""
Truth table33%1/3
not(A) or B
A= not defined $bytesB= $bytes eq ""
ABresult
1X1
011
000
157  
158 # Fast path: attempt to write immediately when there is no pending buffer.
159 # This reduces latency and avoids reliance on EPOLLOUT notifications to start a drain.
160 4 ▶ if ($self->buffered_bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->buffered_bytes == 0) 4 0 4
161 4 my $n = syswrite($self->{fh}, $bytes);
162 4 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) { } 3 1 4
163 3 ▶ return 1 if $n == length($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if $n == length $bytes 3 0 3
164 # partial: buffer remainder
165 0 substr($bytes, 0, $n, '');
166 } else {
167 1 my $errno = $! + 0;
168 # EAGAIN: buffer whole payload
169 1 ▶ if ($errno != 11) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno != 11) 0 1 1
170 0 $self->{last_error} = "io:$errno";
171 0 _fire_on_error($self, $errno);
172 0 _close_now($self);
173 0 return 0;
174 }
175 }
176 }
177  
178 # Buffer what remains (or all, if immediate write did not happen).
179 1 my $buf = $self->{wbuf};
180 1 my $off = $self->{woff};
181  
182 1 ▶ if ($off && ($off > 65_536 || $off > (length($buf) >> 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off and $off > 65536 || $off > length($buf) >> 1) 0 1 1
Condition16%1/6
$off and $off > 65536 || $off > length($buf) >> 1
!ll&&!rl&&r
100
$off > 65536 or $off > length($buf) >> 1
l!l&&r!l&&!r
000
MC/DC0%0/3
$off and $off > 65536 || $off > length($buf) >> 1
$off $off > 65536 $off > length($buf) >> 1
Truth table25%1/4
A and B or C
A= $offB= $off > 65536C= $off > length($buf) >> 1
ABCresult
0XX0
1000
11X1
1011
183 0 substr($buf, 0, $off, '');
184 0 $off = 0;
185 }
186  
187 1 $buf .= $bytes;
188  
189 1 $self->{wbuf} = $buf;
190 1 $self->{woff} = $off;
191  
192 1 my $pending = length($buf) - $off;
193 1 ▶ if (!$self->{write_blocked} && $pending > $self->{high_watermark}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"write_blocked"} and $pending > $self->{"high_watermark"}) 1 0 1
Condition33%1/3
not $self->{"write_blocked"} and $pending > $self->{"high_watermark"}
!ll&&!rl&&r
001
MC/DC0%0/2
not $self->{"write_blocked"} and $pending > $self->{"high_watermark"}
not $self->{"write_blocked"} $pending > $self->{"high_watermark"}
Truth table33%1/3
not(A) and B
A= not $self->{"write_blocked"}B= $pending > $self->{"high_watermark"}
ABresult
0X0
100
111
194 1 $self->{write_blocked} = 1;
195 }
196  
197 1 $self->{watcher}->enable_write;
198 1 return 1;
199 }
200  
201 ▶sub write_message ($self, $msg) {
Subroutine write_message: called CC=3 SCAR=12.0
Pod documented
202 1 ▶ return 0 if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 1 1
203 1 ▶ my $codec = $self->{codec} // croak 'write_message() requires a codec (use on_message mode)';
Condition33%1/3
$self->{"codec"} // croak("write_message() requires a codec (use on_message mode)")
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"codec"} // croak("write_message() requires a codec (use on_message mode)")
$self->{"codec"} croak("write_message() requires a codec (use on_message mode)")
Truth table33%1/3
A // B
A= $self->{"codec"}B= croak("write_message() requires a codec (use on_message mode)")
ABresult
1X1
011
000
204 1 my $bytes = $codec->{encode}->($codec, $msg);
205 1 return $self->write($bytes);
206 }
207  
208 ▶sub pause_read ($self) {
Subroutine pause_read: called CC=3 SCAR=12.0
Pod documented
209 1 ▶ return $self if $self->{closed} || $self->{read_paused};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"read_paused"} 0 1 1
Condition33%1/3
$self->{"closed"} or $self->{"read_paused"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"closed"} or $self->{"read_paused"}
$self->{"closed"} $self->{"read_paused"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"read_paused"}
ABresult
1X1
011
000
210 1 $self->{read_paused} = 1;
211 1 $self->{watcher}->disable_read;
212 1 return $self;
213 }
214  
215 ▶sub resume_read ($self) {
Subroutine resume_read: not called CC=4 SCAR=30.0
Pod documented
216 0 ▶ return $self if $self->{closed} || !$self->{read_paused} || $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or not $self->{"read_paused"} or $self->{"closing"} 0 0 0
Condition0%0/6
$self->{"closed"} or not $self->{"read_paused"} or $self->{"closing"}
l!l&&r!l&&!r
000
$self->{"closed"} or not $self->{"read_paused"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"closed"} or not $self->{"read_paused"} or $self->{"closing"}
$self->{"closed"} not $self->{"read_paused"} $self->{"closing"}
Truth table0%0/4
A or not(B) or C
A= $self->{"closed"}B= not $self->{"read_paused"}C= $self->{"closing"}
ABCresult
1XX1
01X1
0011
0000
217 0 $self->{read_paused} = 0;
218 0 $self->{watcher}->enable_read;
219 0 return $self;
220 }
221  
222 ▶sub close_after_drain ($self) {
Subroutine close_after_drain: called CC=3 SCAR=11.6
Pod documented
223 1 ▶ return $self if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 1 1
224 1 $self->{closing} = 1;
225  
226 1 $self->pause_read;
227  
228 1 ▶ if ($self->buffered_bytes == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->buffered_bytes == 0) { } 1 0 1
229 1 _close_now($self);
230 } else {
231 0 $self->{watcher}->enable_write;
232 }
233  
234 1 return $self;
235 }
236  
237 ▶sub close ($self) {
Subroutine close: called CC=2 SCAR=7.1
Pod documented
238 4 ▶ return $self if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 4 4
239 4 _close_now($self);
240 4 return $self;
241 }
242  
243 # ---- internals ----
244  
245 ▶sub _set_nonblocking ($fh) {
Subroutine _set_nonblocking: called CC=3 SCAR=13.3
246 6 my $flags = fcntl($fh, F_GETFL, 0);
247 6 ▶ return if !defined $flags; # best-effort
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags 0 6 6
248 6 ▶ return if ($flags & O_NONBLOCK);
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 2048 6 0 6
249 0 fcntl($fh, F_SETFL, $flags | O_NONBLOCK);
250 0 return;
251 }
252  
253 ▶sub _fire_on_error ($self, $errno) {
Subroutine _fire_on_error: called CC=4 SCAR=17.1
254 1 ▶ $self->{last_error} //= ($errno ? "io:$errno" : 'error');
Branch: 1 branch
BranchTrueFalseTotal
$errno ? : 0 0 0
Condition33%1/3
$self->{"last_error"} //= $errno ? "io:$errno" : "error"
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"last_error"} //= $errno ? "io:$errno" : "error"
$self->{"last_error"} $errno ? "io:$errno" : "error"
Truth table33%1/3
A //= B
A= $self->{"last_error"}B= $errno ? "io:$errno" : "error"
ABresult
1X1
011
000
255 1 ▶ my $cb = $self->{on_error} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"on_error"} or return 0 1 1
256 1 $cb->($self, $errno, $self->{data});
257 1 return;
258 }
259  
260 ▶sub _fire_on_close ($self) {
Subroutine _fire_on_close: called CC=3 SCAR=11.0
261 6 ▶ return if $self->{close_cb_fired}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_cb_fired"}++ 0 6 6
262 6 ▶ my $cb = $self->{on_close} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->{"on_close"} or return 2 4 6
263 4 $cb->($self, $self->{data});
264 4 return;
265 }
266  
267 ▶sub _close_now ($self) {
Subroutine _close_now: called CC=4 SCAR=14.6
268 6 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 6 6
269  
270 6 $self->{closed} = 1;
271 6 $self->{closing} = 0;
272  
273 6 ▶ if (my $w = $self->{watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $w = $self->{"watcher"}) 6 0 6
274 6 $w->cancel;
275 }
276  
277 6 ▶ if ($self->{close_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"close_fh"}) 0 6 6
278 0 CORE::close($self->{fh});
279 }
280  
281 6 _fire_on_close($self);
282 6 return;
283 }
284  
285 ▶sub _on_read_ready ($self, $watcher, $fh) {
Subroutine _on_read_ready: called CC=22 SCAR=38.2
286 3 ▶ return if $self->{closed} || $self->{read_paused} || $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"read_paused"} or $self->{"closing"} 0 3 3
Condition33%2/6
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
l!l&&r!l&&!r
003
$self->{"closed"} or $self->{"read_paused"}
l!l&&r!l&&!r
003
MC/DC0%0/3
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
$self->{"closed"} $self->{"read_paused"} $self->{"closing"}
Truth table25%1/4
A or B or C
A= $self->{"closed"}B= $self->{"read_paused"}C= $self->{"closing"}
ABCresult
1XX1
01X1
0011
0000
287  
288 3 my $read_size = $self->{read_size};
289 3 my $max_bytes = $self->{max_read_per_tick};
290  
291 3 my $total = 0;
292  
293 3 while (1) {
294 4 my $bytes = '';
295 4 my $n = sysread($fh, $bytes, $read_size);
296  
297 4 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 3 1 4
298 3 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 3 3
299 0 _close_now($self);
300 0 return;
301 }
302  
303 3 $total += $n;
304  
305 3 ▶ if (my $cb = $self->{on_read}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $self->{'on_read'}) { } 0 3 3
elsif (my $on_message = $self->{'on_message'}) { } 3 0 3
306 # raw bytes mode
307 0 $cb->($self, $bytes, $self->{data});
308 } elsif (my $on_message = $self->{on_message}) {
309 # framed/message mode
310 3 my $rbuf = $self->{rbuf};
311 3 $rbuf .= $bytes;
312  
313 3 my $max = $self->{max_inbuf};
314 3 ▶ if (defined($max) && length($rbuf) > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max and length $rbuf > $max) 1 2 3
Condition66%2/3
defined $max and length $rbuf > $max
!ll&&!rl&&r
021
MC/DC50%1/2
defined $max and length $rbuf > $max
defined $max length $rbuf > $max
Truth table66%2/3
A and B
A= defined $maxB= length $rbuf > $max
ABresult
0X0
100
111
315 1 $self->{last_error} = "codec:max_inbuf_exceeded:$max";
316 1 _fire_on_error($self, 0);
317 1 _close_now($self);
318 1 return;
319 }
320  
321 2 my @out;
322 2 my $codec = $self->{codec};
323 2 my ($ok, $err) = $codec->{decode}->($codec, \$rbuf, \@out);
324 2 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 2 2
325 0 ▶ $err //= 'decode error';
Condition0%0/2
$err //= "decode error"
l!l
00
MC/DC0%0/1
$err //= "decode error"
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
326 0 $self->{last_error} = "codec:$err";
327 0 _fire_on_error($self, 0);
328 0 _close_now($self);
329 0 return;
330 }
331  
332 2 $self->{rbuf} = $rbuf;
333  
334 2 for my $msg (@out) {
335 3 $on_message->($self, $msg, $self->{data});
336 3 ▶ last if $self->{closed} || $self->{read_paused} || $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"read_paused"} or $self->{"closing"} 0 2 2
Condition66%4/6
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
l!l&&r!l&&!r
102
$self->{"closed"} or $self->{"read_paused"}
l!l&&r!l&&!r
102
MC/DC33%1/3
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
$self->{"closed"} $self->{"read_paused"} $self->{"closing"}
Truth table50%2/4
A or B or C
A= $self->{"closed"}B= $self->{"read_paused"}C= $self->{"closing"}
ABCresult
1XX1
01X1
0011
0000
337 }
338 }
339  
340 2 ▶ last if $self->{closed} || $self->{read_paused} || $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"read_paused"} or $self->{"closing"} 0 1 1
Condition66%4/6
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
l!l&&r!l&&!r
101
$self->{"closed"} or $self->{"read_paused"}
l!l&&r!l&&!r
101
MC/DC33%1/3
$self->{"closed"} or $self->{"read_paused"} or $self->{"closing"}
$self->{"closed"} $self->{"read_paused"} $self->{"closing"}
Truth table50%2/4
A or B or C
A= $self->{"closed"}B= $self->{"read_paused"}C= $self->{"closing"}
ABCresult
1XX1
01X1
0011
0000
341 1 ▶ last if $max_bytes && $total >= $max_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if $max_bytes and $total >= $max_bytes 0 1 1
Condition33%1/3
$max_bytes and $total >= $max_bytes
!ll&&!rl&&r
100
MC/DC0%0/2
$max_bytes and $total >= $max_bytes
$max_bytes $total >= $max_bytes
Truth table33%1/3
A and B
A= $max_bytesB= $total >= $max_bytes
ABresult
0X0
100
111
342  
343 1 next;
344 }
345  
346 1 my $errno = $! + 0;
347 1 ▶ return if $errno == 11; # EAGAIN on Linux
Branch: 1 branch
BranchTrueFalseTotal
if $errno == 11 1 0 1
348  
349 0 $self->{last_error} = "io:$errno";
350 0 _fire_on_error($self, $errno);
351 0 _close_now($self);
352 0 return;
353 }
354  
355 1 return;
356 }
357  
358 ▶sub _on_write_ready ($self, $watcher, $fh) {
Subroutine _on_write_ready: called CC=10 SCAR=25.9
359 90 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 90 90
360  
361 90 my $buf = $self->{wbuf};
362 90 my $off = $self->{woff};
363  
364 90 my $pending = length($buf) - $off;
365  
366 90 ▶ if ($pending <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending <= 0) 0 90 90
367 0 $self->{wbuf} = '';
368 0 $self->{woff} = 0;
369 0 $watcher->disable_write;
370 0 ▶ _close_now($self) if $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closing"} 0 0 0
371 0 return;
372 }
373  
374 90 ▶ while ($pending > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pending > 0) 179 1 180
375 179 my $n = syswrite($fh, $buf, $pending, $off);
376  
377 179 ▶ if (defined $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $n) 90 89 179
378 90 $off += $n;
379 90 $pending -= $n;
380  
381 # FIX: persist partial progress so buffered_bytes() and watermark state
382 # can advance even when we return on EAGAIN later.
383 90 $self->{woff} = $off;
384  
385 90 ▶ if ($self->{write_blocked} && $pending < $self->{low_watermark}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"write_blocked"} and $pending < $self->{"low_watermark"}) 1 89 90
Condition66%2/3
$self->{"write_blocked"} and $pending < $self->{"low_watermark"}
!ll&&!rl&&r
0891
MC/DC50%1/2
$self->{"write_blocked"} and $pending < $self->{"low_watermark"}
$self->{"write_blocked"} $pending < $self->{"low_watermark"}
Truth table66%2/3
A and B
A= $self->{"write_blocked"}B= $pending < $self->{"low_watermark"}
ABresult
0X0
100
111
386 1 $self->{write_blocked} = 0;
387 }
388  
389 90 next;
390 }
391  
392 89 my $errno = $! + 0;
393  
394 # FIX: also persist offset before returning.
395 89 $self->{woff} = $off;
396  
397 89 ▶ return if $errno == 11; # EAGAIN on Linux
Branch: 1 branch
BranchTrueFalseTotal
if $errno == 11 89 0 89
398  
399 0 $self->{last_error} = "io:$errno";
400 0 _fire_on_error($self, $errno);
401 0 _close_now($self);
402 0 return;
403 }
404  
405 # drained
406 1 $self->{wbuf} = '';
407 1 $self->{woff} = 0;
408 1 $watcher->disable_write;
409  
410 1 ▶ _close_now($self) if $self->{closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closing"} 0 1 1
411 1 return;
412 }
413  
414 ▶sub _on_error_ready ($self, $watcher, $fh) {
Subroutine _on_error_ready: not called CC=3 SCAR=24.8
415 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
416  
417 0 my $errno = $! + 0;
418 0 ▶ $errno = 5 if !$errno; # EIO fallback
Branch: 1 branch
BranchTrueFalseTotal
unless $errno 0 0 0
419  
420 0 $self->{last_error} = "io:$errno";
421 0 _fire_on_error($self, $errno);
422 0 _close_now($self);
423 0 return;
424 }
425  
426 7 1;
427  
428 __END__
« Stream/Codec/Netstring.pm ↑ Summary Stream/Codec.pm »