| 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::Poll; | ||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | $VERSION = '1.01'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use base 'IOMux'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | use Log::Report 'iomux'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | use List::Util 'min'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | use POSIX 'errno_h'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | use IO::Poll; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | $SIG{PIPE} = 'IGNORE'; # pipes are handled in select | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | my $poll; | |||||||||||||||||||||||||||
| 29 | sub init($) | ||||||||||||||||||||||||||||
| 30 | 7 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 7 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 32 | 7 | ▶ | $poll ||= IO::Poll->new; | ||||||||||||||||||||||||||
|
Condition33%1/3
$poll ||= "IO::Poll"->new
MC/DC0%0/2
$poll ||= "IO::Poll"->new
$poll
"IO::Poll"->new
Truth table33%1/3
A ||= B
A= $pollB= "IO::Poll"->new
| |||||||||||||||||||||||||||||
| 33 | 7 | $self; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | #------------- | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub poller {$poll} | ||||||||||||||||||||||||||
|
Subroutine
poller: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | #------------- | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub fdset($$$$$) | ||||||||||||||||||||||||||||
| 43 | 32 | ▶ | { my ($self, $fileno, $state, $r, $w, $e) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fdset: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 32 | ▶ | my $conn = $self->handler($fileno) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 19 | my $fh = $conn->fh; | |||||||||||||||||||||||||||
| 46 | 19 | ▶ | my $mask = $poll->mask($fh) || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$poll->mask($fh) || 0
MC/DC100%1/1
$poll->mask($fh) || 0
$poll->mask($fh)
Truth table100%2/2
A
A= $poll->mask($fh)
| |||||||||||||||||||||||||||||
| 47 | 19 | ▶ | if($state==0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 13 | ▶ | { $mask &= ~POLLIN if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 13 | ▶ | $mask &= ~POLLOUT if $w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 13 | ▶ | $mask &= ~POLLERR if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else | ||||||||||||||||||||||||||||
| 53 | 6 | ▶ | { $mask |= POLLIN if $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | $mask |= POLLOUT if $w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 6 | ▶ | $mask |= POLLERR if $e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 19 | $poll->mask($fh, $mask); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub one_go($$) | ||||||||||||||||||||||||||||
| 61 | 12 | ▶ | { my ($self, $wait, $heartbeat) = @_; | ||||||||||||||||||||||||||
|
Subroutine
one_go: called CC=6 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 12 | my $numready = $poll->poll($wait); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | ▶ | $heartbeat->($self, $numready, undef) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | if $heartbeat; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 12 | ▶ | if($numready < 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 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
| |||||||||||||||||||||||||||||
| 70 | 0 | alert "leaving loop"; | |||||||||||||||||||||||||||
| 71 | 0 | return 0; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | $numready | ||||||||||||||||||||||||||||
| 75 | 12 | ▶ | or return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 12 | $self->_ready(muxReadFlagged => POLLIN|POLLHUP); | |||||||||||||||||||||||||||
| 78 | 12 | $self->_ready(muxWriteFlagged => POLLOUT); | |||||||||||||||||||||||||||
| 79 | 12 | $self->_ready(muxExceptFlagged => POLLERR); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | 1; # success | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # It would be nice to have an algorithm which is better than O(n) | ||||||||||||||||||||||||||||
| 85 | sub _ready($$) | ||||||||||||||||||||||||||||
| 86 | 36 | ▶ | { my ($self, $call, $mask) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_ready: called CC=5 SCAR=18.6
| |||||||||||||||||||||||||||||
| 87 | 36 | my $handlers = $self->_handlers; | |||||||||||||||||||||||||||
| 88 | 36 | foreach my $fh ($poll->handles($mask)) | |||||||||||||||||||||||||||
| 89 | 12 | ▶ | { my $fileno = $fh->fileno or next; # close filehandle | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 12 | ▶ | if(my $conn = $handlers->{$fileno}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | { # filehandle flagged | ||||||||||||||||||||||||||||
| 92 | 12 | $conn->$call($fileno); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else | ||||||||||||||||||||||||||||
| 95 | { # Handler administration error, but when write and error it may | ||||||||||||||||||||||||||||
| 96 | # be caused by read errors. | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | alert "connection for ".$fh->fileno." not registered in $call" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | if $call eq 'muxReadFlagged'; | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 8 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||