« lib/auto/share/dist/App-SocialCalc-Multiplayer/socketpolicy.pl
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/Base.pm »
| 1 | package PocketIO::Pool; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Scalar::Util qw(blessed); | ||
| 7 | |||
| 8 | use PocketIO::Connection; | ||
| 9 | |||
| 10 | use constant DEBUG => $ENV{POCKETIO_POOL_DEBUG}; | ||
| 11 | |||
| 12 | sub find_connection { | ||
| 13 | my $self = shift; | ||
| 14 | my ($conn) = @_; | ||
| 15 | |||
| 16 | my $id = blessed $conn ? $conn->id : $conn; | ||
| 17 | |||
| 18 | return $self->_instance->{connections}->{$id}; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub connections { | ||
| 22 | my $self = shift; | ||
| 23 | |||
| 24 | return values %{$self->_instance->{connections}}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub add_connection { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | my $conn = $self->_build_connection(@_); | ||
| 31 | |||
| 32 | $self->_instance->{connections}->{$conn->id} = $conn; | ||
| 33 | |||
| 34 | $conn->connecting; | ||
| 35 | |||
| 36 | DEBUG && warn "Added connection '" . $conn->id . "'\n"; | ||
| 37 | |||
| 38 | return $conn; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub remove_connection { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | my $id = blessed $_[0] ? $_[0]->id : $_[0]; | ||
| 45 | |||
| 46 | delete $self->_instance->{connections}->{$id}; | ||
| 47 | |||
| 48 | DEBUG && warn "Removed connection '" . $id . "'\n"; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _instance { | ||
| 52 | my $class = shift; | ||
| 53 | |||
| 54 | no strict; | ||
| 55 | |||
| 56 | ${"$class\::_instance"} ||= $class->_new_instance(@_); | ||
| 57 | |||
| 58 | return ${"$class\::_instance"}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub _new_instance { | ||
| 62 | my $class = shift; | ||
| 63 | |||
| 64 | my $self = bless {@_}, $class; | ||
| 65 | |||
| 66 | $self->{connections} = {}; | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _build_connection { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | return PocketIO::Connection->new(@_, | ||
| 75 | on_connect_failed => sub { $self->remove_connection(@_) }); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | 1; | ||
| 80 | __END__ |