IOMux/Select.pm

filter: Statementstmt 35.0% 27 / 77 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 8 / 16 Podpod 66.6% 4 / 6 totaltotal 28.4% 39 / 137 CC 19 SCAR 31.6
CC
19
Cov
24%
one_go
72.0
fdset
20.0
showFlags
12.0
CRAP 23.5
« IOMux/Handler/Write.pm ↑ Summary IOMux/Net/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::Select;
10 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = '1.01';
12  
13 1 ▶use base 'IOMux';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Log::Report 'iomux';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POSIX 'errno_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 $SIG{PIPE} = 'IGNORE'; # pipes are handled in select
24  
25  
26 sub init($)
27 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
28 0 $self->SUPER::init($args);
29 0 $self->{IMS_readers} = '';
30 0 $self->{IMS_writers} = '';
31 0 $self->{IMS_excepts} = '';
32 0 $self;
33 }
34  
35 #-----------------
36  
37 sub _flags2string($);
38 sub showFlags($;$$)
39 0 ▶{ my $self = shift;
Subroutine showFlags: not called CC=3 SCAR=24.8
Pod documented
40 0 ▶ return _flags2string(shift)
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
41 if @_==1;
42  
43 0 ▶ my ($rdbits, $wrbits, $exbits) = @_ ? @_ : $self->selectFlags;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
44 0 my $rd = _flags2string $rdbits;
45 0 my $wr = _flags2string $wrbits;
46 0 my $ex = _flags2string $exbits;
47  
48 0 <<__SHOW;
49 read: $rd
50 write: $wr
51 except: $ex
52 __SHOW
53 }
54  
55 sub _flags2string($)
56 0 ▶{ my $bytes = shift;
Subroutine _flags2string: not called CC=4 SCAR=24.6
57 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 0 my $bits = length($bytes) * 8;
59 0 my $out = '';
60 0 for my $fileno (0..$bits-1)
61 0 ▶ { $out .= vec($bytes, $fileno, 1)==1 ? ($fileno%10) : '-';
Branch: 1 branch
BranchTrueFalseTotal
vec($bytes, $fileno, 1) == 1 ? : 0 0 0
62 }
63 0 $out =~ s/-+$//;
64 0 ▶ length $out ? $out : '(none)';
Branch: 1 branch
BranchTrueFalseTotal
length $out ? : 0 0 0
65 }
66  
67 #--------------------------
68  
69 sub fdset($$$$$)
70 0 ▶{ my ($self, $fileno, $state, $r, $w, $e) = @_;
Subroutine fdset: not called CC=4 SCAR=30.0
Pod documented
71 0 ▶ vec($self->{IMS_readers}, $fileno, 1) = $state if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
72 0 ▶ vec($self->{IMS_writers}, $fileno, 1) = $state if $w;
Branch: 1 branch
BranchTrueFalseTotal
if $w 0 0 0
73 0 ▶ vec($self->{IMS_excepts}, $fileno, 1) = $state if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 0 0
74 # trace "fdset(@_), now: " .$self->showFlags($self->waitFlags);
75 }
76  
77 sub one_go($$)
78 0 ▶{ my ($self, $wait, $heartbeat) = @_;
Subroutine one_go: not called CC=8 SCAR=42.8
Pod undocumented
79  
80 #trace "SELECT=\n".$self->showFlags($self->waitFlags);
81  
82 my ($rdready, $wrready, $exready)
83 0 = @$self{ qw/IMS_readers IMS_writers IMS_excepts/ };
84  
85 0 my ($numready, $timeleft)
86 = select $rdready, $wrready, $exready, $wait;
87 0 info "time left: $timeleft";
88  
89 #trace "READY=\n".$self->showFlags($rdready, $wrready, $exready);
90  
91 0 ▶ if($heartbeat)
Branch: 1 branch
BranchTrueFalseTotal
if ($heartbeat) 0 0 0
92 { # can be collected from within heartbeat
93 0 $self->{IMS_select_flags} = [$rdready, $wrready, $exready];
94 0 $heartbeat->($self, $numready, $timeleft)
95 }
96  
97 0 ▶ unless(defined $numready)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $numready) 0 0 0
98 0 ▶ { return if $! == EINTR || $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 or $! == 11 0 0 0
Condition0%0/3
$! == 4 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 4 or $! == 11
$! == 4 $! == 11
Truth table0%0/3
A or B
A= $! == 4B= $! == 11
ABresult
1X1
011
000
99 0 alert "leaving loop";
100 0 return 0;
101 }
102  
103 # Hopefully the regexp improves performance when many slow connections
104 0 ▶ $self->_ready(muxReadFlagged => $rdready) if $rdready =~ m/[^\x00]/;
Branch: 1 branch
BranchTrueFalseTotal
if $rdready =~ /[^\x00]/ 0 0 0
105 0 ▶ $self->_ready(muxWriteFlagged => $wrready) if $wrready =~ m/[^\x00]/;
Branch: 1 branch
BranchTrueFalseTotal
if $wrready =~ /[^\x00]/ 0 0 0
106 0 ▶ $self->_ready(muxExceptFlagged => $exready) if $exready =~ m/[^\x00]/;
Branch: 1 branch
BranchTrueFalseTotal
if $exready =~ /[^\x00]/ 0 0 0
107  
108 0 info "sleeping 1";
109 0 sleep 1;
110 0 1; # success
111 }
112  
113 # It would be nice to have an algorithm which is better than O(n)
114 sub _ready($$)
115 0 ▶{ my ($self, $call, $flags) = @_;
Subroutine _ready: not called CC=3 SCAR=24.8
116 0 my $handlers = $self->_handlers;
117 0 ▶ while(my ($fileno, $conn) = each %$handlers)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$handlers) 0 0 0
118 0 ▶ { $conn->$call($fileno) if (vec $flags, $fileno, 1)==1;
Branch: 1 branch
BranchTrueFalseTotal
if vec($flags, $fileno, 1) == 1 0 0 0
119 #warn "$conn $call($fileno)" if (vec $flags, $fileno, 1)==1;
120 }
121 }
122  
123  
124 sub waitFlags()
125 0 ▶{ my $self = shift;
Subroutine waitFlags: not called CC=1 SCAR=6.9
Pod documented
126 0 @{$self}{ qw/IMS_readers IMS_writers IMS_excepts/ };
127 }
128  
129  
130 0 ▶sub selectFlags() { @{shift->{IMS_select_flags} || []} }
Subroutine selectFlags: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{'IMS_select_flags'} || []
l!l
00
MC/DC0%0/1
+(shift())->{'IMS_select_flags'} || []
+(shift())->{'IMS_select_flags'}
Truth table0%0/2
A
A= +(shift())->{'IMS_select_flags'}
Aresult
11
00
131  
132 1 1;
133  
134 __END__
« IOMux/Handler/Write.pm ↑ Summary IOMux/Net/TCP.pm »