Remote/Connection.pm

filter: Statementstmt 15.9% 25 / 157 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 40.0% 8 / 20 Podpod 0.0% 0 / 10 totaltotal 10.6% 33 / 310 CC 52 SCAR 53.3
CC
52
Cov
9%
nextop
506.0
write
210.0
rpc
42.0
CRAP 206.4
↑ Summary Remote.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::IMP::Remote::Connection;
5 1 ▶use fields qw(ev fd rbuf wbuf onwrite onclose need_more_rbuf wire analyzer max_analyzer_id);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Net::IMP::Remote::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Errno 'EIO';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 0 ▶ my ($class,$fd,$side,%args) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
14 my $impl = Net::IMP::Remote::Protocol
15 0 ->load_implementation(delete $args{impl});
16 0 my $wire = $impl->new;
17 0 my $self = fields::new($class);
18 %$self = (
19 ev => delete $args{eventlib},
20 0 fd => $fd,
21 rbuf => '',
22 need_more_rbuf => 1,
23 wbuf => '',
24 wire => $wire,
25 analyzer => {},
26 max_analyzer_id => 1,
27 );
28  
29 0 debug("init wire=$self->{wire} self=$self");
30 0 ▶ if ( my $buf = $wire->init($side) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $buf = $wire->init($side)) 0 0 0
31 0 debug("send initial data");
32 0 ▶ $self->write($buf) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->write($buf) or return 0 0 0
33 }
34  
35 0 return $self;
36 }
37  
38 sub onClose {
39 0 ▶ my ($self,$cb) = @_;
Subroutine onClose: not called CC=1 SCAR=6.9
Pod undocumented
40 0 $self->{onclose} = $cb
41 }
42  
43 sub close:method {
44 0 ▶ my ($self,$error) = @_;
Subroutine close: not called CC=4 SCAR=30.0
Pod undocumented
45 0 debug("close");
46 0 ▶ warn "[$self] error: $error\n" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
47 0 ▶ if ( my $ev = $self->{ev} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ev = $self->{"ev"}) 0 0 0
48 0 $ev->onread($self->{fd},undef);
49 0 $ev->onwrite($self->{fd},undef);
50 }
51 0 ▶ $self->{onclose}->($error) if $self->{onclose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"onclose"} 0 0 0
52 0 %$self = ();
53 }
54  
55 sub add_analyzer {
56 0 ▶ my ($self,$obj,$id) = @_;
Subroutine add_analyzer: not called CC=5 SCAR=34.0
Pod undocumented
57 0 ▶ if ( ! $id ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $id) { } 0 0 0
elsif ($self->{'analyzer'}{$id}) { } 0 0 0
58 0 while (1) {
59 0 $id = $self->{max_analyzer_id}++;
60 0 ▶ $id = $self->{max_analyzer_id} = 1 if $id > 0x7fffffff;
Branch: 1 branch
BranchTrueFalseTotal
if $id > 2147483647 0 0 0
61 0 ▶ last if ! $self->{analyzer}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"analyzer"}{$id} 0 0 0
62 }
63 } elsif ( $self->{analyzer}{$id} ) {
64 0 return;
65 }
66 0 $self->{analyzer}{$id} = $obj;
67 0 return $id;
68 }
69  
70 sub weak_add_analyzer {
71 0 ▶ my $self = shift;
Subroutine weak_add_analyzer: not called CC=2 SCAR=17.9
Pod undocumented
72 0 ▶ my $id = $self->add_analyzer(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $self->add_analyzer(@_) or return 0 0 0
73 0 weaken( $self->{analyzer}{$id} );
74 0 return $id;
75 }
76  
77 sub get_analyzer {
78 0 ▶ my ($self,$id) = @_;
Subroutine get_analyzer: not called CC=1 SCAR=6.9
Pod undocumented
79 0 $self->{analyzer}{$id};
80 }
81  
82 sub del_analyzer {
83 0 ▶ my ($self,$id) = @_;
Subroutine del_analyzer: not called CC=1 SCAR=6.9
Pod undocumented
84 0 delete $self->{analyzer}{$id};
85 }
86  
87 sub rpc {
88 0 ▶ my ($self,$call,$actions) = @_;
Subroutine rpc: not called CC=6 SCAR=37.4
Pod undocumented
89 0 ▶ my $wire = $self->{wire} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $wire = $self->{"wire"} or do { debug("no more wire there to call @$call"); return } 0 0 0
90 0 debug("no more wire there to call @$call");
91 0 return;
92 };
93 0 my $buf = $wire->rpc2buf($call);
94 0 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
95 0 debug("blocking rpc $call->[0]");
96 0 ▶ return $self->write($buf) &&
Branch: 1 branch
BranchTrueFalseTotal
$actions ? : 0 0 0
Condition0%0/3
$self->write($buf) && ($actions ? $self->nextop($actions) : 1)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->write($buf) && ($actions ? $self->nextop($actions) : 1)
$self->write($buf) ($actions ? $self->nextop($actions) : 1)
Truth table0%0/3
A && B
A= $self->write($buf)B= ($actions ? $self->nextop($actions) : 1)
ABresult
0X0
100
111
97 ( $actions ? $self->nextop($actions) : 1 )
98 }
99 0 debug("non-blocking rpc $call->[0]");
100 0 $self->write($buf);
101 0 ▶ $self->nextop($actions) if $actions;
Branch: 1 branch
BranchTrueFalseTotal
if $actions 0 0 0
102 }
103  
104 # write data in buffer
105 # !eventlib || defined wantarray -> blocking write
106 # otherwise: nonblocking with event handler for writing rest
107 sub write {
108 0 ▶ my $self = shift;
Subroutine write: not called CC=14 SCAR=53.5
Pod undocumented
109 0 ▶ $self->{wbuf} .= shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
110 0 ▶ if ( $self->{wbuf} eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'wbuf'} eq '') { } 0 0 0
elsif (defined wantarray or not $self->{'ev'}) { } 0 0 0
111 0 debug("nothing to write");
112 0 return 1; # nothing to write
113 ▶ } elsif ( defined wantarray or ! $self->{ev} ) {
Condition0%0/3
defined wantarray or not $self->{"ev"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined wantarray or not $self->{"ev"}
defined wantarray not $self->{"ev"}
Truth table0%0/3
A or not(B)
A= defined wantarrayB= not $self->{"ev"}
ABresult
1X1
011
000
114 # blocking write
115 0 ▶ while ( $self->{wbuf} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"wbuf"} ne "") 0 0 0
116 0 my $n = syswrite($self->{fd},$self->{wbuf});
117 0 ▶ if ( $n ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n) { } 0 0 0
elsif (not defined $n and $!{'EAGAIN'}) { } 0 0 0
118 0 debug("wrote %d of %d bytes",$n,length($self->{wbuf}));
119 0 substr($self->{wbuf},0,$n,'');
120 ▶ } elsif ( ! defined $n and $!{EAGAIN} ) {
Condition0%0/3
not defined $n and $!{"EAGAIN"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $n and $!{"EAGAIN"}
not defined $n $!{"EAGAIN"}
Truth table0%0/3
not(A) and B
A= not defined $nB= $!{"EAGAIN"}
ABresult
0X0
100
111
121 0 debug("short write - blocking wait for writable socket");
122 0 vec(my $win = '',fileno($self->{fd}),1) = 1;
123 0 ▶ select(undef,$win,undef,undef) or do {
Branch: 1 branch
BranchTrueFalseTotal
select undef, $win, undef, undef or do { $self->close("failed to select: $!"); return } 0 0 0
124 0 $self->close("failed to select: $!");
125 return
126 0 }
127 } else {
128 0 $self->close("failed to write: $!");
129 0 return;
130 }
131 }
132 0 debug("blocking write completed");
133 0 return 1;
134 } else {
135 # async write
136 0 my $n = syswrite($self->{fd},$self->{wbuf});
137 0 ▶ if ( $n ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n) { } 0 0 0
elsif ($!{'EAGAIN'}) { } 0 0 0
138 0 substr($self->{wbuf},0,$n,'');
139 0 ▶ if ( $self->{wbuf} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'wbuf'} eq '') { } 0 0 0
140 0 debug("non-blocking write completed");
141 0 return 1;
142 } else {
143 debug("non-blocking short write %d of %d",$n,
144 0 $n+length($self->{wbuf}));
145 }
146 } elsif ( $!{EAGAIN} ) {
147 0 $self->close("failed to write: $!");
148 0 return;
149 }
150  
151 0 debug("async continue write if socket writable");
152 0 ▶ $self->{ev}->onwrite( $self->{fd}, $self->{onwrite} ||= do {
Condition0%0/3
$self->{"onwrite"} ||= do { builtin::weaken(my $wself = $self); sub { $wself->write or return while $wself->{"wbuf"} ne ""; $wself->{"ev"}->onwrite($wself->{"fd"}, undef); } }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"onwrite"} ||= do { builtin::weaken(my $wself = $self); sub { $wself->write or return while $wself->{"wbuf"} ne ""; $wself->{"ev"}->onwrite($wself->{"fd"}, undef); } }
$self->{"onwrite"} do { builtin::weaken(my $wself = $self); sub { $wself->write or return while $wself->{"wbuf"} ne ""; $wself->{"ev"}->onwrite($wself->{"fd"}, undef); } }
Truth table0%0/3
A ||= B
A= $self->{"onwrite"}B= do { builtin::weaken(my $wself = $self); sub { $wself->write or return while $wself->{"wbuf"} ne ""; $wself->{"ev"}->onwrite($wself->{"fd"}, undef); } }
ABresult
1X1
011
000
153 # callback to write rest of wbuf
154 0 weaken( my $wself = $self );
155 sub {
156 0 ▶ $wself->write() or return while $wself->{wbuf} ne '';
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
$wself->{"wbuf"} ne "" and do { $wself->write or return } 0 0 0
$wself->write or return 0 0 0
157 0 $wself->{ev}->onwrite($wself->{fd},undef);
158 0 };
159 });
160 0 return 1;
161 }
162 }
163  
164  
165 # handle single (or up to $max) operation
166 # !eventlib || defined wantarray -> block until one operation done
167 # otherwise: setup event handler if no operation fully read yet
168 sub nextop {
169 0 ▶ my ($self,$actions,$max,$incb) = @_;
Subroutine nextop: not called CC=22 SCAR=62.3
Pod undocumented
170 0 ▶ my $block = $incb ? 0 : defined wantarray || ! $self->{ev};
Branch: 1 branch
BranchTrueFalseTotal
$incb ? : 0 0 0
Condition0%0/3
defined wantarray or not $self->{"ev"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined wantarray or not $self->{"ev"}
defined wantarray not $self->{"ev"}
Truth table0%0/3
A or not(B)
A= defined wantarrayB= not $self->{"ev"}
ABresult
1X1
011
000
171  
172 NEXTOP:
173 0 ▶ while ( $self->{need_more_rbuf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"need_more_rbuf"}) 0 0 0
174 0 ▶ debug("trying to read%s...", $incb ? ' inside read-callback':'' );
Branch: 1 branch
BranchTrueFalseTotal
$incb ? : 0 0 0
175 0 my $n = sysread($self->{fd},$self->{rbuf},8192,length($self->{rbuf}));
176 0 ▶ debug("read done -> ".( $n // $! ));
Condition0%0/3
$n // $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$n // $!
$n $!
Truth table0%0/3
A // B
A= $nB= $!
ABresult
1X1
011
000
177 0 ▶ if ( $n ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($n) { } 0 0 0
elsif (defined $n) { } 0 0 0
elsif (not $!{'EAGAIN'}) { } 0 0 0
elsif ($incb) { } 0 0 0
elsif ($block) { } 0 0 0
178 0 last;
179 } elsif ( defined $n ) {
180 # eof
181 0 ▶ if ( $self->{rbuf} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rbuf'} eq '') { } 0 0 0
182 0 $self->close();
183 0 return 0
184 } else {
185 # consider eof within data block as error
186 0 $! = EIO;
187 0 $self->close("eof inside operation");
188 0 return;
189 }
190 } elsif ( ! $!{EAGAIN} ) {
191 0 $self->close("failed to read: $!");
192 0 return;
193 } elsif ( $incb ) {
194 # async wait, but we are inside callback already, so just
195 # wait for more data
196 0 debug("waiting for more inside existing callback");
197 0 return;
198 } elsif ( $block ) {
199 # blocking wait for new data
200 0 vec(my $rin = '',fileno($self->{fd}),1) = 1;
201 0 ▶ select($rin,undef,undef,undef) or do {
Branch: 1 branch
BranchTrueFalseTotal
select $rin, undef, undef, undef or do { $self->close("select failed: $!"); return } 0 0 0
202 0 $self->close("select failed: $!");
203 0 return;
204 };
205 0 next;
206 }
207  
208 # async wait for more
209 0 debug("install onread callback for more data");
210 0 weaken(my $wself = $self);
211 $self->{ev}->onread($self->{fd}, sub {
212 0 ▶ $wself->nextop($actions,$max,1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 });
214 0 return;
215 }
216  
217 0 my $rpc = $self->{wire}->buf2rpc(\$self->{rbuf});
218 0 ▶ if ( ! $rpc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rpc) 0 0 0
219 0 $self->{need_more_rbuf} = 1;
220 0 goto NEXTOP;
221 }
222  
223 0 $self->{need_more_rbuf} = $self->{rbuf} eq '';
224  
225 0 my ($type,@args) = @$rpc;
226 #debug(Dumper($rpc)); use Data::Dumper;
227 0 debug("processing $type");
228 0 ▶ my $act = $actions->{$type+0} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $act = $$actions{$type + 0} or do { $self->close("no handler for return type $type"); return } 0 0 0
229 0 $self->close( "no handler for return type $type" );
230 0 return;
231 };
232  
233 0 ▶ if ( ref($act) eq 'CODE' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $act eq 'CODE') { } 0 0 0
elsif (ref $act eq 'ARRAY') { } 0 0 0
234 0 $act->(@args)
235 } elsif ( ref($act) eq 'ARRAY' ) {
236 0 ▶ if (! @$act) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$act) { } 0 0 0
237 0 @$act = @args;
238 } else {
239 # assume code+args
240 0 my ($code,@m) = @$act;
241 0 $code->(@m,@args);
242 }
243 } else {
244 # assume object
245 0 $act->$type(@args)
246 }
247
248 0 ▶ --$max if $max && $max>0;
Branch: 1 branch
BranchTrueFalseTotal
if $max and $max > 0 0 0 0
Condition0%0/3
$max and $max > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$max and $max > 0
$max $max > 0
Truth table0%0/3
A and B
A= $maxB= $max > 0
ABresult
0X0
100
111
249 0 ▶ return 1 if ! $max; # one-shot or done
Branch: 1 branch
BranchTrueFalseTotal
unless $max 0 0 0
250 0 ▶ $incb = 0 if $max>0; # redo onread callback with changed $max
Branch: 1 branch
BranchTrueFalseTotal
if $max > 0 0 0 0
251 0 goto NEXTOP;
252 }
253  
254  
255 1 1;
↑ Summary Remote.pm »