blib/lib/IO/Async/Handle.pm

filter: Statementstmt 93.0% 202 / 217 Branchbran 83.9% 99 / 118 Conditioncond 70.8% 17 / 24 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 94.1% 32 / 34 Podpod 94.1% 16 / 17 totaltotal 87.2% 370 / 424 CC 69 SCAR 22.3
CC
69
Cov
89%
configure
25.1
set_handles
12.0
__ANON__
12.0
CRAP 9.3
« blib/lib/IO/Async/Routine.pm ↑ Summary blib/lib/IO/Async/Function.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2006-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::Handle 0.805;
7  
8 75 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 75 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 75 ▶use base qw( IO::Async::Notifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 75 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 75 ▶use IO::Handle; # give methods to bare IO handles
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 75 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 75 ▶use Future::Utils qw( try_repeat );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 75 ▶use IO::Async::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 C<IO::Async::Handle> - event callbacks for a non-blocking file descriptor
24  
25 =head1 SYNOPSIS
26  
27 =for highlighter language=perl
28  
29 This class is likely not to be used directly, because subclasses of it exist
30 to handle more specific cases. Here is an example of how it would be used to
31 watch a listening socket for new connections. In real code, it is likely that
32 the C<< Loop->listen >> method would be used instead.
33  
34 use IO::Socket::INET;
35 use IO::Async::Handle;
36  
37 use IO::Async::Loop;
38 my $loop = IO::Async::Loop->new;
39  
40 my $socket = IO::Socket::INET->new( LocalPort => 1234, Listen => 1 );
41  
42 my $handle = IO::Async::Handle->new(
43 handle => $socket,
44  
45 on_read_ready => sub {
46 my $new_client = $socket->accept;
47 ...
48 },
49 );
50  
51 $loop->add( $handle );
52  
53 For most other uses with sockets, pipes or other filehandles that carry a byte
54 stream, the L<IO::Async::Stream> class is likely to be more suitable. For
55 non-stream sockets, see L<IO::Async::Socket>.
56  
57 =head1 DESCRIPTION
58  
59 This subclass of L<IO::Async::Notifier> allows non-blocking IO on filehandles.
60 It provides event handlers for when the filehandle is read- or write-ready.
61  
62 =cut
63  
64 =head1 EVENTS
65  
66 The following events are invoked, either using subclass methods or CODE
67 references in parameters:
68  
69 =head2 on_read_ready
70  
71 Invoked when the read handle becomes ready for reading.
72  
73 =head2 on_write_ready
74  
75 Invoked when the write handle becomes ready for writing.
76  
77 =head2 on_closed
78  
79 Optional. Invoked when the handle becomes closed.
80  
81 This handler is invoked before the filehandles are closed and the Handle
82 removed from its containing Loop. The C<loop> will still return the containing
83 Loop object.
84  
85 =cut
86  
87 =head1 PARAMETERS
88  
89 The following named parameters may be passed to C<new> or C<configure>:
90  
91 =head2 read_handle => IO
92  
93 =head2 write_handle => IO
94  
95 The reading and writing IO handles. Each must implement the C<fileno> method.
96 Primarily used for passing C<STDIN> / C<STDOUT>; see the SYNOPSIS section of
97 L<IO::Async::Stream> for an example.
98  
99 =head2 handle => IO
100  
101 The IO handle for both reading and writing; instead of passing each separately
102 as above. Must implement C<fileno> method in way that C<IO::Handle> does.
103  
104 =head2 read_fileno => INT
105  
106 =head2 write_fileno => INT
107  
108 File descriptor numbers for reading and writing. If these are given as an
109 alternative to C<read_handle> or C<write_handle> then a new C<IO::Handle>
110 instance will be constructed around each.
111  
112 =head2 on_read_ready => CODE
113  
114 =head2 on_write_ready => CODE
115  
116 =head2 on_closed => CODE
117  
118 CODE references for event handlers.
119  
120 =head2 want_readready => BOOL
121  
122 =head2 want_writeready => BOOL
123  
124 If present, enable or disable read- or write-ready notification as per the
125 C<want_readready> and C<want_writeready> methods.
126  
127 It is required that a matching C<on_read_ready> or C<on_write_ready> are
128 available for any handle that is provided; either passed as a callback CODE
129 reference or as an overridden the method. I.e. if only a C<read_handle> is
130 given, then C<on_write_ready> can be absent. If C<handle> is used as a
131 shortcut, then both read and write-ready callbacks or methods are required.
132  
133 If no IO handles are provided at construction time, the object is still
134 created but will not yet be fully-functional as a Handle. IO handles can be
135 assigned later using the C<set_handle> or C<set_handles> methods, or by
136 C<configure>. This may be useful when constructing an object to represent a
137 network connection, before the C<connect(2)> has actually been performed yet.
138  
139 =cut
140  
141 sub configure
142 {
143 1110 ▶ my $self = shift;
Subroutine configure: called CC=24 SCAR=32.2
Pod documented
144 1110 my %params = @_;
145  
146 1110 ▶ if( exists $params{on_read_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_read_ready"}) 76 1034 1110
147 76 $self->{on_read_ready} = delete $params{on_read_ready};
148 76 undef $self->{cb_r};
149  
150 76 ▶ $self->_watch_read(0), $self->_watch_read(1) if $self->want_readready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->want_readready 4 72 76
151 }
152  
153 1110 ▶ if( exists $params{on_write_ready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_write_ready"}) 17 1093 1110
154 17 $self->{on_write_ready} = delete $params{on_write_ready};
155 17 undef $self->{cb_w};
156  
157 17 ▶ $self->_watch_write(0), $self->_watch_write(1) if $self->want_writeready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->want_writeready 1 16 17
158 }
159  
160 1110 ▶ if( exists $params{on_closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"on_closed"}) 16 1094 1110
161 16 $self->{on_closed} = delete $params{on_closed};
162 }
163  
164 1110 ▶ if( defined $params{read_fileno} and defined $params{write_fileno} and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'read_fileno'} and defined $params{'write_fileno'} and $params{'read_fileno'} == $params{'write_fileno'}) { } 1 1109 1110
Condition83%5/6
defined $params{"read_fileno"} and defined $params{"write_fileno"} and $params{"read_fileno"} == $params{"write_fileno"}
!ll&&!rl&&r
110901
defined $params{"read_fileno"} and defined $params{"write_fileno"}
!ll&&!rl&&r
110811
MC/DC66%2/3
defined $params{"read_fileno"} and defined $params{"write_fileno"} and $params{"read_fileno"} == $params{"write_fileno"}
defined $params{"read_fileno"} defined $params{"write_fileno"} $params{"read_fileno"} == $params{"write_fileno"}
Truth table75%3/4
A and B and C
A= defined $params{"read_fileno"}B= defined $params{"write_fileno"}C= $params{"read_fileno"} == $params{"write_fileno"}
ABCresult
0XX0
10X0
1100
1111
165 $params{read_fileno} == $params{write_fileno} ) {
166 1 $params{handle} = IO::Handle->new_from_fd( $params{read_fileno}, "r+" );
167  
168 1 delete $params{read_fileno};
169 1 delete $params{write_fileno};
170 }
171 else {
172 $params{read_handle} = IO::Handle->new_from_fd( delete $params{read_fileno}, "r" )
173 1109 ▶ if defined $params{read_fileno};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"read_fileno"} 1 1108 1109
174  
175 $params{write_handle} = IO::Handle->new_from_fd( delete $params{write_fileno}, "w" )
176 1109 ▶ if defined $params{write_fileno};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"write_fileno"} 1 1108 1109
177 }
178  
179 # 'handle' is a shortcut for setting read_ and write_
180 1110 ▶ if( exists $params{handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"handle"}) 39 1071 1110
181 39 $params{read_handle} = $params{handle};
182 39 $params{write_handle} = $params{handle};
183 39 delete $params{handle};
184 }
185  
186 1110 ▶ if( exists $params{read_handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"read_handle"}) 752 358 1110
187 752 my $read_handle = delete $params{read_handle};
188  
189 752 ▶ if( defined $read_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_handle) { } 691 61 752
190 691 ▶ if( !defined eval { $read_handle->fileno } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined eval { do { $read_handle->fileno } }) 1 690 691
191 1 croak 'Expected that read_handle can ->fileno';
192 }
193  
194 690 ▶ unless( $self->can_event( 'on_read_ready' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_event("on_read_ready")) 0 690 690
195 0 croak 'Expected either a on_read_ready callback or an ->on_read_ready method';
196 }
197  
198 690 my @layers = PerlIO::get_layers( $read_handle );
199 690 ▶ if( grep m/^encoding\(/, @layers or grep m/^utf8$/, @layers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^encoding\(/u, @layers or grep /^utf8$/u, @layers) 0 690 690
Condition33%1/3
grep /^encoding\(/u, @layers or grep /^utf8$/u, @layers
l!l&&r!l&&!r
00690
MC/DC0%0/2
grep /^encoding\(/u, @layers or grep /^utf8$/u, @layers
grep /^encoding\(/u, @layers grep /^utf8$/u, @layers
Truth table33%1/3
A or B
A= grep /^encoding\(/u, @layersB= grep /^utf8$/u, @layers
ABresult
1X1
011
000
200 # Only warn for now, because if it's UTF-8 by default but only
201 # passes ASCII then all will be well
202 0 carp "Constructing a ".ref($self)." with an encoding-enabled handle may not read correctly";
203 }
204  
205 690 $self->{read_handle} = $read_handle;
206  
207 690 $self->want_readready( defined $read_handle );
208 }
209 else {
210 61 $self->want_readready( 0 );
211  
212 61 undef $self->{read_handle};
213 }
214  
215 # In case someone has reopened the filehandles during an on_closed handler
216 751 undef $self->{handle_closing};
217 }
218  
219 1109 ▶ if( exists $params{write_handle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"write_handle"}) 229 880 1109
220 229 my $write_handle = delete $params{write_handle};
221  
222 229 ▶ if( defined $write_handle ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $write_handle) { } 161 68 229
223 161 ▶ if( !defined eval { $write_handle->fileno } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined eval { do { $write_handle->fileno } }) 0 161 161
224 0 croak 'Expected that write_handle can ->fileno';
225 }
226  
227 161 ▶ unless( $self->can_event( 'on_write_ready' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_event("on_write_ready")) 0 161 161
228 # This used not to be fatal. Make it just a warning for now.
229 0 carp 'A write handle was provided but neither a on_write_ready callback nor an ->on_write_ready method were. Perhaps you mean \'read_handle\' instead?';
230 }
231  
232 161 $self->{write_handle} = $write_handle;
233 }
234 else {
235 68 $self->want_writeready( 0 );
236  
237 68 undef $self->{write_handle};
238 }
239  
240 # In case someone has reopened the filehandles during an on_closed handler
241 229 undef $self->{handle_closing};
242 }
243  
244 1109 ▶ if( exists $params{want_readready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"want_readready"}) 0 1109 1109
245 0 $self->want_readready( delete $params{want_readready} );
246 }
247  
248 1109 ▶ if( exists $params{want_writeready} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"want_writeready"}) 3 1106 1109
249 3 $self->want_writeready( delete $params{want_writeready} );
250 }
251  
252 1109 $self->SUPER::configure( %params );
253 }
254  
255 # We'll be calling these any of three times
256 # adding to/removing from loop
257 # caller en/disables readiness checking
258 # changing filehandle
259  
260 sub _watch_read
261 {
262 2531 ▶ my $self = shift;
Subroutine _watch_read: called CC=5 SCAR=16.1
263 2531 my ( $want ) = @_;
264  
265 2531 ▶ my $loop = $self->loop or return;
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $self->loop or return 735 1796 2531
266 1796 ▶ my $fh = $self->read_handle or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->read_handle or return 568 1228 1796
267  
268 1228 ▶ if( $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want) { } 665 563 1228
269 665 ▶ $self->{cb_r} ||= $self->make_event_cb( 'on_read_ready' );
Condition66%2/3
$self->{"cb_r"} ||= $self->make_event_cb("on_read_ready")
l!l&&r!l&&!r
26630
MC/DC0%0/2
$self->{"cb_r"} ||= $self->make_event_cb("on_read_ready")
$self->{"cb_r"} $self->make_event_cb("on_read_ready")
Truth table66%2/3
A ||= B
A= $self->{"cb_r"}B= $self->make_event_cb("on_read_ready")
ABresult
1X1
011
000
270  
271 $loop->watch_io(
272 handle => $fh,
273 on_read_ready => $self->{cb_r},
274 665 );
275 }
276 else {
277 563 $loop->unwatch_io(
278 handle => $fh,
279 on_read_ready => 1,
280 );
281 }
282 }
283  
284 sub _watch_write
285 {
286 797 ▶ my $self = shift;
Subroutine _watch_write: called CC=5 SCAR=16.1
287 797 my ( $want ) = @_;
288  
289 797 ▶ my $loop = $self->loop or return;
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $self->loop or return 4 793 797
290 793 ▶ my $fh = $self->write_handle or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->write_handle or return 620 173 793
291  
292 173 ▶ if( $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want) { } 73 100 173
293 73 ▶ $self->{cb_w} ||= $self->make_event_cb( 'on_write_ready' );
Condition66%2/3
$self->{"cb_w"} ||= $self->make_event_cb("on_write_ready")
l!l&&r!l&&!r
11620
MC/DC0%0/2
$self->{"cb_w"} ||= $self->make_event_cb("on_write_ready")
$self->{"cb_w"} $self->make_event_cb("on_write_ready")
Truth table66%2/3
A ||= B
A= $self->{"cb_w"}B= $self->make_event_cb("on_write_ready")
ABresult
1X1
011
000
294  
295 $loop->watch_io(
296 handle => $fh,
297 on_write_ready => $self->{cb_w},
298 73 );
299 }
300 else {
301 100 $loop->unwatch_io(
302 handle => $fh,
303 on_write_ready => 1,
304 );
305 }
306 }
307  
308 sub _add_to_loop
309 {
310 784 ▶ my $self = shift;
Subroutine _add_to_loop: called CC=3 SCAR=11.0
311 784 my ( $loop ) = @_;
312  
313 784 ▶ $self->_watch_read(1) if $self->want_readready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->want_readready 661 123 784
314 784 ▶ $self->_watch_write(1) if $self->want_writeready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->want_writeready 4 780 784
315 }
316  
317 sub _remove_from_loop
318 {
319 668 ▶ my $self = shift;
Subroutine _remove_from_loop: called CC=1 SCAR=0.0
320 668 my ( $loop ) = @_;
321  
322 668 $self->_watch_read(0);
323 668 $self->_watch_write(0);
324 }
325  
326 sub notifier_name
327 {
328 35 ▶ my $self = shift;
Subroutine notifier_name: called CC=9 SCAR=22.2
Pod documented
329  
330 35 my @parts;
331  
332 35 ▶ if( length( my $name = $self->SUPER::notifier_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $name = $self->SUPER::notifier_name)) 30 5 35
333 30 push @parts, $name;
334 }
335  
336 35 my $r = $self->read_fileno;
337 35 my $w = $self->write_fileno;
338  
339 35 ▶ if( defined $r and defined $w and $r == $w ) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $r and defined $w and $r == $w) { } 2 33 35
elsif (defined $r and defined $w) { } 0 33 33
elsif (defined $r) { } 1 32 33
elsif (defined $w) { } 1 31 32
Condition83%5/6
defined $r and defined $w and $r == $w
!ll&&!rl&&r
3302
defined $r and defined $w
!ll&&!rl&&r
3212
MC/DC66%2/3
defined $r and defined $w and $r == $w
defined $r defined $w $r == $w
Truth table75%3/4
A and B and C
A= defined $rB= defined $wC= $r == $w
ABCresult
0XX0
10X0
1100
1111
340 2 push @parts, "rw=$r";
341 }
342 ▶ elsif( defined $r and defined $w ) {
Condition66%2/3
defined $r and defined $w
!ll&&!rl&&r
3210
MC/DC0%0/2
defined $r and defined $w
defined $r defined $w
Truth table66%2/3
A and B
A= defined $rB= defined $w
ABresult
0X0
100
111
343 0 push @parts, "r=$r,w=$w";
344 }
345 elsif( defined $r ) {
346 1 push @parts, "r=$r";
347 }
348 elsif( defined $w ) {
349 1 push @parts, "w=$w";
350 }
351  
352 35 return join ",", @parts;
353 }
354  
355 =head1 METHODS
356  
357 The following methods documented in C<await> expressions return L<Future>
358 instances.
359  
360 =cut
361  
362 =head2 set_handle
363  
364 $handle->set_handles( %params );
365  
366 Sets new reading or writing filehandles. Equivalent to calling the
367 C<configure> method with the same parameters.
368  
369 =cut
370  
371 sub set_handles
372 {
373 0 ▶ my $self = shift;
Subroutine set_handles: not called CC=3 SCAR=24.8
Pod undocumented
374 0 my %params = @_;
375  
376 $self->configure(
377 exists $params{read_handle} ? ( read_handle => $params{read_handle} ) : (),
378 0 ▶ exists $params{write_handle} ? ( write_handle => $params{write_handle} ) : (),
Branch: 2 branches
BranchTrueFalseTotal
exists $params{'read_handle'} ? : 0 0 0
exists $params{'write_handle'} ? : 0 0 0
379 );
380 }
381  
382 =head2 set_handle
383  
384 $handle->set_handle( $fh );
385  
386 Shortcut for
387  
388 $handle->configure( handle => $fh );
389  
390 =cut
391  
392 sub set_handle
393 {
394 18 ▶ my $self = shift;
Subroutine set_handle: called CC=1 SCAR=0.0
Pod documented
395 18 my ( $fh ) = @_;
396  
397 18 $self->configure( handle => $fh );
398 }
399  
400 =head2 close
401  
402 $handle->close;
403  
404 This method calls C<close> on the underlying IO handles. This method will then
405 remove the handle from its containing loop.
406  
407 =cut
408  
409 sub close
410 {
411 640 ▶ my $self = shift;
Subroutine close: called CC=4 SCAR=13.9
Pod documented
412  
413 # Prevent infinite loops if there's two crosslinked handles
414 640 ▶ return if $self->{handle_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handle_closing"} 52 588 640
415 588 $self->{handle_closing} = 1;
416  
417 588 $self->want_readready( 0 );
418 588 $self->want_writeready( 0 );
419  
420 588 my $read_handle = delete $self->{read_handle};
421 588 ▶ $read_handle->close if defined $read_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $read_handle 524 64 588
422  
423 588 my $write_handle = delete $self->{write_handle};
424 588 ▶ $write_handle->close if defined $write_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $write_handle 72 516 588
425  
426 588 $self->_closed;
427 }
428  
429 sub _closed
430 {
431 605 ▶ my $self = shift;
Subroutine _closed: called CC=3 SCAR=11.0
432  
433 605 $self->maybe_invoke_event( on_closed => );
434 605 ▶ if( $self->{close_futures} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"close_futures"}) 171 434 605
435 171 $_->done for @{ $self->{close_futures} };
436 }
437 605 $self->remove_from_parent;
438 }
439  
440 =head2 close_read
441  
442 =head2 close_write
443  
444 $handle->close_read;
445  
446 $handle->close_write;
447  
448 Closes the underlying read or write handle, and deconfigures it from the
449 object. Neither of these methods will invoke the C<on_closed> event, nor
450 remove the object from the Loop if there is still one open handle in the
451 object. Only when both handles are closed, will C<on_closed> be fired, and the
452 object removed.
453  
454 =cut
455  
456 sub close_read
457 {
458 2 ▶ my $self = shift;
Subroutine close_read: called CC=3 SCAR=11.0
Pod documented
459  
460 2 $self->want_readready( 0 );
461  
462 2 my $read_handle = delete $self->{read_handle};
463 2 ▶ $read_handle->close if defined $read_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $read_handle 2 0 2
464  
465 2 ▶ $self->_closed if !$self->{write_handle};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"write_handle"} 1 1 2
466 }
467  
468 sub close_write
469 {
470 20 ▶ my $self = shift;
Subroutine close_write: called CC=3 SCAR=11.0
Pod documented
471  
472 20 $self->want_writeready( 0 );
473  
474 20 my $write_handle = delete $self->{write_handle};
475 20 ▶ $write_handle->close if defined $write_handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $write_handle 20 0 20
476  
477 20 ▶ $self->_closed if !$self->{read_handle};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"read_handle"} 16 4 20
478 }
479  
480 =head2 new_close_future
481  
482 await $handle->new_close_future;
483  
484 Returns a new L<IO::Async::Future> object which will become done when the
485 handle is closed. Cancelling the C<$future> will remove this notification
486 ability but will not otherwise affect the C<$handle>.
487  
488 =cut
489  
490 sub new_close_future
491 {
492 197 ▶ my $self = shift;
Subroutine new_close_future: called CC=1 SCAR=2.6
Pod documented
493  
494 197 push @{ $self->{close_futures} }, my $future = $self->loop->new_future;
495 $future->on_cancel(
496 $self->_capture_weakself( sub {
497 0 ▶ my $self = shift or return;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return 0 0 0
498 0 my $future = shift;
499  
500 0 ▶ @{ $self->{close_futures} } = grep { $_ and $_ != $future } @{ $self->{close_futures} };
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_ != $future 0 0 0
501 })
502 197 );
503  
504 197 return $future;
505 }
506  
507 =head2 read_handle
508  
509 =head2 write_handle
510  
511 $handle = $handle->read_handle;
512  
513 $handle = $handle->write_handle;
514  
515 These accessors return the underlying IO handles.
516  
517 =cut
518  
519 sub read_handle
520 {
521 4320 ▶ my $self = shift;
Subroutine read_handle: called CC=1 SCAR=0.0
Pod documented
522 4320 return $self->{read_handle};
523 }
524  
525 sub write_handle
526 {
527 1636 ▶ my $self = shift;
Subroutine write_handle: called CC=1 SCAR=0.0
Pod documented
528 1636 return $self->{write_handle};
529 }
530  
531 =head2 read_fileno
532  
533 =head2 write_fileno
534  
535 $fileno = $handle->read_fileno;
536  
537 $fileno = $handle->write_fileno;
538  
539 These accessors return the file descriptor numbers of the underlying IO
540 handles.
541  
542 =cut
543  
544 sub read_fileno
545 {
546 36 ▶ my $self = shift;
Subroutine read_fileno: called CC=2 SCAR=6.9
Pod documented
547 36 ▶ my $handle = $self->read_handle or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $self->read_handle or return undef 32 4 36
548 4 return $handle->fileno;
549 }
550  
551 sub write_fileno
552 {
553 36 ▶ my $self = shift;
Subroutine write_fileno: called CC=2 SCAR=6.9
Pod documented
554 36 ▶ my $handle = $self->write_handle or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $self->write_handle or return undef 32 4 36
555 4 return $handle->fileno;
556 }
557  
558 =head2 want_readready
559  
560 =head2 want_writeready
561  
562 $value = $handle->want_readready;
563  
564 $oldvalue = $handle->want_readready( $newvalue );
565  
566 $value = $handle->want_writeready;
567  
568 $oldvalue = $handle->want_writeready( $newvalue );
569  
570 These are the accessor for the C<want_readready> and C<want_writeready>
571 properties, which define whether the object is interested in knowing about
572 read- or write-readiness on the underlying file handle.
573  
574 =cut
575  
576 sub want_readready
577 {
578 2203 ▶ my $self = shift;
Subroutine want_readready: called CC=5 SCAR=16.1
Pod documented
579 2203 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1342 861 2203
580 1342 my ( $new ) = @_;
581  
582 1342 $new = !!$new;
583 1342 ▶ return $new if !$new == !$self->{want_readready}; # compare bools
Branch: 1 branch
BranchTrueFalseTotal
if !$new == !$self->{"want_readready"} 127 1215 1342
584  
585 1215 ▶ if( $new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 688 527 1215
586 688 ▶ defined $self->read_handle or
Branch: 1 branch
BranchTrueFalseTotal
defined $self->read_handle or croak("Cannot want_readready in a Handle with no read_handle") 0 688 688
587 croak 'Cannot want_readready in a Handle with no read_handle';
588 }
589  
590 1215 my $old = $self->{want_readready};
591 1215 $self->{want_readready} = $new;
592  
593 1215 $self->_watch_read( $new );
594  
595 1215 return $old;
596 }
597 else {
598 861 return $self->{want_readready};
599 }
600 }
601  
602 sub want_writeready
603 {
604 1706 ▶ my $self = shift;
Subroutine want_writeready: called CC=5 SCAR=16.1
Pod documented
605 1706 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 893 813 1706
606 893 my ( $new ) = @_;
607  
608 893 $new = !!$new;
609 893 ▶ return $new if !$new == !$self->{want_writeready}; # compare bools
Branch: 1 branch
BranchTrueFalseTotal
if !$new == !$self->{"want_writeready"} 762 131 893
610  
611 131 ▶ if( $new ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new) 73 58 131
612 73 ▶ defined $self->write_handle or
Branch: 1 branch
BranchTrueFalseTotal
defined $self->write_handle or croak("Cannot want_writeready in a Handle with no write_handle") 1 72 73
613 croak 'Cannot want_writeready in a Handle with no write_handle';
614 }
615  
616 130 my $old = $self->{want_writeready};
617 130 $self->{want_writeready} = $new;
618  
619 130 $self->_watch_write( $new );
620  
621 130 return $old;
622 }
623 else {
624 813 return $self->{want_writeready};
625 }
626 }
627  
628 =head2 socket
629  
630 $handle->socket( $ai );
631  
632 Convenient shortcut to creating a socket handle, as given by an addrinfo
633 structure, and setting it as the read and write handle for the object.
634  
635 C<$ai> may be either a C<HASH> or C<ARRAY> reference of the same form as given
636 to L<IO::Async::OS>'s C<extract_addrinfo> method.
637  
638 This method returns nothing if it succeeds, or throws an exception if it
639 fails.
640  
641 =cut
642  
643 sub socket
644 {
645 3 ▶ my $self = shift;
Subroutine socket: called CC=1 SCAR=0.0
Pod documented
646 3 my ( $ai ) = @_;
647  
648 # TODO: Something about closing the old one?
649  
650 3 my ( $family, $socktype, $protocol ) = IO::Async::OS->extract_addrinfo( $ai );
651  
652 3 my $sock = IO::Async::OS->socket( $family, $socktype, $protocol );
653 3 $sock->blocking( 0 );
654  
655 3 $self->set_handle( $sock );
656 }
657  
658 =head2 bind
659  
660 $handle = await $handle->bind( %args );
661  
662 Performs a C<getaddrinfo> resolver operation with the C<passive> flag set,
663 and then attempts to bind a socket handle of any of the return values.
664  
665 =head2 bind (1 argument)
666  
667 $handle = await $handle->bind( $ai );
668  
669 When invoked with a single argument, this method is a convenient shortcut to
670 creating a socket handle and C<bind()>ing it to the address as given by an
671 addrinfo structure, and setting it as the read and write handle for the
672 object.
673  
674 C<$ai> may be either a C<HASH> or C<ARRAY> reference of the same form as given
675 to L<IO::Async::OS>'s C<extract_addrinfo> method.
676  
677 The returned future returns the handle object itself for convenience.
678  
679 =cut
680  
681 sub bind
682 {
683 3 ▶ my $self = shift;
Subroutine bind: called CC=3 SCAR=11.0
Pod documented
684  
685 3 ▶ if( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 2 1 3
686 2 my ( $ai ) = @_;
687  
688 2 $self->socket( $ai );
689 2 my $addr = ( IO::Async::OS->extract_addrinfo( $ai ) )[3];
690  
691 2 ▶ $self->read_handle->bind( $addr ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->read_handle->bind($addr) or return "Future"->fail("Cannot bind - $!", "bind", $self->read_handle, $addr, $!) 0 2 2
692 return Future->fail( "Cannot bind - $!", bind => $self->read_handle, $addr, $! );
693  
694 2 return Future->done( $self );
695 }
696  
697 $self->loop->resolver->getaddrinfo( passive => 1, @_ )->then( sub {
698 1 ▶ my @addrs = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
699  
700 try_repeat {
701 1 ▶ my $ai = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
702  
703 1 $self->bind( $ai );
704 } foreach => \@addrs,
705 1 ▶ until => sub { shift->is_done };
Subroutine __ANON__: called CC=1 SCAR=0.0
706 1 });
707 }
708  
709 =head2 connect
710  
711 $handle = await $handle->connect( %args );
712  
713 A convenient wrapper for calling the C<connect> method on the underlying
714 L<IO::Async::Loop> object.
715  
716 =cut
717  
718 sub connect
719 {
720 2 ▶ my $self = shift;
Subroutine connect: called CC=3 SCAR=11.0
Pod documented
721 2 my %args = @_;
722  
723 2 ▶ my $loop = $self->loop or croak "Cannot ->connect a Handle that is not in a Loop";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $self->loop or croak("Cannot ->connect a Handle that is not in a Loop") 0 2 2
724  
725 $self->debug_printf( "CONNECT " . join( ", ",
726 # These args should be stringy
727 2 ▶ ( map { defined $args{$_} ? "$_=$args{$_}" : () } qw( host service family socktype protocol local_host local_service ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$_} ? : 4 10 14
728 ) );
729  
730 2 return $self->loop->connect( %args, handle => $self );
731 }
732  
733 =head1 SEE ALSO
734  
735 =over 4
736  
737 =item *
738  
739 L<IO::Handle> - Supply object methods for I/O handles
740  
741 =back
742  
743 =head1 AUTHOR
744  
745 Paul Evans <leonerd@leonerd.org.uk>
746  
747 =cut
748  
749 75 0x55AA;
« blib/lib/IO/Async/Routine.pm ↑ Summary blib/lib/IO/Async/Function.pm »