| 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::Read; | ||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 7 | $VERSION = '1.01'; | |||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use base 'IOMux::Handler'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use Log::Report 'iomux'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | use Fcntl; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | use POSIX 'errno_h'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | use File::Basename 'basename'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | sub init($) | ||||||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 26 | 8 | $self->SUPER::init($args); | |||||||||||||||||||||||||||||||||
| 27 | 8 | ▶ | $self->{IMHR_read_size} = $args->{read_size} || 32768; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'read_size'} || 32768
MC/DC100%1/1
$args->{'read_size'} || 32768
$args->{'read_size'}
Truth table100%2/2
A
A= $args->{'read_size'}
| |||||||||||||||||||||||||||||||||||
| 28 | 8 | $self->{IMHR_inbuf} = ''; | |||||||||||||||||||||||||||||||||
| 29 | 8 | $self; | |||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||
| 32 | #------------------- | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | sub readSize(;$) | ||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
readSize: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | @_ ? $self->{IMHR_read_size} = shift : $self->{IMHR_read_size}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | #----------------------- | ||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||
| 41 | sub readline($) | ||||||||||||||||||||||||||||||||||
| 42 | 8 | ▶ | { my ($self, $cb) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
readline: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 43 | 8 | ▶ | if($self->{IMHR_inbuf} =~ s/^([^\r\n]*)(?:\r?\n)//) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 44 | 4 | { return $cb->($self, "$1\n"); | |||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | if($self->{IMHR_eof}) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 47 | { # eof already before readline and no trailing nl | ||||||||||||||||||||||||||||||||||
| 48 | 0 | my $line = $self->{IMHR_inbuf}; | |||||||||||||||||||||||||||||||||
| 49 | 0 | $self->{IMHR_inbuf} = ''; | |||||||||||||||||||||||||||||||||
| 50 | 0 | return $cb->($self, $line); | |||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | $self->{IMHR_read_more} = sub | ||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | { my ($in, $eof) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if($eof) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 56 | 2 | { delete $self->{IMHR_read_more}; | |||||||||||||||||||||||||||||||||
| 57 | 2 | my $line = $self->{IMHR_inbuf}; | |||||||||||||||||||||||||||||||||
| 58 | 2 | $self->{IMHR_inbuf} = ''; | |||||||||||||||||||||||||||||||||
| 59 | 2 | return $cb->($self, $line); | |||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | ${$_[0]} =~ s/^([^\r\n]*)\r?\n// | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 62 | or return; | ||||||||||||||||||||||||||||||||||
| 63 | 2 | delete $self->{IMHR_read_more}; | |||||||||||||||||||||||||||||||||
| 64 | 2 | $cb->($self, "$1\n"); | |||||||||||||||||||||||||||||||||
| 65 | 4 | }; | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | sub slurp($) | ||||||||||||||||||||||||||||||||||
| 70 | 4 | ▶ | { my ($self, $cb) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
slurp: called CC=3 SCAR=12.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if($self->{IMHR_eof}) # eof already before readline | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | { my $in = $self->{IMHR_inbuf} or return $cb->($self, \''); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 0 | my $bytes = $$in; # does copy the bytes. Cannot help it easily | |||||||||||||||||||||||||||||||||
| 75 | 0 | $$in = ''; | |||||||||||||||||||||||||||||||||
| 76 | 0 | return $cb->($self, \$bytes); | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | $self->{IMHR_read_more} = sub | ||||||||||||||||||||||||||||||||||
| 80 | 8 | ▶ | { my ($in, $eof) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 81 | 8 | ▶ | $eof or return; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 82 | 4 | delete $self->{IMHR_read_more}; | |||||||||||||||||||||||||||||||||
| 83 | 4 | my $bytes = $$in; # does copy the bytes | |||||||||||||||||||||||||||||||||
| 84 | 4 | $$in = ''; | |||||||||||||||||||||||||||||||||
| 85 | 4 | $cb->($self, \$bytes); | |||||||||||||||||||||||||||||||||
| 86 | 4 | }; | |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | #------------------------- | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | sub muxInit($) | ||||||||||||||||||||||||||||||||||
| 92 | 6 | ▶ | { my ($self, $mux) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
muxInit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 93 | 6 | $self->SUPER::muxInit($mux); | |||||||||||||||||||||||||||||||||
| 94 | 6 | $self->fdset(1, 1, 0, 0); | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | sub muxReadFlagged($) | ||||||||||||||||||||||||||||||||||
| 98 | 12 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||||||||
|
Subroutine
muxReadFlagged: called CC=6 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | my $bytes_read | ||||||||||||||||||||||||||||||||||
| 101 | = sysread $self->fh, $self->{IMHR_inbuf}, $self->{IMHR_read_size} | ||||||||||||||||||||||||||||||||||
| 102 | 12 | , length($self->{IMHR_inbuf}); | |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 12 | ▶ | if($bytes_read) # > 0 | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 105 | 6 | { $self->muxInput(\$self->{IMHR_inbuf}); | |||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||
| 107 | elsif(defined $bytes_read) # == 0 | ||||||||||||||||||||||||||||||||||
| 108 | 6 | { $self->fdset(0, 1, 0, 0); | |||||||||||||||||||||||||||||||||
| 109 | 6 | $self->muxEOF(\$self->{IMHR_inbuf}); | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | ▶ | elsif($!==EINTR || $!==EAGAIN || $!==EWOULDBLOCK) | |||||||||||||||||||||||||||||||||
|
Condition0%0/6
$! == 4 or $! == 11 or $! == 11
$! == 4 or $! == 11
MC/DC0%0/3
$! == 4 or $! == 11 or $! == 11
$! == 4
$! == 11
$! == 11
Truth table0%0/4
A or B or C
A= $! == 4B= $! == 11C= $! == 11
| |||||||||||||||||||||||||||||||||||
| 112 | { # a bit unexpected, but ok | ||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | else | ||||||||||||||||||||||||||||||||||
| 115 | 0 | { warning __x"read from {name} closed unexpectedly: {err}" | |||||||||||||||||||||||||||||||||
| 116 | , name => $self->name, err => $!; | ||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->close; | |||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | sub muxInput($) | ||||||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | { my ($self, $inbuf) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
muxInput: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 124 | return $self->{IMHR_read_more}->($inbuf, 0) | ||||||||||||||||||||||||||||||||||
| 125 | 6 | ▶ | if $self->{IMHR_read_more}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | sub muxEOF($) | ||||||||||||||||||||||||||||||||||
| 130 | 6 | ▶ | { my ($self, $inbuf) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
muxEOF: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 131 | 6 | $self->{IMHR_eof} = 1; | |||||||||||||||||||||||||||||||||
| 132 | $self->{IMHR_read_more}->($inbuf, 1) | ||||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if $self->{IMHR_read_more}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | 7 | 1; | |||||||||||||||||||||||||||||||||