| 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 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 10 | ▶ | sub new { return bless { }, shift; } | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 12 | |||||||||||
| 13 | 9 | ▶ | sub add { | ||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 14 | 9 | my ($self) = @_; | |||||||||
| 15 | |||||||||||
| 16 | 9 | my $str = $self->_generate_text(); | |||||||||
| 17 | |||||||||||
| 18 | 9 | 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 | 1 | ▶ | sub remove { | ||||||||
|
Subroutine
remove: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||
| 26 | 1 | my ($self, $text) = @_; | |||||||||
| 27 | |||||||||||
| 28 | 1 | ▶ | if ( delete $self->{$text} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 1 | $self->_reset(); | |||||||||
| 30 | 1 | return 1; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | return 0; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 19 | ▶ | sub get_count { | ||||||||
|
Subroutine
get_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 19 | my ($self) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 19 | return 0 + keys %$self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | #---------------------------------------------------------------------- | ||||||||||
| 43 | |||||||||||
| 44 | 9 | ▶ | sub _generate_text { | ||||||||
|
Subroutine
_generate_text: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 9 | my ($self) = @_; | |||||||||
| 46 | |||||||||||
| 47 | 9 | return sprintf( | |||||||||
| 48 | '%s UTC: ping #%d (%x)', | ||||||||||
| 49 | scalar(gmtime), | ||||||||||
| 50 | 1 + $self->get_count(), | ||||||||||
| 51 | substr(rand, 2), | ||||||||||
| 52 | ); | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | sub _reset { | ||||||||
|
Subroutine
_reset: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 1 | my ($self) = @_; | |||||||||
| 57 | |||||||||||
| 58 | 1 | return %$self = (); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 11 | 1; | |||||||||