| 1 | |||||||||||||||||||||||||||||
| 2 | package IO::Socket::INET::Daemon; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 2 | my ($class, %rc) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | croak "Called with no/invalid port.\n" if(!$rc{port} or $rc{port} =~ /\D/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $rc{"port"} or $rc{"port"} =~ /\D/
MC/DC0%0/2
not $rc{"port"} or $rc{"port"} =~ /\D/
not $rc{"port"}
$rc{"port"} =~ /\D/
Truth table33%1/3
not(A) or B
A= not $rc{"port"}B= $rc{"port"} =~ /\D/
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | return bless { | ||||||||||||||||||||||||||||
| 21 | port => $rc{port}, | ||||||||||||||||||||||||||||
| 22 | host => $rc{host} || 'localhost', | ||||||||||||||||||||||||||||
| 23 | callback => $rc{callback} || {}, | ||||||||||||||||||||||||||||
| 24 | timeout => $rc{timeout}, | ||||||||||||||||||||||||||||
| 25 | 2 | ▶ | }, $class; | ||||||||||||||||||||||||||
|
Condition50%2/4
$rc{'host'} || 'localhost'
$rc{'callback'} || {}
MC/DC0%0/1
$rc{'host'} || 'localhost'
$rc{'host'}
MC/DC0%0/1
$rc{'callback'} || {}
$rc{'callback'}
Truth table50%1/2
A
A= $rc{'host'}
Truth table50%1/2
A
A= $rc{'callback'}
| |||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub callback { | ||||||||||||||||||||||||||
|
Subroutine
callback: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 2 | my ($self, %callback) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | @{$self->{callback}}{keys %callback} = values %callback; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=21.5
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Create server socket. | ||||||||||||||||||||||||||||
| 40 | my $host = $self->{sck} = new IO::Socket::INET( | ||||||||||||||||||||||||||||
| 41 | LocalHost => $self->{host}, | ||||||||||||||||||||||||||||
| 42 | LocalPort => $self->{port}, | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | Proto => 'tcp', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | ReuseAddr => !0, | ||||||||||||||||||||||||||||
| 45 | Listen => 32, | ||||||||||||||||||||||||||||
| 46 | ) or return; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | $self->{stop} = 0; | |||||||||||||||||||||||||||
| 49 | 1 | my $select = $self->{select} = new IO::Select($host); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # The main loop. | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | until($self->{stop}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 5 | $self->call('tick', undef); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Get readable sockets. | ||||||||||||||||||||||||||||
| 56 | 5 | for my $io ($select->can_read($self->{timeout})) { | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # If the server socket is readable, get the pending incoming | ||||||||||||||||||||||||||||
| 59 | # connection, call the callback and add the peer to our list. | ||||||||||||||||||||||||||||
| 60 | 5 | ▶ | if($io == $host) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 2 | my $peer = $io->accept; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | if($self->call('add', $peer)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | $select->add($peer); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | else { | ||||||||||||||||||||||||||||
| 67 | 0 | $self->remove($peer); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # If it's a peer, call the data callback. Remove peer if the | ||||||||||||||||||||||||||||
| 72 | # callback returns something false or if the connection is dead. | ||||||||||||||||||||||||||||
| 73 | elsif($io->connected) { | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | if(!$self->call('data', $io)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | $self->call('remove', $io); | |||||||||||||||||||||||||||
| 76 | 1 | $self->remove($io); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||
| 80 | 0 | $self->call('remove', $io); | |||||||||||||||||||||||||||
| 81 | 0 | $self->remove($io); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Call a callback function. For internal use only. Takes the name of the | ||||||||||||||||||||||||||||
| 88 | # callback (add, remove or data) and the socket handle as arguments. Returns | ||||||||||||||||||||||||||||
| 89 | # true if there was no such callback, it returns non-zero by default, otherwise | ||||||||||||||||||||||||||||
| 90 | # the return value of the callback is returned. | ||||||||||||||||||||||||||||
| 91 | 11 | ▶ | sub call { | ||||||||||||||||||||||||||
|
Subroutine
call: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 11 | my ($self, $callback, $io) = @_; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | my $func = $self->{callback}->{$callback}; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 11 | ▶ | return $func ? &{$func}($io, $self) : !0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # This closes a connection to a peer and removes it from our socket list. | ||||||||||||||||||||||||||||
| 100 | 3 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 3 | my ($self, $io) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 3 | ▶ | if($io and $io != $self->{sck}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$io and $io != $self->{"sck"}
MC/DC50%1/2
$io and $io != $self->{"sck"}
$io
$io != $self->{"sck"}
Truth table66%2/3
A and B
A= $ioB= $io != $self->{"sck"}
| |||||||||||||||||||||||||||||
| 104 | 2 | my $select = $self->{select}; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | $select->remove($io); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | $io->shutdown(SHUT_RDWR); | |||||||||||||||||||||||||||
| 109 | 2 | $io->close; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # This simply sets a variable to a true value, so the main loop will stop after | ||||||||||||||||||||||||||||
| 114 | # the next cycle. | ||||||||||||||||||||||||||||
| 115 | 1 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 117 | 1 | $self->{stop} = !0; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # This closes all connections and the server socket. Can be called to clean up | ||||||||||||||||||||||||||||
| 121 | # manually, but is also called automatically from DESTROY. | ||||||||||||||||||||||||||||
| 122 | 2 | ▶ | sub destroy { | ||||||||||||||||||||||||||
|
Subroutine
destroy: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | $self->{stop} = !0; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | my $select = $self->{select}; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | if($select) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 1 | $self->remove($_) for($select->handles); | |||||||||||||||||||||||||||
| 131 | 1 | delete $self->{select}; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | my $host = delete $self->{sck}; | |||||||||||||||||||||||||||
| 135 | 2 | ▶ | if($host) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | $host->shutdown(SHUT_RDWR); | |||||||||||||||||||||||||||
| 137 | 1 | $host->close; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 142 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 143 | 2 | $self->destroy; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 2 | !0; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||