| 1 | package AnyEvent::WebSocket::Connection; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 14 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use Protocol::WebSocket::Frame; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | 14 | ▶ | use AE; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use AnyEvent::WebSocket::Message; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use PerlX::Maybe qw( maybe provided ); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | # ABSTRACT: WebSocket connection for AnyEvent | ||||||||||||||||||||||||||||||||||||
| 15 | 14 | our $VERSION = '0.55'; # VERSION | |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||
| 18 | 14 | has handle => ( | |||||||||||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 20 | required => 1, | ||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||
| 24 | has masked => ( | ||||||||||||||||||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 26 | 45 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 27 | 14 | ); | |||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | 14 | has subprotocol => ( | |||||||||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 14 | has max_payload_size => ( | |||||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | 14 | has max_fragments => ( | |||||||||||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||
| 45 | 14 | has close_code => ( | |||||||||||||||||||||||||||||||||||
| 46 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 14 | has close_reason => ( | |||||||||||||||||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | 14 | has close_error => ( | |||||||||||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 14 | foreach my $type (qw( each_message next_message finish parse_error )) | |||||||||||||||||||||||||||||||||||
| 60 | { | ||||||||||||||||||||||||||||||||||||
| 61 | has "_${type}_cb" => ( | ||||||||||||||||||||||||||||||||||||
| 62 | is => 'ro', | ||||||||||||||||||||||||||||||||||||
| 63 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||
| 64 | 276 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 65 | 56 | ); | |||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | 14 | foreach my $flag (qw( _is_read_open _is_write_open )) | |||||||||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||||||||||
| 70 | has $flag => ( | ||||||||||||||||||||||||||||||||||||
| 71 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 72 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||
| 73 | 138 | ▶ | default => sub { 1 }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 74 | 28 | ); | |||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | has "_is_finished" => ( | ||||||||||||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||||||||||
| 79 | init_arg => undef, | ||||||||||||||||||||||||||||||||||||
| 80 | 69 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 81 | 14 | ); | |||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | sub BUILD | ||||||||||||||||||||||||||||||||||||
| 84 | 69 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 85 | 69 | my $self = shift; | |||||||||||||||||||||||||||||||||||
| 86 | 69 | Scalar::Util::weaken $self; | |||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||
| 88 | 69 | my @temp_messages = (); | |||||||||||||||||||||||||||||||||||
| 89 | 69 | my $are_callbacks_supposed_to_be_ready = 0; | |||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 35 | ▶ | my $finish = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 92 | 35 | my(undef, undef, $message) = @_; | |||||||||||||||||||||||||||||||||||
| 93 | 35 | my $strong_self = $self; # preserve $self because otherwise $self can be destroyed in the callbacks. | |||||||||||||||||||||||||||||||||||
| 94 | 35 | ▶ | return if $self->_is_finished; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 95 | eval | ||||||||||||||||||||||||||||||||||||
| 96 | 35 | { | |||||||||||||||||||||||||||||||||||
| 97 | 35 | $self->_process_message($_) foreach @temp_messages; | |||||||||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||||||||||
| 99 | 35 | @temp_messages = (); | |||||||||||||||||||||||||||||||||||
| 100 | 35 | $self->_is_finished(1); | |||||||||||||||||||||||||||||||||||
| 101 | 35 | $self->handle->push_shutdown; | |||||||||||||||||||||||||||||||||||
| 102 | 35 | $self->_is_read_open(0); | |||||||||||||||||||||||||||||||||||
| 103 | 35 | $self->_is_write_open(0); | |||||||||||||||||||||||||||||||||||
| 104 | 35 | ▶ | $self->close_error($message) if defined $message; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 105 | 35 | $_->($self, $message) for @{ $self->_finish_cb }; | |||||||||||||||||||||||||||||||||||
| 106 | 69 | }; | |||||||||||||||||||||||||||||||||||
| 107 | 69 | $self->handle->on_error($finish); | |||||||||||||||||||||||||||||||||||
| 108 | 69 | $self->handle->on_eof($finish); | |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 69 | my $frame = Protocol::WebSocket::Frame->new( | |||||||||||||||||||||||||||||||||||
| 111 | maybe max_payload_size => $self->max_payload_size, | ||||||||||||||||||||||||||||||||||||
| 112 | maybe max_fragments_amount => $self->max_fragments, | ||||||||||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||
| 115 | 236 | ▶ | my $read_cb = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 116 | 236 | my ($handle) = @_; | |||||||||||||||||||||||||||||||||||
| 117 | 236 | local $@; | |||||||||||||||||||||||||||||||||||
| 118 | 236 | my $strong_self = $self; # preserve $self because otherwise $self can be destroyed in the callbacks | |||||||||||||||||||||||||||||||||||
| 119 | my $success = eval | ||||||||||||||||||||||||||||||||||||
| 120 | 236 | { | |||||||||||||||||||||||||||||||||||
| 121 | 236 | $frame->append($handle->{rbuf}); | |||||||||||||||||||||||||||||||||||
| 122 | 236 | ▶ | while(defined(my $body = $frame->next_bytes)) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||||||||||
| 124 | 105 | ▶ | next if !$self->_is_read_open; # not 'last' but 'next' in order to consume data in $frame buffer. | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 125 | 104 | my $message = AnyEvent::WebSocket::Message->new( | |||||||||||||||||||||||||||||||||||
| 126 | body => $body, | ||||||||||||||||||||||||||||||||||||
| 127 | opcode => $frame->opcode, | ||||||||||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||||||||||
| 129 | 104 | ▶ | if($are_callbacks_supposed_to_be_ready) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 130 | { | ||||||||||||||||||||||||||||||||||||
| 131 | 61 | $self->_process_message($message); | |||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||
| 133 | else | ||||||||||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||||||||||
| 135 | 43 | push(@temp_messages, $message); | |||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||
| 138 | 232 | 1; # succeed to parse. | |||||||||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||||||||||
| 140 | 236 | ▶ | if(!$success) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||||||||||
| 142 | 4 | $self->_force_shutdown(); | |||||||||||||||||||||||||||||||||||
| 143 | 4 | $_->($self, $@) for @{ $self->_parse_error_cb }; | |||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||
| 145 | 69 | }; | |||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||
| 148 | # Message processing (calling _process_message) is delayed by | ||||||||||||||||||||||||||||||||||||
| 149 | # $are_callbacks_supposed_to_be_ready flag. This is necessary to | ||||||||||||||||||||||||||||||||||||
| 150 | # make sure all received messages are delivered to each_message and | ||||||||||||||||||||||||||||||||||||
| 151 | # next_message callbacks. If there is some data in rbuf, changing | ||||||||||||||||||||||||||||||||||||
| 152 | # the on_read callback makes the callback fire, but there is of | ||||||||||||||||||||||||||||||||||||
| 153 | # course no each_message/next_message callback to receive the | ||||||||||||||||||||||||||||||||||||
| 154 | # message yet. So we put messages to @temp_messages for a | ||||||||||||||||||||||||||||||||||||
| 155 | # while. After the control is returned to the user, who sets up | ||||||||||||||||||||||||||||||||||||
| 156 | # each_message/next_message callbacks, @temp_messages are processed. | ||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||
| 158 | # An alternative approach would be temporarily disabling on_read by | ||||||||||||||||||||||||||||||||||||
| 159 | # $self->handle->on_read(undef). However, this can cause a weird | ||||||||||||||||||||||||||||||||||||
| 160 | # situation in TLS mode, because on_eof can fire even if we don't | ||||||||||||||||||||||||||||||||||||
| 161 | # have any on_read ( | ||||||||||||||||||||||||||||||||||||
| 162 | # https://metacpan.org/pod/AnyEvent::Handle#I-get-different-callback-invocations-in-TLS-mode-Why-cant-I-pause-reading | ||||||||||||||||||||||||||||||||||||
| 163 | # ) | ||||||||||||||||||||||||||||||||||||
| 164 | 69 | $self->handle->on_read($read_cb); | |||||||||||||||||||||||||||||||||||
| 165 | 69 | ▶ | my $idle_w; $idle_w = AE::idle sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 166 | 57 | undef $idle_w; | |||||||||||||||||||||||||||||||||||
| 167 | 57 | ▶ | if(defined($self)) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||||||||||
| 169 | 23 | my $strong_self = $self; | |||||||||||||||||||||||||||||||||||
| 170 | 23 | $are_callbacks_supposed_to_be_ready = 1; | |||||||||||||||||||||||||||||||||||
| 171 | 23 | local $@; | |||||||||||||||||||||||||||||||||||
| 172 | my $success = eval | ||||||||||||||||||||||||||||||||||||
| 173 | 23 | { | |||||||||||||||||||||||||||||||||||
| 174 | 23 | $self->_process_message($_) foreach @temp_messages; | |||||||||||||||||||||||||||||||||||
| 175 | 23 | 1; | |||||||||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||||||||||
| 177 | 23 | @temp_messages = (); | |||||||||||||||||||||||||||||||||||
| 178 | 23 | ▶ | if(!$success) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 179 | { | ||||||||||||||||||||||||||||||||||||
| 180 | 0 | $self->_force_shutdown(); | |||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||
| 183 | 69 | }; | |||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||
| 186 | sub _process_message | ||||||||||||||||||||||||||||||||||||
| 187 | 104 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_process_message: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||
| 188 | 104 | my ($self, $received_message) = @_; | |||||||||||||||||||||||||||||||||||
| 189 | 104 | ▶ | return if !$self->_is_read_open; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||
| 191 | 103 | ▶ | if($received_message->is_text || $received_message->is_binary) | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$received_message->is_text or $received_message->is_binary
MC/DC100%2/2
$received_message->is_text or $received_message->is_binary
$received_message->is_text
$received_message->is_binary
Truth table100%3/3
A or B
A= $received_message->is_textB= $received_message->is_binary
| |||||||||||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||||||||||
| 193 | # make a copy in order to allow specifying new callbacks inside the | ||||||||||||||||||||||||||||||||||||
| 194 | # currently executed next_callback itself. otherwise, any next_callback | ||||||||||||||||||||||||||||||||||||
| 195 | # added inside the currently executed callback would be added to the end | ||||||||||||||||||||||||||||||||||||
| 196 | # of the array and executed for the currently processed message instead of | ||||||||||||||||||||||||||||||||||||
| 197 | # actually the next one. | ||||||||||||||||||||||||||||||||||||
| 198 | 77 | my @next_callbacks = @{ $self->_next_message_cb }; | |||||||||||||||||||||||||||||||||||
| 199 | 77 | @{ $self->_next_message_cb } = (); | |||||||||||||||||||||||||||||||||||
| 200 | 77 | $_->($self, $received_message) for @next_callbacks; | |||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | # make a copy in case one of the callbacks get | ||||||||||||||||||||||||||||||||||||
| 203 | # unregistered in the middle of the loop | ||||||||||||||||||||||||||||||||||||
| 204 | 77 | my @callbacks = @{ $self->_each_message_cb }; | |||||||||||||||||||||||||||||||||||
| 205 | $_->($self, $received_message, $self->_cancel_for(each_message => $_) ) | ||||||||||||||||||||||||||||||||||||
| 206 | 77 | for @callbacks; | |||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||
| 208 | elsif($received_message->is_close) | ||||||||||||||||||||||||||||||||||||
| 209 | { | ||||||||||||||||||||||||||||||||||||
| 210 | 25 | my $body = $received_message->body; | |||||||||||||||||||||||||||||||||||
| 211 | 25 | ▶ | if($body) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 212 | { | ||||||||||||||||||||||||||||||||||||
| 213 | 19 | my($code, $reason) = unpack 'na*', $body; | |||||||||||||||||||||||||||||||||||
| 214 | 19 | $self->close_code($code); | |||||||||||||||||||||||||||||||||||
| 215 | 19 | $self->close_reason(Encode::decode('UTF-8', $reason)); | |||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||
| 217 | 25 | $self->_is_read_open(0); | |||||||||||||||||||||||||||||||||||
| 218 | 25 | $self->close(); | |||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||
| 220 | elsif($received_message->is_ping) | ||||||||||||||||||||||||||||||||||||
| 221 | { | ||||||||||||||||||||||||||||||||||||
| 222 | 1 | $self->send(AnyEvent::WebSocket::Message->new(opcode => 10, body => $received_message->body)); | |||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||
| 226 | sub _force_shutdown | ||||||||||||||||||||||||||||||||||||
| 227 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_force_shutdown: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 228 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||
| 229 | 4 | $self->handle->push_shutdown; | |||||||||||||||||||||||||||||||||||
| 230 | 4 | $self->_is_write_open(0); | |||||||||||||||||||||||||||||||||||
| 231 | 4 | $self->_is_read_open(0); | |||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||
| 235 | sub send | ||||||||||||||||||||||||||||||||||||
| 236 | 131 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
send: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 237 | 131 | my($self, $message) = @_; | |||||||||||||||||||||||||||||||||||
| 238 | 131 | my $frame; | |||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||
| 240 | 131 | ▶ | return $self if !$self->_is_write_open; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||
| 242 | 112 | ▶ | if(ref $message) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 243 | { | ||||||||||||||||||||||||||||||||||||
| 244 | 47 | $frame = Protocol::WebSocket::Frame->new(opcode => $message->opcode, buffer => $message->body, masked => $self->masked, max_payload_size => 0); | |||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||
| 246 | else | ||||||||||||||||||||||||||||||||||||
| 247 | { | ||||||||||||||||||||||||||||||||||||
| 248 | 65 | $frame = Protocol::WebSocket::Frame->new(buffer => $message, masked => $self->masked, max_payload_size => 0); | |||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||
| 250 | 112 | $self->handle->push_write($frame->to_bytes); | |||||||||||||||||||||||||||||||||||
| 251 | 112 | $self; | |||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | sub _cancel_for | ||||||||||||||||||||||||||||||||||||
| 256 | 136 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
_cancel_for: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 257 | 136 | my( $self, $event, $handler ) = @_; | |||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | 136 | my $handler_id = Scalar::Util::refaddr($handler); | |||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||
| 261 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 262 | 2 | my $accessor = "_${event}_cb"; | |||||||||||||||||||||||||||||||||||
| 263 | 5 | @{ $self->$accessor } = grep { Scalar::Util::refaddr($_) != $handler_id } | |||||||||||||||||||||||||||||||||||
| 264 | 2 | @{ $self->$accessor }; | |||||||||||||||||||||||||||||||||||
| 265 | 136 | }; | |||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||
| 268 | sub on | ||||||||||||||||||||||||||||||||||||
| 269 | 85 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
on: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 270 | 85 | my($self, $event, $cb) = @_; | |||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||
| 272 | 85 | ▶ | if($event eq 'next_message') | ||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||||||||||
| 274 | 32 | push @{ $self->_next_message_cb }, $cb; | |||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||
| 276 | elsif($event eq 'each_message') | ||||||||||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||||||||||
| 278 | 16 | push @{ $self->_each_message_cb }, $cb; | |||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||
| 280 | elsif($event eq 'finish') | ||||||||||||||||||||||||||||||||||||
| 281 | { | ||||||||||||||||||||||||||||||||||||
| 282 | 35 | push @{ $self->_finish_cb }, $cb; | |||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||
| 284 | elsif($event eq 'parse_error') | ||||||||||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||||||||||
| 286 | 2 | push @{ $self->_parse_error_cb }, $cb; | |||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||
| 288 | else | ||||||||||||||||||||||||||||||||||||
| 289 | { | ||||||||||||||||||||||||||||||||||||
| 290 | 0 | Carp::croak "unrecongized event: $event"; | |||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||
| 293 | 85 | return $self->_cancel_for($event,$cb); | |||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||
| 297 | sub close | ||||||||||||||||||||||||||||||||||||
| 298 | 40 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 299 | 40 | my($self, $code, $reason) = @_; | |||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||
| 301 | 40 | ▶ | my $body = pack('n', ($code) ? $code : '1000'); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||
| 303 | 40 | ▶ | $body .= Encode::encode 'UTF-8', $reason if defined $reason; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||
| 305 | 40 | $self->send(AnyEvent::WebSocket::Message->new( | |||||||||||||||||||||||||||||||||||
| 306 | opcode => 8, | ||||||||||||||||||||||||||||||||||||
| 307 | body => $body, | ||||||||||||||||||||||||||||||||||||
| 308 | )); | ||||||||||||||||||||||||||||||||||||
| 309 | 40 | $self->handle->push_shutdown; | |||||||||||||||||||||||||||||||||||
| 310 | 40 | $self->_is_write_open(0); | |||||||||||||||||||||||||||||||||||
| 311 | 40 | $self; | |||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||
| 314 | 14 | ▶ | if($] < 5.010) | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 315 | { | ||||||||||||||||||||||||||||||||||||
| 316 | # This is a workaround for GH#19 | ||||||||||||||||||||||||||||||||||||
| 317 | # https://github.com/plicease/AnyEvent-WebSocket-Client/issues/19 | ||||||||||||||||||||||||||||||||||||
| 318 | # I am not 100% sure about this, but maybe as a trade off it isn't | ||||||||||||||||||||||||||||||||||||
| 319 | # too bad? The previous workaround was to downgrade to AE 6.x | ||||||||||||||||||||||||||||||||||||
| 320 | # something. Unfortunately, we now require AE 7.x something for | ||||||||||||||||||||||||||||||||||||
| 321 | # SSL bug fixes. | ||||||||||||||||||||||||||||||||||||
| 322 | *DEMOLISH = sub | ||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||
| 324 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | eval { $self->handle->push_shutdown } if $self->_is_write_open; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 326 | 0 | }; | |||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||
| 329 | 14 | 1; | |||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||
| 331 | __END__ | ||||||||||||||||||||||||||||||||||||