« lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/JSONPPolling.pm
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/BasePolling.pm »
| 1 | package PocketIO::Connection; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use AnyEvent; | ||
| 7 | use JSON (); | ||
| 8 | use Encode (); | ||
| 9 | use Try::Tiny; | ||
| 10 | |||
| 11 | use PocketIO::Pool; | ||
| 12 | |||
| 13 | use constant DEBUG => $ENV{POCKETIO_CONNECTION_DEBUG}; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | |||
| 18 | my $self = {@_}; | ||
| 19 | bless $self, $class; | ||
| 20 | |||
| 21 | $self->{connect_timeout} ||= 15; | ||
| 22 | $self->{reconnect_timeout} ||= 15; | ||
| 23 | |||
| 24 | $self->{max_messages_to_stage} ||= 32; | ||
| 25 | $self->{messages} = []; | ||
| 26 | |||
| 27 | $self->{on_connect_failed} ||= sub { }; | ||
| 28 | $self->{on_connect} ||= sub { }; | ||
| 29 | $self->{on_reconnect} ||= sub { }; | ||
| 30 | $self->{on_reconnect_failed} ||= sub { }; | ||
| 31 | $self->{on_message} ||= sub { }; | ||
| 32 | $self->{on_disconnect} ||= sub { }; | ||
| 33 | $self->{on_error} ||= sub { }; | ||
| 34 | |||
| 35 | $self->{data} = ''; | ||
| 36 | |||
| 37 | DEBUG && warn "Connection created\n"; | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub type { shift->{type} } | ||
| 43 | |||
| 44 | sub is_connected { $_[0]->{is_connected} } | ||
| 45 | |||
| 46 | sub connecting { | ||
| 47 | my $self = shift; | ||
| 48 | |||
| 49 | DEBUG && warn "State 'connecting'\n"; | ||
| 50 | |||
| 51 | $self->{connect_timer} = AnyEvent->timer( | ||
| 52 | after => $self->{connect_timeout}, | ||
| 53 | cb => sub { | ||
| 54 | DEBUG && warn "Timeout 'connect_timeout'"; | ||
| 55 | |||
| 56 | $self->on('connect_failed')->($self); | ||
| 57 | } | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub reconnecting { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | DEBUG && warn "State 'reconnecting'\n"; | ||
| 65 | |||
| 66 | $self->{reconnect_timer} = AnyEvent->timer( | ||
| 67 | after => $self->{reconnect_timeout}, | ||
| 68 | cb => sub { | ||
| 69 | DEBUG && warn "Timeout 'reconnect_timeout'\n"; | ||
| 70 | |||
| 71 | $self->on('reconnect_failed')->($self); | ||
| 72 | } | ||
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub connected { | ||
| 77 | my $self = shift; | ||
| 78 | |||
| 79 | DEBUG && warn "State 'connected'\n"; | ||
| 80 | |||
| 81 | delete $self->{connect_timer}; | ||
| 82 | |||
| 83 | $self->{is_connected} = 1; | ||
| 84 | |||
| 85 | $self->on('connect')->($self); | ||
| 86 | |||
| 87 | return $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub reconnected { | ||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | DEBUG && warn "State 'reconnected'\n"; | ||
| 94 | |||
| 95 | delete $self->{reconnect_timer}; | ||
| 96 | |||
| 97 | $self->on('reconnect')->($self); | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub disconnected { | ||
| 103 | my $self = shift; | ||
| 104 | |||
| 105 | DEBUG && warn "State 'disconnected'\n"; | ||
| 106 | |||
| 107 | delete $self->{connect_timer}; | ||
| 108 | delete $self->{reconnect_timer}; | ||
| 109 | |||
| 110 | $self->{data} = ''; | ||
| 111 | $self->{messages} = []; | ||
| 112 | |||
| 113 | $self->{is_connected} = 0; | ||
| 114 | |||
| 115 | $self->{disconnect_timer} = AnyEvent->timer( | ||
| 116 | after => 0, | ||
| 117 | cb => sub { | ||
| 118 | $self->on('disconnect')->($self); | ||
| 119 | } | ||
| 120 | ); | ||
| 121 | |||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub id { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | $self->{id} ||= $self->_generate_id; | ||
| 129 | |||
| 130 | return $self->{id}; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub on_message { shift->on(message => @_) } | ||
| 134 | sub on_disconnect { shift->on(disconnect => @_) } | ||
| 135 | sub on_error { shift->on(error => @_) } | ||
| 136 | sub on_write { shift->on(write => @_) } | ||
| 137 | |||
| 138 | sub on { | ||
| 139 | my $self = shift; | ||
| 140 | my $event = shift; | ||
| 141 | |||
| 142 | my $name = "on_$event"; | ||
| 143 | |||
| 144 | unless (@_) { | ||
| 145 | DEBUG && warn "Event 'on_$event'\n"; | ||
| 146 | |||
| 147 | return $self->{$name}; | ||
| 148 | } | ||
| 149 | |||
| 150 | $self->{$name} = $_[0]; | ||
| 151 | |||
| 152 | return $self; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub read { | ||
| 156 | my $self = shift; | ||
| 157 | my ($data) = @_; | ||
| 158 | |||
| 159 | return $self unless defined $data; | ||
| 160 | |||
| 161 | $self->{data} .= Encode::decode('UTF-8', $data); | ||
| 162 | |||
| 163 | while (my $message = $self->_parse_data) { | ||
| 164 | $self->on('message')->($self, $message); | ||
| 165 | } | ||
| 166 | |||
| 167 | return $self; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub send_heartbeat { | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | $self->{heartbeat}++; | ||
| 174 | |||
| 175 | return $self->send_message('~h~' . $self->{heartbeat}); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub send_message { | ||
| 179 | my $self = shift; | ||
| 180 | my ($message) = @_; | ||
| 181 | |||
| 182 | $message = $self->build_message($message); | ||
| 183 | |||
| 184 | if ($self->on_write) { | ||
| 185 | $self->on('write')->($self, $message); | ||
| 186 | } | ||
| 187 | else { | ||
| 188 | $self->stage_message($message); | ||
| 189 | } | ||
| 190 | |||
| 191 | return $self; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub stage_message { | ||
| 195 | my $self = shift; | ||
| 196 | my ($message) = @_; | ||
| 197 | |||
| 198 | return if @{$self->{messages}} >= $self->{max_messages_to_stage}; | ||
| 199 | |||
| 200 | push @{$self->{messages}}, $message; | ||
| 201 | |||
| 202 | return $self; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub has_staged_messages { | ||
| 206 | my $self = shift; | ||
| 207 | |||
| 208 | return @{$self->{messages}} > 0; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub staged_message { | ||
| 212 | my $self = shift; | ||
| 213 | |||
| 214 | return shift @{$self->{messages}}; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub send_broadcast { | ||
| 218 | my $self = shift; | ||
| 219 | my ($message) = @_; | ||
| 220 | |||
| 221 | foreach my $conn (PocketIO::Pool->connections) { | ||
| 222 | next if $conn->id eq $self->id; | ||
| 223 | next unless $conn->is_connected; | ||
| 224 | |||
| 225 | $conn->send_message($message); | ||
| 226 | } | ||
| 227 | |||
| 228 | return $self; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub send_id_message { | ||
| 232 | my $self = shift; | ||
| 233 | |||
| 234 | my $message = $self->build_id_message; | ||
| 235 | |||
| 236 | $self->on('write')->($self, $message); | ||
| 237 | |||
| 238 | return $self; | ||
| 239 | } | ||
| 240 | |||
| 241 | sub build_id_message { | ||
| 242 | my $self = shift; | ||
| 243 | |||
| 244 | return $self->build_message($self->id); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub build_message { | ||
| 248 | my $self = shift; | ||
| 249 | my ($message) = @_; | ||
| 250 | |||
| 251 | if (ref $message) { | ||
| 252 | $message = '~j~' . JSON::encode_json($message); | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | $message = Encode::encode('UTF-8', $message); | ||
| 256 | } | ||
| 257 | |||
| 258 | return '~m~' . length(Encode::decode('UTF-8', $message)) . '~m~' . $message; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _generate_id { | ||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | my $string = ''; | ||
| 265 | |||
| 266 | for (1 .. 16) { | ||
| 267 | $string .= int(rand(10)); | ||
| 268 | } | ||
| 269 | |||
| 270 | return $string; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub _parse_data { | ||
| 274 | my $self = shift; | ||
| 275 | |||
| 276 | if ($self->{data} =~ s/^~m~(\d+)~m~//) { | ||
| 277 | my $length = $1; | ||
| 278 | |||
| 279 | my $message = substr($self->{data}, 0, $length, ''); | ||
| 280 | if (length($message) == $length) { | ||
| 281 | if ($message =~ m/^~h~(\d+)/) { | ||
| 282 | my $heartbeat = $1; | ||
| 283 | |||
| 284 | return $self->_parse_data; | ||
| 285 | } | ||
| 286 | elsif ($message =~ m/^~j~(.*)/) { | ||
| 287 | my $json; | ||
| 288 | |||
| 289 | try { | ||
| 290 | $json = JSON::decode_json(Encode::encode('UTF-8', $1)); | ||
| 291 | }; | ||
| 292 | |||
| 293 | return $json if defined $json; | ||
| 294 | |||
| 295 | return $self->_parse_data; | ||
| 296 | } | ||
| 297 | else { | ||
| 298 | return $message; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | $self->{data} = ''; | ||
| 304 | return; | ||
| 305 | } | ||
| 306 | |||
| 307 | 1; | ||
| 308 | __END__ |