blib/lib/Net/Server/NonBlocking.pm

filter: Statementstmt 16.6% 49 / 294 Branchbran 0.0% 0 / 136 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 32.3% 11 / 34 Podpod 72.2% 13 / 18 totaltotal 14.3% 73 / 510 CC 85 SCAR 61.8
CC
85
Cov
11%
start
1122.0
add
702.0
flush_output
132.0
CRAP 483.8
↑ Summary
1 package Net::Server::NonBlocking;
2  
3 1 ▶use 5.000503;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Tie::RefHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 $VERSION = '0.048';
17  
18 1 my @now=localtime(time);
19 1 my $cronCounter=$now[0]+60*$now[1]+3600*$now[2]+3600*24*$now[3];
20 1 my %buff;
21  
22 # begin with empty buffers
23 1 my %inbuffer = ();
24 1 my %outbuffer = ();
25 1 my %ready = ();
26  
27 1 my %turn_timeout;
28 my %turn_timeout_trigger;
29 1 my $select = IO::Select->new();
30 1 my %idle;
31 my %timer;
32 1 my %map_all;
33 1 my %map_specific;
34 1 my %map_server;
35 1 my %map_client;
36 1 my %alive;
37  
38 1 tie %ready, 'Tie::RefHash';
39  
40 0 ▶sub new{
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
41 0 my($proto,@arg)=@_;
42 0 ▶ my $class=ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
43 0 my $hash=$arg[0];
44  
45 0 my $self={};
46 0 ▶ $self->{pidfile}=exists $hash->{pidfile} ? $hash->{pidfile} : '/tmp/anonymous_server';
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'pidfile'} ? : 0 0 0
47  
48 0 bless $self,$class;
49 }
50  
51 0 ▶sub add {
Subroutine add: not called CC=26 SCAR=65.5
Pod documented
52 0 my $self=shift;
53 0 my $hash=shift;
54  
55 0 ▶ die("server_name is required") if not exists $hash->{server_name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash->{"server_name"} 0 0 0
56
57 0 ▶ if (not exists $hash->{local_port}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $hash->{'local_port'}) { } 0 0 0
58 $self->{listen}->{$hash->{server_name}}->{delimiter}=
59 0 ▶ exists $hash->{delimiter} ? $hash->{delimiter} : "\0";
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'delimiter'} ? : 0 0 0
60 $self->{listen}->{$hash->{server_name}}->{string_format}=
61 0 ▶ exists $hash->{string_format} ? $hash->{string_format} : '.*?';
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'string_format'} ? : 0 0 0
62 $self->{listen}->{$hash->{server_name}}->{timeout}=
63 0 ▶ exists $hash->{timeout} ? $hash->{timeout} : 300;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'timeout'} ? : 0 0 0
64 $self->{listen}->{$hash->{server_name}}->{on_disconnected}=
65 0 ▶ exists $hash->{on_disconnected} ? $hash->{on_disconnected} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_disconnected'} ? : 0 0 0
66 $self->{listen}->{$hash->{server_name}}->{on_recv_msg}=
67 0 ▶ exists $hash->{on_recv_msg} ? $hash->{on_recv_msg} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_recv_msg'} ? : 0 0 0
68 $self->{listen}->{$hash->{server_name}}->{read_buffer} =
69 0 ▶ exists $hash->{read_buffer} ? $hash->{read_buffer} : \&read_buffer;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'read_buffer'} ? : 0 0 0
70 $self->{listen}->{$hash->{server_name}}->{on_disconnected_param}=
71 0 ▶ exists $hash->{on_disconnected_param} ? $hash->{on_disconnected_param} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_disconnected_param'} ? : 0 0 0
72 $self->{listen}->{$hash->{server_name}}->{on_recv_msg_param}=
73 0 ▶ exists $hash->{on_recv_msg_param} ? $hash->{on_recv_msg_param} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_recv_msg_param'} ? : 0 0 0
74  
75 0 return undef;
76 } else {
77 0 my $server;
78  
79 0 ▶ if (exists $hash->{local_address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{'local_address'}) { } 0 0 0
80 $server = IO::Socket::INET->new(
81 LocalAddr => $hash->{local_address},
82 LocalPort => $hash->{local_port},
83 0 ▶ Listen => 50,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::INET"->new("LocalAddr", $hash->{"local_address"}, "LocalPort", $hash->{"local_port"}, "Listen", 50, "Proto", "tcp", "Reuse", 1, "Blocking", 0) or die "Can't make server socket -- $@\n" 0 0 0
84 Proto => 'tcp',
85 Reuse => 1,
86 Blocking => 0)
87 or die "Can't make server socket -- $@\n";
88 } else {
89 $server = IO::Socket::INET->new(
90 LocalPort => $hash->{local_port},
91 0 ▶ Listen => 50,
Branch: 1 branch
BranchTrueFalseTotal
$server = "IO::Socket::INET"->new("LocalPort", $hash->{"local_port"}, "Listen", 50, "Proto", "tcp", "Reuse", 1, "Blocking", 0) or die "Can't make server socket -- $@\n" 0 0 0
92 Proto => 'tcp',
93 Reuse => 1,
94 Blocking => 0)
95 or die "Can't make server socket -- $@\n";
96 }
97 0 $self->nonblock($server);
98  
99 0 $self->{listen}->{$hash->{server_name}}->{socket}=$server;
100 0 ▶ $self->{listen}->{$hash->{server_name}}->{local_address}=$hash->{local_address} || "localhost";
Condition0%0/2
$hash->{'local_address'} || 'localhost'
l!l
00
MC/DC0%0/1
$hash->{'local_address'} || 'localhost'
$hash->{'local_address'}
Truth table0%0/2
A
A= $hash->{'local_address'}
Aresult
11
00
101 0 $self->{listen}->{$hash->{server_name}}->{local_port}=$hash->{local_port};
102 $self->{listen}->{$hash->{server_name}}->{delimiter}=
103 0 ▶ exists $hash->{delimiter} ? $hash->{delimiter} : "\0";
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'delimiter'} ? : 0 0 0
104 $self->{listen}->{$hash->{server_name}}->{string_format}=
105 0 ▶ exists $hash->{string_format} ? $hash->{string_format} : '.*?';
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'string_format'} ? : 0 0 0
106 $self->{listen}->{$hash->{server_name}}->{timeout}=
107 0 ▶ exists $hash->{timeout} ? $hash->{timeout} : 300;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'timeout'} ? : 0 0 0
108 $self->{listen}->{$hash->{server_name}}->{on_connected}=
109 0 ▶ exists $hash->{on_connected} ? $hash->{on_connected} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_connected'} ? : 0 0 0
110 $self->{listen}->{$hash->{server_name}}->{on_disconnected}=
111 0 ▶ exists $hash->{on_disconnected} ? $hash->{on_disconnected} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_disconnected'} ? : 0 0 0
112 $self->{listen}->{$hash->{server_name}}->{on_recv_msg}=
113 0 ▶ exists $hash->{on_recv_msg} ? $hash->{on_recv_msg} : sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_recv_msg'} ? : 0 0 0
114 $self->{listen}->{$hash->{server_name}}->{read_buffer} =
115 0 ▶ exists $hash->{read_buffer} ? $hash->{read_buffer} : \&read_buffer;
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'read_buffer'} ? : 0 0 0
116 $self->{listen}->{$hash->{server_name}}->{on_connected_param}=
117 0 ▶ exists $hash->{on_connected_param} ? $hash->{on_connected_param} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_connected_param'} ? : 0 0 0
118 $self->{listen}->{$hash->{server_name}}->{on_disconnected_param}=
119 0 ▶ exists $hash->{on_disconnected_param} ? $hash->{on_disconnected_param} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_disconnected_param'} ? : 0 0 0
120 $self->{listen}->{$hash->{server_name}}->{on_recv_msg_param}=
121 0 ▶ exists $hash->{on_recv_msg_param} ? $hash->{on_recv_msg_param} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $hash->{'on_recv_msg_param'} ? : 0 0 0
122  
123 0 ▶ if (exists $hash->{local_address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{'local_address'}) { } 0 0 0
124 $map_specific{"$hash->{local_address}:$hash->{local_port}"}=
125 0 $hash->{server_name};
126 } else {
127 $map_all{$hash->{local_port}}=
128 0 $hash->{server_name};
129 }
130  
131 0 $map_server{$server} = $hash->{server_name};
132  
133 0 return $server;
134 }
135 }
136  
137 0 ▶sub bind {
Subroutine bind: not called CC=1 SCAR=6.9
Pod documented
138 0 my $self=shift;
139 0 my $server_name=shift;
140 0 my $client=shift;
141  
142 0 $select->add($client);
143 0 $self->nonblock($client);
144  
145 0 $alive{$client}=1;
146 0 $idle{$client}=time;
147 0 $turn_timeout{$client}=-1;
148  
149 0 $map_client{$client}=$server_name;
150 }
151  
152 0 ▶sub nonblock {
Subroutine nonblock: not called CC=3 SCAR=24.8
Pod undocumented
153 0 my $self=shift;
154 0 my $socket=shift;
155 0 my $flags;
156  
157 0 ▶ $flags = fcntl($socket, F_GETFL, 0)
Branch: 1 branch
BranchTrueFalseTotal
$flags = fcntl($socket, 3, 0) or die "Can't get flags for socket: $!\n" 0 0 0
158 or die "Can't get flags for socket: $!\n";
159 0 ▶ fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $socket, 4, $flags | 2048 or die "Can't make socket nonblocking: $!\n" 0 0 0
160 or die "Can't make socket nonblocking: $!\n";
161 }
162  
163 0 ▶sub handle {
Subroutine handle: not called CC=2 SCAR=17.9
Pod undocumented
164 0 my $self=shift;
165 0 my $server_name=shift;
166 0 my $client = shift;
167 0 my $request;
168  
169 # requests are in $ready{$client}
170 # send output to $outbuffer{$client}
171  
172 0 foreach $request (@{$ready{$client}}) {
173 # $request is the text of the request
174 # put text of reply into $outbuffer{$client}
175  
176 0 $self->{listen}->{$server_name}->{on_recv_msg}->($self,$client,$request,@{$self->{listen}->{$server_name}->{on_recv_msg_param}});
177 }
178  
179 0 delete $ready{$client};
180 }
181  
182 0 ▶sub get_server_socket {
Subroutine get_server_socket: not called CC=1 SCAR=6.9
Pod documented
183 0 my $self=shift;
184 0 my $server_name = shift;
185  
186 0 $self->{listen}->{$server_name}->{socket};
187 }
188  
189 0 ▶sub get_server_name {
Subroutine get_server_name: not called CC=4 SCAR=30.0
Pod documented
190 0 my $self=shift;
191 0 my $client=shift;
192 #my @caller=caller();
193  
194 0 ▶ return $map_server{$client} if exists $map_server{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_server{$client} 0 0 0
195 0 ▶ return $map_client{$client} if exists $map_client{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_client{$client} 0 0 0
196
197 0 ▶ if (exists $map_specific{$client->sockhost().":".$client->sockport()}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map_specific{$client->sockhost . ':' . $client->sockport}) { } 0 0 0
198 0 return $map_specific{$client->sockhost().":".$client->sockport()};
199 } else {
200 0 return $map_all{$client->sockport()};
201 }
202 }
203  
204 0 ▶sub start_turn {
Subroutine start_turn: not called CC=1 SCAR=6.9
Pod documented
205 0 my $self=shift;
206 0 my $client=shift;
207 0 my $time=shift;
208  
209 0 $turn_timeout{$client}=$time;
210 0 $turn_timeout_trigger{$client}=$_[0];
211 }
212  
213 0 ▶sub reset_turn {
Subroutine reset_turn: not called CC=1 SCAR=6.9
Pod documented
214 0 my $self=shift;
215 0 my $client=shift;
216  
217 0 $turn_timeout{$client}=-1;
218 0 delete($turn_timeout_trigger{$client});
219 }
220  
221 #sub flush_input {
222 # my $self=shift;
223 # my $client=shift;
224 # my $server_name=$self->get_server_name($client);
225 #
226 # my $rin='';
227 # vec($rin,fileno($client),1)=1;
228 #
229 # select(my $rout=$rin,undef,undef,0);
230 #
231 # if (vec($rout,fileno($client),1)) {
232 # my $data = '';
233 # my $rv = $client->recv($data, POSIX::BUFSIZ, 0);
234 #
235 # unless (defined($rv) && length $data) {
236 # # This would be the end of file, so close the client
237 # $self->erase_client($server_name,$client);
238 # next;
239 # }
240 #
241 # $inbuffer{$client} .= $data;
242 # $self->{listen}->{$server_name}->{read_buffer}->($self,\$inbuffer{$client},\$ready{$client},$server_name);
243 #
244 # $idle{$client}=time;
245 # }
246 #
247 # $self->handle($server_name,$client);
248 #}
249  
250 0 ▶sub flush_output {
Subroutine flush_output: not called CC=11 SCAR=48.8
Pod documented
251 0 my $self=shift;
252 0 my $client=shift;
253 0 my $server_name=$self->get_server_name($client);
254  
255 0 ▶ return unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
256  
257 0 my $rin='';
258 0 vec($rin,fileno($client),1)=1;
259  
260 0 select(undef,my $rout=$rin,undef,0);
261  
262 0 ▶ if (vec($rout,fileno($client),1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (vec $rout, fileno $client, 1) 0 0 0
263 0 ▶ while ($outbuffer{$client}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outbuffer{$client}) 0 0 0
264 0 my $rv;
265  
266 0 eval{
267 0 $rv = $client->send($outbuffer{$client}, 0);
268 };
269 0 ▶ return if $@; #the $client is disconnected
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
270  
271 0 ▶ unless (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
272 # Whine, but move on.
273
274 0 warn "I was told I could write, but I can't.\n";
275 0 next;
276 }
277  
278 0 ▶ if ( $rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == length $outbuffer{$client} or $! == 11) { } 0 0 0
Condition0%0/3
$rv == length $outbuffer{$client} or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == length $outbuffer{$client} or $! == 11
$rv == length $outbuffer{$client} $! == 11
Truth table0%0/3
A or B
A= $rv == length $outbuffer{$client}B= $! == 11
ABresult
1X1
011
000
279 0 substr($outbuffer{$client}, 0, $rv) = '';
280 0 ▶ delete $outbuffer{$client} unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
281 } else {
282 # Couldn't write all the data
283  
284 0 ▶ substr($outbuffer{$client}, 0,$rv,'') if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 0 0 0
285 0 ▶ delete $outbuffer{$client} unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
286 }
287 }
288 }
289 }
290  
291 0 ▶sub close_client {
Subroutine close_client: not called CC=3 SCAR=24.8
Pod documented
292 0 my $self=shift;
293 0 my $client=shift;
294  
295 #print "Idle delete close_client $client\n";
296  
297 0 delete $alive{$client};
298 0 delete $turn_timeout{$client};
299 0 delete $turn_timeout_trigger{$client};
300 0 delete $idle{$client};
301 0 delete $inbuffer{$client};
302 0 delete $outbuffer{$client};
303 0 delete $ready{$client};
304 0 ▶ delete $map_client{$client} if exists $map_client{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_client{$client} 0 0 0
305  
306 0 $select->remove($client);
307 0 ▶ close $client if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 0 0 0
308 }
309  
310 0 ▶sub erase_client {
Subroutine erase_client: not called CC=3 SCAR=24.8
Pod documented
311 0 my $self=shift;
312 0 my $server_name=shift;
313 0 my $client=shift;
314  
315 0 delete $alive{$client};
316 0 delete $turn_timeout{$client};
317 0 delete $turn_timeout_trigger{$client};
318 0 delete $idle{$client};
319 0 delete $inbuffer{$client};
320 0 delete $outbuffer{$client};
321 0 delete $ready{$client};
322 0 ▶ delete $map_client{$client} if exists $map_client{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_client{$client} 0 0 0
323  
324 0 $self->{listen}->{$server_name}->{on_disconnected}->($self,$client,@{$self->{listen}->{$server_name}->{on_disconnected_param}});
325  
326 0 $select->remove($client);
327 0 ▶ close $client if $client;
Branch: 1 branch
BranchTrueFalseTotal
if $client 0 0 0
328 }
329  
330 0 ▶sub enqueue {
Subroutine enqueue: not called CC=3 SCAR=24.8
Pod documented
331 0 my $self=shift;
332 0 my $client=shift;
333 0 my $data=shift;
334  
335 0 ▶ return unless $client and $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $client and $data 0 0 0
Condition0%0/3
$client and $data
!ll&&!rl&&r
000
MC/DC0%0/2
$client and $data
$client $data
Truth table0%0/3
A and B
A= $clientB= $data
ABresult
0X0
100
111
336  
337 0 $outbuffer{$client}.=$data;
338 }
339  
340 0 ▶sub read_buffer {
Subroutine read_buffer: not called CC=2 SCAR=17.9
Pod undocumented
341 0 my $self=shift;
342 0 my $raw_input=shift;
343 0 my $cooked_input=shift;
344 0 my $server_name=shift;
345  
346 0 my $dm=$self->{listen}->{$server_name}->{delimiter};
347 0 my $sf=$self->{listen}->{$server_name}->{string_format};
348  
349 0 ▶ while ($$raw_input =~ s/($sf)$dm//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$raw_input =~ s/($sf)$dm//s) 0 0 0
350 0 push( @{$$cooked_input}, $1 );
351 }
352 }
353  
354 0 ▶sub start{
Subroutine start: not called CC=33 SCAR=70.2
Pod documented
355 0 my $self=shift;
356 0 my $current_time=time;
357  
358 0 foreach (keys %{$self->{listen}}) {
359 0 ▶ next unless $self->{listen}->{$_}->{local_port};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"listen"}{$_}{"local_port"} 0 0 0
360 warn "Listen on ".$self->{listen}->{$_}->{local_address}.":".
361 0 $self->{listen}->{$_}->{local_port}."\n";
362 0 $select->add($self->{listen}->{$_}->{socket});
363 }
364  
365 0 ▶ open(FILE,">".$self->{pidfile}) or die "Cannot write PID file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, '>' . $self->{"pidfile"} or die "Cannot write PID file: $!\n" 0 0 0
366 0 print FILE $$;
367 0 close(FILE);
368  
369 0 while (1) {
370 0 my $client;
371 my $rv;
372 0 my $data;
373  
374 # cron
375 0 my $this_time=time;
376 0 ▶ if ($current_time != $this_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_time != $this_time) 0 0 0
377 0 foreach $client($select->handles) {
378 0 ▶ next if exists $map_server{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_server{$client} 0 0 0
379 0 ▶ next unless exists $alive{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $alive{$client} 0 0 0
380  
381 0 ▶ if ($turn_timeout{$client} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn_timeout{$client} != -1) 0 0 0
382 0 ▶ if ($turn_timeout{$client} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turn_timeout{$client} <= 0) { } 0 0 0
383 0 &{$turn_timeout_trigger{$client}}($self,$client);
384 0 delete $turn_timeout_trigger{$client};
385 0 $turn_timeout{$client} = -1;
386 } else {
387 0 --$turn_timeout{$client};
388 }
389 }
390 }
391  
392 0 $self->onSheddo;
393 0 $current_time=$this_time;
394 }
395  
396 #timeout the Idles
397  
398 0 foreach $client ($select->handles) {
399 0 ▶ next if exists $map_server{$client};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map_server{$client} 0 0 0
400 0 ▶ next unless exists $alive{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $alive{$client} 0 0 0
401  
402 0 my $server_name=$self->get_server_name($client);
403  
404 0 my $this_time=time;
405 0 ▶ if( $this_time - $idle{$client} >= $self->{listen}->{$server_name}->{timeout} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($this_time - $idle{$client} >= $self->{"listen"}{$server_name}{"timeout"}) 0 0 0
406 0 $self->erase_client($server_name,$client);
407 0 next;
408 }
409 }
410 # check for new information on the connections we have
411  
412 # anything to read or accept?
413 0 foreach $client ($select->can_read(1)) {
414 0 ▶ if (exists $map_server{$client}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map_server{$client}) { } 0 0 0
415 0 my $server_name=$self->get_server_name($client);
416 # accept a new connection
417 0 $client = $self->{listen}->{$server_name}->{socket}->accept();
418 0 ▶ unless ($client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client) 0 0 0
419 0 warn "Accepting new socket error: $!\n";
420 0 next;
421 }
422  
423 0 $select->add($client);
424 0 $self->nonblock($client);
425  
426 0 $alive{$client}=1;
427 0 $self->{listen}->{$server_name}->{on_connected}->($self,$client,@{$self->{listen}->{$server_name}->{on_connected_param}});
428 0 $idle{$client}=time;
429 0 $turn_timeout{$client}=-1;
430 } else {
431 0 ▶ next unless exists $alive{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $alive{$client} 0 0 0
432 0 my $server_name=$self->get_server_name($client);
433 # read data
434  
435 0 $data = '';
436 0 $rv = $client->recv($data, POSIX::BUFSIZ, 0);
437
438 0 ▶ unless (defined($rv) && length $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv and length $data) 0 0 0
Condition0%0/3
defined $rv and length $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rv and length $data
defined $rv length $data
Truth table0%0/3
A and B
A= defined $rvB= length $data
ABresult
0X0
100
111
439 # This would be the end of file, so close the client
440 0 $self->erase_client($server_name,$client);
441 0 next;
442 }
443  
444 0 $inbuffer{$client} .= $data;
445 0 $self->{listen}->{$server_name}->{read_buffer}->($self,\$inbuffer{$client},\$ready{$client},$server_name);
446  
447 0 $idle{$client}=time;
448 }
449 }
450  
451 # Any complete requests to process?
452 0 foreach $client (keys %ready) {
453 0 my $server_name=$self->get_server_name($client);
454 0 $self->handle($server_name,$client);
455 }
456  
457 0 my @bad_client;
458  
459 # Buffers to flush?
460 0 foreach $client ($select->can_write(1)) {
461 0 ▶ next unless exists $alive{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $alive{$client} 0 0 0
462  
463 0 my $server_name=$self->get_server_name($client);
464
465 # Skip this client if we have nothing to say
466 0 ▶ next unless exists $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $outbuffer{$client} 0 0 0
467
468 0 eval{
469 0 $rv = $client->send($outbuffer{$client}, 0);
470 };
471 0 ▶ push(@bad_client,$client),next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
472
473 0 ▶ unless (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
474 # Whine, but move on.
475
476 0 warn "I was told I could write, but I can't.\n";
477 0 next;
478 }
479  
480 0 ▶ if ( $rv == length $outbuffer{$client} || $! == POSIX::EWOULDBLOCK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == length $outbuffer{$client} or $! == 11) { } 0 0 0
Condition0%0/3
$rv == length $outbuffer{$client} or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$rv == length $outbuffer{$client} or $! == 11
$rv == length $outbuffer{$client} $! == 11
Truth table0%0/3
A or B
A= $rv == length $outbuffer{$client}B= $! == 11
ABresult
1X1
011
000
481 0 substr($outbuffer{$client}, 0, $rv) = '';
482 0 ▶ delete $outbuffer{$client} unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
483 } else {
484 # Couldn't write all the data
485  
486 0 ▶ substr($outbuffer{$client}, 0,$rv,'') if defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 0 0 0
487 0 ▶ delete $outbuffer{$client} unless length $outbuffer{$client};
Branch: 1 branch
BranchTrueFalseTotal
unless length $outbuffer{$client} 0 0 0
488 0 next;
489 }
490 }
491  
492 0 foreach $client (@bad_client){
493 0 my $server_name=$self->get_server_name($client);
494 0 $self->erase_client($server_name,$client);
495 }
496  
497 # Out of band data?
498 0 foreach $client ($select->has_exception(0)) {
499 # arg is timeout
500 # Deal with out-of-band data here, if you want to.
501 }
502 }
503  
504 }
505  
506 0 ▶sub onSheddo{
Subroutine onSheddo: not called CC=3 SCAR=24.8
Pod undocumented
507 0 my $self=shift;
508  
509 0 foreach (sort {$a <=> $b} keys %timer) {
510 0 ▶ unless ($cronCounter % $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cronCounter % $_) 0 0 0
511 0 my $count=@{$timer{$_}};
512 0 &{$timer{$_}->[0]}($self,@{$timer{$_}}[1..($count-1)]);
513 }
514 }
515  
516 0 ++$cronCounter;
517 }
518  
519 0 ▶sub cron {
Subroutine cron: not called CC=1 SCAR=6.9
Pod documented
520 0 my $self=shift;
521 0 my $sec=shift;
522 0 my $sub=shift;
523  
524 0 $timer{$sec}=[$sub,@_];
525 }
526  
527 0 ▶sub select {
Subroutine select: not called CC=1 SCAR=6.9
Pod undocumented
528 0 my $self=shift;
529  
530 0 $select;
531 }
532  
533 1 1;
534  
535 __END__
↑ Summary