blib/lib/IO/Multiplex.pm

filter: Statementstmt 64.9% 191 / 294 Branchbran 32.3% 46 / 142 Conditioncond 15.8% 16 / 101 MC/DCmcdc 3.1% 2 / 64 Subroutinesub 73.9% 34 / 46 Podpod 85.0% 17 / 20 totaltotal 45.8% 306 / 667 CC 110 SCAR 58.4
CC
110
Cov
47%
loop
884.1
shutdown
110.0
close
56.0
CRAP 344.8
↑ Summary
1 package IO::Multiplex;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '1.16';
7  
8 =head1 NAME
9  
10 IO::Multiplex - Manage IO on many file handles
11  
12 =head1 SYNOPSIS
13  
14 use IO::Multiplex;
15  
16 my $mux = new IO::Multiplex;
17 $mux->add($fh1);
18 $mux->add(\*FH2);
19 $mux->set_callback_object(...);
20 $mux->listen($server_socket);
21 $mux->loop;
22  
23 sub mux_input { ... }
24  
25 C<IO::Multiplex> is designed to take the effort out of managing
26 multiple file handles. It is essentially a really fancy front end to
27 the C<select> system call. In addition to maintaining the C<select>
28 loop, it buffers all input and output to/from the file handles. It
29 can also accept incoming connections on one or more listen sockets.
30  
31 =head1 DESCRIPTION
32  
33 It is object oriented in design, and will notify you of significant events
34 by calling methods on an object that you supply. If you are not using
35 objects, you can simply supply C<__PACKAGE__> instead of an object reference.
36  
37 You may have one callback object registered for each file handle, or
38 one global one. Possibly both -- the per-file handle callback object
39 will be used instead of the global one.
40  
41 Each file handle may also have a timer associated with it. A callback
42 function is called when the timer expires.
43  
44 =head2 Handling input on descriptors
45  
46 When input arrives on a file handle, the C<mux_input> method is called
47 on the appropriate callback object. This method is passed three
48 arguments (in addition to the object reference itself of course):
49  
50 =over 4
51  
52 =item 1
53  
54 a reference to the mux,
55  
56 =item 2
57  
58 A reference to the file handle, and
59  
60 =item 3
61  
62 a reference to the input buffer for the file handle.
63  
64 =back
65  
66 The method should remove the data that it has consumed from the
67 reference supplied. It may leave unconsumed data in the input buffer.
68  
69 =head2 Handling output to descriptors
70  
71 If C<IO::Multiplex> did not handle output to the file handles as well
72 as input from them, then there is a chance that the program could
73 block while attempting to write. If you let the multiplexer buffer
74 the output, it will write the data only when the file handle is
75 capable of receiveing it.
76  
77 The basic method for handing output to the multiplexer is the C<write>
78 method, which simply takes a file descriptor and the data to be
79 written, like this:
80  
81 $mux->write($fh, "Some data");
82  
83 For convenience, when the file handle is C<add>ed to the multiplexer, it
84 is tied to a special class which intercepts all attempts to write to the
85 file handle. Thus, you can use print and printf to send output to the
86 handle in a normal manner:
87  
88 printf $fh "%s%d%X", $foo, $bar, $baz
89  
90 Unfortunately, Perl support for tied file handles is incomplete, and
91 functions such as C<send> cannot be supported.
92  
93 Also, file handle object methods such as the C<send> method of
94 C<IO::Socket> cannot be intercepted.
95  
96 =head1 EXAMPLES
97  
98 =head2 Simple Example
99  
100 This is a simple telnet-like program, which demonstrates the concepts
101 covered so far. It does not really work too well against a telnet
102 server, but it does OK against the sample server presented further down.
103  
104 use IO::Socket;
105 use IO::Multiplex;
106  
107 # Create a multiplex object
108 my $mux = new IO::Multiplex;
109 # Connect to the host/port specified on the command line,
110 # or localhost:23
111 my $sock = new IO::Socket::INET(Proto => 'tcp',
112 PeerAddr => shift || 'localhost',
113 PeerPort => shift || 23)
114 or die "socket: $@";
115  
116 # add the relevant file handles to the mux
117 $mux->add($sock);
118 $mux->add(\*STDIN);
119 # We want to buffer output to the terminal. This prevents the program
120 # from blocking if the user hits CTRL-S for example.
121 $mux->add(\*STDOUT);
122  
123 # We're not object oriented, so just request callbacks to the
124 # current package
125 $mux->set_callback_object(__PACKAGE__);
126  
127 # Enter the main mux loop.
128 $mux->loop;
129  
130 # mux_input is called when input is available on one of
131 # the descriptors.
132 sub mux_input {
133 my $package = shift;
134 my $mux = shift;
135 my $fh = shift;
136 my $input = shift;
137  
138 # Figure out whence the input came, and send it on to the
139 # other place.
140 if ($fh == $sock) {
141 print STDOUT $$input;
142 } else {
143 print $sock $$input;
144 }
145 # Remove the input from the input buffer.
146 $$input = '';
147 }
148  
149 # This gets called if the other end closes the connection.
150 sub mux_close {
151 print STDERR "Connection Closed\n";
152 exit;
153 }
154  
155 =head2 A server example
156  
157 Servers are just as simple to write. We just register a listen socket
158 with the multiplex object C<listen> method. It will automatically
159 accept connections on it and add them to its list of active file handles.
160  
161 This example is a simple chat server.
162  
163 use IO::Socket;
164 use IO::Multiplex;
165  
166 my $mux = new IO::Multiplex;
167  
168 # Create a listening socket
169 my $sock = new IO::Socket::INET(Proto => 'tcp',
170 LocalPort => shift || 2300,
171 Listen => 4)
172 or die "socket: $@";
173  
174 # We use the listen method instead of the add method.
175 $mux->listen($sock);
176  
177 $mux->set_callback_object(__PACKAGE__);
178 $mux->loop;
179  
180 sub mux_input {
181 my $package = shift;
182 my $mux = shift;
183 my $fh = shift;
184 my $input = shift;
185  
186 # The handles method returns a list of references to handles which
187 # we have registered, except for listen sockets.
188 foreach $c ($mux->handles) {
189 print $c $$input;
190 }
191 $$input = '';
192 }
193  
194 =head2 A more complex server example
195  
196 Let us take a look at the beginnings of a multi-user game server. We will
197 have a Player object for each player.
198  
199 # Paste the above example in here, up to but not including the
200 # mux_input subroutine.
201  
202 # mux_connection is called when a new connection is accepted.
203 sub mux_connection {
204 my $package = shift;
205 my $mux = shift;
206 my $fh = shift;
207  
208 # Construct a new player object
209 Player->new($mux, $fh);
210 }
211  
212 package Player;
213  
214 my %players = ();
215  
216 sub new {
217 my $package = shift;
218 my $self = bless { mux => shift,
219 fh => shift } => $package;
220  
221 # Register the new player object as the callback specifically for
222 # this file handle.
223  
224 $self->{mux}->set_callback_object($self, $self->{fh});
225 print $self->{fh}
226 "Greetings, Professor. Would you like to play a game?\n";
227  
228 # Register this player object in the main list of players
229 $players{$self} = $self;
230 $mux->set_timeout($self->{fh}, 1);
231 }
232  
233 sub players { return values %players; }
234  
235 sub mux_input {
236 my $self = shift;
237 shift; shift; # These two args are boring
238 my $input = shift; # Scalar reference to the input
239  
240 # Process each line in the input, leaving partial lines
241 # in the input buffer
242 while ($$input =~ s/^(.*?)\n//) {
243 $self->process_command($1);
244 }
245 }
246  
247 sub mux_close {
248 my $self = shift;
249  
250 # Player disconnected;
251 # [Notify other players or something...]
252 delete $players{$self};
253 }
254 # This gets called every second to update player info, etc...
255 sub mux_timeout {
256 my $self = shift;
257 my $mux = shift;
258  
259 $self->heartbeat;
260 $mux->set_timeout($self->{fh}, 1);
261 }
262  
263 =head1 METHODS
264  
265 =cut
266  
267 5 ▶use POSIX qw(errno_h BUFSIZ);
Subroutine BEGIN: called CC=1 SCAR=0.0
268 5 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 5 ▶use FileHandle qw(autoflush);
Subroutine BEGIN: called CC=1 SCAR=0.0
270 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 5 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 5 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
273 5 ▶use constant IsWin => ($^O eq 'MSWin32');
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275  
276 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
277 5 eval {
278 # Can optionally use Hi Res timers if available
279 5 require Time::HiRes;
280 5 Time::HiRes->import('time');
281 };
282 }
283  
284 # This is what you want. Trust me.
285 5 $SIG{PIPE} = 'IGNORE';
286  
287 5 ▶{ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 5 if(IsWin) { *EWOULDBLOCK = sub() {10035} }
289 }
290  
291 =head2 new
292  
293 Construct a new C<IO::Multiplex> object.
294  
295 $mux = new IO::Multiplex;
296  
297 =cut
298  
299 sub new
300 4 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
301 4 my $package = shift;
302 4 my $self = bless { _readers => '',
303 _writers => '',
304 _fhs => {},
305 _handles => {},
306 _timerkeys => {},
307 _timers => [],
308 _listen => {} } => $package;
309 4 return $self;
310 }
311  
312 =head2 listen
313  
314 Add a socket to be listened on. The socket should have had the
315 C<bind> and C<listen> system calls already applied to it. The C<IO::Socket>
316 module will do this for you.
317  
318 $socket = new IO::Socket::INET(Listen => ..., LocalAddr => ...);
319 $mux->listen($socket);
320  
321 Connections will be automatically accepted and C<add>ed to the multiplex
322 object. C<The mux_connection> callback method will also be called.
323  
324 =cut
325  
326 sub listen
327 2 ▶{
Subroutine listen: called CC=1 SCAR=0.0
Pod documented
328 2 my $self = shift;
329 2 my $fh = shift;
330  
331 2 $self->add($fh);
332 2 $self->{_fhs}{"$fh"}{listen} = 1;
333 2 $fh;
334 }
335  
336 =head2 add
337  
338 Add a file handle to the multiplexer.
339  
340 $mux->add($fh);
341  
342 As a side effect, this sets non-blocking mode on the handle, and disables
343 STDIO buffering. It also ties it to intercept output to the handle.
344  
345 =cut
346  
347 sub add
348 7 ▶{
Subroutine add: called CC=4 SCAR=13.9
Pod documented
349 7 my $self = shift;
350 7 my $fh = shift;
351  
352 7 ▶ return if $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_fhs"}{"$fh"} 0 7 7
353  
354 7 nonblock($fh);
355 7 autoflush($fh, 1);
356 7 fd_set($self->{_readers}, $fh, 1);
357  
358 7 my $sockopt = getsockopt $fh, SOL_SOCKET, SO_TYPE;
359 7 ▶ $self->{_fhs}{"$fh"}{udp_true} = 1
Branch: 1 branch
BranchTrueFalseTotal
if defined $sockopt and 2 == unpack("i", $sockopt) 3 4 7
Condition66%2/3
defined $sockopt and 2 == unpack("i", $sockopt)
!ll&&!rl&&r
043
MC/DC50%1/2
defined $sockopt and 2 == unpack("i", $sockopt)
defined $sockopt 2 == unpack("i", $sockopt)
Truth table66%2/3
A and B
A= defined $sockoptB= 2 == unpack("i", $sockopt)
ABresult
0X0
100
111
360 if defined $sockopt && SOCK_DGRAM == unpack "i", $sockopt;
361  
362 7 $self->{_fhs}{"$fh"}{inbuffer} = '';
363 7 $self->{_fhs}{"$fh"}{outbuffer} = '';
364 7 $self->{_fhs}{"$fh"}{fileno} = fileno($fh);
365 7 $self->{_handles}{"$fh"} = $fh;
366 7 tie *$fh, "IO::Multiplex::Handle", $self, $fh;
367 7 return $fh;
368 }
369  
370 =head2 remove
371  
372 Removes a file handle from the multiplexer. This also unties the
373 handle. It does not currently turn STDIO buffering back on, or turn
374 off non-blocking mode.
375  
376 $mux->remove($fh);
377  
378 =cut
379  
380 sub remove
381 0 ▶{
Subroutine remove: not called CC=1 SCAR=6.9
Pod documented
382 0 my $self = shift;
383 0 my $fh = shift;
384 0 fd_set($self->{_writers}, $fh, 0);
385 0 fd_set($self->{_readers}, $fh, 0);
386 0 delete $self->{_fhs}{"$fh"};
387 0 delete $self->{_handles}{"$fh"};
388 0 $self->_removeTimer($fh);
389 0 untie *$fh;
390 0 return 1;
391 }
392  
393 =head2 set_callback_object
394  
395 Set the object on which callbacks are made. If you are not using objects,
396 you can specify the name of the package into which the method calls are
397 to be made.
398  
399 If a file handle is supplied, the callback object is specific for that
400 handle:
401  
402 $mux->set_callback_object($object, $fh);
403  
404 Otherwise, it is considered a default callback object, and is used when
405 events occur on a file handle that does not have its own callback object.
406  
407 $mux->set_callback_object(__PACKAGE__);
408  
409 The previously registered object (if any) is returned.
410  
411 See also the CALLBACK INTERFACE section.
412  
413 =cut
414  
415 sub set_callback_object
416 4 ▶{
Subroutine set_callback_object: called CC=5 SCAR=17.5
Pod documented
417 4 my $self = shift;
418 4 my $obj = shift;
419 4 my $fh = shift;
420 4 ▶ return if $fh && !exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
if $fh and not exists $self->{"_fhs"}{"$fh"} 0 4 4
Condition33%1/3
$fh and not exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
400
MC/DC0%0/2
$fh and not exists $self->{"_fhs"}{"$fh"}
$fh not exists $self->{"_fhs"}{"$fh"}
Truth table33%1/3
A and not(B)
A= $fhB= not exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
421  
422 4 ▶ my $old = $fh ? $self->{_fhs}{"$fh"}{object} : $self->{_object};
Branch: 1 branch
BranchTrueFalseTotal
$fh ? : 0 4 4
423  
424 4 ▶ $fh ? $self->{_fhs}{"$fh"}{object} : $self->{_object} = $obj;
Branch: 1 branch
BranchTrueFalseTotal
$fh ? : 0 4 4
425 4 return $old;
426 }
427  
428 =head2 kill_output
429  
430 Remove any pending output on a file descriptor.
431  
432 $mux->kill_output($fh);
433  
434 =cut
435  
436 sub kill_output
437 0 ▶{
Subroutine kill_output: not called CC=3 SCAR=24.8
Pod documented
438 0 my $self = shift;
439 0 my $fh = shift;
440 0 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 0 0
Condition0%0/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table0%0/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
441  
442 0 $self->{_fhs}{"$fh"}{outbuffer} = '';
443 0 fd_set($self->{_writers}, $fh, 0);
444 }
445  
446 =head2 outbuffer
447  
448 Return or set the output buffer for a descriptor
449  
450 $output = $mux->outbuffer($fh);
451 $mux->outbuffer($fh, $output);
452  
453 =cut
454  
455 sub outbuffer
456 0 ▶{
Subroutine outbuffer: not called CC=6 SCAR=37.4
Pod documented
457 0 my $self = shift;
458 0 my $fh = shift;
459 0 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 0 0
Condition0%0/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table0%0/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
460  
461 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
462 0 ▶ $self->{_fhs}{"$fh"}{outbuffer} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
463 0 ▶ fd_set($self->{_writers}, $fh, 0) if !$_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
464 }
465  
466 0 $self->{_fhs}{"$fh"}{outbuffer};
467 }
468  
469 =head2 inbuffer
470  
471 Return or set the input buffer for a descriptor
472  
473 $input = $mux->inbuffer($fh);
474 $mux->inbuffer($fh, $input);
475  
476 =cut
477  
478 sub inbuffer
479 0 ▶{
Subroutine inbuffer: not called CC=5 SCAR=34.0
Pod documented
480 0 my $self = shift;
481 0 my $fh = shift;
482 0 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 0 0
Condition0%0/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table0%0/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
483  
484 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
485 0 ▶ $self->{_fhs}{"$fh"}{inbuffer} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
486 }
487  
488 0 return $self->{_fhs}{"$fh"}{inbuffer};
489 }
490  
491 =head2 set_timeout
492  
493 Set the timer for a file handle. The timeout value is a certain number of
494 seconds in the future, after which the C<mux_timeout> callback is called.
495  
496 If the C<Time::HiRes> module is installed, the timers may be specified in
497 fractions of a second.
498  
499 Timers are not reset automatically.
500  
501 $mux->set_timeout($fh, 23.6);
502  
503 Use C<$mux-E<gt>set_timeout($fh, undef)> to cancel a timer.
504  
505 =cut
506  
507 sub set_timeout
508 4 ▶{
Subroutine set_timeout: called CC=4 SCAR=15.5
Pod documented
509 4 my $self = shift;
510 4 my $fh = shift;
511 4 my $timeout = shift;
512 4 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 4 4
Condition33%1/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
004
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table33%1/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
513  
514 4 ▶ if (defined $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timeout) { } 4 0 4
515 4 $self->_addTimer($fh, $timeout + time);
516 } else {
517 0 $self->_removeTimer($fh);
518 }
519 }
520  
521 =head2 handles
522  
523 Returns a list of handles that the C<IO::Multiplex> object knows about,
524 excluding listen sockets.
525  
526 @handles = $mux->handles;
527  
528 =cut
529  
530 sub handles
531 0 ▶{
Subroutine handles: not called CC=1 SCAR=6.9
Pod documented
532 0 my $self = shift;
533  
534 0 return grep(!$self->{_fhs}{"$_"}{listen}, values %{$self->{_handles}});
535 }
536  
537 4 ▶sub _addTimer {
Subroutine _addTimer: called CC=1 SCAR=0.0
538 4 my $self = shift;
539 4 my $fh = shift;
540 4 my $time = shift;
541  
542 # Set a key so that we can quickly tell if a given $fh has
543 # a timer set
544 4 $self->{_timerkeys}{"$fh"} = 1;
545  
546 # Store the timeout in an array, and resort it
547 0 @{$self->{_timers}} = sort { $a->[1] <=> $b->[1] } (@{$self->{_timers}}, [ $fh, $time ] );
548 }
549  
550 4 ▶sub _removeTimer {
Subroutine _removeTimer: called CC=2 SCAR=7.0
551 4 my $self = shift;
552 4 my $fh = shift;
553  
554 # Return quickly if no timer is set
555 4 ▶ return unless exists $self->{_timerkeys}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_timerkeys"}{"$fh"} 0 4 4
556  
557 # Remove the timeout from the sorted array
558 4 @{$self->{_timers}} = grep { $_->[0] ne $fh } @{$self->{_timers}};
559  
560 # Get rid of the key
561 4 delete $self->{_timerkeys}{"$fh"};
562 }
563  
564  
565 =head2 loop
566  
567 Enter the main loop and start processing IO events.
568  
569 $mux->loop;
570  
571 =cut
572  
573 sub loop
574 4 ▶{
Subroutine loop: called CC=58 SCAR=67.8
Pod documented
575 4 my $self = shift;
576 4 my $heartbeat = shift;
577 4 $self->{_endloop} = 0;
578  
579 4 ▶ while (!$self->{_endloop} && keys %{$self->{_fhs}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_endloop"} and keys %{$$self{"_fhs"};}) 14 2 16
Condition66%2/3
not $self->{"_endloop"} and keys %{$$self{"_fhs"};}
!ll&&!rl&&r
2014
MC/DC50%1/2
not $self->{"_endloop"} and keys %{$$self{"_fhs"};}
not $self->{"_endloop"} keys %{$$self{"_fhs"};}
Truth table66%2/3
not(A) and B
A= not $self->{"_endloop"}B= keys %{$$self{"_fhs"};}
ABresult
0X0
100
111
580 14 my $rv;
581 my $data;
582 14 my $rdready = "";
583 14 my $wrready = "";
584 14 my $timeout = undef;
585  
586 14 foreach my $fh (values %{$self->{_handles}}) {
587 21 ▶ fd_set($rdready, $fh, 1) if
Branch: 1 branch
BranchTrueFalseTotal
if ref($fh) =~ /SSL/ and $fh->can("pending") and $fh->pending 0 21 21
Condition33%2/6
ref($fh) =~ /SSL/ and $fh->can("pending") and $fh->pending
!ll&&!rl&&r
2100
ref($fh) =~ /SSL/ and $fh->can("pending")
!ll&&!rl&&r
2100
MC/DC0%0/3
ref($fh) =~ /SSL/ and $fh->can("pending") and $fh->pending
ref($fh) =~ /SSL/ $fh->can("pending") $fh->pending
Truth table25%1/4
A and B and C
A= ref($fh) =~ /SSL/B= $fh->can("pending")C= $fh->pending
ABCresult
0XX0
10X0
1100
1111
588 ref($fh) =~ /SSL/ &&
589 $fh->can("pending") &&
590 $fh->pending;
591 }
592  
593 14 ▶ if (!length $rdready) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $rdready) 14 0 14
594 14 ▶ if (@{$self->{_timers}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_timers"};}) 4 10 14
595 4 $timeout = $self->{_timers}[0][1] - time;
596 }
597  
598 my $numready = select($rdready=$self->{_readers},
599 $wrready=$self->{_writers},
600 undef,
601 14 $timeout);
602  
603 14 ▶ unless(defined($numready)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $numready) 0 14 14
604 0 ▶ if ($! == EINTR || $! == EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4 or $! == 11) { } 0 0 0
Condition0%0/3
$! == 4 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 4 or $! == 11
$! == 4 $! == 11
Truth table0%0/3
A or B
A= $! == 4B= $! == 11
ABresult
1X1
011
000
605 0 next;
606 } else {
607 0 last;
608 }
609 }
610 }
611  
612 0 ▶ &{ $heartbeat } ($rdready, $wrready) if $heartbeat;
Branch: 1 branch
BranchTrueFalseTotal
if $heartbeat 0 14 14
613  
614 14 foreach my $k (keys %{$self->{_handles}}) {
615 19 ▶ my $fh = $self->{_handles}->{$k} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"_handles"}{$k} or next 19 0 19
616  
617 # Avoid creating a permanent empty hash ref for "$fh"
618 # by attempting to access its {object} element
619 # if it has already been closed.
620 19 ▶ next unless exists $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} 0 19 19
621  
622 # It is not easy to replace $self->{_fhs}{"$fh"} with a
623 # variable, because some mux_* routines may remove it as
624 # side-effect.
625  
626 # Get the callback object.
627 my $obj = $self->{_fhs}{"$fh"}{object} ||
628 19 ▶ $self->{_object};
Condition33%1/3
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
l!l&&r!l&&!r
0190
MC/DC0%0/2
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
$self->{'_fhs'}{"$fh"}{'object'} $self->{'_object'}
Truth table33%1/3
A || B
A= $self->{'_fhs'}{"$fh"}{'object'}B= $self->{'_object'}
ABresult
1X1
011
000
629  
630 # Is this descriptor ready for reading?
631 19 ▶ if (fd_isset($rdready, $fh))
Branch: 1 branch
BranchTrueFalseTotal
if (fd_isset($rdready, $fh)) 10 9 19
632 {
633 10 ▶ if ($self->{_fhs}{"$fh"}{listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_fhs'}{"$fh"}{'listen'}) { } 2 8 10
634 # It's a server socket, so a new connection is
635 # waiting to be accepted
636 2 my $client = $fh->accept;
637 2 ▶ next unless ($client);
Branch: 1 branch
BranchTrueFalseTotal
unless $client 0 2 2
638 2 $self->add($client);
639 2 ▶ $obj->mux_connection($self, $client)
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_connection") 2 0 2
Condition33%1/3
$obj and $obj->can("mux_connection")
!ll&&!rl&&r
002
MC/DC0%0/2
$obj and $obj->can("mux_connection")
$obj $obj->can("mux_connection")
Truth table33%1/3
A and B
A= $objB= $obj->can("mux_connection")
ABresult
0X0
100
111
640 if $obj && $obj->can("mux_connection");
641 } else {
642 8 ▶ if ($self->is_udp($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_udp($fh)) { } 6 2 8
643 6 $rv = recv($fh, $data, BUFSIZ, 0);
644 6 ▶ if (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv) 6 0 6
645 # Remember where the last UDP packet came from
646 6 $self->{_fhs}{"$fh"}{udp_peer} = $rv;
647 }
648 } else {
649 2 $rv = &POSIX::read(fileno($fh), $data, BUFSIZ);
650 }
651  
652 8 ▶ if (defined($rv) && length($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv and length $data) { } 8 0 8
Condition33%1/3
defined $rv and length $data
!ll&&!rl&&r
008
MC/DC0%0/2
defined $rv and length $data
defined $rv length $data
Truth table33%1/3
A and B
A= defined $rvB= length $data
ABresult
0X0
100
111
653 # Append the data to the client's receive buffer,
654 # and call process_input to see if anything needs to
655 # be done.
656 8 $self->{_fhs}{"$fh"}{inbuffer} .= $data;
657 $obj->mux_input($self, $fh,
658 \$self->{_fhs}{"$fh"}{inbuffer})
659 8 ▶ if $obj && $obj->can("mux_input");
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_input") 8 0 8
Condition33%1/3
$obj and $obj->can("mux_input")
!ll&&!rl&&r
008
MC/DC0%0/2
$obj and $obj->can("mux_input")
$obj $obj->can("mux_input")
Truth table33%1/3
A and B
A= $objB= $obj->can("mux_input")
ABresult
0X0
100
111
660 } else {
661 0 ▶ unless (defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
662 next if
663 0 ▶ $! == EINTR ||
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 or $! == 11 or $! == 11 0 0 0
Condition0%0/6
$! == 4 or $! == 11 or $! == 11
l!l&&r!l&&!r
000
$! == 4 or $! == 11
l!l&&r!l&&!r
000
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
ABCresult
1XX1
01X1
0011
0000
664 $! == EAGAIN ||
665 $! == EWOULDBLOCK;
666 0 ▶ warn "IO::Multiplex read error: $!"
Branch: 1 branch
BranchTrueFalseTotal
if $! != 104 0 0 0
667 if $! != ECONNRESET;
668 }
669 # There's an error, or we received EOF. If
670 # there's pending data to be written, we leave
671 # the connection open so it can be sent. If
672 # the other end is closed for writing, the
673 # send will error and we close down there.
674 # Either way, we remove it from _readers as
675 # we're no longer interested in reading from
676 # it.
677 0 fd_set($self->{_readers}, $fh, 0);
678 $obj->mux_eof($self, $fh,
679 \$self->{_fhs}{"$fh"}{inbuffer})
680 0 ▶ if $obj && $obj->can("mux_eof");
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_eof") 0 0 0
Condition0%0/3
$obj and $obj->can("mux_eof")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("mux_eof")
$obj $obj->can("mux_eof")
Truth table0%0/3
A and B
A= $objB= $obj->can("mux_eof")
ABresult
0X0
100
111
681  
682 0 ▶ if (exists $self->{_fhs}{"$fh"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_fhs"}{"$fh"}) 0 0 0
683 0 $self->{_fhs}{"$fh"}{inbuffer} = '';
684 # The mux_eof handler could have responded
685 # with a shutdown for writing.
686 $self->close($fh)
687 unless exists $self->{_fhs}{"$fh"}
688 0 ▶ && length $self->{_fhs}{"$fh"}{outbuffer};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} and length $self->{"_fhs"}{"$fh"}{"outbuffer"} 0 0 0
Condition0%0/3
exists $self->{"_fhs"}{"$fh"} and length $self->{"_fhs"}{"$fh"}{"outbuffer"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_fhs"}{"$fh"} and length $self->{"_fhs"}{"$fh"}{"outbuffer"}
exists $self->{"_fhs"}{"$fh"} length $self->{"_fhs"}{"$fh"}{"outbuffer"}
Truth table0%0/3
A and B
A= exists $self->{"_fhs"}{"$fh"}B= length $self->{"_fhs"}{"$fh"}{"outbuffer"}
ABresult
0X0
100
111
689 }
690 0 next;
691 }
692 }
693 } # end if readable
694 17 ▶ next unless exists $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} 0 17 17
695  
696 17 ▶ if (fd_isset($wrready, $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd_isset($wrready, $fh)) 0 17 17
697 0 ▶ unless (length $self->{_fhs}{"$fh"}{outbuffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"_fhs"}{"$fh"}{"outbuffer"}) 0 0 0
698 0 fd_set($self->{_writers}, $fh, 0);
699 0 ▶ $obj->mux_outbuffer_empty($self, $fh)
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_outbuffer_empty") 0 0 0
Condition0%0/3
$obj and $obj->can("mux_outbuffer_empty")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("mux_outbuffer_empty")
$obj $obj->can("mux_outbuffer_empty")
Truth table0%0/3
A and B
A= $objB= $obj->can("mux_outbuffer_empty")
ABresult
0X0
100
111
700 if ($obj && $obj->can("mux_outbuffer_empty"));
701 0 next;
702 }
703 $rv = &POSIX::write(fileno($fh),
704 $self->{_fhs}{"$fh"}{outbuffer},
705 0 length($self->{_fhs}{"$fh"}{outbuffer}));
706 0 ▶ unless (defined($rv)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
707 # We got an error writing to it. If it's
708 # EWOULDBLOCK (shouldn't happen if select told us
709 # we can write) or EAGAIN, or EINTR we don't worry
710 # about it. otherwise, close it down.
711 0 ▶ unless ($! == EWOULDBLOCK ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($! == 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
712 $! == EINTR ||
713 $! == EAGAIN) {
714 0 ▶ if ($! == EPIPE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 32) { } 0 0 0
715 0 ▶ $obj->mux_epipe($self, $fh)
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_epipe") 0 0 0
Condition0%0/3
$obj and $obj->can("mux_epipe")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("mux_epipe")
$obj $obj->can("mux_epipe")
Truth table0%0/3
A and B
A= $objB= $obj->can("mux_epipe")
ABresult
0X0
100
111
716 if $obj && $obj->can("mux_epipe");
717 } else {
718 0 warn "IO::Multiplex: write error: $!\n";
719 }
720 0 $self->close($fh);
721 }
722 0 next;
723 }
724 0 substr($self->{_fhs}{"$fh"}{outbuffer}, 0, $rv) = '';
725 0 ▶ unless (length $self->{_fhs}{"$fh"}{outbuffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"_fhs"}{"$fh"}{"outbuffer"}) 0 0 0
726 # Mark us as not writable if there's nothing more to
727 # write
728 0 fd_set($self->{_writers}, $fh, 0);
729 0 ▶ $obj->mux_outbuffer_empty($self, $fh)
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_outbuffer_empty") 0 0 0
Condition0%0/3
$obj and $obj->can("mux_outbuffer_empty")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("mux_outbuffer_empty")
$obj $obj->can("mux_outbuffer_empty")
Truth table0%0/3
A and B
A= $objB= $obj->can("mux_outbuffer_empty")
ABresult
0X0
100
111
730 if ($obj && $obj->can("mux_outbuffer_empty"));
731  
732 0 ▶ if ( $self->{_fhs}{"$fh"}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fhs"}{"$fh"} and $self->{"_fhs"}{"$fh"}{"shutdown"}) 0 0 0
Condition0%0/3
$self->{"_fhs"}{"$fh"} and $self->{"_fhs"}{"$fh"}{"shutdown"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_fhs"}{"$fh"} and $self->{"_fhs"}{"$fh"}{"shutdown"}
$self->{"_fhs"}{"$fh"} $self->{"_fhs"}{"$fh"}{"shutdown"}
Truth table0%0/3
A and B
A= $self->{"_fhs"}{"$fh"}B= $self->{"_fhs"}{"$fh"}{"shutdown"}
ABresult
0X0
100
111
733 && $self->{_fhs}{"$fh"}{shutdown}) {
734 # If we've been marked for shutdown after write
735 # do it.
736 0 shutdown($fh, 1);
737 0 $self->{_fhs}{"$fh"}{outbuffer} = '';
738 0 ▶ unless (length $self->{_fhs}{"$fh"}{inbuffer}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"_fhs"}{"$fh"}{"inbuffer"}) 0 0 0
739 # We'd previously been shutdown for reading
740 # also, so close out completely
741 0 $self->close($fh);
742 0 next;
743 }
744 }
745 }
746 } # End if writeable
747  
748 17 ▶ next unless exists $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} 0 17 17
749  
750 } # End foreach $fh (...)
751  
752 12 ▶ $self->_checkTimeouts() if @{$self->{_timers}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"_timers"};} 5 7 12
753  
754 } # End while(loop)
755 }
756  
757 5 ▶sub _checkTimeouts {
Subroutine _checkTimeouts: called CC=8 SCAR=21.8
758 5 my $self = shift;
759  
760 # Get the current time
761 5 my $time = time;
762  
763 # Copy all of the timers that should go off into
764 # a temporary array. This allows us to modify the
765 # real array as we process the timers, without
766 # interfering with the loop.
767  
768 5 my @timers = ();
769 5 foreach my $timer (@{$self->{_timers}}) {
770 # If the timer is in the future, we can stop
771 5 ▶ last if $timer->[1] > $time;
Branch: 1 branch
BranchTrueFalseTotal
if $timer->[1] > $time 1 4 5
772 4 push @timers, $timer;
773 }
774  
775 5 foreach my $timer (@timers) {
776 4 my $fh = $timer->[0];
777 4 $self->_removeTimer($fh);
778  
779 4 ▶ next unless exists $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} 0 4 4
780  
781 4 ▶ my $obj = $self->{_fhs}{"$fh"}{object} || $self->{_object};
Condition33%1/3
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
$self->{'_fhs'}{"$fh"}{'object'} $self->{'_object'}
Truth table33%1/3
A || B
A= $self->{'_fhs'}{"$fh"}{'object'}B= $self->{'_object'}
ABresult
1X1
011
000
782 4 ▶ $obj->mux_timeout($self, $fh) if $obj && $obj->can("mux_timeout");
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_timeout") 4 0 4
Condition33%1/3
$obj and $obj->can("mux_timeout")
!ll&&!rl&&r
004
MC/DC0%0/2
$obj and $obj->can("mux_timeout")
$obj $obj->can("mux_timeout")
Truth table33%1/3
A and B
A= $objB= $obj->can("mux_timeout")
ABresult
0X0
100
111
783 }
784 }
785  
786  
787 =head2 endloop
788  
789 Prematurly terminate the loop. The loop will automatically terminate
790 when there are no remaining descriptors to be watched.
791  
792 $mux->endloop;
793  
794 =cut
795  
796 sub endloop
797 2 ▶{
Subroutine endloop: called CC=1 SCAR=0.0
Pod documented
798 2 my $self = shift;
799 2 $self->{_endloop} = 1;
800 }
801  
802 =head2 udp_peer
803  
804 Get peer endpoint of where the last udp packet originated.
805  
806 $saddr = $mux->udp_peer($fh);
807  
808 =cut
809  
810 6 ▶sub udp_peer {
Subroutine udp_peer: called CC=1 SCAR=0.0
Pod documented
811 6 my $self = shift;
812 6 my $fh = shift;
813 6 return $self->{_fhs}{"$fh"}{udp_peer};
814 }
815  
816 =head2 is_udp
817  
818 Sometimes UDP packets require special attention.
819 This method will tell if a file handle is of type UDP.
820  
821 $is_udp = $mux->is_udp($fh);
822  
823 =cut
824  
825 14 ▶sub is_udp {
Subroutine is_udp: called CC=1 SCAR=0.0
Pod documented
826 14 my $self = shift;
827 14 my $fh = shift;
828 14 return $self->{_fhs}{"$fh"}{udp_true};
829 }
830  
831 =head2 write
832  
833 Send output to a file handle.
834  
835 $mux->write($fh, "'ere I am, JH!\n");
836  
837 =cut
838  
839 sub write
840 6 ▶{
Subroutine write: called CC=6 SCAR=21.2
Pod documented
841 6 my $self = shift;
842 6 my $fh = shift;
843 6 my $data = shift;
844 6 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 6 6
Condition33%1/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
006
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table33%1/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
845  
846 6 ▶ if ($self->{_fhs}{"$fh"}{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_fhs"}{"$fh"}{"shutdown"}) 0 6 6
847 0 $! = EPIPE;
848 0 return undef;
849 }
850 6 ▶ if ($self->is_udp($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_udp($fh)) 6 0 6
851 6 ▶ if (my $udp_peer = $self->udp_peer($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $udp_peer = $self->udp_peer($fh)) { } 4 2 6
852 # Send the packet back to the last peer that said something
853 4 return send($fh, $data, 0, $udp_peer);
854 } else {
855 # No udp_peer yet?
856 # This better be a connect()ed UDP socket
857 # or else this will fail with ENOTCONN
858 2 return send($fh, $data, 0);
859 }
860 }
861 0 $self->{_fhs}{"$fh"}{outbuffer} .= $data;
862 0 fd_set($self->{_writers}, $fh, 1);
863 0 return length($data);
864 }
865  
866 =head2 shutdown
867  
868 Shut down a socket for reading or writing or both. See the C<shutdown>
869 Perl documentation for further details.
870  
871 If the shutdown is for reading, it happens immediately. However,
872 shutdowns for writing are delayed until any pending output has been
873 successfully written to the socket.
874  
875 $mux->shutdown($socket, 1);
876  
877 =cut
878  
879 sub shutdown
880 0 ▶{
Subroutine shutdown: not called CC=10 SCAR=47.0
Pod documented
881 0 my $self = shift;
882 0 my $fh = shift;
883 0 my $which = shift;
884 0 ▶ return unless $fh && exists($self->{_fhs}{"$fh"});
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and exists $self->{"_fhs"}{"$fh"} 0 0 0
Condition0%0/3
$fh and exists $self->{"_fhs"}{"$fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and exists $self->{"_fhs"}{"$fh"}
$fh exists $self->{"_fhs"}{"$fh"}
Truth table0%0/3
A and B
A= $fhB= exists $self->{"_fhs"}{"$fh"}
ABresult
0X0
100
111
885  
886 0 ▶ if ($which == 0 || $which == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which == 0 or $which == 2) 0 0 0
Condition0%0/3
$which == 0 or $which == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$which == 0 or $which == 2
$which == 0 $which == 2
Truth table0%0/3
A or B
A= $which == 0B= $which == 2
ABresult
1X1
011
000
887 # Shutdown for reading. We can do this now.
888 0 shutdown($fh, 0);
889 # The mux_eof hook must be run from the main loop to consume
890 # the rest of the inbuffer if there is anything left.
891 # It will also remove $fh from _readers.
892 }
893  
894 0 ▶ if ($which == 1 || $which == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which == 1 or $which == 2) 0 0 0
Condition0%0/3
$which == 1 or $which == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$which == 1 or $which == 2
$which == 1 $which == 2
Truth table0%0/3
A or B
A= $which == 1B= $which == 2
ABresult
1X1
011
000
895 # Shutdown for writing. Only do this now if there is no pending
896 # data.
897 0 ▶ if(length $self->{_fhs}{"$fh"}{outbuffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'_fhs'}{"$fh"}{'outbuffer'}) { } 0 0 0
898 0 $self->{_fhs}{"$fh"}{shutdown} = 1;
899 } else {
900 0 shutdown($fh, 1);
901 0 $self->{_fhs}{"$fh"}{outbuffer} = '';
902 }
903 }
904 # Delete the descriptor if it's totally gone.
905 0 ▶ unless (length $self->{_fhs}{"$fh"}{inbuffer} ||
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"_fhs"}{"$fh"}{"inbuffer"} or length $self->{"_fhs"}{"$fh"}{"outbuffer"}) 0 0 0
Condition0%0/3
length $self->{"_fhs"}{"$fh"}{"inbuffer"} or length $self->{"_fhs"}{"$fh"}{"outbuffer"}
l!l&&r!l&&!r
000
MC/DC0%0/2
length $self->{"_fhs"}{"$fh"}{"inbuffer"} or length $self->{"_fhs"}{"$fh"}{"outbuffer"}
length $self->{"_fhs"}{"$fh"}{"inbuffer"} length $self->{"_fhs"}{"$fh"}{"outbuffer"}
Truth table0%0/3
A or B
A= length $self->{"_fhs"}{"$fh"}{"inbuffer"}B= length $self->{"_fhs"}{"$fh"}{"outbuffer"}
ABresult
1X1
011
000
906 length $self->{_fhs}{"$fh"}{outbuffer}) {
907 0 $self->close($fh);
908 }
909 }
910  
911 =head2 close
912  
913 Close a handle. Always use this method to close a handle that is being
914 watched by the multiplexer.
915  
916 $mux->close($fh);
917  
918 =cut
919  
920 sub close
921 0 ▶{
Subroutine close: not called CC=7 SCAR=40.3
Pod documented
922 0 my $self = shift;
923 0 my $fh = shift;
924 0 ▶ return unless exists $self->{_fhs}{"$fh"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_fhs"}{"$fh"} 0 0 0
925  
926 0 ▶ my $obj = $self->{_fhs}{"$fh"}{object} || $self->{_object};
Condition0%0/3
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_fhs'}{"$fh"}{'object'} || $self->{'_object'}
$self->{'_fhs'}{"$fh"}{'object'} $self->{'_object'}
Truth table0%0/3
A || B
A= $self->{'_fhs'}{"$fh"}{'object'}B= $self->{'_object'}
ABresult
1X1
011
000
927 0 ▶ warn "closing with read buffer" if length $self->{_fhs}{"$fh"}{inbuffer};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_fhs"}{"$fh"}{"inbuffer"} 0 0 0
928 0 ▶ warn "closing with write buffer" if length $self->{_fhs}{"$fh"}{outbuffer};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_fhs"}{"$fh"}{"outbuffer"} 0 0 0
929  
930 0 fd_set($self->{_readers}, $fh, 0);
931 0 fd_set($self->{_writers}, $fh, 0);
932  
933 0 delete $self->{_fhs}{"$fh"};
934 0 delete $self->{_handles}{"$fh"};
935 0 untie *$fh;
936 0 close $fh;
937 0 ▶ $obj->mux_close($self, $fh) if $obj && $obj->can("mux_close");
Branch: 1 branch
BranchTrueFalseTotal
if $obj and $obj->can("mux_close") 0 0 0
Condition0%0/3
$obj and $obj->can("mux_close")
!ll&&!rl&&r
000
MC/DC0%0/2
$obj and $obj->can("mux_close")
$obj $obj->can("mux_close")
Truth table0%0/3
A and B
A= $objB= $obj->can("mux_close")
ABresult
0X0
100
111
938 }
939  
940 # We set non-blocking mode on all descriptors. If we don't, then send
941 # might block if the data is larger than the kernel can accept all at once,
942 # even though select told us we can write. With non-blocking mode, we
943 # get a partial write in those circumstances, which is what we want.
944  
945 sub nonblock
946 7 ▶{ my $fh = shift;
Subroutine nonblock: called CC=3 SCAR=11.4
Pod undocumented
947  
948 7 if(IsWin)
949 { ioctl($fh, 0x8004667e, pack("L!", 1));
950 }
951 else
952 7 ▶ { my $flags = fcntl($fh, F_GETFL, 0)
Branch: 1 branch
BranchTrueFalseTotal
my $flags = fcntl($fh, 3, 0) or die "fcntl F_GETFL: $!\n" 7 0 7
953 or die "fcntl F_GETFL: $!\n";
954 7 ▶ fcntl($fh, F_SETFL, $flags | O_NONBLOCK)
Branch: 1 branch
BranchTrueFalseTotal
fcntl $fh, 4, $flags | 2048 or die "fcntl F_SETFL $!\n" 7 0 7
955 or die "fcntl F_SETFL $!\n";
956 }
957 }
958  
959 sub fd_set
960 7 ▶{
Subroutine fd_set: called CC=1 SCAR=0.0
Pod undocumented
961 7 vec($_[0], fileno($_[1]), 1) = $_[2];
962 }
963  
964 sub fd_isset
965 36 ▶{
Subroutine fd_isset: called CC=1 SCAR=0.0
Pod undocumented
966 36 return vec($_[0], fileno($_[1]), 1);
967 }
968  
969 # We tie handles into this package to handle write buffering.
970  
971 package IO::Multiplex::Handle;
972  
973 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
974 5 ▶use Tie::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
975 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
976 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
977 5 @ISA = qw(Tie::Handle);
978  
979 sub FILENO
980 54 ▶{
Subroutine FILENO: called CC=1 SCAR=0.0
981 54 my $self = shift;
982 54 return ($self->{_mux}->{_fhs}->{"$self->{_fh}"}->{fileno});
983 }
984  
985  
986 sub TIEHANDLE
987 7 ▶{
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
988 7 my $package = shift;
989 7 my $mux = shift;
990 7 my $fh = shift;
991  
992 7 my $self = bless { _mux => $mux,
993 _fh => $fh } => $package;
994 7 return $self;
995 }
996  
997 sub WRITE
998 6 ▶{
Subroutine WRITE: called CC=2 SCAR=7.0
999 6 my $self = shift;
1000 6 my ($msg, $len, $offset) = @_;
1001 6 ▶ $offset ||= 0;
Condition50%1/2
$offset ||= 0
l!l
06
MC/DC0%0/1
$offset ||= 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
1002 6 return $self->{_mux}->write($self->{_fh}, substr($msg, $offset, $len));
1003 }
1004  
1005 sub CLOSE
1006 0 ▶{
Subroutine CLOSE: not called CC=1 SCAR=6.9
1007 0 my $self = shift;
1008 0 return $self->{_mux}->shutdown($self->{_fh}, 2);
1009 }
1010  
1011 sub READ
1012 0 ▶{
Subroutine READ: not called CC=1 SCAR=6.9
1013 0 carp "Do not read from a muxed file handle";
1014 }
1015  
1016 sub READLINE
1017 0 ▶{
Subroutine READLINE: not called CC=1 SCAR=6.9
1018 0 carp "Do not read from a muxed file handle";
1019 }
1020  
1021 sub FETCH
1022 0 ▶{
Subroutine FETCH: not called CC=1 SCAR=6.9
1023 0 return "Fnord";
1024 }
1025  
1026 0 ▶sub UNTIE {}
Subroutine UNTIE: not called CC=1 SCAR=0.0
1027  
1028 5 1;
1029  
1030 __END__
↑ Summary