| 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
| |||||||||||||||||||||||||||||
| 41 | if @_==1; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ($rdbits, $wrbits, $exbits) = @_ ? @_ : $self->selectFlags; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | $out =~ s/-+$//; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | length $out ? $out : '(none)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | vec($self->{IMS_writers}, $fileno, 1) = $state if $w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | vec($self->{IMS_excepts}, $fileno, 1) = $state if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | { return if $! == EINTR || $! == EAGAIN; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$! == 4 or $! == 11
MC/DC0%0/2
$! == 4 or $! == 11
$! == 4
$! == 11
Truth table0%0/3
A or B
A= $! == 4B= $! == 11
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $self->_ready(muxWriteFlagged => $wrready) if $wrready =~ m/[^\x00]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $self->_ready(muxExceptFlagged => $exready) if $exready =~ m/[^\x00]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | { $conn->$call($fileno) if (vec $flags, $fileno, 1)==1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'} || []
MC/DC0%0/1
+(shift())->{'IMS_select_flags'} || []
+(shift())->{'IMS_select_flags'}
Truth table0%0/2
A
A= +(shift())->{'IMS_select_flags'}
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||