Server.pm

filter: Statementstmt 76.0% 114 / 150 Branchbran 46.8% 30 / 64 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.0% 19 / 26 Podpod 100% 12 / 12 totaltotal 65.9% 178 / 270 CC 43 SCAR 31.3
CC
43
Cov
65%
start
55.4
on
20.0
new
7.1
CRAP 22.9
↑ Summary Server/Connection.pm »
1 package Net::WebSocket::Server;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Net::WebSocket::Server::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = '0.004000';
15 3 $VERSION = eval $VERSION;
16  
17 3 $SIG{PIPE} = 'IGNORE';
18  
19 1 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
20 1 my $class = shift;
21  
22 1 my %params = @_;
23  
24 my $self = {
25 listen => 80,
26 silence_max => 20,
27 tick_period => 0,
28 watch_readable => [],
29 watch_writable => [],
30 0 ▶ on_connect => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
31 0 ▶ on_tick => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
32 1 ▶ on_shutdown => sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 };
34  
35 1 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 4 1 5
36 4 ▶ croak "Invalid $class parameter '$key'" unless exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 0 4 4
37 4 ▶ croak "$class parameter '$key' expected type is ".ref($self->{$key}) if ref $self->{$key} && ref $value ne ref $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$key} and ref $value ne ref $self->{$key} 0 4 4
Condition66%2/3
ref $self->{$key} and ref $value ne ref $self->{$key}
!ll&&!rl&&r
130
MC/DC0%0/2
ref $self->{$key} and ref $value ne ref $self->{$key}
ref $self->{$key} ref $value ne ref $self->{$key}
Truth table66%2/3
A and B
A= ref $self->{$key}B= ref $value ne ref $self->{$key}
ABresult
0X0
100
111
38 4 $self->{$key} = $value;
39 }
40  
41 1 bless $self, $class;
42  
43 # send a ping every silence_max by checking whether data was received in the last silence_max/2
44 1 $self->{silence_checkinterval} = $self->{silence_max} / 2;
45  
46 1 foreach my $watchtype (qw(readable writable)) {
47 2 $self->{"select_$watchtype"} = IO::Select->new();
48 2 my $key = "watch_$watchtype";
49 2 ▶ croak "$class parameter '$key' expects an arrayref containing an even number of elements" unless @{$self->{$key}} % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{$key};} % 2 == 0 0 2 2
50 2 my @watch = @{$self->{$key}};
51 2 $self->{$key} = {};
52 2 $self->_watch($watchtype, @watch);
53 }
54  
55 1 return $self;
56 }
57  
58 0 ▶sub watch_readable {
Subroutine watch_readable: not called CC=2 SCAR=17.9
Pod documented
59 0 my $self = shift;
60 0 ▶ croak "watch_readable expects an even number of arguments" unless @_ % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
61 0 $self->_watch(readable => @_);
62 }
63  
64 0 ▶sub watched_readable {
Subroutine watched_readable: not called CC=2 SCAR=17.9
Pod documented
65 0 my $self = shift;
66 0 ▶ return $self->{watch_readable}{$_[0]}{cb} if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
67 0 return map {$_->{fh}, $_->{cb}} values %{$self->{watch_readable}};
68 }
69  
70 0 ▶sub watch_writable {
Subroutine watch_writable: not called CC=2 SCAR=17.9
Pod documented
71 0 my $self = shift;
72 0 ▶ croak "watch_writable expects an even number of arguments" unless @_ % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
73 0 $self->_watch(writable => @_);
74 }
75  
76 0 ▶sub watched_writable {
Subroutine watched_writable: not called CC=2 SCAR=17.9
Pod documented
77 0 my $self = shift;
78 0 ▶ return $self->{watch_writable}{$_[0]}{cb} if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
79 0 return map {$_->{fh}, $_->{cb}} values %{$self->{watch_writable}};
80 }
81  
82 2 ▶sub _watch {
Subroutine _watch: called CC=5 SCAR=17.0
83 2 my $self = shift;
84 2 my $watchtype = shift;
85 2 ▶ croak "watch_$watchtype expects an even number of arguments after the type" unless @_ % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 2 2
86 2 ▶ for (my $i = 0; $i < @_; $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 2 2 4
87 2 my ($fh, $cb) = ($_[$i], $_[$i+1]);
88 2 ▶ croak "watch_$watchtype expects the second value of each pair to be a coderef, but element $i was not" unless ref $cb eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cb eq "CODE" 0 2 2
89 2 ▶ if ($self->{"watch_$watchtype"}{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"watch_$watchtype"}{$fh}) 0 2 2
90 0 carp "watch_$watchtype was given a filehandle at index $i which is already being watched; ignoring!";
91 0 next;
92 }
93 2 $self->{"select_$watchtype"}->add($fh);
94 2 $self->{"watch_$watchtype"}{$fh} = {fh=>$fh, cb=>$cb};
95 }
96 }
97  
98 1 ▶sub unwatch_readable {
Subroutine unwatch_readable: called CC=1 SCAR=0.0
Pod documented
99 1 my $self = shift;
100 1 $self->_unwatch(readable => @_);
101 }
102  
103 1 ▶sub unwatch_writable {
Subroutine unwatch_writable: called CC=1 SCAR=0.0
Pod documented
104 1 my $self = shift;
105 1 $self->_unwatch(writable => @_);
106 }
107  
108 2 ▶sub _unwatch {
Subroutine _unwatch: called CC=2 SCAR=6.9
109 2 my $self = shift;
110 2 my $watchtype = shift;
111 2 foreach my $fh (@_) {
112 2 $self->{"select_$watchtype"}->remove($fh);
113 2 delete $self->{"watch_$watchtype"}{$fh};
114 }
115 }
116  
117 0 ▶sub on {
Subroutine on: not called CC=4 SCAR=30.0
Pod documented
118 0 my $self = shift;
119 0 my %params = @_;
120  
121 0 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 0 0 0
122 0 ▶ croak "Invalid event '$key'" unless exists $self->{"on_$key"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self{"on_$key"} 0 0 0
123 0 ▶ croak "Expected a coderef for event '$key'" unless ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 0 0
124 0 $self->{"on_$key"} = $value;
125 }
126 }
127  
128 1 ▶sub start {
Subroutine start: called CC=24 SCAR=40.2
Pod documented
129 1 my $self = shift;
130  
131 1 ▶ if (ref $self->{listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'listen'}) { } 1 0 1
132 # if we got a server, make sure it's valid by clearing errors and checking errors anyway; if there's still an error, it's closed
133 1 $self->{listen}->clearerr;
134 croak "failed to start websocket server; the TCP server provided via 'listen' is invalid. (is the listening socket is closed? are you trying to reuse a server that has already shut down?)"
135 1 ▶ if $self->{listen}->error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"listen"}->error 0 1 1
136 } else {
137 # if we merely got a port, set up a reasonable default tcp server
138 $self->{listen} = IO::Socket::INET->new(
139 Listen => 5,
140 LocalPort => $self->{listen},
141 0 ▶ Proto => 'tcp',
Condition0%0/2
'IO::Socket::INET'->new('Listen', 5, 'LocalPort', $self->{'listen'}, 'Proto', 'tcp', 'ReuseAddr', 1) || croak("failed to listen on port $self->{'listen'}: $!")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('Listen', 5, 'LocalPort', $self->{'listen'}, 'Proto', 'tcp', 'ReuseAddr', 1) || croak("failed to listen on port $self->{'listen'}: $!")
'IO::Socket::INET'->new('Listen', 5, 'LocalPort', $self->{'listen'}, 'Proto', 'tcp', 'ReuseAddr', 1)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Listen', 5, 'LocalPort', $self->{'listen'}, 'Proto', 'tcp', 'ReuseAddr', 1)
Aresult
11
00
142 ReuseAddr => 1,
143 ) || croak "failed to listen on port $self->{listen}: $!";
144 }
145  
146 1 $self->{select_readable}->add($self->{listen});
147  
148 1 $self->{conns} = {};
149 1 ▶ my $silence_nextcheck = $self->{silence_max} ? (time + $self->{silence_checkinterval}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'silence_max'} ? : 1 0 1
150 1 ▶ my $tick_next = $self->{tick_period} ? (time + $self->{tick_period}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tick_period'} ? : 0 1 1
151  
152 1 ▶ while ($self->{listen}->opened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"listen"}->opened) 22 1 23
153 22 ▶ my $silence_checktimeout = $self->{silence_max} ? ($silence_nextcheck - time) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'silence_max'} ? : 22 0 22
154 22 ▶ my $tick_timeout = $self->{tick_period} ? ($tick_next - time) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'tick_period'} ? : 0 22 22
155 22 my $timeout = min(grep {defined} ($silence_checktimeout, $tick_timeout));
156  
157 22 my ($ready_read, $ready_write, undef) = IO::Select->select($self->{select_readable}, $self->{select_writable}, undef, $timeout);
158 22 ▶ foreach my $fh ($ready_read ? @$ready_read : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$ready_read ? : 22 0 22
159 21 ▶ if ($fh == $self->{listen}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($fh == $self->{'listen'}) { } 1 20 21
elsif ($self->{'watch_readable'}{$fh}) { } 1 19 20
elsif ($self->{'conns'}{$fh}) { } 19 0 19
160 1 my $sock = $self->{listen}->accept;
161 1 ▶ next unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 1 1
162 1 my $conn = new Net::WebSocket::Server::Connection(socket => $sock, server => $self);
163 1 $self->{conns}{$sock} = {conn=>$conn, lastrecv=>time};
164 1 $self->{select_readable}->add($sock);
165 1 $self->{on_connect}($self, $conn);
166 } elsif ($self->{watch_readable}{$fh}) {
167 1 $self->{watch_readable}{$fh}{cb}($self, $fh);
168 } elsif ($self->{conns}{$fh}) {
169 19 my $connmeta = $self->{conns}{$fh};
170 19 $connmeta->{lastrecv} = time;
171 19 $connmeta->{conn}->recv();
172 } else {
173 0 warn "filehandle $fh became readable, but no handler took responsibility for it; removing it";
174 0 $self->{select_readable}->remove($fh);
175 }
176 }
177  
178 22 ▶ foreach my $fh ($ready_write ? @$ready_write : ()) {
Branch: 1 branch
BranchTrueFalseTotal
$ready_write ? : 22 0 22
179 1 ▶ if ($self->{watch_writable}{$fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'watch_writable'}{$fh}) { } 1 0 1
180 1 $self->{watch_writable}{$fh}{cb}($self, $fh);
181 } else {
182 0 warn "filehandle $fh became writable, but no handler took responsibility for it; removing it";
183 0 $self->{select_writable}->remove($fh);
184 }
185 }
186  
187 22 ▶ if ($self->{silence_max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"silence_max"}) 22 0 22
188 22 my $now = time;
189 22 ▶ if ($silence_nextcheck < $now) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silence_nextcheck < $now) 0 22 22
190 0 my $lastcheck = $silence_nextcheck - $self->{silence_checkinterval};
191 0 ▶ $_->{conn}->send('ping') for grep { $_->{conn}->is_ready && $_->{lastrecv} < $lastcheck } values %{$self->{conns}};
Condition0%0/3
$_->{'conn'}->is_ready && $_->{'lastrecv'} < $lastcheck
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'conn'}->is_ready && $_->{'lastrecv'} < $lastcheck
$_->{'conn'}->is_ready $_->{'lastrecv'} < $lastcheck
Truth table0%0/3
A && B
A= $_->{'conn'}->is_readyB= $_->{'lastrecv'} < $lastcheck
ABresult
0X0
100
111
192  
193 0 $silence_nextcheck = $now + $self->{silence_checkinterval};
194 }
195 }
196  
197 22 ▶ if ($self->{tick_period} && $tick_next < time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tick_period"} and $tick_next < &Net::WebSocket::Server::time()) 0 22 22
Condition33%1/3
$self->{"tick_period"} and $tick_next < &Net::WebSocket::Server::time()
!ll&&!rl&&r
2200
MC/DC0%0/2
$self->{"tick_period"} and $tick_next < &Net::WebSocket::Server::time()
$self->{"tick_period"} $tick_next < &Net::WebSocket::Server::time()
Truth table33%1/3
A and B
A= $self->{"tick_period"}B= $tick_next < &Net::WebSocket::Server::time()
ABresult
0X0
100
111
198 0 $self->{on_tick}($self);
199 0 $tick_next += $self->{tick_period};
200 }
201 }
202 }
203  
204 2 ▶sub connections { grep {$_->is_ready} map {$_->{conn}} values %{$_[0]{conns}} }
Subroutine connections: called CC=1 SCAR=0.0
Pod documented
205  
206 1 ▶sub shutdown {
Subroutine shutdown: called CC=2 SCAR=6.9
Pod documented
207 1 my ($self) = @_;
208 1 $self->{on_shutdown}($self);
209 1 $self->{select_readable}->remove($self->{listen});
210 1 $self->{listen}->shutdown(2);
211 1 $self->{listen}->close();
212 1 $_->disconnect(1001) for $self->connections;
213 }
214  
215 1 ▶sub disconnect {
Subroutine disconnect: called CC=1 SCAR=0.0
Pod documented
216 1 my ($self, $fh) = @_;
217 1 $self->{select_readable}->remove($fh);
218 1 $fh->close();
219 1 delete $self->{conns}{$fh};
220 }
221  
222 3 1; # End of Net::WebSocket::Server
223  
224 __END__
↑ Summary Server/Connection.pm »