| 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; | ||||||||||||||||||||
| 10 | 8 | ▶ | use vars '$VERSION'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 8 | $VERSION = '1.01'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 8 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 8 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 8 | ▶ | use Log::Report 'iomux'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 8 | ▶ | use Scalar::Util 'weaken'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 8 | ▶ | use Time::HiRes 'time'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 8 | ▶ | use Socket; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 8 | ▶ | use Fcntl; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 8 | my $start_time = time; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 15 | ▶ | sub new(@) {my $class = shift; (bless {}, $class)->init( {@_} ) } | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | sub init($) | ||||||||||||||||||||
| 30 | 16 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||
| 31 | 16 | ▶ | return $self if $self->{IH_name}; # already initialized | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 16 | ▶ | my $name = $self->{IH_name} = $args->{name} || "$self"; | ||||||||||||||||||
|
Condition33%1/3
$args->{'name'} || "$self"
MC/DC0%0/2
$args->{'name'} || "$self"
$args->{'name'}
"$self"
Truth table33%1/3
A || B
A= $args->{'name'}B= "$self"
| |||||||||||||||||||||
| 34 | 16 | ▶ | if(my $fh = $self->{IH_fh} = $args->{fh}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 15 | { $self->{IH_fileno} = $fh->fileno; | |||||||||||||||||||
| 36 | 15 | $self->{IH_uses_ssl} = UNIVERSAL::isa($fh, 'IO::Socket::SSL'); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | 16 | $self; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | ▶ | sub open() {panic} | ||||||||||||||||||
|
Subroutine
open: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | #------------------------- | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 1 | ▶ | sub name() {shift->{IH_name}} | ||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | ▶ | sub mux() {shift->{IH_mux}} | ||||||||||||||||||
|
Subroutine
mux: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | ▶ | sub fileno() {shift->{IH_fileno}} | ||||||||||||||||||
|
Subroutine
fileno: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 51 | 43 | ▶ | sub fh() {shift->{IH_fh}} | ||||||||||||||||||
|
Subroutine
fh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 52 | 0 | ▶ | sub usesSSL(){shift->{IH_uses_ssl}} | ||||||||||||||||||
|
Subroutine
usesSSL: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | #----------------------- | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | sub timeout(;$) | ||||||||||||||||||||
| 57 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
timeout: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||
| 58 | 0 | ▶ | @_ or return $self->{IH_timeout}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | my $old = $self->{IH_timeout}; | |||||||||||||||||||
| 61 | 0 | my $after = shift; | |||||||||||||||||||
| 62 | 0 | ▶ | my $when = !$after ? undef | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 63 | : $after > $start_time ? $after | ||||||||||||||||||||
| 64 | : ($after + time); | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | $self->{IH_mux}->changeTimeout($self->{IH_fileno}, $old, $when); | |||||||||||||||||||
| 67 | 0 | $self->{IH_timeout} = $when; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | sub close(;$) | ||||||||||||||||||||
| 72 | 16 | ▶ | { my ($self, $cb) = @_; | ||||||||||||||||||
|
Subroutine
close: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 73 | 16 | ▶ | if(my $fh = delete $self->{IH_fh}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 13 | ▶ | { if(my $mux = $self->{IH_mux}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 13 | { $mux->remove($self->{IH_fileno}); | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | 13 | $fh->close; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | 16 | local $!; | |||||||||||||||||||
| 80 | 16 | ▶ | $cb->($self) if $cb; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | #------------------------- | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | sub muxInit($;$) | ||||||||||||||||||||
| 86 | 13 | ▶ | { my ($self, $mux, $handler) = @_; | ||||||||||||||||||
|
Subroutine
muxInit: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 13 | $self->{IH_mux} = $mux; | |||||||||||||||||||
| 89 | 13 | weaken($self->{IH_mux}); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 13 | my $fileno = $self->{IH_fileno}; | |||||||||||||||||||
| 92 | 13 | ▶ | $mux->handler($fileno, $handler || $self); | ||||||||||||||||||
|
Condition33%1/3
$handler || $self
MC/DC0%0/2
$handler || $self
$handler
$self
Truth table33%1/3
A || B
A= $handlerB= $self
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 13 | ▶ | if(my $timeout = $self->{IH_timeout}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | 0 | { $mux->changeTimeout($fileno, undef, $timeout); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 13 | trace "mux add #$fileno, $self->{IH_name}"; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | sub muxRemove() | ||||||||||||||||||||
| 103 | 13 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
muxRemove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 104 | 13 | delete $self->{IH_mux}; | |||||||||||||||||||
| 105 | #use Carp 'cluck'; | ||||||||||||||||||||
| 106 | #cluck "REMOVE"; | ||||||||||||||||||||
| 107 | 13 | trace "mux remove #$self->{IH_fileno}, $self->{IH_name}"; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub muxTimeout() | ||||||||||||||||||||
| 112 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
muxTimeout: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 113 | 0 | error __x"timeout set on {name} but not handled", name => $self->name; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | #---------------------- | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | #sub muxReadFlagged($) { panic "no input expected on ". shift->name } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | #sub muxExceptFlagged($) { panic "exception arrived on ". shift->name } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | #sub muxWriteFlagged($) { shift } # simply ignore write offers | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | #------------------------- | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | sub show() | ||||||||||||||||||||
| 131 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
show: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||
| 132 | 0 | my $name = $self->name; | |||||||||||||||||||
| 133 | 0 | ▶ | my $fh = $self->fh | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 134 | or return "fileno=".$self->fileno." is closed; name=$name"; | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | my $mode = 'unknown'; | |||||||||||||||||||
| 137 | 0 | ▶ | unless($^O eq 'Win32') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 0 | ▶ | { my $flags = fcntl $fh, F_GETFL, 0 or fault "fcntl F_GETFL"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | 0 | ▶ | $mode = ($flags & O_WRONLY) ? 'w' | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 140 | : ($flags & O_RDONLY) ? 'r' | ||||||||||||||||||||
| 141 | : ($flags & O_RDWR) ? 'rw' | ||||||||||||||||||||
| 142 | : 'p'; | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 0 | my @show = ("fileno=".$fh->fileno, "mode=$mode"); | |||||||||||||||||||
| 146 | 0 | ▶ | if(my $sockopts = getsockopt $fh, SOL_SOCKET, SO_TYPE) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 147 | { # socket | ||||||||||||||||||||
| 148 | 0 | my $type = unpack "i", $sockopts; | |||||||||||||||||||
| 149 | 0 | ▶ | my $kind = $type==SOCK_DGRAM ? 'UDP' : $type==SOCK_STREAM ? 'TCP' | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 150 | : 'unknown'; | ||||||||||||||||||||
| 151 | 0 | push @show, "sock=$kind"; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | join ", ", @show, "name=$name"; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | sub fdset($$$$) | ||||||||||||||||||||
| 159 | 19 | ▶ | { my $self = shift; | ||||||||||||||||||
|
Subroutine
fdset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 160 | 19 | $self->{IH_mux}->fdset($self->{IH_fileno}, @_); | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | sub extractSocket($) | ||||||||||||||||||||
| 165 | 0 | ▶ | { my ($thing, $args) = @_; | ||||||||||||||||||
|
Subroutine
extractSocket: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 166 | 0 | ▶ | my $class = ref $thing || $thing; | ||||||||||||||||||
|
Condition0%0/3
ref $thing || $thing
MC/DC0%0/2
ref $thing || $thing
ref $thing
$thing
Truth table0%0/3
A || B
A= ref $thingB= $thing
| |||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 0 | my $socket = delete $args->{socket}; | |||||||||||||||||||
| 169 | 0 | ▶ | return $socket if $socket; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | my @sockopts = (Blocking => 0); | |||||||||||||||||||
| 172 | push @sockopts, $_ => $args->{$_} | ||||||||||||||||||||
| 173 | 0 | for grep /^[A-Z]/, keys %$args; | |||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | @sockopts | ||||||||||||||||||||
| 176 | 0 | ▶ | or error __x"pass socket or provide parameters to create one for {pkg}" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | , pkg => $class; | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 0 | my $ssl = delete $args->{use_ssl}; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | # the extension will load these classes | ||||||||||||||||||||
| 182 | 0 | ▶ | my $make = $ssl ? 'IO::Socket::SSL' : 'IO::Socket::INET'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | 0 | ▶ | $socket = $make->new(@sockopts) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | or fault __x"cannot create {pkg} socket", pkg => $class; | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | 0 | $socket; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 8 | 1; | |||||||||||||||||||