Mojo/IOLoop/Stream.pm

filter: Statementstmt 94.2% 82 / 87 Branchbran 72.5% 45 / 62 Conditioncond 35.0% 7 / 20 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 88.8% 24 / 27 Podpod 100% 15 / 15 totaltotal 78.9% 176 / 223 CC 39 SCAR 17.2
CC
39
Cov
79%
_read
20.5
__ANON__
8.7
_write
7.0
CRAP 5.6
« Mojo/Promise.pm ↑ Summary Mojo/URL.pm »
1 package Mojo::IOLoop::Stream;
2 33 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 33 ▶use Errno qw(EAGAIN ECONNRESET EINTR EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 has high_water_mark => 1048576;
10 3 ▶has reactor => sub { Mojo::IOLoop->singleton->reactor }, weak => 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 69 ▶sub DESTROY { shift->close unless ${^GLOBAL_PHASE} eq 'DESTRUCT' }
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless ${^GLOBAL_PHASE} eq "DESTRUCT" 69 0 69
13  
14 0 ▶sub bytes_read { shift->{read} || 0 }
Subroutine bytes_read: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"read"} or 0 0 0 0
15  
16 7 ▶sub bytes_waiting { length(shift->{buffer} // '') }
Subroutine bytes_waiting: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
+(shift())->{"buffer"} // ""
l!l
61
MC/DC100%1/1
+(shift())->{"buffer"} // ""
+(shift())->{"buffer"}
Truth table100%2/2
A
A= +(shift())->{"buffer"}
Aresult
11
00
17  
18 0 ▶sub bytes_written { shift->{written} || 0 }
Subroutine bytes_written: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"written"} or 0 0 0 0
19  
20 4 ▶sub can_write { $_[0]{handle} && $_[0]->bytes_waiting < $_[0]->high_water_mark }
Subroutine can_write: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"handle"} and $_[0]->bytes_waiting < $_[0]->high_water_mark 3 1 4
21  
22 127 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
23 127 my $self = shift;
24 127 ▶ return unless my $reactor = $self->reactor;
Branch: 1 branch
BranchTrueFalseTotal
unless my $reactor = $self->reactor 8 119 127
25 119 ▶ return unless my $handle = delete $self->timeout(0)->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless my $handle = delete $self->timeout(0)->{"handle"} 59 60 119
26 60 $reactor->remove($handle);
27 60 $self->emit('close');
28 }
29  
30 50 ▶sub close_gracefully { $_[0]->is_writing ? $_[0]{graceful}++ : $_[0]->close }
Subroutine close_gracefully: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_writing ? : 3 47 50
31  
32 325 ▶sub handle { shift->{handle} }
Subroutine handle: called CC=1 SCAR=0.0
Pod documented
33  
34 136 ▶sub is_readable {
Subroutine is_readable: called CC=2 SCAR=7.6
Pod documented
35 136 my $self = shift;
36 136 $self->_again;
37 136 ▶ return $self->{handle} && Mojo::Util::_readable(0, fileno $self->{handle});
Condition33%1/3
$self->{'handle'} && Mojo::Util::_readable(0, fileno $self->{'handle'})
!ll&&!rl&&r
01360
MC/DC0%0/2
$self->{'handle'} && Mojo::Util::_readable(0, fileno $self->{'handle'})
$self->{'handle'} Mojo::Util::_readable(0, fileno $self->{'handle'})
Truth table33%1/3
A && B
A= $self->{'handle'}B= Mojo::Util::_readable(0, fileno $self->{'handle'})
ABresult
0X0
100
111
38 }
39  
40 9699 ▶sub is_writing {
Subroutine is_writing: called CC=3 SCAR=11.0
Pod documented
41 9699 my $self = shift;
42 9699 ▶ return undef unless $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} 5 9694 9699
43 9694 ▶ return !!length($self->{buffer}) || $self->has_subscribers('drain');
Condition100%3/3
!!length($self->{'buffer'}) || $self->has_subscribers('drain')
l!l&&r!l&&!r
92745415
MC/DC100%2/2
!!length($self->{'buffer'}) || $self->has_subscribers('drain')
!!length($self->{'buffer'}) $self->has_subscribers('drain')
Truth table100%3/3
A || B
A= !!length($self->{'buffer'})B= $self->has_subscribers('drain')
ABresult
1X1
011
000
44 }
45  
46 81 ▶sub new { shift->SUPER::new(handle => shift, timeout => 15) }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
47  
48 81 ▶sub start {
Subroutine start: called CC=3 SCAR=11.0
Pod documented
49 81 my $self = shift;
50  
51 # Resume
52 81 ▶ return unless $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"handle"} 0 81 81
53 81 my $reactor = $self->reactor;
54 81 ▶ return $reactor->watch($self->{handle}, 1, $self->is_writing) if delete $self->{paused};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"paused"} 1 80 81
55  
56 80 weaken $self;
57 80 ▶ my $cb = sub { pop() ? $self->_write : $self->_read };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
pop() ? : 9647 5815 15462
58 80 $reactor->io($self->timeout($self->{timeout})->{handle} => $cb);
59 }
60  
61 1 ▶sub steal_handle {
Subroutine steal_handle: called CC=1 SCAR=0.0
Pod documented
62 1 my $self = shift;
63 1 $self->reactor->remove($self->{handle});
64 1 return delete $self->{handle};
65 }
66  
67 1 ▶sub stop { $_[0]->reactor->watch($_[0]{handle}, 0, $_[0]->is_writing) if $_[0]{handle} && !$_[0]{paused}++ }
Subroutine stop: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"handle"} and not $_[0]{"paused"}++ 1 0 1
Condition33%1/3
$_[0]{"handle"} and not $_[0]{"paused"}++
!ll&&!rl&&r
001
MC/DC0%0/2
$_[0]{"handle"} and not $_[0]{"paused"}++
$_[0]{"handle"} not $_[0]{"paused"}++
Truth table33%1/3
A and not(B)
A= $_[0]{"handle"}B= not $_[0]{"paused"}++
ABresult
0X0
100
111
68  
69 713 ▶sub timeout {
Subroutine timeout: called CC=5 SCAR=16.8
Pod documented
70 713 my ($self, $timeout) = @_;
71  
72 713 ▶ return $self->{timeout} unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 5 708 713
73 708 $self->{timeout} = $timeout;
74  
75 708 my $reactor = $self->reactor;
76 708 ▶ if ($self->{timer}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'timer'}) { } 569 139 708
elsif ($self->{'timeout'}) { } 80 59 139
77 569 ▶ if (!$self->{timeout}) { $reactor->remove(delete $self->{timer}) }
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'timeout'}) { } 61 508 569
78 508 else { $reactor->again($self->{timer}, $self->{timeout}) }
79 }
80 elsif ($self->{timeout}) {
81 80 weaken $self;
82 $self->{timer}
83 0 ▶ = $reactor->timer($timeout => sub { $self and delete($self->{timer}) and $self->emit('timeout')->close });
Subroutine __ANON__: not called CC=3 SCAR=21.6
Branch: 1 branch
BranchTrueFalseTotal
$self and delete $self->{"timer"} and $self->emit("timeout")->close 0 0 0
Condition0%0/3
$self and delete $self->{"timer"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self and delete $self->{"timer"}
$self delete $self->{"timer"}
Truth table0%0/3
A and B
A= $selfB= delete $self->{"timer"}
ABresult
0X0
100
111
84 }
85  
86 708 return $self;
87 }
88  
89 9620 ▶sub write {
Subroutine write: called CC=4 SCAR=13.9
Pod documented
90 9620 my ($self, $chunk, $cb) = @_;
91  
92 # IO::Socket::SSL will corrupt data with the wrong internal representation
93 9620 utf8::downgrade $chunk;
94 9620 $self->{buffer} .= $chunk;
95 9620 ▶ if ($cb) { $self->once(drain => $cb) }
Branch: 2 branches
BranchTrueFalseTotal
if ($cb) { } 9606 14 9620
elsif (not length $self->{'buffer'}) { } 4 10 14
96 4 elsif (!length $self->{buffer}) { return $self }
97 9616 ▶ $self->reactor->watch($self->{handle}, !$self->{paused}, 1) if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 9616 0 9616
98  
99 9616 return $self;
100 }
101  
102 15550 ▶sub _again { $_[0]->reactor->again($_[0]{timer}) if $_[0]{timer} }
Subroutine _again: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"timer"} 10100 5450 15550
103  
104 5815 ▶sub _read {
Subroutine _read: called CC=7 SCAR=30.2
105 5815 my $self = shift;
106  
107 5815 ▶ if (defined(my $read = $self->{handle}->sysread(my $buffer, 131072, 0))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $read = $self->{"handle"}->sysread(my $buffer, 131072, 0))) 5815 0 5815
108 5815 $self->{read} += $read;
109 5815 ▶ return $read == 0 ? $self->close : $self->emit(read => $buffer)->_again;
Branch: 1 branch
BranchTrueFalseTotal
$read == 0 ? : 5 5810 5815
110 }
111  
112 # Retry
113 0 ▶ return undef if $! == EAGAIN || $! == EINTR || $! == EWOULDBLOCK;
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
114  
115 # Closed (maybe real error)
116 0 ▶ $! == ECONNRESET ? $self->close : $self->emit(error => $!)->close;
Branch: 1 branch
BranchTrueFalseTotal
$! == 104 ? : 0 0 0
117 }
118  
119 9647 ▶sub _write {
Subroutine _write: called CC=7 SCAR=19.5
120 9647 my $self = shift;
121  
122 # Handle errors only when reading (to avoid timing problems)
123 9647 my $handle = $self->{handle};
124 9647 ▶ if (length $self->{buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"buffer"}) 9604 43 9647
125 9604 ▶ return undef unless defined(my $written = $handle->syswrite($self->{buffer}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $written = $handle->syswrite($self->{"buffer"})) 0 9604 9604
126 9604 $self->{written} += $written;
127 9604 $self->emit(write => substr($self->{buffer}, 0, $written, ''))->_again;
128 }
129  
130 # Clear the buffer to free the underlying SV* memory
131 9647 ▶ undef $self->{buffer}, $self->emit('drain') unless length $self->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"buffer"} 9647 0 9647
132 9647 ▶ return undef if $self->is_writing;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_writing 9276 371 9647
133 371 ▶ return $self->close if $self->{graceful};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"graceful"} 2 369 371
134 369 ▶ $self->reactor->watch($handle, !$self->{paused}, 0) if $self->{handle};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle"} 364 5 369
135 }
136  
137 33 1;
138  
139 =encoding utf8
140  
141 =head1 NAME
142  
143 Mojo::IOLoop::Stream - Non-blocking I/O stream
144  
145 =head1 SYNOPSIS
146  
147 use Mojo::IOLoop::Stream;
148  
149 # Create stream
150 my $stream = Mojo::IOLoop::Stream->new($handle);
151 $stream->on(read => sub ($stream, $bytes) {...});
152 $stream->on(close => sub ($stream) {...});
153 $stream->on(error => sub ($stream, $err) {...});
154  
155 # Start and stop watching for new data
156 $stream->start;
157 $stream->stop;
158  
159 # Start reactor if necessary
160 $stream->reactor->start unless $stream->reactor->is_running;
161  
162 =head1 DESCRIPTION
163  
164 L<Mojo::IOLoop::Stream> is a container for I/O streams used by L<Mojo::IOLoop>.
165  
166 =head1 EVENTS
167  
168 L<Mojo::IOLoop::Stream> inherits all events from L<Mojo::EventEmitter> and can emit the following new ones.
169  
170 =head2 close
171  
172 $stream->on(close => sub ($stream) {...});
173  
174 Emitted if the stream gets closed.
175  
176 =head2 drain
177  
178 $stream->on(drain => sub ($stream) {...});
179  
180 Emitted once all data has been written.
181  
182 =head2 error
183  
184 $stream->on(error => sub ($stream, $err) {...});
185  
186 Emitted if an error occurs on the stream, fatal if unhandled.
187  
188 =head2 read
189  
190 $stream->on(read => sub ($stream, $bytes) {...});
191  
192 Emitted if new data arrives on the stream.
193  
194 =head2 timeout
195  
196 $stream->on(timeout => sub ($stream) {...});
197  
198 Emitted if the stream has been inactive for too long and will get closed automatically.
199  
200 =head2 write
201  
202 $stream->on(write => sub ($stream, $bytes) {...});
203  
204 Emitted if new data has been written to the stream.
205  
206 =head1 ATTRIBUTES
207  
208 L<Mojo::IOLoop::Stream> implements the following attributes.
209  
210 =head2 high_water_mark
211  
212 my $size = $msg->high_water_mark;
213 $msg = $msg->high_water_mark(1024);
214  
215 Maximum size of L</"write"> buffer in bytes before L</"can_write"> returns false, defaults to C<1048576> (1MiB).
216  
217 =head2 reactor
218  
219 my $reactor = $stream->reactor;
220 $stream = $stream->reactor(Mojo::Reactor::Poll->new);
221  
222 Low-level event reactor, defaults to the C<reactor> attribute value of the global L<Mojo::IOLoop> singleton. Note that
223 this attribute is weakened.
224  
225 =head1 METHODS
226  
227 L<Mojo::IOLoop::Stream> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
228  
229 =head2 bytes_read
230  
231 my $num = $stream->bytes_read;
232  
233 Number of bytes received.
234  
235 =head2 bytes_waiting
236  
237 my $num = $stream->bytes_waiting;
238  
239 Number of bytes that have been enqueued with L</"write"> and are waiting to be written.
240  
241 =head2 bytes_written
242  
243 my $num = $stream->bytes_written;
244  
245 Number of bytes written.
246  
247 =head2 can_write
248  
249 my $bool = $stream->can_write;
250  
251 Returns true if calling L</"write"> is safe.
252  
253 =head2 close
254  
255 $stream->close;
256  
257 Close stream immediately.
258  
259 =head2 close_gracefully
260  
261 $stream->close_gracefully;
262  
263 Close stream gracefully.
264  
265 =head2 handle
266  
267 my $handle = $stream->handle;
268  
269 Get handle for stream, usually an L<IO::Socket::IP> or L<IO::Socket::SSL> object.
270  
271 =head2 is_readable
272  
273 my $bool = $stream->is_readable;
274  
275 Quick non-blocking check if stream is readable, useful for identifying tainted sockets.
276  
277 =head2 is_writing
278  
279 my $bool = $stream->is_writing;
280  
281 Check if stream is writing.
282  
283 =head2 new
284  
285 my $stream = Mojo::IOLoop::Stream->new($handle);
286  
287 Construct a new L<Mojo::IOLoop::Stream> object.
288  
289 =head2 start
290  
291 $stream->start;
292  
293 Start or resume watching for new data on the stream.
294  
295 =head2 steal_handle
296  
297 my $handle = $stream->steal_handle;
298  
299 Steal L</"handle"> and prevent it from getting closed automatically.
300  
301 =head2 stop
302  
303 $stream->stop;
304  
305 Stop watching for new data on the stream.
306  
307 =head2 timeout
308  
309 my $timeout = $stream->timeout;
310 $stream = $stream->timeout(45);
311  
312 Maximum amount of time in seconds stream can be inactive before getting closed automatically, defaults to C<15>.
313 Setting the value to C<0> will allow this stream to be inactive indefinitely.
314  
315 =head2 write
316  
317 $stream = $stream->write($bytes);
318 $stream = $stream->write($bytes => sub {...});
319  
320 Enqueue data to be written to the stream as soon as possible, the optional drain callback will be executed once all
321 data has been written.
322  
323 =head1 SEE ALSO
324  
325 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
326  
327 =cut
« Mojo/Promise.pm ↑ Summary Mojo/URL.pm »