IOMux/Handler/Write.pm

filter: Statementstmt 53.1% 51 / 96 Branchbran 19.0% 8 / 42 Conditioncond 9.0% 1 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 68.1% 15 / 22 Podpod 90.9% 10 / 11 totaltotal 45.2% 85 / 188 CC 26 SCAR 31.9
CC
26
Cov
40%
muxWriteFlagged
90.0
write
24.5
say
12.0
CRAP 24.3
« IOMux/Service/TCP.pm ↑ Summary IOMux/Select.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::Write;
10 8 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 $VERSION = '1.01';
12  
13 8 ▶use base 'IOMux::Handler';
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 8 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use POSIX 'errno_h';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use File::Basename 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 8 ▶use constant PIPE_BUF_SIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 sub init($)
28 8 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
29 8 $self->SUPER::init($args);
30 8 ▶ $self->{IMHW_write_size} = $args->{write_size} || 4096;
Condition50%1/2
$args->{'write_size'} || 4096
l!l
08
MC/DC0%0/1
$args->{'write_size'} || 4096
$args->{'write_size'}
Truth table50%1/2
A
A= $args->{'write_size'}
Aresult
11
00
31 8 $self;
32 }
33  
34 #-------------------
35  
36 sub writeSize(;$)
37 0 ▶{ my $self = shift;
Subroutine writeSize: not called CC=2 SCAR=17.9
Pod documented
38 0 ▶ @_ ? $self->{IMHW_write_size} = shift : $self->{IMHW_write_size};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
39 }
40  
41 #-----------------------
42  
43 sub print(@)
44 6 ▶{ my $self = shift;
Subroutine print: called CC=4 SCAR=19.1
Pod documented
45 $self->write( !ref $_[0] ? (@_>1 ? \join('',@_) : \shift)
46 0 ▶ : ref $_[0] eq 'ARRAY' ? \join('',@{$_[0]}) : $_[0] );
Branch: 3 branches
BranchTrueFalseTotal
!ref($_[0]) ? : 6 0 6
@_ > 1 ? : 0 6 6
ref $_[0] eq 'ARRAY' ? : 0 0 0
47 }
48  
49  
50 sub say(@)
51 0 ▶{ my $self = shift;
Subroutine say: not called CC=3 SCAR=24.8
Pod documented
52 $self->write
53 ( !ref $_[0] ? \join('',@_, "\n")
54 0 ▶ : ref $_[0] eq 'ARRAY' ? \join('',@{$_[0]}, "\n")
Branch: 2 branches
BranchTrueFalseTotal
!ref($_[0]) ? : 0 0 0
ref $_[0] eq 'ARRAY' ? : 0 0 0
55 : $_[0]."\n"
56 );
57 }
58  
59  
60 sub printf($@)
61 0 ▶{ my $self = shift;
Subroutine printf: not called CC=1 SCAR=6.9
Pod documented
62 0 $self->write(\sprintf(@_));
63 }
64  
65  
66 sub write($;$)
67 12 ▶{ my ($self, $blob, $more) = @_;
Subroutine write: called CC=8 SCAR=32.0
Pod documented
68  
69 12 ▶ if(exists $self->{IMHW_outbuf})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"IMHW_outbuf"}) 0 12 12
70 { # There is already output waiting, glue this
71 0 ${$self->{IMHW_outbuf}} .= $$blob;
72 0 $self->{IMHW_more} = $more;
73 0 return;
74 }
75  
76  
77 12 my $bytes_written = syswrite $self->fh, $$blob, $self->{IMHW_write_size};
78 12 ▶ if(!defined $bytes_written)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $bytes_written) { } 0 12 12
elsif ($bytes_written == length $$blob) { } 12 0 12
79 0 ▶ { unless($!==EWOULDBLOCK || $!==EINTR)
Branch: 1 branch
BranchTrueFalseTotal
unless ($! == 11 or $! == 4) 0 0 0
Condition0%0/3
$! == 11 or $! == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 4
$! == 11 $! == 4
Truth table0%0/3
A or B
A= $! == 11B= $! == 4
ABresult
1X1
011
000
80 0 { $self->close;
81 0 fault __x"write to {name} failed", name => $self->name;
82 }
83 # we cannot send the bytes yet
84 }
85 elsif($bytes_written==length $$blob)
86 { # we got rit of all at once. Cheap! No need to wait in mux
87 12 ▶ $more->($self) if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 0 12 12
88 $self->{IMHW_is_closing}->($self)
89 12 ▶ if $self->{IMHW_is_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"IMHW_is_closing"} 0 12 12
90 12 return;
91 }
92 else
93 { # some bytes sent
94 0 substr($$blob, 0, $bytes_written) = '';
95 }
96  
97 # start waiting for write-ready flag, to send more
98 0 $self->{IMHW_outbuf} = $blob;
99 0 $self->{IMHW_more} = $more;
100 0 $self->fdset(1, 0, 1, 0);
101 }
102  
103  
104 #-------------------------
105  
106 sub muxInit($)
107 7 ▶{ my ($self, $mux) = @_;
Subroutine muxInit: called CC=1 SCAR=0.0
Pod documented
108 7 $self->SUPER::muxInit($mux);
109 # $self->fdset(1, 0, 1, 0);
110 7 $self->fdset(0, 0, 1, 0);
111 }
112  
113 sub muxWriteFlagged()
114 0 ▶{ my $self = shift;
Subroutine muxWriteFlagged: not called CC=9 SCAR=45.0
Pod documented
115 0 my $outbuf = $self->{IMHW_outbuf};
116 0 ▶ unless($outbuf)
Branch: 1 branch
BranchTrueFalseTotal
unless ($outbuf) 0 0 0
117 { # trigger follow-up event, which may produce more data to be sent
118 0 $self->muxOutbufferEmpty;
119 0 $outbuf = $self->{IMHW_outbuf};
120 0 ▶ unless(defined $outbuf)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $outbuf) 0 0 0
121 { # nothing can be produced on call, so we don't need the
122 # empty-write signals on the moment (enabled at next write)
123 0 return;
124 }
125 0 ▶ unless(length $$outbuf)
Branch: 1 branch
BranchTrueFalseTotal
unless (length $$outbuf) 0 0 0
126 { # retry callback at next interval
127 0 delete $self->{IMHW_outbuf};
128 0 return;
129 }
130 }
131  
132 0 my $bytes_written = syswrite $self->fh, $$outbuf, $self->{IMHW_write_size};
133 0 ▶ if(!defined $bytes_written)
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $bytes_written) { } 0 0 0
elsif ($bytes_written == length $$outbuf) { } 0 0 0
134 { # should happen, but we're kind
135 0 ▶ return if $! == EWOULDBLOCK || $! == EINTR || $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 or $! == 4 or $! == 11 0 0 0
Condition0%0/6
$! == 11 or $! == 4 or $! == 11
l!l&&r!l&&!r
000
$! == 11 or $! == 4
l!l&&r!l&&!r
000
MC/DC0%0/3
$! == 11 or $! == 4 or $! == 11
$! == 11 $! == 4 $! == 11
Truth table0%0/4
A or B or C
A= $! == 11B= $! == 4C= $! == 11
ABCresult
1XX1
01X1
0011
0000
136 0 warning __x"write to {name} failed: {err}"
137 , name => $self->name, err => $!;
138 0 $self->close;
139 }
140 elsif($bytes_written==length $$outbuf)
141 0 { delete $self->{IMHW_outbuf};
142 0 $self->muxOutbufferEmpty;
143 }
144 else
145 0 { substr($$outbuf, 0, $bytes_written) = '';
146 }
147 }
148  
149  
150 sub muxOutbufferEmpty()
151 0 ▶{ my $self = shift;
Subroutine muxOutbufferEmpty: not called CC=3 SCAR=24.8
Pod documented
152 0 my $more = delete $self->{IMHW_more};
153 0 ▶ return $more->() if defined $more;
Branch: 1 branch
BranchTrueFalseTotal
if defined $more 0 0 0
154  
155 0 $self->fdset(0, 0, 1, 0);
156 $self->{IMHW_is_closing}->($self)
157 0 ▶ if $self->{IMHW_is_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"IMHW_is_closing"} 0 0 0
158 }
159  
160  
161 0 ▶sub muxOutputWaiting() { exists shift->{IMHW_outbuf} }
Subroutine muxOutputWaiting: not called CC=1 SCAR=6.9
Pod documented
162  
163 # Closing write handlers is a little complex: it should be delayed
164 # until the write buffer is empty.
165  
166 sub close(;$)
167 10 ▶{ my ($self, $cb) = @_;
Subroutine close: called CC=2 SCAR=8.4
Pod documented
168 10 ▶ if($self->{IMHW_outbuf})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IMHW_outbuf'}) { } 0 10 10
169 { # delay closing until write buffer is empty
170 0 ▶ $self->{IMHW_is_closing} = sub { $self->SUPER::close($cb)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 }
172 else
173 { # can close immediately
174 10 $self->SUPER::close($cb);
175 }
176 }
177  
178 8 1;
« IOMux/Service/TCP.pm ↑ Summary IOMux/Select.pm »