blib/lib/Discord/Client/Gateway/WebSocket.pm

filter: Statementstmt 81.6% 89 / 109 Branchbran 23.8% 10 / 42 Conditioncond 25.0% 5 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 71.8% 23 / 32 Podpod 0.0% 0 / 5 totaltotal 58.5% 127 / 217 CC 30 SCAR 17.7
CC
30
Cov
61%
connect
14.1
__ANON__
12.0
__ANON__
12.0
CRAP 5.9
« blib/lib/Discord/Client/Gateway/Shards.pm ↑ Summary blib/lib/Discord/Client/Intents.pm »
1 package Discord::Client::Gateway::WebSocket;
2  
3 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Net::Async::WebSocket::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 our $VERSION = '0.002';
14  
15 3 ▶sub new {
Subroutine new: called CC=1 SCAR=3.3
Pod undocumented
16 3 my ( $class, %args ) = @_;
17 3 my $self = bless { %args, closed => 0 }, $class;
18  
19 # The adapter owns its socket; callbacks must not create an ownership cycle.
20 3 my $weak = $self;
21 3 weaken($weak);
22 my $client = Discord::Client::Gateway::WebSocket::Client->new(
23 0 ▶ on_parser_error => sub { $weak->_notify(4002) if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
24 9 ▶ on_text_frame => sub { $weak->{on_text}->( $_[1] ) if $weak && !$weak->{closed} },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak and not $weak->{"closed"} 9 0 9
Condition33%1/3
$weak and not $weak->{"closed"}
!ll&&!rl&&r
009
MC/DC0%0/2
$weak and not $weak->{"closed"}
$weak not $weak->{"closed"}
Truth table33%1/3
A and not(B)
A= $weakB= not $weak->{"closed"}
ABresult
0X0
100
111
25 0 ▶ on_binary_frame => sub { $weak->_notify(4002) if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
26 0 ▶ on_close_frame => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
27 0 my $bytes = $_[1];
28 0 ▶ $weak->_notify( length($bytes) >= 2 ? unpack( 'n', $bytes ) : 1005 ) if $weak;
Branch: 2 branches
BranchTrueFalseTotal
if $weak 0 0 0
length $bytes >= 2 ? : 0 0 0
29 },
30 0 ▶ on_ping_frame => sub { $_[0]->send_pong_frame( $_[1] ) if $weak && !$weak->{closed} },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $weak and not $weak->{"closed"} 0 0 0
Condition0%0/3
$weak and not $weak->{"closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
$weak and not $weak->{"closed"}
$weak not $weak->{"closed"}
Truth table0%0/3
A and not(B)
A= $weakB= not $weak->{"closed"}
ABresult
0X0
100
111
31 0 ▶ on_closed => sub { $weak->_notify(1006) if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
32 0 ▶ on_read_error => sub { $weak->_notify(1006) if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
33 0 ▶ on_write_error => sub { $weak->_notify(1006) if $weak },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $weak 0 0 0
34 3 );
35 3 $self->{client} = $client;
36 3 $args{loop}->add($client);
37 3 return $self;
38 }
39  
40 3 ▶sub connect {
Subroutine connect: called CC=7 SCAR=26.5
Pod undocumented
41 3 my ( $self, $url ) = @_;
42  
43 # Require TLS in production and force peer and hostname verification.
44 3 my $uri = URI->new($url);
45 croak 'Gateway URL must use WSS'
46 unless ( $uri->scheme // '' ) eq 'wss'
47 3 ▶ || ( $self->{allow_insecure} && ( $uri->scheme // '' ) eq 'ws' );
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme // "") eq "wss" or $self->{"allow_insecure"} and ($uri->scheme // "") eq "ws" 0 3 3
Condition20%2/10
($uri->scheme // "") eq "wss" or $self->{"allow_insecure"} and ($uri->scheme // "") eq "ws"
l!l&&r!l&&!r
300
$uri->scheme // ""
l!l
30
$self->{"allow_insecure"} and ($uri->scheme // "") eq "ws"
!ll&&!rl&&r
000
$uri->scheme // ""
l!l
00
MC/DC0%0/3
($uri->scheme // "") eq "wss" or $self->{"allow_insecure"} and ($uri->scheme // "") eq "ws"
$uri->scheme $self->{"allow_insecure"} $uri->scheme
Truth table25%1/4
A or B and C
A= $uri->schemeB= $self->{"allow_insecure"}C= $uri->scheme
ABCresult
1XX1
0111
00X0
0100
48 return $self->{client}->connect(
49 url => $uri,
50 3 ▶ %{ $self->{tls} // {} },
Condition50%1/2
$self->{"tls"} // {}
l!l
30
MC/DC0%0/1
$self->{"tls"} // {}
$self->{"tls"}
Truth table50%1/2
A
A= $self->{"tls"}
Aresult
11
00
51 SSL_verify_mode => 1,
52 SSL_verifycn_scheme => 'http'
53 );
54 }
55  
56 5 ▶sub send { $_[0]->{client}->send_text_frame( $_[1] ) }
Subroutine send: called CC=1 SCAR=0.0
Pod undocumented
57  
58 0 ▶sub _notify {
Subroutine _notify: not called CC=2 SCAR=17.9
59 0 my ( $self, $code ) = @_;
60  
61 # Notify the driver once even if read, write and close events arrive together.
62 0 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
63 0 $self->{closed} = 1;
64 0 $self->{on_close}->($code);
65 0 $self->_dispose;
66 }
67  
68 3 ▶sub _dispose {
Subroutine _dispose: called CC=1 SCAR=0.0
69 3 my ($self) = @_;
70  
71 # Disconnect callbacks before closing the stream to prevent recursive notification.
72 $self->{client}->configure(
73 3 map { $_ => undef }
74 qw(on_text_frame on_binary_frame on_close_frame
75 on_ping_frame on_closed on_read_error on_write_error on_read on_parser_error)
76 );
77 3 $self->{client}->close;
78 }
79  
80 1 ▶sub abort {
Subroutine abort: called CC=1 SCAR=0.0
Pod undocumented
81 1 my ($self) = @_;
82  
83 # Abort immediately without waiting for a close frame to reach the peer.
84 1 $self->{closed} = 1;
85 1 $self->_dispose;
86 1 return $self->{loop}->new_future->done;
87 }
88  
89 2 ▶sub close {
Subroutine close: called CC=4 SCAR=14.5
Pod undocumented
90 2 my ( $self, $code ) = @_;
91  
92 # Without a connected write handle there is no close frame to flush.
93 2 ▶ return $self->abort unless $self->{client}->write_handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"client"}->write_handle 0 2 2
94 2 $self->{closed} = 1;
95 2 ▶ my $write = eval { $self->{client}->send_close_frame( pack( 'n', $code // 1000 ) ) };
Condition50%1/2
$code // 1000
l!l
20
MC/DC0%0/1
$code // 1000
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
96 2 ▶ return $self->abort unless $write;
Branch: 1 branch
BranchTrueFalseTotal
unless $write 0 2 2
97  
98 # Bound graceful shutdown even if the peer stops reading close frames.
99 2 my $timeout = $self->{loop}->delay_future( after => 1 );
100 2 my $wait = Future->wait_any( $write, $timeout );
101 2 my $result = $self->{loop}->new_future;
102 $wait->on_ready(
103 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
104 2 $self->_dispose;
105 2 ▶ $result->done unless $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->is_ready 2 0 2
106 }
107 2 );
108  
109 # Cancellation still disposes the socket and its remaining waiters.
110 $result->on_cancel(
111 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 $wait->cancel;
113 0 $self->_dispose;
114 }
115 2 );
116 2 return $result;
117 }
118  
119 package Discord::Client::Gateway::WebSocket::Client;
120  
121 11 ▶use parent 'Net::Async::WebSocket::Client';
Subroutine BEGIN: called CC=1 SCAR=0.0
122  
123 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
124 3 my ($self) = @_;
125 3 $self->SUPER::_init;
126  
127 # Bound inbound frames before they can accumulate unbounded payload bytes.
128 3 $self->{framebuffer} = Protocol::WebSocket::Frame->new( max_payload_size => 16 * 1024 * 1024 );
129 }
130  
131 15 ▶sub configure {
Subroutine configure: called CC=2 SCAR=6.9
132 15 my ( $self, %args ) = @_;
133 15 ▶ $self->{on_parser_error} = delete $args{on_parser_error} if exists $args{on_parser_error};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"on_parser_error"} 6 9 15
134 15 $self->SUPER::configure(%args);
135 }
136  
137 15 ▶sub on_read {
Subroutine on_read: called CC=3 SCAR=13.5
138 15 my ( $self, @args ) = @_;
139  
140 # Translate parser exceptions into a protocol close instead of escaping the loop.
141 15 my $result = eval { $self->SUPER::on_read(@args) };
142 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
143 0 ▶ $self->{on_parser_error}->() if $self->{on_parser_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_parser_error"} 0 0 0
144 0 return 0;
145 }
146 15 return $result;
147 }
148  
149 3 ▶sub _do_handshake {
Subroutine _do_handshake: called CC=1 SCAR=0.8
150 3 my ( $self, @args ) = @_;
151  
152 # Watch the HTTP upgrade parser separately from the established frame reader.
153 3 my $future = $self->SUPER::_do_handshake(@args);
154 3 my $reader = $self->can_event('on_read');
155 $self->SUPER::configure(
156 3 ▶ on_read => sub {
Subroutine __ANON__: called CC=5 SCAR=22.5
157 3 my @read_args = @_;
158 3 my $result = eval { $reader->(@read_args) };
159 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
160 0 ▶ $future->fail( 'Invalid WebSocket handshake', 'protocol' ) unless $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $future->is_ready 0 0 0
161 0 ▶ $self->{on_parser_error}->() if $self->{on_parser_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_parser_error"} 0 0 0
162 0 return 0;
163 }
164  
165 # The HTTP upgrade and first Gateway frame can share one socket read.
166 # Re-enter the Stream reader after switching away from the handshake.
167 3 ▶ return $future->is_done ? 1 : $result;
Branch: 1 branch
BranchTrueFalseTotal
$future->is_done ? : 3 0 3
168 }
169 3 );
170 3 return $future;
171 }
172  
173 11 1;
« blib/lib/Discord/Client/Gateway/Shards.pm ↑ Summary blib/lib/Discord/Client/Intents.pm »