Client/IO.pm

filter: Statementstmt 23.1% 32 / 138 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 62.5% 10 / 16 Podpod 100% 4 / 4 totaltotal 16.2% 46 / 284 SCAR 73.5
CC
48
Cov
13%
_try_to_write
462.0
_try_to_read
210.0
receive_data
42.0
CRAP 1556.4
« Client/Frame.pm ↑ Summary Client/Connection.pm »
1 #+##############################################################################
2 # #
3 # File: Net/STOMP/Client/IO.pm #
4 # #
5 # Description: Input/Output support for Net::STOMP::Client #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Net::STOMP::Client::IO;
14 2 ▶use 5.005; # need the four-argument form of substr()
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our $VERSION = "2.4";
18 2 our $REVISION = sprintf("%d.%02d", q$Revision: 2.2 $ =~ /(\d+)\.(\d+)/);
19  
20 #
21 # used modules
22 #
23  
24 2 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use No::Worries::Log qw(log_debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use POSIX qw(:errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #
31 # constants
32 #
33  
34 2 ▶use constant READ_LENGTH => 32_768; # chunk size for sysread()
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant WRITE_LENGTH => 32_768; # minimum length for syswrite()
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 #+++############################################################################
38 # #
39 # private helpers #
40 # #
41 #---############################################################################
42  
43 #
44 # attempt to read data from the socket to the buffer
45 #
46 # note: we read at least once even if the buffer contains enough data
47 #
48 # common scenarios:
49 # - timeout=undef minlen=undef: loop until we successfully read once
50 # - timeout=undef minlen=N: loop until we read at least N bytes
51 # - timeout=0 minlen=undef: read only once (successful or not)
52 # - timeout=0 minlen=N: loop until we read >=N bytes or fail once
53 # - timeout=T minlen=undef: loop until timeout
54 # - timeout=T minlen=N: loop until we read >=N bytes or timeout
55 #
56  
57 sub _try_to_read ($$$) { ## no critic 'ProhibitExcessComplexity'
58 0 ▶ my($self, $timeout, $minlen) = @_;
Subroutine _try_to_read: not called CC=14 SCAR=53.5
59 0 my($maxtime, $total, $count, $sleeptime, $remaining);
60  
61 0 $self->{incoming_buflen} = length($self->{incoming_buffer});
62 # boundary conditions
63 0 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 0 0 0
64 0 ▶ return(0) unless $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout > 0 0 0 0
65 # timer starts now
66 0 $maxtime = Time::HiRes::time() + $timeout;
67 }
68 # try to read, in a loop, until we are done
69 0 $total = 0;
70 0 while (1) {
71 # attempt to read once
72 $count = sysread($self->{socket}, $self->{incoming_buffer},
73 0 READ_LENGTH, $self->{incoming_buflen});
74 0 ▶ if (defined($count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count) { } 0 0 0
75 # we could read this time
76 0 ▶ unless ($count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 0 0
77 # ... but we hit the EOF
78 0 $self->{error} = "cannot sysread(): EOF";
79 0 return($total);
80 }
81 # this is a normal successful read
82 0 $self->{incoming_time} = Time::HiRes::time();
83 0 $self->{incoming_buflen} += $count;
84 0 $total += $count;
85 # check if we have worked enough
86 0 ▶ return($total) unless $minlen and $total < $minlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $minlen and $total < $minlen 0 0 0
Condition0%0/3
$minlen and $total < $minlen
!ll&&!rl&&r
000
MC/DC0%0/2
$minlen and $total < $minlen
$minlen $total < $minlen
Truth table0%0/3
A and B
A= $minlenB= $total < $minlen
ABresult
0X0
100
111
87 } else {
88 # we could not read this time
89 0 ▶ if ($! != EAGAIN and $! != EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 11 and $! != 11) 0 0 0
Condition0%0/3
$! != 11 and $! != 11
!ll&&!rl&&r
000
MC/DC0%0/2
$! != 11 and $! != 11
$! != 11 $! != 11
Truth table0%0/3
A and B
A= $! != 11B= $! != 11
ABresult
0X0
100
111
90 # unexpected error
91 0 $self->{error} = "cannot sysread(): $!";
92 0 return(undef);
93 }
94 }
95 # check time
96 0 ▶ if (not defined($timeout)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $timeout) { } 0 0 0
elsif ($timeout) { } 0 0 0
97 # timeout = undef => loop forever until we are done
98 0 $sleeptime = 0.01;
99 } elsif ($timeout) {
100 # timeout > 0 => try again only if not too late
101 0 $remaining = $maxtime - Time::HiRes::time();
102 0 ▶ return($total) unless $remaining > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $remaining > 0 0 0 0
103 0 $sleeptime = min($remaining, 0.01);
104 } else {
105 # timeout = 0 => try again unless last read failed
106 0 ▶ return($total) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
107 }
108 # sleep a bit...
109 0 ▶ Time::HiRes::sleep($sleeptime) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
110 }
111 }
112  
113 #
114 # attempt to write data from the queue and buffer to the socket
115 #
116 # common scenarios:
117 # - timeout=undef minlen=undef: loop until we successfully write once
118 # - timeout=undef minlen=N: loop until we write at least N bytes
119 # - timeout=0 minlen=undef: write only once (successful or not)
120 # - timeout=0 minlen=N: loop until we write >=N bytes or fail once
121 # - timeout=T minlen=undef: loop until timeout
122 # - timeout=T minlen=N: loop until we write >=N bytes or timeout
123 #
124  
125 sub _try_to_write ($$$) { ## no critic 'ProhibitExcessComplexity'
126 0 ▶ my($self, $timeout, $minlen) = @_;
Subroutine _try_to_write: not called CC=21 SCAR=61.4
127 0 my($maxtime, $total, $count, $sleeptime, $remaining, $data);
128  
129 0 $self->{outgoing_buflen} = length($self->{outgoing_buffer});
130 # boundary conditions
131 0 ▶ return(0) unless $self->{outgoing_buflen} or @{ $self->{outgoing_queue} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};} 0 0 0
Condition0%0/3
$self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};}
$self->{"outgoing_buflen"} @{$self->{"outgoing_queue"};}
Truth table0%0/3
A or B
A= $self->{"outgoing_buflen"}B= @{$self->{"outgoing_queue"};}
ABresult
1X1
011
000
132 0 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) 0 0 0
133 0 ▶ return(0) unless $timeout > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout > 0 0 0 0
134 # timer starts now
135 0 $maxtime = Time::HiRes::time() + $timeout;
136 }
137 # try to write, in a loop, until we are done
138 0 $total = 0;
139 0 while (1) {
140 # make sure there is enough data in the outgoing buffer
141 0 ▶ while ($self->{outgoing_buflen} < WRITE_LENGTH
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outgoing_buflen"} < 32768 and @{$self->{"outgoing_queue"};}) 0 0 0
Condition0%0/3
$self->{"outgoing_buflen"} < 32768 and @{$self->{"outgoing_queue"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"outgoing_buflen"} < 32768 and @{$self->{"outgoing_queue"};}
$self->{"outgoing_buflen"} < 32768 @{$self->{"outgoing_queue"};}
Truth table0%0/3
A and B
A= $self->{"outgoing_buflen"} < 32768B= @{$self->{"outgoing_queue"};}
ABresult
0X0
100
111
142 0 and @{ $self->{outgoing_queue} }) {
143 0 $data = shift(@{ $self->{outgoing_queue} });
144 0 $self->{outgoing_buffer} .= ${ $data };
145 0 $self->{outgoing_buflen} += length(${ $data });
146 }
147 0 ▶ return($total) unless $self->{outgoing_buflen};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"outgoing_buflen"} 0 0 0
148 # attempt to write once
149 $count = syswrite($self->{socket}, $self->{outgoing_buffer},
150 0 $self->{outgoing_buflen});
151 0 ▶ if (defined($count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $count) { } 0 0 0
152 # we could write this time
153 0 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
154 # this is a normal successful write
155 0 $self->{outgoing_time} = Time::HiRes::time();
156 0 $self->{outgoing_buflen} -= $count;
157 0 $total += $count;
158 0 substr($self->{outgoing_buffer}, 0, $count, "");
159 0 $self->{outgoing_length} -= $count;
160 # check if we have worked enough
161 return($total) unless $self->{outgoing_buflen}
162 0 ▶ or @{ $self->{outgoing_queue} };
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};} 0 0 0
Condition0%0/3
$self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"outgoing_buflen"} or @{$self->{"outgoing_queue"};}
$self->{"outgoing_buflen"} @{$self->{"outgoing_queue"};}
Truth table0%0/3
A or B
A= $self->{"outgoing_buflen"}B= @{$self->{"outgoing_queue"};}
ABresult
1X1
011
000
163 0 ▶ return($total) unless $minlen and $total < $minlen;
Branch: 1 branch
BranchTrueFalseTotal
unless $minlen and $total < $minlen 0 0 0
Condition0%0/3
$minlen and $total < $minlen
!ll&&!rl&&r
000
MC/DC0%0/2
$minlen and $total < $minlen
$minlen $total < $minlen
Truth table0%0/3
A and B
A= $minlenB= $total < $minlen
ABresult
0X0
100
111
164 }
165 } else {
166 # we could not write this time
167 0 ▶ if ($! != EAGAIN and $! != EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 11 and $! != 11) 0 0 0
Condition0%0/3
$! != 11 and $! != 11
!ll&&!rl&&r
000
MC/DC0%0/2
$! != 11 and $! != 11
$! != 11 $! != 11
Truth table0%0/3
A and B
A= $! != 11B= $! != 11
ABresult
0X0
100
111
168 # unexpected error
169 0 $self->{error} = "cannot syswrite(): $!";
170 0 return(undef);
171 }
172 }
173 # check time
174 0 ▶ if (not defined($timeout)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $timeout) { } 0 0 0
elsif ($timeout) { } 0 0 0
175 # timeout = undef => loop forever until we are done
176 0 $sleeptime = 0.01;
177 } elsif ($timeout) {
178 # timeout > 0 => try again only if not too late
179 0 $remaining = $maxtime - Time::HiRes::time();
180 0 ▶ return($total) unless $remaining > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $remaining > 0 0 0 0
181 0 $sleeptime = min($remaining, 0.01);
182 } else {
183 # timeout = 0 => try again unless last write failed
184 0 ▶ return($total) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
185 }
186 # sleep a bit...
187 0 ▶ Time::HiRes::sleep($sleeptime) unless $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $count 0 0 0
188 }
189 }
190  
191 #+++############################################################################
192 # #
193 # object oriented interface #
194 # #
195 #---############################################################################
196  
197 #
198 # constructor
199 #
200  
201 sub new : method {
202 0 ▶ my($class, $socket) = @_;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
203 0 my($self);
204  
205 0 ▶ dief("missing or invalid socket")
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and ref $socket and $socket->isa("IO::Socket") 0 0 0
Condition0%0/6
$socket and ref $socket and $socket->isa("IO::Socket")
!ll&&!rl&&r
000
$socket and ref $socket
!ll&&!rl&&r
000
MC/DC0%0/3
$socket and ref $socket and $socket->isa("IO::Socket")
$socket ref $socket $socket->isa("IO::Socket")
Truth table0%0/4
A and B and C
A= $socketB= ref $socketC= $socket->isa("IO::Socket")
ABCresult
0XX0
10X0
1100
1111
206 unless $socket and ref($socket) and $socket->isa("IO::Socket");
207 0 $socket->blocking(0);
208 0 $self = {};
209 0 $self->{socket} = $socket;
210 0 $self->{incoming_buffer} = "";
211 0 $self->{incoming_buflen} = 0;
212 0 $self->{outgoing_buffer} = "";
213 0 $self->{outgoing_buflen} = 0; # buffer length only
214 0 $self->{outgoing_queue} = [];
215 0 $self->{outgoing_length} = 0; # buffer + queue length
216 0 return(bless($self, $class));
217 }
218  
219 #
220 # queue the given data (a scalar reference!)
221 #
222  
223 sub queue_data : method {
224 0 ▶ my($self, $data) = @_;
Subroutine queue_data: not called CC=3 SCAR=24.8
Pod documented
225 0 my($length);
226  
227 0 ▶ dief("unexpected data: %s", $data) unless ref($data) eq "SCALAR";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "SCALAR" 0 0 0
228 0 $length = length(${ $data });
229 0 ▶ if ($length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length) 0 0 0
230 0 push(@{ $self->{outgoing_queue} }, $data);
231 0 $self->{outgoing_length} += $length;
232 }
233 0 return($self->{outgoing_length});
234 }
235  
236 #
237 # send the queued data
238 #
239  
240 sub send_data : method {
241 0 ▶ my($self, %option) = @_;
Subroutine send_data: not called CC=5 SCAR=34.0
Pod documented
242 0 my($minlen, $count);
243  
244 0 ▶ unless ($self->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"error"}) 0 0 0
245 # send some data
246 0 $minlen = $self->{outgoing_length};
247 0 $count = _try_to_write($self, $option{timeout}, $minlen);
248 }
249 0 ▶ dief($self->{error}) unless defined($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
250 # so far so good
251 log_debug("sent %d bytes", $count)
252 0 ▶ if $option{debug} and $option{debug} =~ /\b(io|all)\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/ 0 0 0
Condition0%0/3
$option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/
$option{"debug"} $option{"debug"} =~ /\b(io|all)\b/
Truth table0%0/3
A and B
A= $option{"debug"}B= $option{"debug"} =~ /\b(io|all)\b/
ABresult
0X0
100
111
253 0 return($count);
254 }
255  
256 #
257 # receive some data
258 #
259  
260 sub receive_data : method {
261 0 ▶ my($self, %option) = @_;
Subroutine receive_data: not called CC=6 SCAR=37.4
Pod documented
262 0 my($minlen, $count);
263  
264 0 ▶ unless ($self->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"error"}) 0 0 0
265 # receive some data
266 0 ▶ $minlen = $option{timeout} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$option{'timeout'} ? : 0 0 0
267 0 $count = _try_to_read($self, $option{timeout}, $minlen);
268 }
269 0 ▶ dief($self->{error}) unless defined($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
270 # so far so good
271 log_debug("received %d bytes", $count)
272 0 ▶ if $option{debug} and $option{debug} =~ /\b(io|all)\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/ 0 0 0
Condition0%0/3
$option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$option{"debug"} and $option{"debug"} =~ /\b(io|all)\b/
$option{"debug"} $option{"debug"} =~ /\b(io|all)\b/
Truth table0%0/3
A and B
A= $option{"debug"}B= $option{"debug"} =~ /\b(io|all)\b/
ABresult
0X0
100
111
273 0 return($count);
274 }
275  
276 2 1;
277  
278 __END__
« Client/Frame.pm ↑ Summary Client/Connection.pm »