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

filter: Statementstmt 99.2% 136 / 137 Branchbran 87.8% 65 / 74 Conditioncond 68.1% 79 / 116 MC/DCmcdc 41.8% 23 / 55 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 13 totaltotal 78.0% 331 / 424 CC 77 SCAR 26.9
CC
77
Cov
86%
new
39.3
receive
19.2
_receive_dispatch
17.3
CRAP 14.7
« blib/lib/Discord/Client/Gateway.pm ↑ Summary blib/lib/Discord/Client/Gateway/IdentifyLimiter.pm »
1 package Discord::Client::Gateway::Session;
2  
3 14 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 14 ▶use Discord::Client::Intents ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 14 our $VERSION = '0.003';
12  
13 # Pure protocol decision logic: no sockets, event loop, timers, or callbacks.
14 # Each method returns a list reference of declarative actions for an IO driver.
15 # Deliberately internal and not a stable public API yet.
16  
17 111 ▶sub new {
Subroutine new: called CC=18 SCAR=36.7
Pod undocumented
18 111 my ( $class, %args ) = @_;
19  
20 # Validate static session inputs before recording resumable state.
21 111 ▶ croak 'token is required' if !defined( $args{token} ) || ref( $args{token} ) || $args{token} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $args{"token"} or ref $args{"token"} or $args{"token"} eq "" 0 111 111
Condition33%2/6
not defined $args{"token"} or ref $args{"token"} or $args{"token"} eq ""
l!l&&r!l&&!r
00111
not defined $args{"token"} or ref $args{"token"}
l!l&&r!l&&!r
00111
MC/DC0%0/3
not defined $args{"token"} or ref $args{"token"} or $args{"token"} eq ""
not defined $args{"token"} ref $args{"token"} $args{"token"} eq ""
Truth table25%1/4
not(A) or B or C
A= not defined $args{"token"}B= ref $args{"token"}C= $args{"token"} eq ""
ABCresult
1XX1
01X1
0011
0000
22  
23 111 ▶ my $intents = $args{intents} // 0;
Condition100%2/2
$args{"intents"} // 0
l!l
6546
MC/DC100%1/1
$args{"intents"} // 0
$args{"intents"}
Truth table100%2/2
A
A= $args{"intents"}
Aresult
11
00
24 111 ▶ if ( ref $intents eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $intents eq "ARRAY") 53 58 111
25 53 $intents = Discord::Client::Intents->bits(@$intents);
26 }
27 110 ▶ croak 'intents must be a nonnegative integer or arrayref of names'
Branch: 1 branch
BranchTrueFalseTotal
if ref $intents or not $intents =~ /\A\d+\z/u 0 110 110
Condition33%1/3
ref $intents or not $intents =~ /\A\d+\z/u
l!l&&r!l&&!r
00110
MC/DC0%0/2
ref $intents or not $intents =~ /\A\d+\z/u
ref $intents not $intents =~ /\A\d+\z/u
Truth table33%1/3
A or not(B)
A= ref $intentsB= not $intents =~ /\A\d+\z/u
ABresult
1X1
011
000
28 if ref($intents) || $intents !~ /\A\d+\z/;
29  
30 0 ▶ my $random = $args{random} // sub { rand() };
Subroutine __ANON__: not called CC=1 SCAR=0.6
Condition66%2/3
$args{"random"} // sub { rand(); }
l!l&&r!l&&!r
10910
MC/DC0%0/2
$args{"random"} // sub { rand(); }
$args{"random"} sub { rand(); }
Truth table66%2/3
A // B
A= $args{"random"}B= sub { rand(); }
ABresult
1X1
011
000
31 110 ▶ croak 'random must be a coderef' if ref($random) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $random ne "CODE" 0 110 110
32 110 my $shard = $args{shard};
33 110 ▶ croak 'shard must contain a valid shard ID and total'
Branch: 1 branch
BranchTrueFalseTotal
if defined $shard and ref $shard ne "ARRAY" || @$shard != 2 || !_uint($shard->[0]) || !_uint($shard->[1]) || !$shard->[1] || $shard->[0] >= $shard->[1] 0 110 110
Condition38%7/18
defined $shard and ref $shard ne "ARRAY" || @$shard != 2 || !_uint($shard->[0]) || !_uint($shard->[1]) || !$shard->[1] || $shard->[0] >= $shard->[1]
!ll&&!rl&&r
96140
ref $shard ne "ARRAY" or @$shard != 2 or not _uint($shard->[0]) or not _uint($shard->[1]) or not $shard->[1] or $shard->[0] >= $shard->[1]
l!l&&r!l&&!r
0014
ref $shard ne "ARRAY" or @$shard != 2 or not _uint($shard->[0]) or not _uint($shard->[1]) or not $shard->[1]
l!l&&r!l&&!r
0014
ref $shard ne "ARRAY" or @$shard != 2 or not _uint($shard->[0]) or not _uint($shard->[1])
l!l&&r!l&&!r
0014
ref $shard ne "ARRAY" or @$shard != 2 or not _uint($shard->[0])
l!l&&r!l&&!r
0014
ref $shard ne "ARRAY" or @$shard != 2
l!l&&r!l&&!r
0014
MC/DC0%0/7
defined $shard and ref $shard ne "ARRAY" || @$shard != 2 || !_uint($shard->[0]) || !_uint($shard->[1]) || !$shard->[1] || $shard->[0] >= $shard->[1]
defined $shard ref $shard ne "ARRAY" @$shard != 2 not _uint($shard->[0]) not _uint($shard->[1]) not $shard->[1] $shard->[0] >= $shard->[1]
Truth table25%2/8
A and B or C or not(D) or not(E) or not(F) or G
A= defined $shardB= ref $shard ne "ARRAY"C= @$shard != 2D= not _uint($shard->[0])E= not _uint($shard->[1])F= not $shard->[1]G= $shard->[0] >= $shard->[1]
ABCDEFGresult
0XXXXXX0
10000000
11XXXXX1
101XXXX1
1001XXX1
10001XX1
100001X1
10000011
34 if defined($shard)
35 && ( ref($shard) ne 'ARRAY'
36 || @$shard != 2
37 || !_uint( $shard->[0] )
38 || !_uint( $shard->[1] )
39 || !$shard->[1]
40 || $shard->[0] >= $shard->[1] );
41  
42 # A new session has neither a sequence nor a resumable connection identity.
43 return bless {
44 token => $args{token},
45 intents => 0 + $intents,
46 random => $random,
47 110 ▶ shard => $shard ? [ map { 0 + $_ } @$shard ] : undef,
Branch: 1 branch
BranchTrueFalseTotal
$shard ? : 14 96 110
48 state => 'disconnected',
49 seq => undef,
50 session_id => undef,
51 resume_gateway_url => undef,
52 heartbeat_interval_ms => undef,
53 heartbeat_pending => 0,
54 }, $class;
55 }
56  
57 40 ▶sub state { $_[0]->{state} }
Subroutine state: called CC=1 SCAR=0.0
Pod undocumented
58 17 ▶sub sequence { $_[0]->{seq} }
Subroutine sequence: called CC=1 SCAR=0.0
Pod undocumented
59 15 ▶sub session_id { $_[0]->{session_id} }
Subroutine session_id: called CC=1 SCAR=0.0
Pod undocumented
60 11 ▶sub resume_gateway_url { $_[0]->{resume_gateway_url} }
Subroutine resume_gateway_url: called CC=1 SCAR=0.0
Pod undocumented
61 12 ▶sub heartbeat_interval_ms { $_[0]->{heartbeat_interval_ms} }
Subroutine heartbeat_interval_ms: called CC=1 SCAR=0.0
Pod undocumented
62 3 ▶sub heartbeat_pending { $_[0]->{heartbeat_pending} }
Subroutine heartbeat_pending: called CC=1 SCAR=0.0
Pod undocumented
63  
64 216 ▶sub can_resume {
Subroutine can_resume: called CC=3 SCAR=11.4
Pod undocumented
65 216 my ($self) = @_;
66  
67 # All three fields are required to resume the same Discord session.
68 return
69 defined( $self->{session_id} )
70 && defined( $self->{seq} )
71 216 ▶ && defined( $self->{resume_gateway_url} );
Condition66%4/6
defined $self->{'session_id'} && defined $self->{'seq'} && defined $self->{'resume_gateway_url'}
!ll&&!rl&&r
168048
defined $self->{"session_id"} and defined $self->{"seq"}
!ll&&!rl&&r
168048
MC/DC33%1/3
defined $self->{'session_id'} && defined $self->{'seq'} && defined $self->{'resume_gateway_url'}
defined $self->{"session_id"} defined $self->{"seq"} defined $self->{'resume_gateway_url'}
Truth table50%2/4
A and B && C
A= defined $self->{"session_id"}B= defined $self->{"seq"}C= defined $self->{'resume_gateway_url'}
ABCresult
0XX0
10X0
1100
1111
72 }
73  
74 116 ▶sub opened {
Subroutine opened: called CC=1 SCAR=0.0
Pod undocumented
75 116 my ($self) = @_;
76  
77 # A new socket awaits HELLO while retaining any resumable session identity.
78 116 $self->{state} = 'awaiting_hello';
79 116 $self->{heartbeat_pending} = 0;
80 116 $self->{heartbeat_interval_ms} = undef;
81 116 return [];
82 }
83  
84 339 ▶sub receive {
Subroutine receive: called CC=19 SCAR=29.5
Pod undocumented
85 339 my ( $self, $packet ) = @_;
86  
87 # Reject malformed envelopes before switching on the protocol opcode.
88 339 ▶ croak 'gateway packet must be a hashref' if ref $packet ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $packet ne "HASH" 3 336 339
89 336 my $op = $packet->{op};
90 336 ▶ croak 'gateway packet opcode missing or invalid'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $op or ref $op or not $op =~ /\A\d+\z/u 3 333 336
Condition100%6/6
not defined $op or ref $op or not $op =~ /\A\d+\z/u
l!l&&r!l&&!r
21333
not defined $op or ref $op
l!l&&r!l&&!r
11334
MC/DC100%3/3
not defined $op or ref $op or not $op =~ /\A\d+\z/u
not defined $op ref $op not $op =~ /\A\d+\z/u
Truth table100%4/4
not(A) or B or not(C)
A= not defined $opB= ref $opC= not $op =~ /\A\d+\z/u
ABCresult
1XX1
01X1
0011
0000
91 if !defined($op) || ref($op) || $op !~ /\A\d+\z/;
92  
93 333 ▶ return $self->_receive_hello( $packet->{d} ) if $op == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $op == 10 122 211 333
94  
95 211 ▶ if ( $op == 11 ) { # HEARTBEAT ACK
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 11) 27 184 211
96 27 $self->{heartbeat_pending} = 0;
97 27 return [];
98 }
99  
100 184 ▶ if ( $op == 1 ) { # Discord requests an immediate heartbeat
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 1) 1 183 184
101 croak 'HEARTBEAT received outside a session'
102 1 ▶ unless $self->{state} eq 'authenticating' || $self->{state} eq 'ready';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} eq "authenticating" or $self->{"state"} eq "ready" 0 1 1
Condition33%1/3
$self->{"state"} eq "authenticating" or $self->{"state"} eq "ready"
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"state"} eq "authenticating" or $self->{"state"} eq "ready"
$self->{"state"} eq "authenticating" $self->{"state"} eq "ready"
Truth table33%1/3
A or B
A= $self->{"state"} eq "authenticating"B= $self->{"state"} eq "ready"
ABresult
1X1
011
000
103 1 return [ $self->_heartbeat_action ];
104 }
105  
106 183 ▶ return $self->_receive_dispatch($packet) if $op == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op == 0 173 10 183
107  
108 10 ▶ if ( $op == 7 ) { # RECONNECT
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 7) 3 7 10
109 3 $self->{state} = 'disconnected';
110 3 $self->{heartbeat_pending} = 0;
111 3 return [ $self->_reconnect_action(0) ];
112 }
113  
114 7 ▶ if ( $op == 9 ) { # INVALID SESSION
Branch: 1 branch
BranchTrueFalseTotal
if ($op == 9) 6 1 7
115 croak 'INVALID_SESSION requires a boolean'
116 unless JSON::PP::is_bool( $packet->{d} )
117 6 ▶ || ( defined( $packet->{d} ) && !ref( $packet->{d} ) && $packet->{d} =~ /\A[01]\z/ );
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($packet->{"d"}) or defined $packet->{"d"} and not ref $packet->{"d"} and $packet->{"d"} =~ /\A[01]\z/u 2 4 6
Condition88%8/9
JSON::PP::is_bool($packet->{"d"}) or defined $packet->{"d"} and not ref $packet->{"d"} and $packet->{"d"} =~ /\A[01]\z/u
l!l&&r!l&&!r
132
defined $packet->{"d"} and not ref $packet->{"d"} and $packet->{"d"} =~ /\A[01]\z/u
!ll&&!rl&&r
113
defined $packet->{"d"} and not ref $packet->{"d"}
!ll&&!rl&&r
014
MC/DC75%3/4
JSON::PP::is_bool($packet->{"d"}) or defined $packet->{"d"} and not ref $packet->{"d"} and $packet->{"d"} =~ /\A[01]\z/u
JSON::PP::is_bool($packet->{"d"}) defined $packet->{"d"} not ref $packet->{"d"} $packet->{"d"} =~ /\A[01]\z/u
Truth table80%4/5
A or B and not(C) and D
A= JSON::PP::is_bool($packet->{"d"})B= defined $packet->{"d"}C= not ref $packet->{"d"}D= $packet->{"d"} =~ /\A[01]\z/u
ABCDresult
1XXX1
01111
00XX0
010X0
01100
118 4 ▶ my $resumable = $packet->{d} && $self->can_resume;
Condition66%2/3
$packet->{'d'} && $self->can_resume
!ll&&!rl&&r
301
MC/DC50%1/2
$packet->{'d'} && $self->can_resume
$packet->{'d'} $self->can_resume
Truth table66%2/3
A && B
A= $packet->{'d'}B= $self->can_resume
ABresult
0X0
100
111
119 4 ▶ $self->_forget_session unless $resumable;
Branch: 1 branch
BranchTrueFalseTotal
unless $resumable 3 1 4
120 4 $self->{state} = 'disconnected';
121 4 $self->{heartbeat_pending} = 0;
122  
123 # Discord requires a randomized delay before attempting a new session.
124 4 return [ $self->_reconnect_action( 1000 + int( 4000 * $self->_random ) ) ];
125 }
126  
127 # Preserve compatibility with future Gateway opcodes; consumer may inspect.
128 1 return [ { action => 'opcode', op => 0 + $op, data => $packet->{d} } ];
129 }
130  
131 122 ▶sub _receive_hello {
Subroutine _receive_hello: called CC=8 SCAR=20.8
132 122 my ( $self, $data ) = @_;
133  
134 # Validate HELLO before committing the heartbeat schedule and authentication state.
135 122 ▶ croak 'HELLO received outside handshake' if $self->{state} ne 'awaiting_hello';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} ne "awaiting_hello" 1 121 122
136 121 ▶ croak 'HELLO payload must be a hashref' if ref($data) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 1 120 121
137 120 my $interval = $data->{heartbeat_interval};
138 120 ▶ croak 'invalid heartbeat interval'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $interval or ref $interval or not $interval =~ /\A\d+\z/u or $interval <= 0 6 114 120
Condition100%9/9
not defined $interval or ref $interval or not $interval =~ /\A\d+\z/u or $interval <= 0
l!l&&r!l&&!r
51114
not defined $interval or ref $interval or not $interval =~ /\A\d+\z/u
l!l&&r!l&&!r
23115
not defined $interval or ref $interval
l!l&&r!l&&!r
11118
MC/DC100%4/4
not defined $interval or ref $interval or not $interval =~ /\A\d+\z/u or $interval <= 0
not defined $interval ref $interval not $interval =~ /\A\d+\z/u $interval <= 0
Truth table100%5/5
not(A) or B or not(C) or D
A= not defined $intervalB= ref $intervalC= not $interval =~ /\A\d+\z/uD= $interval <= 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
139 if !defined($interval) || ref($interval) || $interval !~ /\A\d+\z/ || $interval <= 0;
140  
141 # Jitter the first heartbeat, then choose Resume or Identify from saved state.
142 114 my $first = int( $interval * $self->_random );
143 109 $self->{heartbeat_interval_ms} = 0 + $interval;
144 109 $self->{state} = 'authenticating';
145 109 ▶ my $auth = $self->can_resume ? $self->_resume : $self->_identify;
Branch: 1 branch
BranchTrueFalseTotal
$self->can_resume ? : 10 99 109
146 return [
147 109 { action => 'heartbeat_schedule', first_delay_ms => $first, interval_ms => 0 + $interval },
148 { action => 'send', payload => $auth },
149 ];
150 }
151  
152 173 ▶sub _receive_dispatch {
Subroutine _receive_dispatch: called CC=16 SCAR=28.5
153 173 my ( $self, $packet ) = @_;
154  
155 # Validate dispatch data before updating the sequence used by heartbeats and Resume.
156 croak 'DISPATCH received outside a session'
157 173 ▶ unless $self->{state} eq 'authenticating' || $self->{state} eq 'ready';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} eq "authenticating" or $self->{"state"} eq "ready" 0 173 173
Condition66%2/3
$self->{"state"} eq "authenticating" or $self->{"state"} eq "ready"
l!l&&r!l&&!r
101720
MC/DC0%0/2
$self->{"state"} eq "authenticating" or $self->{"state"} eq "ready"
$self->{"state"} eq "authenticating" $self->{"state"} eq "ready"
Truth table66%2/3
A or B
A= $self->{"state"} eq "authenticating"B= $self->{"state"} eq "ready"
ABresult
1X1
011
000
158 croak 'DISPATCH missing event name'
159 173 ▶ if !defined( $packet->{t} ) || ref( $packet->{t} ) || !length( $packet->{t} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $packet->{"t"} or ref $packet->{"t"} or not length $packet->{"t"} 1 172 173
Condition50%3/6
not defined $packet->{"t"} or ref $packet->{"t"} or not length $packet->{"t"}
l!l&&r!l&&!r
01172
not defined $packet->{"t"} or ref $packet->{"t"}
l!l&&r!l&&!r
00173
MC/DC33%1/3
not defined $packet->{"t"} or ref $packet->{"t"} or not length $packet->{"t"}
not defined $packet->{"t"} ref $packet->{"t"} not length $packet->{"t"}
Truth table50%2/4
not(A) or B or not(C)
A= not defined $packet->{"t"}B= ref $packet->{"t"}C= not length $packet->{"t"}
ABCresult
1XX1
01X1
0011
0000
160 172 ▶ croak 'DISPATCH sequence is invalid' unless _uint( $packet->{s} );
Branch: 1 branch
BranchTrueFalseTotal
unless _uint($packet->{"s"}) 3 169 172
161 169 my $event = $packet->{t};
162 169 my $data = $packet->{d};
163 169 ▶ if ( $event eq 'READY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($event eq 'READY') { } 93 76 169
elsif ($event eq 'RESUMED') { } 8 68 76
164 croak 'READY requires session_id and resume_gateway_url'
165 if ref($data) ne 'HASH'
166 || !defined( $data->{session_id} )
167 || ref( $data->{session_id} )
168 || !length( $data->{session_id} )
169 || !defined( $data->{resume_gateway_url} )
170 || ref( $data->{resume_gateway_url} )
171 93 ▶ || $data->{resume_gateway_url} !~ m{\Awss://[^\s]+\z};
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"} or not defined $data->{"resume_gateway_url"} or ref $data->{"resume_gateway_url"} or not $data->{"resume_gateway_url"} =~ m[\Awss://[^\s]+\z]u 3 90 93
Condition77%14/18
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"} or not defined $data->{"resume_gateway_url"} or ref $data->{"resume_gateway_url"} or not $data->{"resume_gateway_url"} =~ m[\Awss://[^\s]+\z]u
l!l&&r!l&&!r
2190
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"} or not defined $data->{"resume_gateway_url"} or ref $data->{"resume_gateway_url"}
l!l&&r!l&&!r
2091
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"} or not defined $data->{"resume_gateway_url"}
l!l&&r!l&&!r
2091
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"}
l!l&&r!l&&!r
2091
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"}
l!l&&r!l&&!r
1191
ref $data ne "HASH" or not defined $data->{"session_id"}
l!l&&r!l&&!r
0192
MC/DC42%3/7
ref $data ne "HASH" or not defined $data->{"session_id"} or ref $data->{"session_id"} or not length $data->{"session_id"} or not defined $data->{"resume_gateway_url"} or ref $data->{"resume_gateway_url"} or not $data->{"resume_gateway_url"} =~ m[\Awss://[^\s]+\z]u
ref $data ne "HASH" not defined $data->{"session_id"} ref $data->{"session_id"} not length $data->{"session_id"} not defined $data->{"resume_gateway_url"} ref $data->{"resume_gateway_url"} not $data->{"resume_gateway_url"} =~ m[\Awss://[^\s]+\z]u
Truth table50%4/8
A or not(B) or C or not(D) or not(E) or F or not(G)
A= ref $data ne "HASH"B= not defined $data->{"session_id"}C= ref $data->{"session_id"}D= not length $data->{"session_id"}E= not defined $data->{"resume_gateway_url"}F= ref $data->{"resume_gateway_url"}G= not $data->{"resume_gateway_url"} =~ m[\Awss://[^\s]+\z]u
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
172 90 $self->{session_id} = $data->{session_id};
173 90 $self->{resume_gateway_url} = $data->{resume_gateway_url};
174 90 $self->{state} = 'ready';
175 }
176 elsif ( $event eq 'RESUMED' ) {
177 8 $self->{state} = 'ready';
178 }
179  
180 # Publish the new sequence only after validating session-establishing events.
181 166 $self->{seq} = $packet->{s};
182 166 return [ { action => 'event', name => $event, data => $data } ];
183 }
184  
185 32 ▶sub heartbeat_due {
Subroutine heartbeat_due: called CC=5 SCAR=17.3
Pod undocumented
186 32 my ($self) = @_;
187 return []
188 if !defined( $self->{heartbeat_interval_ms} )
189 || $self->{state} eq 'disconnected'
190 32 ▶ || $self->{state} eq 'stopped';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"heartbeat_interval_ms"} or $self->{"state"} eq "disconnected" or $self->{"state"} eq "stopped" 0 32 32
Condition33%2/6
not defined $self->{"heartbeat_interval_ms"} or $self->{"state"} eq "disconnected" or $self->{"state"} eq "stopped"
l!l&&r!l&&!r
0032
not defined $self->{"heartbeat_interval_ms"} or $self->{"state"} eq "disconnected"
l!l&&r!l&&!r
0032
MC/DC0%0/3
not defined $self->{"heartbeat_interval_ms"} or $self->{"state"} eq "disconnected" or $self->{"state"} eq "stopped"
not defined $self->{"heartbeat_interval_ms"} $self->{"state"} eq "disconnected" $self->{"state"} eq "stopped"
Truth table25%1/4
not(A) or B or C
A= not defined $self->{"heartbeat_interval_ms"}B= $self->{"state"} eq "disconnected"C= $self->{"state"} eq "stopped"
ABCresult
1XX1
01X1
0011
0000
191  
192 # Do not send another scheduled heartbeat to a connection that failed to acknowledge.
193 32 ▶ if ( $self->{heartbeat_pending} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"heartbeat_pending"}) 3 29 32
194 3 $self->{state} = 'disconnected';
195 3 $self->{heartbeat_pending} = 0;
196 3 return [ $self->_reconnect_action(0) ];
197 }
198 29 return [ $self->_heartbeat_action ];
199 }
200  
201 33 ▶sub closed {
Subroutine closed: called CC=5 SCAR=16.1
Pod undocumented
202 33 my ( $self, $code ) = @_;
203 33 ▶ $code = 0 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 33 33
204 33 $self->{heartbeat_pending} = 0;
205 33 $self->{heartbeat_interval_ms} = undef;
206 33 ▶ return [] if $self->{state} eq 'stopped';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} eq "stopped" 0 33 33
207  
208 # Authentication and configuration rejections cannot be repaired by reconnecting.
209 33 ▶ if ( $code =~ /\A(?:4004|4010|4011|4012|4013|4014)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /\A(?:4004|4010|4011|4012|4013|4014)\z/u) 9 24 33
210 9 $self->_forget_session;
211 9 $self->{state} = 'stopped';
212 9 return [ { action => 'fatal', close_code => 0 + $code } ];
213 }
214  
215 # Only normal closure and invalid session/sequence codes discard resumable state.
216 24 ▶ $self->_forget_session if $code =~ /\A(?:1000|1001|4007|4009)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /\A(?:1000|1001|4007|4009)\z/u 4 20 24
217 24 $self->{state} = 'disconnected';
218 24 return [ $self->_reconnect_action(0) ];
219 }
220  
221 58 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod undocumented
222 58 my ($self) = @_;
223  
224 # An explicit stop invalidates both heartbeat state and resumable identity.
225 58 $self->_forget_session;
226 58 $self->{heartbeat_interval_ms} = undef;
227 58 $self->{heartbeat_pending} = 0;
228 58 $self->{state} = 'stopped';
229 58 return [];
230 }
231  
232 99 ▶sub _identify {
Subroutine _identify: called CC=2 SCAR=6.9
233 99 my ($self) = @_;
234  
235 # Build only protocol data; the transport decides when an Identify may be sent.
236 return {
237 op => 2,
238 d => {
239 token => $self->{token},
240 intents => $self->{intents},
241 99 ▶ ( $self->{shard} ? ( shard => [ @{ $self->{shard} } ] ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'shard'} ? : 12 87 99
242 properties => {
243 os => $^O,
244 browser => 'Discord-Client',
245 device => 'Discord-Client',
246 },
247 },
248 };
249 }
250  
251 10 ▶sub _resume {
Subroutine _resume: called CC=1 SCAR=0.0
252 10 my ($self) = @_;
253  
254 # Resume the saved session at its last validated dispatch sequence.
255 return {
256 op => 6,
257 d => {
258 token => $self->{token},
259 session_id => $self->{session_id},
260 seq => $self->{seq},
261 },
262 10 };
263 }
264  
265 30 ▶sub _heartbeat_action {
Subroutine _heartbeat_action: called CC=1 SCAR=0.0
266 30 my ($self) = @_;
267 30 $self->{heartbeat_pending} = 1;
268 30 return { action => 'send', payload => { op => 1, d => $self->{seq} } };
269 }
270  
271 74 ▶sub _forget_session {
Subroutine _forget_session: called CC=1 SCAR=0.0
272 74 my ($self) = @_;
273  
274 # Discard the whole resume identity so a later socket must Identify again.
275 74 $self->{session_id} = undef;
276 74 $self->{resume_gateway_url} = undef;
277 74 $self->{seq} = undef;
278 }
279  
280 34 ▶sub _reconnect_action {
Subroutine _reconnect_action: called CC=2 SCAR=6.9
281 34 my ( $self, $delay_ms ) = @_;
282 return {
283 34 ▶ action => 'reconnect',
Branch: 1 branch
BranchTrueFalseTotal
$self->can_resume ? : 25 9 34
284 resume => $self->can_resume ? 1 : 0,
285 delay_ms => $delay_ms,
286 };
287 }
288  
289 118 ▶sub _random {
Subroutine _random: called CC=6 SCAR=17.9
290 118 my ($self) = @_;
291  
292 # Injected randomness must obey the same bounds as rand before it affects timing.
293 118 my $value = $self->{random}->();
294 118 ▶ croak 'random generator must return a number in [0, 1)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0 or $value >= 1 5 113 118
Condition91%11/12
not defined $value or ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0 or $value >= 1
l!l&&r!l&&!r
41113
not defined $value or ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0
l!l&&r!l&&!r
40114
not defined $value or ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u
l!l&&r!l&&!r
22114
not defined $value or ref $value
l!l&&r!l&&!r
11116
MC/DC80%4/5
not defined $value or ref $value or not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u or $value < 0 or $value >= 1
not defined $value ref $value not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u $value < 0 $value >= 1
Truth table83%5/6
not(A) or B or not(C) or D or E
A= not defined $valueB= ref $valueC= not $value =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/uD= $value < 0E= $value >= 1
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
295 if !defined($value)
296 || ref($value)
297 || $value !~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/
298 || $value < 0
299 || $value >= 1;
300 113 return $value;
301 }
302  
303 200 ▶sub _uint { defined( $_[0] ) && !ref( $_[0] ) && $_[0] =~ /\A\d+\z/ }
Subroutine _uint: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] =~ /\A\d+\z/u 198 2 200
Condition100%3/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
11198
MC/DC100%2/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table100%3/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
304  
305 14 1;
306  
307 __END__
« blib/lib/Discord/Client/Gateway.pm ↑ Summary blib/lib/Discord/Client/Gateway/IdentifyLimiter.pm »