Connection/Raw.pm

filter: Statementstmt 50.7% 70 / 138 Branchbran 21.8% 14 / 64 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 56.0% 14 / 25 Podpod 11.1% 1 / 9 totaltotal 40.1% 100 / 249 CC 41 SCAR 26.8
CC
41
Cov
40%
push_write
42.0
say
42.0
recv
20.0
CRAP 14.6
↑ Summary Connection.pm »
1 package #hide
2 AnyEvent::Connection::Raw;
3  
4 2 ▶use common::sense 2;m{
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use strict;
6 use warnings;
7 };
8 2 ▶use Object::Event 1.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use base 'Object::Event';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use AnyEvent::Connection::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # @rewrite s/^# //;
15 # use Devel::Leak::Cb;
16  
17 4 ▶sub _call_waiting {
Subroutine _call_waiting: called CC=4 SCAR=14.1
18 4 my $me = shift;
19 4 for my $k (keys %{ $me->{waitingcb} }) {
20 1 ▶ warn "call waiting $k with @_" if $me->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"debug"} 0 1 1
21 1 ▶ if ($me->{waitingcb}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"waitingcb"}{$k}) 1 0 1
22 1 $me->{waitingcb}{$k}->(undef, @_);
23 }
24 1 delete $me->{waitingcb}{$k};
25 }
26 }
27  
28 4 ▶sub new {
Subroutine new: called CC=4 SCAR=16.1
Pod documented
29 4 my $pkg = shift;
30 4 my $self = $pkg->SUPER::new(@_);
31 4 ▶ $self->{nl} = "\015\012" unless defined $self->{nl};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"nl"} 4 0 4
32 4 ▶ $self->{debug} = 0 unless defined $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"debug"} 0 4 4
33 4 weaken(my $me = $self);
34 # @rewrite s/sub /cb 'conn.cb.eof' /;
35 0 ▶ $self->{cb}{eof} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
36 0 ▶ $me or return;
Branch: 1 branch
BranchTrueFalseTotal
$me or return 0 0 0
37 #local *__ANON__ = 'conn.cb.eof';
38 0 warn "[\U$me->{side}\E] Eof on handle";
39 0 delete $me->{h};
40 0 $me->event('disconnect');
41 0 $me->_call_waiting("EOF from handle");
42 4 } ;
43 # @rewrite s/sub /cb 'conn.cb.err' /;
44 1 ▶ $self->{cb}{err} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
45 1 ▶ $me or return;
Branch: 1 branch
BranchTrueFalseTotal
$me or return 1 0 1
46 #local *__ANON__ = 'conn.cb.err';
47 #use Carp;Carp::cluck((0+$!).": $!");
48 1 my $e = "$!";
49 1 ▶ if ( $me->{destroying} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{'destroying'}) { } 0 1 1
50 0 warn "err on destroy";
51 0 $e = "Connection closed";
52 } else {
53 #warn "[\U$me->{side}\E] Error on handle: $e"; # uncomment
54 }
55 1 delete $me->{h};
56 1 $self->event( disconnect => "Error: $e" );
57 1 $me->_call_waiting($e);
58 4 };
59 4 ▶ $self->{timeout} ||= 30;
Condition50%1/2
$self->{"timeout"} ||= 30
l!l
40
MC/DC0%0/1
$self->{"timeout"} ||= 30
$self->{"timeout"}
Truth table50%1/2
A
A= $self->{"timeout"}
Aresult
11
00
60 $self->{h} = AnyEvent::Handle->new(
61 fh => $self->{fh},
62 autocork => 1,
63 keepalive => 1,
64 on_eof => $self->{cb}{eof},
65 on_error => $self->{cb}{err},
66 4 );
67 4 $self;
68 }
69  
70 4 ▶sub destroy {
Subroutine destroy: called CC=1 SCAR=0.0
Pod undocumented
71 4 my ($self) = @_;
72 4 $self->DESTROY;
73 4 bless $self, "AnyEvent::Connection::Raw::destroyed";
74 }
75 2 *close = \&destroy;
76 1 ▶sub AnyEvent::Connection::Raw::destroyed::AUTOLOAD {}
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
77 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
78 4 my $self = shift;
79 4 ▶ warn "(".int($self).") Destroying AE::CNN::Raw" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 4 4
80 4 delete $self->{fh};
81 4 $self->_call_waiting("destroying connection");
82 4 ▶ $self->{h} and $self->{h}->destroy;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} and $self->{"h"}->destroy 3 1 4
83 4 delete $self->{h};
84 4 %$self = ();
85 4 return;
86 }
87  
88 0 ▶sub push_write {
Subroutine push_write: not called CC=6 SCAR=37.4
Pod undocumented
89 0 my $self = shift;
90 0 ▶ $self->{h} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return 0 0 0
91 0 for (@_) {
92 0 ▶ if (!ref and utf8::is_utf8($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_ and utf8::is_utf8($_)) 0 0 0
Condition0%0/3
not ref $_ and utf8::is_utf8($_)
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_ and utf8::is_utf8($_)
not ref $_ utf8::is_utf8($_)
Truth table0%0/3
not(A) and B
A= not ref $_B= utf8::is_utf8($_)
ABresult
0X0
100
111
93 0 $_ = $_;
94 0 utf8::encode $_;
95 }
96 }
97 0 $self->{h}->push_write(@_);
98 0 ▶ warn ">> @_ " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
99 }
100  
101 0 ▶sub push_read {
Subroutine push_read: not called CC=3 SCAR=24.8
Pod undocumented
102 0 my $self = shift;
103 0 my $cb = pop;
104 0 ▶ $self->{h} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return 0 0 0
105 0 ▶ $self->{h}->timeout($self->{timeout}) if $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"timeout"} 0 0 0
106 0 ▶ $self->{h}->push_read(@_,sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 shift->timeout(undef); # disable timeout and remove handle from @_
108 0 $cb->($self,@_);
109 0 undef $cb;
110 0 });
111 }
112  
113 0 ▶sub unshift_read {
Subroutine unshift_read: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my $self = shift;
115 0 ▶ $self->{h} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return 0 0 0
116 0 $self->{h}->unshift_read(@_);
117 }
118  
119 0 ▶sub say {
Subroutine say: not called CC=6 SCAR=37.4
Pod undocumented
120 0 my $self = shift;
121 0 ▶ $self->{h} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return 0 0 0
122 0 for (@_) {
123 0 ▶ if (!ref and utf8::is_utf8($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_ and utf8::is_utf8($_)) 0 0 0
Condition0%0/3
not ref $_ and utf8::is_utf8($_)
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_ and utf8::is_utf8($_)
not ref $_ utf8::is_utf8($_)
Truth table0%0/3
not(A) and B
A= not ref $_B= utf8::is_utf8($_)
ABresult
0X0
100
111
124 0 $_ = $_;
125 0 utf8::encode $_;
126 }
127 }
128 0 $self->{h}->push_write("@_$self->{nl}");
129 0 ▶ warn ">> @_ " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
130 0 return;
131 }
132 2 *reply = \&say;
133  
134 0 ▶sub recv {
Subroutine recv: not called CC=4 SCAR=30.0
Pod undocumented
135 0 my ($self,$bytes,%args) = @_;
136 0 ▶ $args{cb} or croak "no cb for recv at @{[ (caller)[1,2] ]}";
Branch: 1 branch
BranchTrueFalseTotal
$args{"cb"} or croak("no cb for recv at @{[(caller)[1, 2]];}") 0 0 0
137 0 ▶ $self->{h} or return $args{cb}(undef,"Not connected");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return $args{"cb"}->(undef, "Not connected") 0 0 0
138 0 ▶ warn "<+ read $bytes " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
139 0 weaken( $self->{waitingcb}{int $args{cb}} = $args{cb} );
140 0 ▶ $self->{h}->unshift_read( chunk => $bytes, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
141 #local *__ANON__ = 'conn.recv.read';
142 # Also eat CRLF or LF from read buffer
143 0 ▶ substr( $self->{h}{rbuf}, 0, 1 ) = '' if substr( $self->{h}{rbuf}, 0, 1 ) eq "\015";
Branch: 1 branch
BranchTrueFalseTotal
if substr($self->{"h"}{"rbuf"}, 0, 1) eq "\r" 0 0 0
144 0 ▶ substr( $self->{h}{rbuf}, 0, 1 ) = '' if substr( $self->{h}{rbuf}, 0, 1 ) eq "\012";
Branch: 1 branch
BranchTrueFalseTotal
if substr($self->{"h"}{"rbuf"}, 0, 1) eq "\n" 0 0 0
145 0 delete $self->{waitingcb}{int $args{cb}};
146 0 shift; (delete $args{cb})->(@_);
147 0 %args = ();
148 0 } );
149 }
150  
151 1 ▶sub command {
Subroutine command: called CC=6 SCAR=25.0
Pod undocumented
152 1 my $self = shift;
153 1 my $write = shift;
154 1 ▶ if (utf8::is_utf8($write)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($write)) 0 1 1
155 0 utf8::encode $write;
156 }
157 1 my %args = @_;
158 0 ▶ $args{cb} or croak "no cb for command at @{[ (caller)[1,2] ]}";
Branch: 1 branch
BranchTrueFalseTotal
$args{"cb"} or croak("no cb for command at @{[(caller)[1, 2]];}") 1 0 1
159 1 ▶ $self->{h} or return $args{cb}(undef,"Not connected"),%args = ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return $args{"cb"}->(undef, "Not connected"), %args = () 1 0 1
160 1 weaken( $self->{waitingcb}{int $args{cb}} = $args{cb} );
161
162 #my $i if 0;
163 #my $c = ++$i;
164 1 ▶ warn ">> $write " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
165 1 $self->{h}->push_write("$write$self->{nl}");
166 #$self->{h}->timeout( $self->{select_timeout} );
167 1 ▶ warn "<? read " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
168 # @rewrite s/sub {/cb 'conn.command.read' {/;
169 0 ▶ $self->{h}->push_read( regex => qr<\015?\012>, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
170 #local *__ANON__ = 'conn.command.read';
171 0 shift;
172 0 for (@_) {
173 0 chomp;
174 0 ▶ substr($_,-1,1) = '' if substr($_, -1,1) eq "\015";
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, -1, 1) eq "\r" 0 0 0
175 }
176 0 ▶ warn "<< @_ " if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
177 0 delete $self->{waitingcb}{int $args{cb}};
178 0 delete($args{cb})->(@_);
179 0 %args = ();
180 0 undef $self;
181 1 } );
182 #sub {
183 #$self->{state}{handle}->timeout( 0 ) if $self->_qsize < 1;
184 #diag "<< $c. $write: $_[1] (".$self->_qsize."), timeout ".($self->{state}{handle}->timeout ? 'enabled' : 'disabled');
185 #$cb->(@_);
186 #});
187 }
188  
189 # Serverside feature
190 0 ▶sub want_command {
Subroutine want_command: not called CC=2 SCAR=17.9
Pod undocumented
191 0 my $self = shift;
192 0 ▶ $self->{h} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"h"} or return 0 0 0
193 # @rewrite s/sub {/cb 'conn.wand_command.read' {/;
194 0 ▶ $self->{h}->push_read( regex => qr<\015?\012>, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
195 #local *__ANON__ = 'conn.want_command.read';
196 0 shift;
197 0 for (@_) {
198 0 chomp;
199 0 ▶ substr($_,-1,1) = '' if substr($_, -1,1) eq "\015";
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, -1, 1) eq "\r" 0 0 0
200 }
201 0 $self->event( command => @_ );
202 0 $self->want_command;
203 0 });
204 }
205  
206 2 1;
↑ Summary Connection.pm »