| 1 | package DBGp::Client::AnyEvent::Connection; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use AnyEvent::Handle; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use DBGp::Client::AsyncConnection; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Scalar::Util qw(weaken blessed); | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, %args) = @_; | ||
| 12 | my $self = bless { | ||
| 13 | handle => undef, | ||
| 14 | on_stream => undef, | ||
| 15 | on_notify => undef, | ||
| 16 | connection => DBGp::Client::AsyncConnection->new(socket => $args{socket}), | ||
| 17 | }, $class; | ||
| 18 | my $weak_self = $self; | ||
| 19 | weaken($weak_self); | ||
| 20 | my $handle = AnyEvent::Handle->new( | ||
| 21 | fh => $args{socket}, | ||
| 22 | on_error => sub { | ||
| 23 | my ($handle, $fatal, $message) = @_; | ||
| 24 | |||
| 25 | $weak_self->{handle} = undef; | ||
| 26 | $handle->destroy; | ||
| 27 | $weak_self->{connection}->closed; | ||
| 28 | }, | ||
| 29 | on_read => sub { | ||
| 30 | my ($handle) = @_; | ||
| 31 | |||
| 32 | $weak_self->{connection}->add_data($handle->{rbuf}); | ||
| 33 | substr $handle->{rbuf}, 0, length($handle->{rbuf}), '' | ||
| 34 | if defined $handle->{rbuf}; | ||
| 35 | }, | ||
| 36 | on_eof => sub { | ||
| 37 | my ($handle) = @_; | ||
| 38 | |||
| 39 | $weak_self->{handle} = undef; | ||
| 40 | $handle->destroy; | ||
| 41 | $weak_self->{connection}->closed; | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | $self->{handle} = $handle; | ||
| 46 | $self->{on_stream_cb} = sub { | ||
| 47 | $weak_self->{on_stream}->(@_); | ||
| 48 | }; | ||
| 49 | $self->{on_notify_cb} = sub { | ||
| 50 | $weak_self->{on_notify}->(@_); | ||
| 51 | }; | ||
| 52 | |||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub DESTROY { | ||
| 57 | my ($self) = @_; | ||
| 58 | |||
| 59 | $self->{handle}->destroy if $self->{handle} && !$self->{handle}->destroyed; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub init { $_[0]->{connection}->init } | ||
| 63 | |||
| 64 | sub send_command { | ||
| 65 | my ($self, $callback_or_condvar, @rest) = @_; | ||
| 66 | my ($condvar, $callback); | ||
| 67 | |||
| 68 | if (!defined $callback_or_condvar) { | ||
| 69 | $condvar = AnyEvent->condvar; | ||
| 70 | $callback = sub { $condvar->send($_[0]) }; | ||
| 71 | } elsif (ref $callback_or_condvar eq 'CODE') { | ||
| 72 | $condvar = AnyEvent->condvar; | ||
| 73 | $callback = sub { $condvar->send($_[0]); $callback_or_condvar->($_[0]); }; | ||
| 74 | } elsif (blessed $callback_or_condvar && $callback_or_condvar->isa('AnyEvent::CondVar')) { | ||
| 75 | $condvar = $callback_or_condvar; | ||
| 76 | $callback = sub { $condvar->send($_[0]) }; | ||
| 77 | } else { | ||
| 78 | die "callback_or_condvar can be undefined, a code reference or a condvar"; | ||
| 79 | } | ||
| 80 | $self->{connection}->send_command($callback, @rest); | ||
| 81 | |||
| 82 | return $condvar; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub on_stream { | ||
| 86 | my ($self, $cb) = @_; | ||
| 87 | |||
| 88 | $self->{on_stream} = $cb; | ||
| 89 | if ($cb) { | ||
| 90 | $self->{connection}->on_stream($self->{on_stream_cb}); | ||
| 91 | } else { | ||
| 92 | $self->{connection}->on_stream(undef); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub on_notify { | ||
| 97 | my ($self, $cb) = @_; | ||
| 98 | |||
| 99 | $self->{on_notify} = $cb; | ||
| 100 | if ($cb) { | ||
| 101 | $self->{connection}->on_notify($self->{on_notify_cb}); | ||
| 102 | } else { | ||
| 103 | $self->{connection}->on_notify(undef); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||