Twiggy/Prefork/Metabolic/Server/SS.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 116 CC 5 SCAR 21.0
CC
5
Cov
0%
_create_ss_tcp_server
20.0
start_listen
6.0
BEGIN
2.0
CRAP 8.2
1 package Twiggy::Prefork::Metabolic::Server::SS;
2  
3 # almost all code are stolen from Twiggy::Prefork::Server::SS
4  
5 use strict;
6 use warnings;
7 use parent qw(Twiggy::Prefork::Metabolic::Server);
8 use AnyEvent;
9 use AnyEvent::Util qw(fh_nonblocking guard);
10 use AnyEvent::Socket qw(format_address);
11 use Server::Starter qw(server_ports);
12  
13 sub start_listen {
14 my ($self, $app) = @_;
15  
16 if (Twiggy::Prefork::Metabolic::Server::DEBUG() && $self->{listen}) {
17 warn "'listen' option is currently ignored when used in conjunction with Server::Starter (start_server script)";
18 }
19  
20 my $host = $self->{host} || '';
21  
22 my @listen;
23 my $ports = server_ports();
24 while (my ($hostport, $fd) = each %$ports ) {
25 push @listen, $hostport;
26 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 $self->{listen} = \@listen;
31 }
32  
33 sub _create_ss_tcp_server {
34 my ($self, $hostport, $fd, $app) = @_;
35  
36 my $is_tcp = 1; # currently no unix socket support
37  
38 my ($host, $port);
39 if ($hostport =~ /(.*):(\d+)/) {
40 $host = $1;
41 $port = $2;
42 } else {
43 $host ||= '0.0.0.0';
44 $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 my %state;
52 $state{fh} = IO::Socket::INET->new(
53 Proto => 'tcp',
54 Listen => 128,
55 );
56  
57 $state{fh}->fdopen( $fd, 'w' ) or
58 Carp::croak "failed to bind to listening socket: $!";
59 fh_nonblocking $state{fh}, 1;
60  
61 my($listen_host, $listen_port);
62  
63 my $len;
64 my $prepare = $self->_accept_prepare_handler(\$listen_host, \$listen_port);
65 if ($prepare) {
66 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr getsockname $state{fh};
67 $len = $prepare && $prepare->( $state{fh}, format_address $host, $service );
68 }
69  
70 $len ||= 128;
71  
72 listen $state{fh}, $len or Carp::croak "listen: $!";
73  
74 my $accept = $self->_accept_handler($app, $is_tcp, \$listen_host, \$listen_port);
75 $state{aw} = AE::io $state{fh}, 0, sub {
76 # this closure keeps $state alive
77 while ($state{fh} && (my $peer = accept my $fh, $state{fh})) {
78 fh_nonblocking $fh, 1; # POSIX requires inheritance, the outside world does not
79  
80 my ($service, $host) = AnyEvent::Socket::unpack_sockaddr($peer);
81 $accept->($fh, format_address $host, $service);
82 }
83 };
84  
85 defined wantarray
86 ? guard { %state = () } # clear fh and watcher, which breaks the circular dependency
87 : ()
88 }
89  
90 1;
91  
92 __END__