| 1 | package Net::WebSocket::PingStore; | ||||||||||
| 2 | |||||||||||
| 3 | #---------------------------------------------------------------------- | ||||||||||
| 4 | # This isn’t really meant for public consumption, but it is at least | ||||||||||
| 5 | # useful in Net::WAMP for implementing the same behavior as WebSocket uses. | ||||||||||
| 6 | #---------------------------------------------------------------------- | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub new { return bless { }, shift; } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 12 | |||||||||||
| 13 | sub add { | ||||||||||
| 14 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 15 | |||||||||||
| 16 | 0 | my $str = $self->_generate_text(); | |||||||||
| 17 | |||||||||||
| 18 | 0 | return $self->{$str} = $str; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | #NB: We expect a response to any ping that we’ve sent; any pong | ||||||||||
| 22 | #we receive that doesn’t actually correlate to a ping we’ve sent | ||||||||||
| 23 | #is ignored—i.e., it doesn’t reset the ping counter. This means that | ||||||||||
| 24 | #we could still timeout even if we’re receiving pongs. | ||||||||||
| 25 | sub remove { | ||||||||||
| 26 | 0 | ▶ | my ($self, $text) = @_; | ||||||||
|
Subroutine
remove: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | ▶ | if ( delete $self->{$text} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | $self->_reset(); | |||||||||
| 30 | 0 | return 1; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | return 0; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | sub get_count { | ||||||||||
| 37 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
get_count: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 0 | return 0 + keys %$self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | #---------------------------------------------------------------------- | ||||||||||
| 43 | |||||||||||
| 44 | sub _generate_text { | ||||||||||
| 45 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_generate_text: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | |||||||||||
| 47 | 0 | return sprintf( | |||||||||
| 48 | '%s UTC: ping #%d (%x)', | ||||||||||
| 49 | scalar(gmtime), | ||||||||||
| 50 | $self->get_count(), | ||||||||||
| 51 | substr(rand, 2), | ||||||||||
| 52 | ); | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | sub _reset { | ||||||||||
| 56 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_reset: not called CC=1 SCAR=6.9
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | return %$self = (); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 1 | 1; | |||||||||