Server/Connection.pm

filter: Statementstmt 80.3% 94 / 117 Branchbran 52.9% 36 / 68 Conditioncond 25.0% 4 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 55.1% 16 / 29 Podpod 100% 13 / 13 totaltotal 64.4% 163 / 253 SCAR 46.8
CC
42
Cov
67%
nodelay
20.0
recv
17.2
ip
12.0
CRAP 107.3
« Server.pm ↑ Summary
1 package Net::WebSocket::Server::Connection;
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 Protocol::WebSocket::Handshake::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Socket qw(IPPROTO_TCP TCP_NODELAY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 1 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=20.1
Pod documented
15  
16 1 my %params = @_;
17  
18 my $self = {
19 socket => undef,
20 server => undef,
21 nodelay => 1,
22 1 max_send_size => eval { Protocol::WebSocket::Frame->new->{max_payload_size} } || 65536,
23 ▶ on_handshake => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
24 ▶ on_ready => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
25 ▶ on_disconnect => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
26 ▶ on_utf8 => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
27 ▶ on_pong => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
28 ▶ on_binary => sub{},
Subroutine __ANON__: not called CC=1 SCAR=0.0
29 1 ▶ };
Condition50%1/2
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
l!l
10
MC/DC0%0/1
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } } || 65536
eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Truth table50%1/2
A
A= eval { do { 'Protocol::WebSocket::Frame'->new->{'max_payload_size'} } }
Aresult
11
00
30  
31 1 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 2 1 3
32 2 ▶ croak "Invalid $class parameter '$key'" unless exists $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$key} 0 2 2
33 2 ▶ croak "$class parameter '$key' expects a coderef" if ref $self->{$key} eq 'CODE' && ref $value ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$key} eq "CODE" and ref $value ne "CODE" 0 2 2
Condition33%1/3
ref $self->{$key} eq "CODE" and ref $value ne "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
ref $self->{$key} eq "CODE" and ref $value ne "CODE"
ref $self->{$key} eq "CODE" ref $value ne "CODE"
Truth table33%1/3
A and B
A= ref $self->{$key} eq "CODE"B= ref $value ne "CODE"
ABresult
0X0
100
111
34 2 $self->{$key} = $value;
35 }
36  
37 1 croak "$class construction requires '$_'" for grep { !defined $self->{$_} } qw(socket server);
38  
39 1 $self->{handshake} = new Protocol::WebSocket::Handshake::Server();
40 1 $self->{disconnecting} = 0;
41  
42 1 bless $self, $class;
43 }
44  
45 sub on {
46 1 ▶ my $self = shift;
Subroutine on: called CC=4 SCAR=14.0
Pod documented
47 1 my %params = @_;
48  
49 1 ▶ while (my ($key, $value) = each %params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %params) 6 1 7
50 6 ▶ croak "Invalid event '$key'" unless exists $self->{"on_$key"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$self{"on_$key"} 0 6 6
51 6 ▶ croak "Expected a coderef for event '$key'" unless ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "CODE" 0 6 6
52 6 $self->{"on_$key"} = $value;
53 }
54 }
55  
56  
57 ### accessors
58  
59 0 ▶sub server { $_[0]->{server} }
Subroutine server: not called CC=1 SCAR=6.9
Pod documented
60  
61 0 ▶sub socket { $_[0]->{socket} }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
62  
63 sub ip {
64 0 ▶ my $sock = $_[0]->{socket};
Subroutine ip: not called CC=3 SCAR=24.8
Pod documented
65 0 ▶ return $sock && $sock->connected ? $sock->peerhost : "0.0.0.0";
Branch: 1 branch
BranchTrueFalseTotal
$sock && $sock->connected ? : 0 0 0
Condition0%0/3
$sock and $sock->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$sock and $sock->connected
$sock $sock->connected
Truth table0%0/3
A and B
A= $sockB= $sock->connected
ABresult
0X0
100
111
66 }
67  
68 sub port {
69 0 ▶ my $sock = $_[0]->{socket};
Subroutine port: not called CC=3 SCAR=24.8
Pod documented
70 0 ▶ return $sock && $sock->connected ? $sock->peerport : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sock && $sock->connected ? : 0 0 0
Condition0%0/3
$sock and $sock->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$sock and $sock->connected
$sock $sock->connected
Truth table0%0/3
A and B
A= $sockB= $sock->connected
ABresult
0X0
100
111
71 }
72  
73 sub nodelay {
74 0 ▶ my $self = shift;
Subroutine nodelay: not called CC=4 SCAR=30.0
Pod documented
75 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
76 0 $self->{nodelay} = $_[0];
77 0 ▶ setsockopt($self->{socket}, IPPROTO_TCP, TCP_NODELAY, $self->{nodelay} ? 1 : 0) unless $self->{handshake};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"handshake"} 0 0 0
$self->{'nodelay'} ? : 0 0 0
78 }
79 0 return $self->{nodelay};
80 }
81  
82 sub max_send_size {
83 0 ▶ my $self = shift;
Subroutine max_send_size: not called CC=2 SCAR=17.9
Pod documented
84 0 ▶ $self->{max_send_size} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
85 0 return $self->{max_send_size};
86 }
87  
88  
89 ### methods
90  
91 sub disconnect {
92 2 ▶ my ($self, $code, $reason) = @_;
Subroutine disconnect: called CC=7 SCAR=20.5
Pod documented
93 2 ▶ return if $self->{disconnecting};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disconnecting"} 1 1 2
94 1 $self->{disconnecting} = 1;
95  
96 1 $self->_event('on_disconnect', $code, $reason);
97  
98 1 my $data = '';
99 1 ▶ if (defined $code || defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code or defined $reason) 1 0 1
Condition33%1/3
defined $code or defined $reason
l!l&&r!l&&!r
100
MC/DC0%0/2
defined $code or defined $reason
defined $code defined $reason
Truth table33%1/3
A or B
A= defined $codeB= defined $reason
ABresult
1X1
011
000
100 1 ▶ $code ||= 1000;
Condition50%1/2
$code ||= 1000
l!l
10
MC/DC0%0/1
$code ||= 1000
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
101 1 ▶ $reason = '' unless defined $reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reason 0 1 1
102 1 $data = pack("na*", $code, $reason);
103 }
104 1 ▶ $self->send(close => $data) unless $self->{handshake};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handshake"} 1 0 1
105  
106 1 $self->{server}->disconnect($self->{socket});
107 }
108  
109 sub send_binary {
110 14 ▶ $_[0]->send(binary => $_[1]);
Subroutine send_binary: called CC=1 SCAR=0.0
Pod documented
111 }
112  
113 sub send_utf8 {
114 5 ▶ $_[0]->send(text => Encode::encode('UTF-8', $_[1]));
Subroutine send_utf8: called CC=1 SCAR=0.0
Pod documented
115 }
116  
117 sub send {
118 20 ▶ my ($self, $type, $data) = @_;
Subroutine send: called CC=5 SCAR=19.8
Pod documented
119  
120 20 ▶ if ($self->{handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handshake"}) 0 20 20
121 0 carp "tried to send data before finishing handshake";
122 0 return 0;
123 }
124  
125 20 my $frame = new Protocol::WebSocket::Frame(type => $type, max_payload_size => $self->{max_send_size});
126 20 ▶ $frame->append($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 20 0 20
127  
128 20 my $bytes = eval { $frame->to_bytes };
129 20 ▶ if (!defined $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bytes) 0 20 20
130 0 ▶ carp "error while building message: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
131 0 return;
132 }
133  
134 20 syswrite($self->{socket}, $bytes);
135 }
136  
137 sub recv {
138 19 ▶ my ($self) = @_;
Subroutine recv: called CC=15 SCAR=28.4
Pod documented
139  
140 19 my ($len, $data) = (0, "");
141 19 ▶ if (!($len = sysread($self->{socket}, $data, 8192))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($len = sysread($self->{"socket"}, $data, 8192)) 0 19 19
142 0 $self->disconnect();
143 0 return;
144 }
145  
146 # read remaining data
147 19 ▶ $len = sysread($self->{socket}, $data, 8192, length($data)) while $len >= 8192;
Branch: 1 branch
BranchTrueFalseTotal
$len >= 8192 and do { $len = sysread($self->{"socket"}, $data, 8192, length $data) } 12 19 31
148  
149 19 ▶ if ($self->{handshake}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handshake"}) 1 18 19
150 1 $self->{handshake}->parse($data);
151 1 ▶ if ($self->{handshake}->error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'handshake'}->error) { } 0 1 1
elsif ($self->{'handshake'}->is_done) { } 1 0 1
152 0 $self->disconnect(1002);
153 } elsif ($self->{handshake}->is_done) {
154 1 $self->_event(on_handshake => $self->{handshake});
155 1 ▶ return unless do { local $SIG{__WARN__} = sub{}; $self->{socket}->connected };
Subroutine __ANON__: not called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
unless do { local $SIG{"__WARN__"} = sub { } ; $self->{"socket"}->connected } 0 1 1
156  
157 1 syswrite($self->{socket}, $self->{handshake}->to_string);
158 1 delete $self->{handshake};
159  
160 1 $self->{parser} = new Protocol::WebSocket::Frame();
161 1 ▶ setsockopt($self->{socket}, IPPROTO_TCP, TCP_NODELAY, 1) if $self->{nodelay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nodelay"} 1 0 1
162 1 $self->_event('on_ready');
163 }
164 1 return;
165 }
166  
167 18 $self->{parser}->append($data);
168  
169 18 my $bytes;
170 18 ▶ while (defined ($bytes = eval { $self->{parser}->next_bytes })) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($bytes = eval { do { $self->{"parser"}->next_bytes } })) 18 17 35
171 18 ▶ if ($self->{parser}->is_binary) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'parser'}->is_binary) { } 6 12 18
elsif ($self->{'parser'}->is_text) { } 5 7 12
elsif ($self->{'parser'}->is_pong) { } 6 1 7
elsif ($self->{'parser'}->is_close) { } 1 0 1
172 6 $self->_event(on_binary => $bytes);
173 } elsif ($self->{parser}->is_text) {
174 5 $self->_event(on_utf8 => Encode::decode('UTF-8', $bytes));
175 } elsif ($self->{parser}->is_pong) {
176 6 $self->_event(on_pong => $bytes);
177 } elsif ($self->{parser}->is_close) {
178 1 ▶ $self->disconnect(length $bytes ? unpack("na*",$bytes) : ());
Branch: 1 branch
BranchTrueFalseTotal
length $bytes ? : 1 0 1
179 1 return;
180 }
181 }
182  
183 17 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 17 17
184 0 $self->disconnect(1002);
185 0 return;
186 }
187 }
188  
189 ### internal methods
190  
191 sub _event {
192 20 ▶ my ($self, $event, @args) = @_;
Subroutine _event: called CC=1 SCAR=0.0
193 20 $self->{$event}($self, @args);
194 }
195  
196 3 1; # End of Net::WebSocket::Server
197  
198 __END__
« Server.pm ↑ Summary