| 1 | package Twiggy::Prefork::Server::SS; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # almost all code are stolen from Twiggy::Server::SS | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use base qw(Twiggy::Prefork::Server); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use AnyEvent::Util qw(fh_nonblocking guard); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use AnyEvent::Socket qw(format_address); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Server::Starter qw(server_ports); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | sub start_listen { | ||||||||||||||||||||||||||
|
Subroutine
start_listen: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 11 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 11 | if (Twiggy::Prefork::Server::DEBUG() && $self->{listen}) { | |||||||||||||||||||||||||||
| 17 | warn "'listen' option is currently ignored when used in conjunction with Server::Starter (start_server script)"; | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | ▶ | my $host = $self->{host} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'host'} || ''
MC/DC0%0/1
$self->{'host'} || ''
$self->{'host'}
Truth table50%1/2
A
A= $self->{'host'}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 11 | my @listen; | |||||||||||||||||||||||||||
| 23 | 11 | my $ports = server_ports(); | |||||||||||||||||||||||||||
| 24 | 11 | ▶ | while (my ($hostport, $fd) = each %$ports ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 11 | push @listen, $hostport; | |||||||||||||||||||||||||||
| 26 | 11 | push @{$self->{listen_guards}}, $self->_create_ss_tcp_server($hostport, $fd, $app); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # overwrite, just in case somebody wants to refer to it afterwards | ||||||||||||||||||||||||||||
| 30 | 11 | $self->{listen} = \@listen; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 11 | ▶ | sub _create_ss_tcp_server { | ||||||||||||||||||||||||||
|
Subroutine
_create_ss_tcp_server: called CC=9 SCAR=23.1
| |||||||||||||||||||||||||||||
| 34 | 11 | my ($self, $hostport, $fd, $app) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 11 | my $is_tcp = 1; # currently no unix socket support | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 11 | my ($host, $port); | |||||||||||||||||||||||||||
| 39 | 11 | ▶ | if ($hostport =~ /(.*):(\d+)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | $host = $1; | |||||||||||||||||||||||||||
| 41 | 0 | $port = $2; | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 11 | ▶ | $host ||= '0.0.0.0'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$host ||= "0.0.0.0"
MC/DC0%0/1
$host ||= "0.0.0.0"
$host
Truth table50%1/2
A
A= $host
| |||||||||||||||||||||||||||||
| 44 | 11 | $port = $hostport; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # /WE/ don't care what the address family, type of socket we got, just | ||||||||||||||||||||||||||||
| 48 | # create a new handle, and perform a fdopen on it. So that part of | ||||||||||||||||||||||||||||
| 49 | # AE::Socket::tcp_server is stripped out | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 11 | my %state; | |||||||||||||||||||||||||||
| 52 | 11 | $state{fh} = IO::Socket::INET->new( | |||||||||||||||||||||||||||
| 53 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 54 | Listen => 128, | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | ▶ | $state{fh}->fdopen( $fd, 'w' ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | Carp::croak "failed to bind to listening socket: $!"; | ||||||||||||||||||||||||||||
| 59 | 11 | fh_nonblocking $state{fh}, 1; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 11 | my($listen_host, $listen_port); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 11 | my $len; | |||||||||||||||||||||||||||
| 64 | 11 | my $prepare = $self->_accept_prepare_handler(\$listen_host, \$listen_port); | |||||||||||||||||||||||||||
| 65 | 11 | ▶ | if ($prepare) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 11 | my ($service, $host) = AnyEvent::Socket::unpack_sockaddr getsockname $state{fh}; | |||||||||||||||||||||||||||
| 67 | 11 | ▶ | $len = $prepare && $prepare->( $state{fh}, format_address $host, $service ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$prepare && &$prepare($state{'fh'}, &format_address($host), $service)
MC/DC0%0/2
$prepare && &$prepare($state{'fh'}, &format_address($host), $service)
$prepare
&$prepare($state{'fh'}, &format_address($host), $service)
Truth table33%1/3
A && B
A= $prepareB= &$prepare($state{'fh'}, &format_address($host), $service)
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 11 | ▶ | $len ||= 128; | ||||||||||||||||||||||||||
|
Condition50%1/2
$len ||= 128
MC/DC0%0/1
$len ||= 128
$len
Truth table50%1/2
A
A= $len
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 11 | ▶ | listen $state{fh}, $len or Carp::croak "listen: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | my $accept = $self->_accept_handler($app, $is_tcp, \$listen_host, \$listen_port); | |||||||||||||||||||||||||||
| 75 | 1 | ▶ | $state{aw} = AE::io $state{fh}, 0, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 76 | # this closure keeps $state alive | ||||||||||||||||||||||||||||
| 77 | 1 | ▶ | while ($state{fh} && (my $peer = accept my $fh, $state{fh})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$state{"fh"} and my $peer = accept(my $fh, $state{"fh"})
MC/DC50%1/2
$state{"fh"} and my $peer = accept(my $fh, $state{"fh"})
$state{"fh"}
my $peer = accept(my $fh, $state{"fh"})
Truth table66%2/3
A and B
A= $state{"fh"}B= my $peer = accept(my $fh, $state{"fh"})
| |||||||||||||||||||||||||||||
| 78 | 2 | fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | my ($service, $host) = AnyEvent::Socket::unpack_sockaddr($peer); | |||||||||||||||||||||||||||
| 81 | 2 | $accept->($fh, format_address $host, $service); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 11 | }; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | defined wantarray | ||||||||||||||||||||||||||||
| 86 | 10 | ▶ | ? guard { %state = () } # clear fh and watcher, which breaks the circular dependency | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | : () | ||||||||||||||||||||||||||||
| 88 | 11 | ▶ | } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 11 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||