« lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO.pm
↑ Summary
lib/auto/share/dist/App-SocialCalc-Multiplayer/socketpolicy.pl »
| 1 | package PocketIO::Handle; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use AnyEvent::Handle; | ||
| 7 | |||
| 8 | use constant DEBUG => $ENV{POCKETIO_HANDLE_DEBUG}; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my $class = shift; | ||
| 12 | my ($fh) = @_; | ||
| 13 | |||
| 14 | my $self = {handle => AnyEvent::Handle->new(fh => $fh)}; | ||
| 15 | bless $self, $class; | ||
| 16 | |||
| 17 | $self->{heartbeat_timeout} ||= 10; | ||
| 18 | |||
| 19 | $fh->autoflush; | ||
| 20 | |||
| 21 | $self->{handle}->no_delay(1); | ||
| 22 | $self->{handle}->on_eof(sub { warn "Unhandled handle eof" }); | ||
| 23 | $self->{handle}->on_error(sub { warn "Unhandled handle error: $_[2]" }); | ||
| 24 | |||
| 25 | # This is needed for the correct EOF handling | ||
| 26 | $self->{handle}->on_read(sub { }); | ||
| 27 | |||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub on_heartbeat { | ||
| 32 | my $self = shift; | ||
| 33 | my ($cb) = @_; | ||
| 34 | |||
| 35 | $self->{handle}->timeout($self->{heartbeat_timeout}); | ||
| 36 | $self->{handle}->on_timeout($cb); | ||
| 37 | |||
| 38 | return $self; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub on_read { | ||
| 42 | my $self = shift; | ||
| 43 | my ($cb) = @_; | ||
| 44 | |||
| 45 | $self->{handle}->on_read( | ||
| 46 | sub { | ||
| 47 | my $handle = shift; | ||
| 48 | |||
| 49 | $handle->push_read( | ||
| 50 | sub { | ||
| 51 | $cb->($self, $_[0]->rbuf); | ||
| 52 | } | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | ); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub on_eof { | ||
| 61 | my $self = shift; | ||
| 62 | my ($cb) = @_; | ||
| 63 | |||
| 64 | $self->{handle}->on_eof( | ||
| 65 | sub { | ||
| 66 | $cb->($self); | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub on_error { | ||
| 74 | my $self = shift; | ||
| 75 | my ($cb) = @_; | ||
| 76 | |||
| 77 | $self->{handle}->on_error( | ||
| 78 | sub { | ||
| 79 | $cb->($self); | ||
| 80 | } | ||
| 81 | ); | ||
| 82 | |||
| 83 | return $self; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub write { | ||
| 87 | my $self = shift; | ||
| 88 | my ($chunk, $cb) = @_; | ||
| 89 | |||
| 90 | my $handle = $self->{handle}; | ||
| 91 | return $self unless $handle && $handle->fh; | ||
| 92 | |||
| 93 | $handle->push_write($chunk); | ||
| 94 | |||
| 95 | if ($cb) { | ||
| 96 | $handle->on_drain( | ||
| 97 | sub { | ||
| 98 | my $handle = shift; | ||
| 99 | |||
| 100 | $handle->on_drain(undef); | ||
| 101 | |||
| 102 | $cb->($self); | ||
| 103 | } | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | return $self; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub close { | ||
| 111 | my $self = shift; | ||
| 112 | |||
| 113 | my $handle = delete $self->{handle}; | ||
| 114 | return $self unless $handle; | ||
| 115 | |||
| 116 | $handle->timeout(0); | ||
| 117 | |||
| 118 | $handle->on_drain; | ||
| 119 | $handle->on_error; | ||
| 120 | |||
| 121 | $handle->on_drain(sub { | ||
| 122 | shutdown $_[0]->fh, 1; | ||
| 123 | close $handle->fh; | ||
| 124 | |||
| 125 | $_[0]->destroy; | ||
| 126 | undef $handle; | ||
| 127 | }); | ||
| 128 | |||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | 1; | ||
| 133 | __END__ |