IOMux/Handler.pm

filter: Statementstmt 61.0% 58 / 95 Branchbran 20.4% 9 / 44 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 70.8% 17 / 24 Podpod 93.7% 15 / 16 totaltotal 52.0% 101 / 194 CC 27 SCAR 34.9
CC
27
Cov
47%
show
110.0
extractSocket
56.0
timeout
20.0
CRAP 32.7
↑ Summary IOMux/Service/TCP.pm »
1 # Copyrights 2011-2020 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution IOMux. Meta-POD processed with OODoc
6 # into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package IOMux::Handler;
10 8 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 $VERSION = '1.01';
12  
13  
14 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use Log::Report 'iomux';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 my $start_time = time;
25  
26  
27 15 ▶sub new(@) {my $class = shift; (bless {}, $class)->init( {@_} ) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28  
29 sub init($)
30 16 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=4 SCAR=14.2
Pod undocumented
31 16 ▶ return $self if $self->{IH_name}; # already initialized
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"IH_name"} 0 16 16
32  
33 16 ▶ my $name = $self->{IH_name} = $args->{name} || "$self";
Condition33%1/3
$args->{'name'} || "$self"
l!l&&r!l&&!r
1600
MC/DC0%0/2
$args->{'name'} || "$self"
$args->{'name'} "$self"
Truth table33%1/3
A || B
A= $args->{'name'}B= "$self"
ABresult
1X1
011
000
34 16 ▶ if(my $fh = $self->{IH_fh} = $args->{fh})
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = $self->{"IH_fh"} = $args->{"fh"}) 15 1 16
35 15 { $self->{IH_fileno} = $fh->fileno;
36 15 $self->{IH_uses_ssl} = UNIVERSAL::isa($fh, 'IO::Socket::SSL');
37 }
38 16 $self;
39 }
40  
41  
42 0 ▶sub open() {panic}
Subroutine open: not called CC=1 SCAR=6.9
Pod documented
43  
44 #-------------------------
45  
46 1 ▶sub name() {shift->{IH_name}}
Subroutine name: called CC=1 SCAR=0.0
Pod documented
47 0 ▶sub mux() {shift->{IH_mux}}
Subroutine mux: not called CC=1 SCAR=6.9
Pod documented
48  
49  
50 2 ▶sub fileno() {shift->{IH_fileno}}
Subroutine fileno: called CC=1 SCAR=0.0
Pod documented
51 43 ▶sub fh() {shift->{IH_fh}}
Subroutine fh: called CC=1 SCAR=0.0
Pod documented
52 0 ▶sub usesSSL(){shift->{IH_uses_ssl}}
Subroutine usesSSL: not called CC=1 SCAR=6.9
Pod documented
53  
54 #-----------------------
55  
56 sub timeout(;$)
57 0 ▶{ my $self = shift;
Subroutine timeout: not called CC=4 SCAR=30.0
Pod documented
58 0 ▶ @_ or return $self->{IH_timeout};
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self->{"IH_timeout"} 0 0 0
59  
60 0 my $old = $self->{IH_timeout};
61 0 my $after = shift;
62 0 ▶ my $when = !$after ? undef
Branch: 2 branches
BranchTrueFalseTotal
!$after ? : 0 0 0
$after > $start_time ? : 0 0 0
63 : $after > $start_time ? $after
64 : ($after + time);
65  
66 0 $self->{IH_mux}->changeTimeout($self->{IH_fileno}, $old, $when);
67 0 $self->{IH_timeout} = $when;
68 }
69  
70  
71 sub close(;$)
72 16 ▶{ my ($self, $cb) = @_;
Subroutine close: called CC=4 SCAR=13.9
Pod documented
73 16 ▶ if(my $fh = delete $self->{IH_fh})
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"IH_fh"}) 13 3 16
74 13 ▶ { if(my $mux = $self->{IH_mux})
Branch: 1 branch
BranchTrueFalseTotal
if (my $mux = $self->{"IH_mux"}) 13 0 13
75 13 { $mux->remove($self->{IH_fileno});
76 }
77 13 $fh->close;
78 }
79 16 local $!;
80 16 ▶ $cb->($self) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 9 7 16
81 }
82  
83 #-------------------------
84  
85 sub muxInit($;$)
86 13 ▶{ my ($self, $mux, $handler) = @_;
Subroutine muxInit: called CC=3 SCAR=11.8
Pod documented
87  
88 13 $self->{IH_mux} = $mux;
89 13 weaken($self->{IH_mux});
90  
91 13 my $fileno = $self->{IH_fileno};
92 13 ▶ $mux->handler($fileno, $handler || $self);
Condition33%1/3
$handler || $self
l!l&&r!l&&!r
0130
MC/DC0%0/2
$handler || $self
$handler $self
Truth table33%1/3
A || B
A= $handlerB= $self
ABresult
1X1
011
000
93  
94 13 ▶ if(my $timeout = $self->{IH_timeout})
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = $self->{"IH_timeout"}) 0 13 13
95 0 { $mux->changeTimeout($fileno, undef, $timeout);
96 }
97  
98 13 trace "mux add #$fileno, $self->{IH_name}";
99 }
100  
101  
102 sub muxRemove()
103 13 ▶{ my $self = shift;
Subroutine muxRemove: called CC=1 SCAR=0.0
Pod documented
104 13 delete $self->{IH_mux};
105 #use Carp 'cluck';
106 #cluck "REMOVE";
107 13 trace "mux remove #$self->{IH_fileno}, $self->{IH_name}";
108 }
109  
110  
111 sub muxTimeout()
112 0 ▶{ my $self = shift;
Subroutine muxTimeout: not called CC=1 SCAR=6.9
Pod documented
113 0 error __x"timeout set on {name} but not handled", name => $self->name;
114 }
115  
116 #----------------------
117  
118  
119 #sub muxReadFlagged($) { panic "no input expected on ". shift->name }
120  
121  
122 #sub muxExceptFlagged($) { panic "exception arrived on ". shift->name }
123  
124  
125 #sub muxWriteFlagged($) { shift } # simply ignore write offers
126  
127  
128 #-------------------------
129  
130 sub show()
131 0 ▶{ my $self = shift;
Subroutine show: not called CC=10 SCAR=47.0
Pod documented
132 0 my $name = $self->name;
133 0 ▶ my $fh = $self->fh
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->fh or return 'fileno=' . $self->fileno . " is closed; name=$name" 0 0 0
134 or return "fileno=".$self->fileno." is closed; name=$name";
135  
136 0 my $mode = 'unknown';
137 0 ▶ unless($^O eq 'Win32')
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "Win32") 0 0 0
138 0 ▶ { my $flags = fcntl $fh, F_GETFL, 0 or fault "fcntl F_GETFL";
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($fh, 3, 0) or &fault("fcntl F_GETFL") 0 0 0
139 0 ▶ $mode = ($flags & O_WRONLY) ? 'w'
Branch: 3 branches
BranchTrueFalseTotal
$flags & 1 ? : 0 0 0
$flags & 0 ? : 0 0 0
$flags & 2 ? : 0 0 0
140 : ($flags & O_RDONLY) ? 'r'
141 : ($flags & O_RDWR) ? 'rw'
142 : 'p';
143 }
144  
145 0 my @show = ("fileno=".$fh->fileno, "mode=$mode");
146 0 ▶ if(my $sockopts = getsockopt $fh, SOL_SOCKET, SO_TYPE)
Branch: 1 branch
BranchTrueFalseTotal
if (my $sockopts = getsockopt($fh, 1, 3)) 0 0 0
147 { # socket
148 0 my $type = unpack "i", $sockopts;
149 0 ▶ my $kind = $type==SOCK_DGRAM ? 'UDP' : $type==SOCK_STREAM ? 'TCP'
Branch: 2 branches
BranchTrueFalseTotal
$type == 2 ? : 0 0 0
$type == 1 ? : 0 0 0
150 : 'unknown';
151 0 push @show, "sock=$kind";
152 }
153  
154 0 join ", ", @show, "name=$name";
155 }
156  
157  
158 sub fdset($$$$)
159 19 ▶{ my $self = shift;
Subroutine fdset: called CC=1 SCAR=0.0
Pod documented
160 19 $self->{IH_mux}->fdset($self->{IH_fileno}, @_);
161 }
162  
163  
164 sub extractSocket($)
165 0 ▶{ my ($thing, $args) = @_;
Subroutine extractSocket: not called CC=7 SCAR=40.3
Pod documented
166 0 ▶ my $class = ref $thing || $thing;
Condition0%0/3
ref $thing || $thing
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thing || $thing
ref $thing $thing
Truth table0%0/3
A || B
A= ref $thingB= $thing
ABresult
1X1
011
000
167  
168 0 my $socket = delete $args->{socket};
169 0 ▶ return $socket if $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket 0 0 0
170  
171 0 my @sockopts = (Blocking => 0);
172 push @sockopts, $_ => $args->{$_}
173 0 for grep /^[A-Z]/, keys %$args;
174  
175 @sockopts
176 0 ▶ or error __x"pass socket or provide parameters to create one for {pkg}"
Branch: 1 branch
BranchTrueFalseTotal
@sockopts or &error(&__x("pass socket or provide parameters to create one for {pkg}", "pkg", $class)) 0 0 0
177 , pkg => $class;
178  
179 0 my $ssl = delete $args->{use_ssl};
180  
181 # the extension will load these classes
182 0 ▶ my $make = $ssl ? 'IO::Socket::SSL' : 'IO::Socket::INET';
Branch: 1 branch
BranchTrueFalseTotal
$ssl ? : 0 0 0
183 0 ▶ $socket = $make->new(@sockopts)
Branch: 1 branch
BranchTrueFalseTotal
$socket = $make->new(@sockopts) or &fault(&__x("cannot create {pkg} socket", "pkg", $class)) 0 0 0
184 or fault __x"cannot create {pkg} socket", pkg => $class;
185  
186 0 $socket;
187 }
188  
189 8 1;
↑ Summary IOMux/Service/TCP.pm »