blib/lib/POE/Component/Server/TCP.pm

filter: Statementstmt 80.4% 218 / 271 Branchbran 62.6% 99 / 158 Conditioncond 47.2% 34 / 72 MC/DCmcdc 18.6% 8 / 43 Subroutinesub 81.0% 30 / 37 Podpod 100% 1 / 1 totaltotal 67.0% 390 / 582 CC 100 SCAR 37.1
CC
100
Cov
70%
new
105.5
__ANON__
20.0
__ANON__
14.0
CRAP 41.0
« blib/lib/POE/Wheel/FollowTail.pm ↑ Summary blib/lib/POE/Wheel/ReadLine.pm »
1 package POE::Component::Server::TCP;
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 15 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
7  
8 15 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Socket qw(INADDR_ANY inet_ntoa inet_aton AF_INET AF_UNIX PF_UNIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Errno qw(ECONNABORTED ECONNRESET);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=15.1
13 # under perl-5.6.2 the warning "leaks" from the eval, while newer versions don't...
14 # it's due to Exporter.pm behaving differently, so we have to shut it up
15 15 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶ local *Carp::carp = sub { die @_ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17  
18 # Socket::GetAddrInfo provides getaddrinfo where earlier Perls' Socket don't.
19 15 eval { Socket->import('getaddrinfo') };
20 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
21 # :newapi is legacy, but we include it to be sure in case the user has an old version of GAI
22 0 eval { require Socket::GetAddrInfo; Socket::GetAddrInfo->import( qw(:newapi getaddrinfo) ) };
23 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
24 0 ▶ *getaddrinfo = sub { Carp::confess("Unable to use IPv6: Socket::GetAddrInfo not available") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 }
26 }
27 }
28  
29 # Explicit use to import the parameter constants.
30 15 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 15 ▶use POE::Driver::SysRW;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 15 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 15 ▶use POE::Wheel::ReadWrite;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 15 ▶use POE::Wheel::SocketFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub DEBUG () { 0 }
37  
38 # Create the server. This is just a handy way to encapsulate
39 # POE::Session->create(). Because the states are so small, it uses
40 # real inline coderefs.
41  
42 21 ▶sub new {
Subroutine new: called CC=48 SCAR=46.6
Pod documented
43 21 my $type = shift;
44  
45 # Helper so we don't have to type it all day. $mi is a name I call
46 # myself.
47 21 my $mi = $type . '->new()';
48  
49 # If they give us lemons, tell them to make their own damn
50 # lemonade.
51 21 ▶ croak "$mi requires an even number of parameters" if (@_ & 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 21 21
52 21 my %param = @_;
53  
54 # Extract parameters.
55 21 my $alias = delete $param{Alias};
56 21 my $address = delete $param{Address};
57 21 my $hname = delete $param{Hostname};
58 21 my $port = delete $param{Port};
59 21 ▶ my $domain = delete($param{Domain}) || AF_INET;
Condition100%2/2
delete $param{'Domain'} || 2
l!l
120
MC/DC100%1/1
delete $param{'Domain'} || 2
delete $param{'Domain'}
Truth table100%2/2
A
A= delete $param{'Domain'}
Aresult
11
00
60 21 my $concurrency = delete $param{Concurrency};
61  
62 21 ▶ $port = 0 unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 3 18 21
63  
64 21 foreach (
65 qw(
66 Acceptor Error ClientInput
67 ClientPreConnect ClientConnected ClientDisconnected
68 ClientError ClientFlushed
69 ClientLow ClientHigh
70 )
71 ) {
72 croak "$_ must be a coderef"
73 210 ▶ if defined($param{$_}) and ref($param{$_}) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $param{$_} and ref $param{$_} ne "CODE" 0 210 210
Condition66%2/3
defined $param{$_} and ref $param{$_} ne "CODE"
!ll&&!rl&&r
157530
MC/DC0%0/2
defined $param{$_} and ref $param{$_} ne "CODE"
defined $param{$_} ref $param{$_} ne "CODE"
Truth table66%2/3
A and B
A= defined $param{$_}B= ref $param{$_} ne "CODE"
ABresult
0X0
100
111
74 }
75  
76 21 my $high_mark_level = delete $param{HighMark};
77 21 my $low_mark_level = delete $param{LowMark};
78 21 my $high_event = delete $param{ClientHigh};
79 21 my $low_event = delete $param{ClientLow};
80  
81 my $mark_param_count = (
82 21 grep { defined $_ }
83 ($high_mark_level, $low_mark_level, $high_event, $low_event)
84 );
85 21 ▶ if ($mark_param_count and $mark_param_count < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mark_param_count and $mark_param_count < 4) 0 21 21
Condition33%1/3
$mark_param_count and $mark_param_count < 4
!ll&&!rl&&r
2100
MC/DC0%0/2
$mark_param_count and $mark_param_count < 4
$mark_param_count $mark_param_count < 4
Truth table33%1/3
A and B
A= $mark_param_countB= $mark_param_count < 4
ABresult
0X0
100
111
86 0 croak "If you use the Mark settings, you must define all four";
87 }
88  
89 21 ▶ $high_event = sub { } unless defined $high_event;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $high_event 21 0 21
90 21 ▶ $low_event = sub { } unless defined $low_event;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined $low_event 21 0 21
91  
92 21 my $accept_callback = delete $param{Acceptor};
93 21 my $error_callback = delete $param{Error};
94  
95 21 my $client_input = delete $param{ClientInput};
96  
97 # Acceptor and ClientInput are mutually exclusive.
98 21 ▶ croak "$mi needs either an Acceptor or a ClientInput but not both"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $accept_callback xor defined $client_input 0 21 21
Condition50%2/4
defined $accept_callback xor defined $client_input
l&&rl&&!r!l&&r!l&&!r
08130
MC/DC0%0/2
defined $accept_callback xor defined $client_input
defined $accept_callback defined $client_input
Truth table50%2/4
A xor B
A= defined $accept_callbackB= defined $client_input
ABresult
110
101
011
000
99 unless defined($accept_callback) xor defined($client_input);
100  
101 # Make sure ClientXyz are accompanied by ClientInput.
102 21 ▶ unless (defined($client_input)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client_input) 8 13 21
103 8 foreach (grep /^Client/, keys %param) {
104 0 croak "$_ not permitted without ClientInput";
105 }
106 }
107  
108 21 my $client_pre_connect = delete $param{ClientPreConnect};
109 21 my $client_connected = delete $param{ClientConnected};
110 21 my $client_disconnected = delete $param{ClientDisconnected};
111 21 my $client_error = delete $param{ClientError};
112 21 my $client_filter = delete $param{ClientFilter};
113 21 my $client_infilter = delete $param{ClientInputFilter};
114 21 my $client_outfilter = delete $param{ClientOutputFilter};
115 21 my $client_flushed = delete $param{ClientFlushed};
116 21 my $session_type = delete $param{SessionType};
117 21 my $session_params = delete $param{SessionParams};
118 21 my $server_started = delete $param{Started};
119 21 my $server_stopped = delete $param{Stopped};
120 21 my $listener_args = delete $param{ListenerArgs};
121  
122 21 ▶ $listener_args = [] unless defined $listener_args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $listener_args 20 1 21
123 21 ▶ croak "ListenerArgs must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $listener_args eq "ARRAY" 0 21 21
124 unless ref($listener_args) eq 'ARRAY';
125  
126 21 ▶ if (exists $param{Args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"Args"}) 0 21 21
127 0 ▶ if (exists $param{ClientArgs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{'ClientArgs'}) { } 0 0 0
128 0 carp "Args is deprecated, and ignored since ClientArgs is present";
129 0 delete $param{Args};
130 }
131 else {
132 0 carp "Args is deprecated but allowed for now. Please use ClientArgs";
133 }
134 }
135  
136 21 ▶ my $client_args = delete($param{ClientArgs}) || delete($param{Args});
Condition66%2/3
delete $param{'ClientArgs'} || delete $param{'Args'}
l!l&&r!l&&!r
1020
MC/DC50%1/2
delete $param{'ClientArgs'} || delete $param{'Args'}
delete $param{'ClientArgs'} delete $param{'Args'}
Truth table66%2/3
A || B
A= delete $param{'ClientArgs'}B= delete $param{'Args'}
ABresult
1X1
011
000
137  
138 21 ▶ if ( (defined $client_infilter and ! defined $client_outfilter) or
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client_infilter and not defined $client_outfilter or defined $client_outfilter and not defined $client_infilter) 0 21 21
Condition33%3/9
defined $client_infilter and not defined $client_outfilter or defined $client_outfilter and not defined $client_infilter
l!l&&r!l&&!r
0021
defined $client_infilter and not defined $client_outfilter
!ll&&!rl&&r
2100
defined $client_outfilter and not defined $client_infilter
!ll&&!rl&&r
2100
MC/DC0%0/4
defined $client_infilter and not defined $client_outfilter or defined $client_outfilter and not defined $client_infilter
defined $client_infilter not defined $client_outfilter defined $client_outfilter not defined $client_infilter
Truth table14%1/7
A and not(B) or C and not(D)
A= defined $client_infilterB= not defined $client_outfilterC= defined $client_outfilterD= not defined $client_infilter
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
139 (defined $client_outfilter and ! defined $client_infilter) ) {
140 0 croak "ClientInputFilter must be used with ClientOutputFilter";
141 }
142  
143 21 ▶ if (defined $client_filter and defined $client_infilter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client_filter and defined $client_infilter) 0 21 21
Condition66%2/3
defined $client_filter and defined $client_infilter
!ll&&!rl&&r
1740
MC/DC0%0/2
defined $client_filter and defined $client_infilter
defined $client_filter defined $client_infilter
Truth table66%2/3
A and B
A= defined $client_filterB= defined $client_infilter
ABresult
0X0
100
111
144 0 carp "ClientFilter ignored with ClientInputFilter and ClientOutputFilter";
145 0 undef $client_filter;
146 }
147  
148 # Defaults.
149  
150 21 ▶ $concurrency = -1 unless defined $concurrency;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $concurrency 17 4 21
151 21 my $accept_session_id;
152  
153 21 ▶ if (!defined $address && defined $hname) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $address and defined $hname) 0 21 21
Condition33%1/3
not defined $address and defined $hname
!ll&&!rl&&r
2100
MC/DC0%0/2
not defined $address and defined $hname
not defined $address defined $hname
Truth table33%1/3
not(A) and B
A= not defined $addressB= defined $hname
ABresult
0X0
100
111
154 0 $address = inet_aton($hname);
155 }
156 21 ▶ $address = INADDR_ANY unless defined $address;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $address 0 21 21
157  
158 21 ▶ $error_callback = \&_default_server_error unless defined $error_callback;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error_callback 21 0 21
159  
160 21 ▶ $session_type = 'POE::Session' unless defined $session_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $session_type 21 0 21
161 21 ▶ if (defined($session_params) && ref($session_params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session_params and ref $session_params) { } 0 21 21
Condition33%1/3
defined $session_params and ref $session_params
!ll&&!rl&&r
2100
MC/DC0%0/2
defined $session_params and ref $session_params
defined $session_params ref $session_params
Truth table33%1/3
A and B
A= defined $session_paramsB= ref $session_params
ABresult
0X0
100
111
162 0 ▶ if (ref($session_params) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $session_params ne "ARRAY") 0 0 0
163 0 croak "SessionParams must be an array reference";
164 }
165 } else {
166 21 $session_params = [ ];
167 }
168  
169 21 ▶ if (defined $client_input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client_input) 13 8 21
170 13 ▶ $client_error = \&_default_client_error unless defined $client_error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client_error 9 4 13
171 13 ▶ $client_args = [] unless defined $client_args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $client_args 12 1 13
172  
173 # Extra states.
174  
175 13 my $inline_states = delete $param{InlineStates};
176 13 ▶ $inline_states = {} unless defined $inline_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inline_states 12 1 13
177  
178 13 my $package_states = delete $param{PackageStates};
179 13 ▶ $package_states = [] unless defined $package_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package_states 12 1 13
180  
181 13 my $object_states = delete $param{ObjectStates};
182 13 ▶ $object_states = [] unless defined $object_states;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object_states 12 1 13
183  
184 13 my $shutdown_on_error = 1;
185 13 ▶ if (exists $param{ClientShutdownOnError}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{"ClientShutdownOnError"}) 0 13 13
186 0 $shutdown_on_error = delete $param{ClientShutdownOnError};
187 }
188  
189 13 ▶ croak "InlineStates must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inline_states eq "HASH" 0 13 13
190 unless ref($inline_states) eq 'HASH';
191  
192 13 ▶ croak "PackageStates must be a list or array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $package_states eq "ARRAY" 0 13 13
193 unless ref($package_states) eq 'ARRAY';
194  
195 13 ▶ croak "ObjectsStates must be a list or array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $object_states eq "ARRAY" 0 13 13
196 unless ref($object_states) eq 'ARRAY';
197  
198 13 ▶ croak "ClientArgs must be an array reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $client_args eq "ARRAY" 0 13 13
199 unless ref($client_args) eq 'ARRAY';
200  
201 # Sanity check, thanks to crab@irc for making this mistake, ha!
202 # TODO we could move this to POE::Session and make it a
203 # "sanity checking" sub somehow...
204 13 if (POE::Kernel::ASSERT_USAGE) {
205 13 my %forbidden_handlers = (
206 _child => 1,
207 _start => 1,
208 _stop => 1,
209 shutdown => 1,
210 tcp_server_got_error => 1,
211 tcp_server_got_flush => 1,
212 tcp_server_got_high => 1,
213 tcp_server_got_input => 1,
214 tcp_server_got_low => 1,
215 );
216  
217 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my(@forbidden_inline_handlers) = grep({exists $inline_states->{$_};} keys %forbidden_handlers)) 7 6 13
218 my @forbidden_inline_handlers = (
219 61 grep { exists $inline_states->{$_} }
220 keys %forbidden_handlers
221 )
222 ) {
223 0 croak "These InlineStates aren't allowed: @forbidden_inline_handlers";
224 }
225  
226 13 my %handlers = (
227 PackageStates => $package_states,
228 ObjectStates => $object_states,
229 );
230  
231 13 ▶ while (my ($name, $states) = each(%handlers)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %handlers) 12 6 18
232 19 my %states_hash = @$states;
233 19 my @forbidden_handlers;
234 12 ▶ while (my ($package, $handlers) = each %states_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %states_hash) 0 12 12
235 0 ▶ croak "Undefined $name member for $package" unless (
Branch: 1 branch
BranchTrueFalseTotal
unless defined $handlers 0 0 0
236 defined $handlers
237 );
238  
239 0 ▶ if (ref($handlers) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $handlers eq 'HASH') { } 0 0 0
elsif (ref $handlers eq 'ARRAY') { } 0 0 0
240 push(
241 @forbidden_handlers,
242 0 grep { exists $handlers->{$_} }
243 keys %forbidden_handlers
244 );
245 }
246 elsif (ref($handlers) eq 'ARRAY') {
247 push(
248 @forbidden_handlers,
249 0 grep { exists $forbidden_handlers{$_} }
250 @$handlers
251 );
252 }
253 else {
254 0 croak "Unknown $name member type for $package";
255 }
256 }
257  
258 12 ▶ croak "These $name aren't allowed: @forbidden_handlers" if (
Branch: 1 branch
BranchTrueFalseTotal
if @forbidden_handlers 0 12 12
259 @forbidden_handlers
260 );
261 }
262 }
263  
264 # Revise the acceptor callback so it spawns a session.
265  
266 6 ▶ unless (defined $accept_callback) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $accept_callback) 6 0 6
267 38 ▶ $accept_callback = sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
268 38 my ($socket, $remote_addr, $remote_port) = @_[ARG0, ARG1, ARG2];
269  
270 $session_type->create(
271 @$session_params,
272 inline_states => {
273 38 ▶ _start => sub {
Subroutine __ANON__: called CC=10 SCAR=26.4
274 38 my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP];
275  
276 38 $heap->{shutdown} = 0;
277 38 $heap->{shutdown_on_error} = $shutdown_on_error;
278  
279 # Unofficial UNIX support, suggested by Damir Dzeko.
280 # Real UNIX socket support should go into a separate
281 # module, but if that module only differs by four
282 # lines of code it would be bad to maintain two
283 # modules for the price of one. One solution would be
284 # to pull most of this into a base class and derive
285 # TCP and UNIX versions from that.
286 38 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($domain == 1 or $domain == 1) { } 0 38 38
elsif (length $remote_addr == 4) { } 38 0 38
Condition33%1/3
$domain == 1 or $domain == 1
l!l&&r!l&&!r
0038
MC/DC0%0/2
$domain == 1 or $domain == 1
$domain == 1 $domain == 1
Truth table33%1/3
A or B
A= $domain == 1B= $domain == 1
ABresult
1X1
011
000
287 $domain == AF_UNIX or $domain == PF_UNIX
288 ) {
289 0 $heap->{remote_ip} = "LOCAL";
290 }
291 elsif (length($remote_addr) == 4) {
292 38 $heap->{remote_ip} = inet_ntoa($remote_addr);
293 }
294 else {
295 0 $heap->{remote_ip} = ( getaddrinfo($remote_addr) )[1];
296 }
297  
298 38 $heap->{remote_port} = $remote_port;
299  
300 38 my $socket = $_[ARG0];
301 38 ▶ if ($client_pre_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_pre_connect) 2 36 38
302 2 $socket = $client_pre_connect->(@_);
303 2 ▶ unless (defined($socket) and ref($socket) and fileno($socket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $socket and ref $socket and fileno $socket) 0 2 2
Condition33%2/6
defined $socket and ref $socket and fileno $socket
!ll&&!rl&&r
002
defined $socket and ref $socket
!ll&&!rl&&r
002
MC/DC0%0/3
defined $socket and ref $socket and fileno $socket
defined $socket ref $socket fileno $socket
Truth table25%1/4
A and B and C
A= defined $socketB= ref $socketC= fileno $socket
ABCresult
0XX0
10X0
1100
1111
304 # TODO - The user ought to know what's going on
305 # here, since it's triggered by something their
306 # callback has done. Should we expose a callback
307 # anyway to avoid potential confusion?
308 0 return;
309 }
310 }
311  
312 38 ▶ $heap->{client} = POE::Wheel::ReadWrite->new(
Branch: 1 branch
BranchTrueFalseTotal
$mark_param_count ? : 0 38 38
313 Handle => $socket,
314 Driver => POE::Driver::SysRW->new(),
315 _get_filters(
316 $client_filter,
317 $client_infilter,
318 $client_outfilter
319 ),
320 InputEvent => 'tcp_server_got_input',
321 ErrorEvent => 'tcp_server_got_error',
322 FlushedEvent => 'tcp_server_got_flush',
323  
324 (
325 $mark_param_count
326 ? (
327 HighMark => $high_mark_level,
328 HighEvent => 'tcp_server_got_high',
329 LowMark => $low_mark_level,
330 LowEvent => 'tcp_server_got_low',
331 )
332 : ()
333 ),
334 );
335  
336 # Expand the Args constructor array, and place a copy
337 # into @_[ARG0..]. There are only 2 parameters.
338 38 splice(@_, ARG0, 2, @{$_[ARG1]});
339  
340 38 ▶ $client_connected and $client_connected->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$client_connected and &$client_connected(@_) 34 4 38
341 },
342 tcp_server_got_high => $high_event,
343 tcp_server_got_low => $low_event,
344  
345 # To quiet ASSERT_STATES.
346 0 ▶ _child => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
347  
348 38 ▶ tcp_server_got_input => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
349 38 ▶ return if $_[HEAP]->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $_[3]{"shutdown"} 0 38 38
350 38 $client_input->(@_);
351 38 undef;
352 },
353 22 ▶ tcp_server_got_error => sub {
Subroutine __ANON__: called CC=4 SCAR=15.0
354 22 DEBUG and warn(
355 "$$: $alias child Error ARG0=$_[ARG0] ARG1=$_[ARG1]"
356 );
357 22 ▶ unless ($_[ARG0] eq 'accept' and $_[ARG1] == ECONNABORTED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[10] eq "accept" and $_[11] == 103) 22 0 22
Condition33%1/3
$_[10] eq "accept" and $_[11] == 103
!ll&&!rl&&r
2200
MC/DC0%0/2
$_[10] eq "accept" and $_[11] == 103
$_[10] eq "accept" $_[11] == 103
Truth table33%1/3
A and B
A= $_[10] eq "accept"B= $_[11] == 103
ABresult
0X0
100
111
358 22 $client_error->(@_);
359 22 ▶ if ($_[HEAP]->{shutdown_on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"shutdown_on_error"}) 22 0 22
360 22 $_[HEAP]->{got_an_error} = 1;
361 22 $_[KERNEL]->yield("shutdown");
362 }
363 }
364 },
365 38 ▶ tcp_server_got_flush => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
366 38 my $heap = $_[HEAP];
367 38 DEBUG and warn "$$: $alias child Flush";
368 38 ▶ $client_flushed and $client_flushed->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$client_flushed and &$client_flushed(@_) 30 8 38
369 38 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 12 26 38
370 12 DEBUG and warn "$$: $alias child Flush, callback";
371 12 ▶ $client_disconnected and $client_disconnected->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$client_disconnected and &$client_disconnected(@_) 12 0 12
372 12 delete $heap->{client};
373 }
374 },
375 52 ▶ shutdown => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
376 52 DEBUG and warn "$$: $alias child Shutdown";
377 52 my $heap = $_[HEAP];
378 52 $heap->{shutdown} = 1;
379 52 ▶ if (defined $heap->{client}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"client"}) 34 18 52
380 34 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"got_an_error"} or not $heap->{"client"}->get_driver_out_octets) 24 10 34
Condition100%3/3
$heap->{"got_an_error"} or not $heap->{"client"}->get_driver_out_octets
l!l&&r!l&&!r
22210
MC/DC100%2/2
$heap->{"got_an_error"} or not $heap->{"client"}->get_driver_out_octets
$heap->{"got_an_error"} not $heap->{"client"}->get_driver_out_octets
Truth table100%3/3
A or not(B)
A= $heap->{"got_an_error"}B= not $heap->{"client"}->get_driver_out_octets
ABresult
1X1
011
000
381 $heap->{got_an_error} or
382 not $heap->{client}->get_driver_out_octets()
383 ) {
384 24 DEBUG and warn "$$: $alias child Shutdown, callback";
385 24 ▶ $client_disconnected and $client_disconnected->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$client_disconnected and &$client_disconnected(@_) 22 2 24
386 24 delete $heap->{client};
387 }
388 }
389 },
390 38 ▶ _stop => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
391 ## concurrency on close
392 38 DEBUG and warn(
393 "$$: $alias _stop accept_session = $accept_session_id"
394 );
395 38 ▶ if( defined $accept_session_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $accept_session_id) { } 38 0 38
396 38 $_[KERNEL]->call( $accept_session_id, 'disconnected' );
397 }
398 else {
399 # This means that the Server::TCP was shutdown before
400 # this connection closed. So it doesn't really matter that
401 # we can't decrement the connection counter.
402 0 DEBUG and warn(
403 "$$: $_[HEAP]->{alias} Disconnected from a connection ",
404 "without POE::Component::Server::TCP parent"
405 );
406 }
407 38 return;
408 },
409  
410 # User supplied states.
411 38 %$inline_states
412 },
413  
414 # More user supplied states.
415 package_states => $package_states,
416 object_states => $object_states,
417  
418 # XXX - If you change the number of args here, also change
419 # the splice elsewhere.
420 args => [ $socket, $client_args ],
421 );
422 6 };
423 }
424 };
425  
426 # Complain about strange things we're given.
427 14 foreach (sort keys %param) {
428 0 carp "$mi doesn't recognize \"$_\" as a parameter";
429 }
430  
431 ## verify concurrency on accept
432 14 my $orig_accept_callback = $accept_callback;
433 62 ▶ $accept_callback = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
434 62 $_[HEAP]->{connections}++;
435 62 DEBUG and warn(
436 "$$: $_[HEAP]->{alias} Connection opened ",
437 "($_[HEAP]->{connections} open)"
438 );
439 62 ▶ if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"concurrency"} != -1 and $_[3]{"listener"}) 36 26 62
Condition66%2/3
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
!ll&&!rl&&r
26036
MC/DC50%1/2
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
$_[3]{"concurrency"} != -1 $_[3]{"listener"}
Truth table66%2/3
A and B
A= $_[3]{"concurrency"} != -1B= $_[3]{"listener"}
ABresult
0X0
100
111
440 36 ▶ if( $_[HEAP]->{connections} >= $_[HEAP]->{concurrency} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"connections"} >= $_[3]{"concurrency"}) 24 12 36
441 24 DEBUG and warn(
442 "$$: $_[HEAP]->{alias} Concurrent connection limit reached, ",
443 "pausing accept"
444 );
445 $_[HEAP]->{listener}->pause_accept()
446 24 }
447 }
448 62 $orig_accept_callback->(@_);
449 14 };
450  
451 # Create the session, at long last.
452 # This is done inline so that closures can customize it.
453 # We save the accept session's ID to avoid self reference.
454  
455 $accept_session_id = $session_type->create(
456 @$session_params,
457 inline_states => {
458 21 ▶ _start => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
459 21 ▶ if (defined $alias) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias) 19 2 21
460 19 $_[HEAP]->{alias} = $alias;
461 19 $_[KERNEL]->alias_set( $alias );
462 }
463  
464 21 $_[HEAP]->{concurrency} = $concurrency;
465 21 $_[HEAP]->{connections} = 0;
466  
467 21 ▶ $_[HEAP]->{listener} = POE::Wheel::SocketFactory->new(
Branch: 1 branch
BranchTrueFalseTotal
$domain == 1 || $domain == 1 ? : 1 20 21
Condition66%2/3
$domain == 1 or $domain == 1
l!l&&r!l&&!r
1020
MC/DC50%1/2
$domain == 1 or $domain == 1
$domain == 1 $domain == 1
Truth table66%2/3
A or B
A= $domain == 1B= $domain == 1
ABresult
1X1
011
000
468 ( ($domain == AF_UNIX or $domain == PF_UNIX)
469 ? ()
470 : ( BindPort => $port )
471 ),
472 BindAddress => $address,
473 SocketDomain => $domain,
474 Reuse => 'yes',
475 SuccessEvent => 'tcp_server_got_connection',
476 FailureEvent => 'tcp_server_got_error',
477 );
478 21 ▶ $server_started and $server_started->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$server_started and &$server_started(@_) 20 1 21
479 },
480 # Catch an error.
481 tcp_server_got_error => $error_callback,
482  
483 # We accepted a connection. Do something with it.
484 tcp_server_got_connection => $accept_callback,
485  
486 # concurrency on close.
487 56 ▶ disconnected => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
488 56 $_[HEAP]->{connections}--;
489 56 DEBUG and warn(
490 "$$: $_[HEAP]->{alias} Connection closed ",
491 "($_[HEAP]->{connections} open)"
492 );
493 56 ▶ if ($_[HEAP]->{connections} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"connections"} < 0) 0 56 56
494 0 warn(
495 "Excessive 'disconnected' event ",
496 "from $_[CALLER_FILE] at line $_[CALLER_LINE]\n"
497 );
498 0 $_[HEAP]->{connections} = 0;
499 }
500 56 ▶ if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"concurrency"} != -1 and $_[3]{"listener"}) 32 24 56
Condition100%3/3
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
!ll&&!rl&&r
20432
MC/DC100%2/2
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
$_[3]{"concurrency"} != -1 $_[3]{"listener"}
Truth table100%3/3
A and B
A= $_[3]{"concurrency"} != -1B= $_[3]{"listener"}
ABresult
0X0
100
111
501 32 ▶ if( $_[HEAP]->{connections} == ($_[HEAP]->{concurrency}-1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"connections"} == $_[3]{"concurrency"} - 1) 22 10 32
502 22 DEBUG and warn(
503 "$$: $_[HEAP]->{alias} Concurrent connection limit ",
504 "reestablished, resuming accept"
505 );
506 22 $_[HEAP]->{listener}->resume_accept();
507 }
508 }
509 },
510  
511 0 ▶ set_concurrency => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
512 0 $_[HEAP]->{concurrency} = $_[ARG0];
513 0 DEBUG and warn(
514 "$$: $_[HEAP]->{alias} Concurrent connection ",
515 "limit = $_[HEAP]->{concurrency}"
516 );
517 0 ▶ if( $_[HEAP]->{concurrency} != -1 and $_[HEAP]->{listener} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"concurrency"} != -1 and $_[3]{"listener"}) 0 0 0
Condition0%0/3
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[3]{"concurrency"} != -1 and $_[3]{"listener"}
$_[3]{"concurrency"} != -1 $_[3]{"listener"}
Truth table0%0/3
A and B
A= $_[3]{"concurrency"} != -1B= $_[3]{"listener"}
ABresult
0X0
100
111
518 0 ▶ if( $_[HEAP]->{connections} >= $_[HEAP]->{concurrency} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'connections'} >= $_[3]{'concurrency'}) { } 0 0 0
519 0 DEBUG and warn(
520 "$$: $_[HEAP]->{alias} Concurrent connection limit ",
521 "reached, pausing accept"
522 );
523 $_[HEAP]->{listener}->pause_accept()
524 0 }
525 else {
526 0 DEBUG and warn(
527 "$$: $_[HEAP]->{alias} Concurrent connection limit ",
528 "reestablished, resuming accept"
529 );
530 0 $_[HEAP]->{listener}->resume_accept();
531 }
532 }
533 },
534  
535 # Shut down.
536 15 ▶ shutdown => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
537 15 delete $_[HEAP]->{listener};
538 $_[KERNEL]->alias_remove( $_[HEAP]->{alias} )
539 15 ▶ if defined $_[HEAP]->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[3]{"alias"} 15 0 15
540 },
541  
542 # Dummy states to prevent warnings.
543 21 ▶ _stop => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
544 21 DEBUG and warn "$$: $_[HEAP]->{alias} _stop";
545 21 ▶ $server_stopped and $server_stopped->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$server_stopped and &$server_stopped(@_) 1 20 21
546 21 undef($accept_session_id);
547 21 return 0;
548 },
549 132 ▶ _child => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
550 },
551  
552 14 args => $listener_args,
553 )->ID;
554  
555 # Return the session ID.
556 14 return $accept_session_id;
557 }
558  
559 38 ▶sub _get_filters {
Subroutine _get_filters: called CC=4 SCAR=15.2
560 38 my ($client_filter, $client_infilter, $client_outfilter) = @_;
561 38 ▶ if (defined $client_infilter or defined $client_outfilter) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $client_infilter or defined $client_outfilter) { } 0 38 38
elsif (defined $client_filter) { } 6 32 38
Condition33%1/3
defined $client_infilter or defined $client_outfilter
l!l&&r!l&&!r
0038
MC/DC0%0/2
defined $client_infilter or defined $client_outfilter
defined $client_infilter defined $client_outfilter
Truth table33%1/3
A or B
A= defined $client_infilterB= defined $client_outfilter
ABresult
1X1
011
000
562 return (
563 0 "InputFilter" => _load_filter($client_infilter),
564 "OutputFilter" => _load_filter($client_outfilter)
565 );
566 }
567 elsif (defined $client_filter) {
568 6 return ( "Filter" => _load_filter($client_filter) );
569 }
570 else {
571 32 return ( Filter => POE::Filter::Line->new(), );
572 }
573  
574 }
575  
576 # Get something: either arrayref, ref, or string
577 # Return filter
578 6 ▶sub _load_filter {
Subroutine _load_filter: called CC=5 SCAR=17.6
579 6 my $filter = shift;
580 6 ▶ if (ref ($filter) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $filter eq 'ARRAY') { } 2 4 6
elsif (ref $filter) { } 0 4 4
581 2 my @args = @$filter;
582 2 $filter = shift @args;
583 2 ▶ if ( _test_filter($filter) ){
Branch: 1 branch
BranchTrueFalseTotal
if (_test_filter($filter)) { } 2 0 2
584 2 return $filter->new(@args);
585 } else {
586 0 return POE::Filter::Line->new(@args);
587 }
588 }
589 elsif (ref $filter) {
590 0 return $filter->clone();
591 }
592 else {
593 4 ▶ if ( _test_filter($filter) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_test_filter($filter)) { } 4 0 4
594 4 return $filter->new();
595 } else {
596 0 return POE::Filter::Line->new();
597 }
598 }
599 }
600  
601 # Test if a Filter can be loaded, return success or failure
602 6 ▶sub _test_filter {
Subroutine _test_filter: called CC=3 SCAR=12.3
603 6 my $filter = shift;
604 6 my $eval = eval {
605 6 (my $mod = $filter) =~ s!::!/!g;
606 6 require "$mod.pm";
607 6 1;
608 };
609 6 ▶ if (!$eval and $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $eval and $@) 0 6 6
Condition33%1/3
not $eval and $@
!ll&&!rl&&r
600
MC/DC0%0/2
not $eval and $@
not $eval $@
Truth table33%1/3
not(A) and B
A= not $evalB= $@
ABresult
0X0
100
111
610 0 carp(
611 "Failed to load [$filter]\n" .
612 "Reason $@\nUsing default POE::Filter::Line "
613 );
614 0 return 0;
615 }
616 6 return 1;
617 }
618  
619 # The default server error handler logs to STDERR and shuts down the
620 # server.
621  
622 0 ▶sub _default_server_error {
Subroutine _default_server_error: not called CC=1 SCAR=6.9
623 0 warn("$$: ".
624 'Server ', $_[SESSION]->ID,
625 " got $_[ARG0] error $_[ARG1] ($_[ARG2])\n"
626 );
627 0 delete $_[HEAP]->{listener};
628 }
629  
630 # The default client error handler logs to STDERR
631  
632 4 ▶sub _default_client_error {
Subroutine _default_client_error: called CC=5 SCAR=24.5
633 4 my ($syscall, $errno, $error) = @_[ARG0..ARG2];
634 4 ▶ unless ($syscall eq "read" and ($errno == 0 or $errno == ECONNRESET)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syscall eq "read" and $errno == 0 || $errno == 104) 0 4 4
Condition33%2/6
$syscall eq "read" and $errno == 0 || $errno == 104
!ll&&!rl&&r
004
$errno == 0 or $errno == 104
l!l&&r!l&&!r
400
MC/DC0%0/3
$syscall eq "read" and $errno == 0 || $errno == 104
$syscall eq "read" $errno == 0 $errno == 104
Truth table25%1/4
A and B or C
A= $syscall eq "read"B= $errno == 0C= $errno == 104
ABCresult
0XX0
1000
11X1
1011
635 0 ▶ $error = "(no error)" unless $errno;
Branch: 1 branch
BranchTrueFalseTotal
unless $errno 0 0 0
636 0 warn("$$: ".
637 'Client session ', $_[SESSION]->ID,
638 " got $syscall error $errno ($error)\n"
639 );
640 }
641 }
642  
643 15 1;
644  
645 __END__
« blib/lib/POE/Wheel/FollowTail.pm ↑ Summary blib/lib/POE/Wheel/ReadLine.pm »