| 1 | package Cache::Memcached::Async::Socket; | ||
| 2 | |||
| 3 | use Danga::Socket; | ||
| 4 | use IO::Socket::INET; | ||
| 5 | |||
| 6 | use base 'Danga::Socket'; | ||
| 7 | use fields qw(buf handlers connect_callback); | ||
| 8 | |||
| 9 | sub new { | ||
| 10 | my Cache::Memcached::Async::Socket $self = shift; | ||
| 11 | my $addr = shift; | ||
| 12 | my %opts = @_; | ||
| 13 | |||
| 14 | my $sock = IO::Socket::INET->new(PeerAddr => $addr, Blocking => 0); | ||
| 15 | |||
| 16 | $self = fields::new($self) unless ref $self; | ||
| 17 | |||
| 18 | $self->SUPER::new($sock); | ||
| 19 | |||
| 20 | $self->{handlers} = []; | ||
| 21 | $self->{connect_callback} = $opts{callback}; | ||
| 22 | $self->{buf} = ''; | ||
| 23 | |||
| 24 | $self->watch_write(1); | ||
| 25 | |||
| 26 | return $self; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub event_write { | ||
| 30 | my Cache::Memcached::Async::Socket $self = shift; | ||
| 31 | |||
| 32 | my $connect_callback = $self->{connect_callback}; | ||
| 33 | |||
| 34 | $connect_callback->() if $connect_callback; | ||
| 35 | |||
| 36 | $self->watch_write(0); | ||
| 37 | |||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub line_parser { | ||
| 42 | my ($bufref, $callback) = @_; | ||
| 43 | if ($$bufref =~ s/^(.*?)\r\n//) { | ||
| 44 | $callback->($1); | ||
| 45 | return 1; | ||
| 46 | } | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub event_read { | ||
| 51 | my Cache::Memcached::Async::Socket $self = shift; | ||
| 52 | |||
| 53 | my $readref = $self->read(65536); | ||
| 54 | |||
| 55 | return unless defined $readref && defined $$readref; | ||
| 56 | |||
| 57 | my $bufref = \$self->{buf}; | ||
| 58 | my $handlers = $self->{handlers}; | ||
| 59 | |||
| 60 | $$bufref .= $$readref; | ||
| 61 | |||
| 62 | while (@$handlers) { | ||
| 63 | my $parser = $handlers->[0]->{parser} || \&line_parser; | ||
| 64 | my $callback = $handlers->[0]->{callback}; | ||
| 65 | my $rv = $parser->($bufref, $callback); | ||
| 66 | |||
| 67 | # If the last parser returned false, then we've got an incomplete run on that parser so far. | ||
| 68 | last unless $rv; | ||
| 69 | |||
| 70 | # If we get here, then the previous parser has finished and is ready to move on. | ||
| 71 | shift @$handlers; | ||
| 72 | } | ||
| 73 | |||
| 74 | $self->watch_read(0) unless @$handlers; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub handle_error { | ||
| 78 | my Cache::Memcached::Async::Socket $self = shift; | ||
| 79 | |||
| 80 | # on errors, let clients know they're not going to get anything back | ||
| 81 | my $handlers = $self->{handlers}; | ||
| 82 | while (@$handlers) { | ||
| 83 | my $handler = shift @$handlers; | ||
| 84 | my $callback = $handler->{callback}; | ||
| 85 | $callback->() if $callback; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub event_err { | ||
| 90 | shift->handle_error(@_); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub event_hup { | ||
| 94 | shift->handle_error(@_); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub close { | ||
| 98 | my $self = shift; | ||
| 99 | $self->handle_error(); | ||
| 100 | $self->SUPER::close(@_); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub run { | ||
| 104 | my Cache::Memcached::Async::Socket $self = shift; | ||
| 105 | my $command = shift; | ||
| 106 | my $callback = shift; | ||
| 107 | my $parser = shift; | ||
| 108 | my $timeout = shift; | ||
| 109 | |||
| 110 | my $handlers = $self->{handlers}; | ||
| 111 | |||
| 112 | { | ||
| 113 | local $SIG{PIPE} = "IGNORE"; | ||
| 114 | $self->write($command); | ||
| 115 | } | ||
| 116 | |||
| 117 | $self->watch_read(1); | ||
| 118 | |||
| 119 | my $timer; | ||
| 120 | if ($timeout) { | ||
| 121 | $timer = Danga::Socket->AddTimer($timeout, sub { $self->close }); | ||
| 122 | } | ||
| 123 | |||
| 124 | push @$handlers, { | ||
| 125 | callback => sub { | ||
| 126 | $timer->cancel() if $timer; | ||
| 127 | $callback->(@_) if $callback; | ||
| 128 | }, | ||
| 129 | parser => $parser, | ||
| 130 | }; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; |