blib/lib/POE/Wheel/SocketFactory.pm

filter: Statementstmt 63.7% 264 / 414 Branchbran 46.5% 122 / 262 Conditioncond 30.9% 22 / 71 MC/DCmcdc 7.5% 3 / 40 Subroutinesub 81.2% 26 / 32 Podpod 85.7% 6 / 7 totaltotal 53.6% 443 / 826 CC 156 SCAR 65.2
CC
156
Cov
55%
new
1340.6
__ANON__
30.3
__ANON__
22.5
CRAP 680.2
↑ Summary blib/lib/POE/Wheel/Run.pm »
1 package POE::Wheel::SocketFactory;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 24 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
7  
8 24 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Symbol qw( gensym );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Errno qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 EWOULDBLOCK EADDRNOTAVAIL EINPROGRESS EADDRINUSE ECONNABORTED
14 ESPIPE
15 24 );
16  
17 24 ▶use Socket qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 AF_INET SOCK_STREAM SOL_SOCKET AF_UNIX PF_UNIX
19 PF_INET SOCK_DGRAM SO_ERROR unpack_sockaddr_in
20 unpack_sockaddr_un PF_UNSPEC SO_REUSEADDR INADDR_ANY
21 pack_sockaddr_in pack_sockaddr_un inet_aton SOMAXCONN
22 24 );
23  
24 24 ▶use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 24 ▶use FileHandle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 24 ▶use POE qw( Wheel );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 24 push @ISA, qw(POE::Wheel);
28  
29 0 ▶sub CRIMSON_SCOPE_HACK ($) { 0 }
Subroutine CRIMSON_SCOPE_HACK: not called CC=1 SCAR=6.9
Pod undocumented
30 sub DEBUG () { 0 }
31  
32 sub MY_SOCKET_HANDLE () { 0 }
33 sub MY_UNIQUE_ID () { 1 }
34 sub MY_EVENT_SUCCESS () { 2 }
35 sub MY_EVENT_FAILURE () { 3 }
36 sub MY_SOCKET_DOMAIN () { 4 }
37 sub MY_STATE_ACCEPT () { 5 }
38 sub MY_STATE_CONNECT () { 6 }
39 sub MY_MINE_SUCCESS () { 7 }
40 sub MY_MINE_FAILURE () { 8 }
41 sub MY_SOCKET_PROTOCOL () { 9 }
42 sub MY_SOCKET_TYPE () { 10 }
43 sub MY_STATE_ERROR () { 11 }
44 sub MY_SOCKET_SELECTED () { 12 }
45  
46 # Fletch has subclassed SSLSocketFactory from SocketFactory.
47 # He's added new members after MY_SOCKET_SELECTED. Be sure, if you
48 # extend this, to extend add stuff BEFORE MY_SOCKET_SELECTED or let
49 # Fletch know you've broken his module.
50  
51 # If IPv6 support can't be loaded, then provide dummies so the code at
52 # least compiles. Suggested in rt.cpan.org 27250.
53 24 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=25.0
54  
55 24 eval { Socket->import( qw(getaddrinfo unpack_sockaddr_in6) ) };
56 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
57 0 ▶ *getaddrinfo = sub { Carp::confess("Unable to use IPv6: Socket doesn't provide getaddrinfo()") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 0 ▶ *unpack_sockaddr_in6 = sub { Carp::confess("Unable to use IPv6: Socket doesn't provide unpack_sockaddr_in6()") };
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 }
60  
61 # Socket6 provides AF_INET6 and PF_INET6 where earlier Perls' Socket don't.
62 24 eval { Socket->import( qw(AF_INET6 PF_INET6) ) };
63 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
64 0 eval { require Socket6; Socket6->import( qw(AF_INET6 PF_INET6) ) };
65 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
66 0 ▶ *AF_INET6 = sub { -1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ *PF_INET6 = sub { -1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 }
69 }
70  
71 24 eval { Socket->import( 'IPPROTO_TCP' ) };
72 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
73 0 *IPPROTO_TCP = (getprotobyname 'tcp')[2];
74 }
75  
76 24 eval { Socket->import( 'IPPROTO_UDP' ) };
77 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
78 0 *IPPROTO_UDP = (getprotobyname 'udp')[2];
79 }
80 }
81  
82 # Common protocols to help support systems that don't have
83 # getprotobyname().
84 24 my %proto_by_name = (
85 tcp => IPPROTO_TCP,
86 udp => IPPROTO_UDP,
87 );
88  
89 24 my %proto_by_number = reverse %proto_by_name;
90  
91 #------------------------------------------------------------------------------
92 # These tables customize the socketfactory. Many protocols share the
93 # same operations, it seems, and this is a way to add new ones with a
94 # minimum of additional code.
95  
96 sub DOM_UNIX () { 'unix' } # UNIX domain socket
97 sub DOM_INET () { 'inet' } # INET domain socket
98 sub DOM_INET6 () { 'inet6' } # INET v6 domain socket
99  
100 # AF_XYZ and PF_XYZ may be different.
101 24 my %map_family_to_domain = (
102 AF_UNIX, DOM_UNIX, PF_UNIX, DOM_UNIX,
103 AF_INET, DOM_INET, PF_INET, DOM_INET,
104 AF_INET6, DOM_INET6,
105 PF_INET6, DOM_INET6,
106 );
107  
108 sub SVROP_LISTENS () { 'listens' } # connect/listen sockets
109 sub SVROP_NOTHING () { 'nothing' } # connectionless sockets
110  
111 # Map family/protocol pairs to connection or connectionless
112 # operations.
113 24 my %supported_protocol = (
114 DOM_UNIX, {
115 none => SVROP_LISTENS
116 },
117 DOM_INET, {
118 tcp => SVROP_LISTENS,
119 udp => SVROP_NOTHING,
120 },
121 DOM_INET6, {
122 tcp => SVROP_LISTENS,
123 udp => SVROP_NOTHING,
124 },
125 );
126  
127 # Sane default socket types for each supported protocol. TODO Maybe
128 # this structure can be combined with %supported_protocol?
129 24 my %default_socket_type = (
130 DOM_UNIX, {
131 none => SOCK_STREAM
132 },
133 DOM_INET, {
134 tcp => SOCK_STREAM,
135 udp => SOCK_DGRAM,
136 },
137 DOM_INET6, {
138 tcp => SOCK_STREAM,
139 udp => SOCK_DGRAM,
140 },
141 );
142  
143 #------------------------------------------------------------------------------
144 # Perform system-dependent translations on Unix addresses, if
145 # necessary.
146  
147 5 ▶sub _condition_unix_address {
Subroutine _condition_unix_address: called CC=3 SCAR=16.7
148 5 my ($address) = @_;
149  
150 # OS/2 would like sockets to use backwhacks, and please place them
151 # in the virtual \socket\ directory. Thank you.
152 5 ▶ if ($^O eq 'os2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "os2") 0 5 5
153 0 $address =~ tr[\\][/];
154 0 ▶ if ($address !~ m{^/socket/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($address =~ m[^/socket/]) 0 0 0
155 0 $address =~ s{^/?}{/socket/};
156 }
157 0 $address =~ tr[/][\\];
158 }
159  
160 5 $address;
161 }
162  
163 #------------------------------------------------------------------------------
164 # Define the select handler that will accept connections.
165  
166 23 ▶sub _define_accept_state {
Subroutine _define_accept_state: called CC=2 SCAR=8.2
167 23 my $self = shift;
168  
169 # We do these stupid closure tricks to avoid putting $self in it
170 # directly. If you include $self in one of the state() closures,
171 # the component will fail to shut down properly: there will be a
172 # circular definition in the closure holding $self alive.
173  
174 23 my $domain = $map_family_to_domain{ $self->[MY_SOCKET_DOMAIN] };
175 23 ▶ $domain = '(undef)' unless defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 23 23
176 23 my $event_success = \$self->[MY_EVENT_SUCCESS];
177 23 my $event_failure = \$self->[MY_EVENT_FAILURE];
178 23 my $unique_id = $self->[MY_UNIQUE_ID];
179  
180 $poe_kernel->state(
181 $self->[MY_STATE_ACCEPT] = ref($self) . "($unique_id) -> select accept",
182 64 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=31.1
183 # prevents SEGV
184 64 0 && CRIMSON_SCOPE_HACK('<');
185  
186 # subroutine starts here
187 64 my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
188  
189 64 my $new_socket = gensym;
190 64 my $peer = accept($new_socket, $handle);
191  
192 64 ▶ if ($peer) {
Branch: 2 branches
BranchTrueFalseTotal
if ($peer) { } 64 0 64
elsif ($! != 11 and $! != 103 and $! != 29) { } 0 0 0
193 64 my ($peer_addr, $peer_port);
194 64 ▶ if ( $domain eq DOM_UNIX ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($domain eq 'unix') { } 2 62 64
elsif ($domain eq 'inet') { } 62 0 62
elsif ($domain eq 'inet6') { } 0 0 0
195 2 $peer_port = undef;
196 2 eval { $peer_addr = unpack_sockaddr_un($peer) };
197 2 ▶ $peer_addr = undef if length $@;
Branch: 1 branch
BranchTrueFalseTotal
if length $@ 0 2 2
198 }
199 elsif ( $domain eq DOM_INET ) {
200 62 ($peer_port, $peer_addr) = unpack_sockaddr_in($peer);
201 }
202 elsif ( $domain eq DOM_INET6 ) {
203 0 ($peer_port, $peer_addr) = unpack_sockaddr_in6($peer);
204 }
205 else {
206 0 die "sanity failure: socket domain == $domain";
207 }
208 64 $k->call(
209 $me, $$event_success,
210 $new_socket, $peer_addr, $peer_port,
211 $unique_id
212 );
213 }
214 ▶ elsif ($! != EWOULDBLOCK and $! != ECONNABORTED and $! != ESPIPE) {
Condition0%0/6
$! != 11 and $! != 103 and $! != 29
!ll&&!rl&&r
000
$! != 11 and $! != 103
!ll&&!rl&&r
000
MC/DC0%0/3
$! != 11 and $! != 103 and $! != 29
$! != 11 $! != 103 $! != 29
Truth table0%0/4
A and B and C
A= $! != 11B= $! != 103C= $! != 29
ABCresult
0XX0
10X0
1100
1111
215 # OSX reports ESPIPE, which isn't documented anywhere.
216 0 ▶ $$event_failure && $k->call(
Branch: 1 branch
BranchTrueFalseTotal
$$event_failure and $k->call($me, $$event_failure, "accept", $! + 0, $!, $unique_id) 0 0 0
217 $me, $$event_failure,
218 'accept', ($!+0), $!, $unique_id
219 );
220 }
221 }
222 23 );
223  
224 23 $self->[MY_SOCKET_SELECTED] = 'yes';
225 23 $poe_kernel->select_read(
226 $self->[MY_SOCKET_HANDLE],
227 $self->[MY_STATE_ACCEPT]
228 );
229 }
230  
231 #------------------------------------------------------------------------------
232 # Define the select handler that will finalize an established
233 # connection.
234  
235 77 ▶sub _define_connect_state {
Subroutine _define_connect_state: called CC=4 SCAR=17.0
236 77 my $self = shift;
237  
238 # We do these stupid closure tricks to avoid putting $self in it
239 # directly. If you include $self in one of the state() closures,
240 # the component will fail to shut down properly: there will be a
241 # circular definition in the closure holding $self alive.
242  
243 77 my $domain = $map_family_to_domain{ $self->[MY_SOCKET_DOMAIN] };
244 77 ▶ $domain = '(undef)' unless defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 77 77
245 77 my $event_success = \$self->[MY_EVENT_SUCCESS];
246 77 my $event_failure = \$self->[MY_EVENT_FAILURE];
247 77 my $unique_id = $self->[MY_UNIQUE_ID];
248 77 my $socket_selected = \$self->[MY_SOCKET_SELECTED];
249  
250 77 my $socket_handle = \$self->[MY_SOCKET_HANDLE];
251 77 my $state_accept = \$self->[MY_STATE_ACCEPT];
252 77 my $state_connect = \$self->[MY_STATE_CONNECT];
253 77 my $mine_success = \$self->[MY_MINE_SUCCESS];
254 77 my $mine_failure = \$self->[MY_MINE_FAILURE];
255  
256 $poe_kernel->state(
257 $self->[MY_STATE_CONNECT] = (
258 ref($self) . "($unique_id) -> select connect"
259 ),
260 77 ▶ sub {
Subroutine __ANON__: called CC=14 SCAR=34.1
261 # This prevents SEGV in older versions of Perl.
262 77 0 && CRIMSON_SCOPE_HACK('<');
263  
264 # Grab some values and stop watching the socket.
265 77 my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
266  
267 77 _shutdown(
268 $socket_selected, $socket_handle,
269 $state_accept, $state_connect,
270 $mine_success, $event_success,
271 $mine_failure, $event_failure,
272 );
273  
274 # Throw a failure if the connection failed.
275 77 $! = unpack('i', getsockopt($handle, SOL_SOCKET, SO_ERROR));
276 77 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 3 74 77
277 3 ▶ (defined $$event_failure) and $k->call(
Branch: 1 branch
BranchTrueFalseTotal
defined $$event_failure and $k->call($me, $$event_failure, "connect", $! + 0, $!, $unique_id) 3 0 3
278 $me, $$event_failure,
279 'connect', ($!+0), $!, $unique_id
280 );
281 3 return;
282 }
283  
284 # Get the remote address, or throw an error if that fails.
285 74 my $peer = getpeername($handle);
286 74 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 74 74
287 0 ▶ (defined $$event_failure) and $k->call(
Branch: 1 branch
BranchTrueFalseTotal
defined $$event_failure and $k->call($me, $$event_failure, "getpeername", $! + 0, $!, $unique_id) 0 0 0
288 $me, $$event_failure,
289 'getpeername', ($!+0), $!, $unique_id
290 );
291 0 return;
292 }
293  
294 # Parse the remote address according to the socket's domain.
295 74 my ($peer_addr, $peer_port);
296  
297 # UNIX sockets have some trouble with peer addresses.
298 74 ▶ if ($domain eq DOM_UNIX) {
Branch: 3 branches
BranchTrueFalseTotal
if ($domain eq 'unix') { } 2 72 74
elsif ($domain eq 'inet') { } 72 0 72
elsif ($domain eq 'inet6') { } 0 0 0
299 2 ▶ if (defined $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) 2 0 2
300 2 eval { $peer_addr = unpack_sockaddr_un($peer) };
301 2 ▶ $peer_addr = undef if length $@;
Branch: 1 branch
BranchTrueFalseTotal
if length $@ 0 2 2
302 }
303 }
304  
305 # INET socket stacks tend not to.
306 elsif ($domain eq DOM_INET) {
307 72 ▶ if (defined $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) 72 0 72
308 72 eval {
309 72 ($peer_port, $peer_addr) = unpack_sockaddr_in($peer);
310 };
311 72 ▶ if (length $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $@) 0 72 72
312 0 $peer_port = $peer_addr = undef;
313 }
314 }
315 }
316  
317 # INET6 socket stacks tend not to.
318 elsif ($domain eq DOM_INET6) {
319 0 ▶ if (defined $peer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $peer) 0 0 0
320 0 eval {
321 0 ($peer_port, $peer_addr) = unpack_sockaddr_in6($peer);
322 };
323 0 ▶ if (length $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $@) 0 0 0
324 0 $peer_port = $peer_addr = undef;
325 }
326 }
327 }
328  
329 # What are we doing here?
330 else {
331 0 die "sanity failure: socket domain == $domain";
332 }
333  
334 # Tell the session it went okay. Also let go of the socket.
335 74 $k->call(
336 $me, $$event_success,
337 $handle, $peer_addr, $peer_port, $unique_id
338 );
339 }
340 77 );
341  
342 # Cygwin and Windows expect an error state registered to expedite.
343 # This code is nearly identical the stuff above.
344 77 ▶ if ($^O eq "cygwin" or $^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "cygwin" or $^O eq "MSWin32") 0 77 77
Condition33%1/3
$^O eq "cygwin" or $^O eq "MSWin32"
l!l&&r!l&&!r
0077
MC/DC0%0/2
$^O eq "cygwin" or $^O eq "MSWin32"
$^O eq "cygwin" $^O eq "MSWin32"
Truth table33%1/3
A or B
A= $^O eq "cygwin"B= $^O eq "MSWin32"
ABresult
1X1
011
000
345 $poe_kernel->state(
346 $self->[MY_STATE_ERROR] = (
347 ref($self) . "($unique_id) -> connect error"
348 ),
349 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
350 # This prevents SEGV in older versions of Perl.
351 0 0 && CRIMSON_SCOPE_HACK('<');
352  
353 # Grab some values and stop watching the socket.
354 0 my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0];
355  
356 0 _shutdown(
357 $socket_selected, $socket_handle,
358 $state_accept, $state_connect,
359 $mine_success, $event_success,
360 $mine_failure, $event_failure,
361 );
362  
363 # Throw a failure if the connection failed.
364 0 $! = unpack('i', getsockopt($handle, SOL_SOCKET, SO_ERROR));
365 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
366 0 ▶ (defined $$event_failure) and $k->call(
Branch: 1 branch
BranchTrueFalseTotal
defined $$event_failure and $k->call($me, $$event_failure, "connect", $! + 0, $!, $unique_id) 0 0 0
367 $me, $$event_failure, 'connect', ($!+0), $!, $unique_id
368 );
369 0 return;
370 }
371 }
372 0 );
373 0 $poe_kernel->select_expedite(
374 $self->[MY_SOCKET_HANDLE],
375 $self->[MY_STATE_ERROR]
376 );
377 }
378  
379 77 $self->[MY_SOCKET_SELECTED] = 'yes';
380 77 $poe_kernel->select_write(
381 $self->[MY_SOCKET_HANDLE],
382 $self->[MY_STATE_CONNECT]
383 );
384 }
385  
386 #------------------------------------------------------------------------------
387  
388 102 ▶sub event {
Subroutine event: called CC=13 SCAR=29.8
Pod documented
389 102 my $self = shift;
390 102 ▶ push(@_, undef) if (scalar(@_) & 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ & 1 0 102 102
391  
392 102 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 204 102 306
393 204 my ($name, $event) = splice(@_, 0, 2);
394  
395 204 ▶ if ($name eq 'SuccessEvent') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'SuccessEvent') { } 102 102 204
elsif ($name eq 'FailureEvent') { } 102 0 102
396 102 ▶ if (defined $event) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event) { } 102 0 102
397 102 ▶ if (ref($event)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event) 0 102 102
398 0 carp "reference for SuccessEvent will be treated as an event name"
399 }
400 102 $self->[MY_EVENT_SUCCESS] = $event;
401 102 undef $self->[MY_MINE_SUCCESS];
402 }
403 else {
404 0 carp "SuccessEvent requires an event name. ignoring undef";
405 }
406 }
407 elsif ($name eq 'FailureEvent') {
408 102 ▶ if (defined $event) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $event) { } 102 0 102
409 102 ▶ if (ref($event)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event) 0 102 102
410 0 carp "reference for FailureEvent will be treated as an event name";
411 }
412 102 $self->[MY_EVENT_FAILURE] = $event;
413 102 undef $self->[MY_MINE_FAILURE];
414 }
415 else {
416 0 carp "FailureEvent requires an event name. ignoring undef";
417 }
418 }
419 else {
420 0 carp "ignoring unknown SocketFactory parameter '$name'";
421 }
422 }
423  
424 102 $self->[MY_SOCKET_SELECTED] = 'yes';
425 102 ▶ if (defined $self->[MY_STATE_ACCEPT]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->[5]) { } 23 79 102
elsif (defined $self->[6]) { } 79 0 79
426 23 $poe_kernel->select_read(
427 $self->[MY_SOCKET_HANDLE],
428 $self->[MY_STATE_ACCEPT]
429 );
430 }
431 elsif (defined $self->[MY_STATE_CONNECT]) {
432 79 $poe_kernel->select_write(
433 $self->[MY_SOCKET_HANDLE],
434 $self->[MY_STATE_CONNECT]
435 );
436 79 ▶ if ($^O eq "cygwin" or $^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "cygwin" or $^O eq "MSWin32") 0 79 79
Condition33%1/3
$^O eq "cygwin" or $^O eq "MSWin32"
l!l&&r!l&&!r
0079
MC/DC0%0/2
$^O eq "cygwin" or $^O eq "MSWin32"
$^O eq "cygwin" $^O eq "MSWin32"
Truth table33%1/3
A or B
A= $^O eq "cygwin"B= $^O eq "MSWin32"
ABresult
1X1
011
000
437 0 $poe_kernel->select_expedite(
438 $self->[MY_SOCKET_HANDLE],
439 $self->[MY_STATE_ERROR]
440 );
441 }
442 }
443 else {
444 0 die "POE developer error - no state defined";
445 }
446 }
447  
448 #------------------------------------------------------------------------------
449  
450 24 ▶sub getsockname {
Subroutine getsockname: called CC=3 SCAR=12.5
Pod documented
451 24 my $self = shift;
452 return undef unless (
453 24 ▶ defined $self->[MY_SOCKET_HANDLE] and
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->[0] and fileno $self->[0] 0 24 24
Condition33%1/3
defined $self->[0] and fileno $self->[0]
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $self->[0] and fileno $self->[0]
defined $self->[0] fileno $self->[0]
Truth table33%1/3
A and B
A= defined $self->[0]B= fileno $self->[0]
ABresult
0X0
100
111
454 fileno($self->[MY_SOCKET_HANDLE])
455 );
456 24 return getsockname($self->[MY_SOCKET_HANDLE]);
457 }
458  
459 10 ▶sub ID {
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
460 10 return $_[0]->[MY_UNIQUE_ID];
461 }
462  
463 #------------------------------------------------------------------------------
464  
465 104 ▶sub new {
Subroutine new: called CC=94 SCAR=72.0
Pod documented
466 104 my $type = shift;
467  
468 # Don't take responsibility for a bad parameter count.
469 104 ▶ croak "$type requires an even number of parameters" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 104 104
470  
471 104 my %params = @_;
472  
473 # The calling convention experienced a hard deprecation.
474 104 ▶ croak "wheels no longer require a kernel reference as their first parameter"
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref $_[0] eq "POE::Kernel" 0 104 104
Condition33%1/3
@_ and ref $_[0] eq "POE::Kernel"
!ll&&!rl&&r
01040
MC/DC0%0/2
@_ and ref $_[0] eq "POE::Kernel"
@_ ref $_[0] eq "POE::Kernel"
Truth table33%1/3
A and B
A= @_B= ref $_[0] eq "POE::Kernel"
ABresult
0X0
100
111
475 if (@_ && (ref($_[0]) eq 'POE::Kernel'));
476  
477 # Ensure some of the basic things are present.
478 104 ▶ croak "$type requires a working Kernel" unless (defined $poe_kernel);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $POE::Wheel::SocketFactory::poe_kernel 0 104 104
479 104 ▶ croak 'SuccessEvent required' unless (defined $params{SuccessEvent});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"SuccessEvent"} 0 104 104
480 104 ▶ croak 'FailureEvent required' unless (defined $params{FailureEvent});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"FailureEvent"} 0 104 104
481 104 my $event_success = $params{SuccessEvent};
482 104 my $event_failure = $params{FailureEvent};
483  
484 # Create the SocketServer. Cache a copy of the socket handle.
485 104 my $socket_handle = gensym();
486 104 my $self = bless(
487 [
488 $socket_handle, # MY_SOCKET_HANDLE
489 &POE::Wheel::allocate_wheel_id(), # MY_UNIQUE_ID
490 $event_success, # MY_EVENT_SUCCESS
491 $event_failure, # MY_EVENT_FAILURE
492 undef, # MY_SOCKET_DOMAIN
493 undef, # MY_STATE_ACCEPT
494 undef, # MY_STATE_CONNECT
495 undef, # MY_MINE_SUCCESS
496 undef, # MY_MINE_FAILURE
497 undef, # MY_SOCKET_PROTOCOL
498 undef, # MY_SOCKET_TYPE
499 undef, # MY_STATE_ERROR
500 undef, # MY_SOCKET_SELECTED
501 ],
502 $type
503 );
504  
505 # Default to Internet sockets.
506 104 my $domain = delete $params{SocketDomain};
507 104 ▶ if (defined $domain) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $domain) { } 25 79 104
508 # [rt.cpan.org 76314] Untaint the domain.
509 25 ($domain) = ($domain =~ /\A(.*)\z/s);
510 }
511 else {
512 79 $domain = AF_INET;
513 }
514 104 $self->[MY_SOCKET_DOMAIN] = $domain;
515  
516 # Abstract the socket domain into something we don't have to keep
517 # testing duplicates of.
518 104 my $abstract_domain = $map_family_to_domain{$self->[MY_SOCKET_DOMAIN]};
519 104 ▶ unless (defined $abstract_domain) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $abstract_domain) 0 104 104
520 0 $poe_kernel->yield(
521 $event_failure,
522 'domain',
523 0,
524 "SocketDomain $domain is currently unsupported on this platform",
525 $self->[MY_UNIQUE_ID]
526 );
527 0 return $self;
528 }
529  
530 #---------------#
531 # Create Socket #
532 #---------------#
533  
534 # Declare the protocol name out here; it'll be needed by
535 # getservbyname later.
536 104 my $protocol_name;
537  
538 # Unix sockets don't use protocols; warn the programmer, and force
539 # PF_UNSPEC.
540 104 ▶ if ($abstract_domain eq DOM_UNIX) {
Branch: 2 branches
BranchTrueFalseTotal
if ($abstract_domain eq 'unix') { } 5 99 104
elsif ($abstract_domain eq 'inet' or $abstract_domain eq 'inet6') { } 99 0 99
541 carp 'SocketProtocol ignored for Unix socket'
542 5 ▶ if defined $params{SocketProtocol};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"SocketProtocol"} 0 5 5
543 5 $self->[MY_SOCKET_PROTOCOL] = PF_UNSPEC;
544 5 $protocol_name = 'none';
545 }
546  
547 # Internet sockets use protocols. Default the INET protocol to tcp,
548 # and try to resolve it.
549 ▶ elsif (
Condition33%1/3
$abstract_domain eq "inet" or $abstract_domain eq "inet6"
l!l&&r!l&&!r
9900
MC/DC0%0/2
$abstract_domain eq "inet" or $abstract_domain eq "inet6"
$abstract_domain eq "inet" $abstract_domain eq "inet6"
Truth table33%1/3
A or B
A= $abstract_domain eq "inet"B= $abstract_domain eq "inet6"
ABresult
1X1
011
000
550 $abstract_domain eq DOM_INET or
551 $abstract_domain eq DOM_INET6
552 ) {
553 my $socket_protocol = (
554 (defined $params{SocketProtocol})
555 ? $params{SocketProtocol}
556 99 ▶ : 'tcp'
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'SocketProtocol'} ? : 5 94 99
557 );
558  
559  
560 99 ▶ if ($socket_protocol !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket_protocol =~ /^\d+$/) 99 0 99
561 0 ▶ unless ($socket_protocol = $proto_by_name{$socket_protocol} || eval { getprotobyname($socket_protocol) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($socket_protocol = $proto_by_name{$socket_protocol} || eval { do { getprotobyname $socket_protocol } }) 0 99 99
Condition33%1/3
$proto_by_name{$socket_protocol} || eval { do { getprotobyname $socket_protocol } }
l!l&&r!l&&!r
9900
MC/DC0%0/2
$proto_by_name{$socket_protocol} || eval { do { getprotobyname $socket_protocol } }
$proto_by_name{$socket_protocol} eval { do { getprotobyname $socket_protocol } }
Truth table33%1/3
A || B
A= $proto_by_name{$socket_protocol}B= eval { do { getprotobyname $socket_protocol } }
ABresult
1X1
011
000
562 0 $poe_kernel->yield(
563 $event_failure, 'getprotobyname', $!+0, $!, $self->[MY_UNIQUE_ID]
564 );
565 0 return $self;
566 }
567 }
568  
569 # Get the protocol's name regardless of what was provided. If the
570 # protocol isn't supported, croak now instead of making the
571 # programmer wonder why things fail later.
572 0 ▶ $protocol_name = $proto_by_number{$socket_protocol} || eval { lc(getprotobynumber($socket_protocol)) };
Condition33%1/3
$proto_by_number{$socket_protocol} || eval { do { lc getprotobynumber($socket_protocol) } }
l!l&&r!l&&!r
9900
MC/DC0%0/2
$proto_by_number{$socket_protocol} || eval { do { lc getprotobynumber($socket_protocol) } }
$proto_by_number{$socket_protocol} eval { do { lc getprotobynumber($socket_protocol) } }
Truth table33%1/3
A || B
A= $proto_by_number{$socket_protocol}B= eval { do { lc getprotobynumber($socket_protocol) } }
ABresult
1X1
011
000
573 99 ▶ unless ($protocol_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($protocol_name) 0 99 99
574 0 $poe_kernel->yield(
575 $event_failure, 'getprotobynumber', $!+0, $!, $self->[MY_UNIQUE_ID]
576 );
577 0 return $self;
578 }
579  
580 99 ▶ unless (defined $supported_protocol{$abstract_domain}->{$protocol_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $supported_protocol{$abstract_domain}{$protocol_name}) 0 99 99
581 0 croak "SocketFactory does not support Internet $protocol_name sockets";
582 }
583  
584 99 $self->[MY_SOCKET_PROTOCOL] = $socket_protocol;
585 }
586 else {
587 0 die "Mail this error to the author of POE: Internal consistency error";
588 }
589  
590 # If no SocketType, default it to something appropriate.
591 104 ▶ if (defined $params{SocketType}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'SocketType'}) { } 0 104 104
592 0 $self->[MY_SOCKET_TYPE] = $params{SocketType};
593 }
594 else {
595 104 ▶ unless (defined $default_socket_type{$abstract_domain}->{$protocol_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $default_socket_type{$abstract_domain}{$protocol_name}) 0 104 104
596 0 croak "SocketFactory does not support $abstract_domain $protocol_name";
597 }
598 $self->[MY_SOCKET_TYPE] =
599 104 $default_socket_type{$abstract_domain}->{$protocol_name};
600 }
601  
602 # o create a dummy socket
603 # o cache the value of SO_OPENTYPE in $win32_socket_opt
604 # o set the overlapped io attribute
605 # o close dummy socket
606 104 my $win32_socket_opt;
607 104 ▶ if ( POE::Kernel::RUNNING_IN_HELL) {
Branch: 1 branch
BranchTrueFalseTotal
if (POE::Kernel::RUNNING_IN_HELL()) 0 104 104
608  
609 # Constants are evaluated first so they exist when the code uses
610 # them.
611 0 eval {
612 0 *SO_OPENTYPE = sub () { 0x7008 };
613 0 *SO_SYNCHRONOUS_ALERT = sub () { 0x10 };
614 0 *SO_SYNCHRONOUS_NONALERT = sub () { 0x20 };
615 };
616 0 ▶ die "Could not install SO constants [$@]" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
617  
618 # Turn on socket overlapped IO attribute per MSKB: Q181611.
619  
620 0 eval {
621 0 ▶ socket(POE, AF_INET, SOCK_STREAM, IPPROTO_TCP)
Branch: 1 branch
BranchTrueFalseTotal
socket POE, 2, 1, 6 or die "socket failed: $!" 0 0 0
622 or die "socket failed: $!";
623 0 my $opt = unpack("I", getsockopt(POE, SOL_SOCKET, SO_OPENTYPE()));
624 0 $win32_socket_opt = $opt;
625 0 $opt &= ~(SO_SYNCHRONOUS_ALERT()|SO_SYNCHRONOUS_NONALERT());
626 0 setsockopt(POE, SOL_SOCKET, SO_OPENTYPE(), $opt);
627 0 close POE;
628 };
629  
630 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
631 }
632  
633 # Create the socket.
634 104 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (socket $socket_handle, $self->[4], $self->[10], $self->[9]) 0 104 104
635 socket( $socket_handle, $self->[MY_SOCKET_DOMAIN],
636 $self->[MY_SOCKET_TYPE], $self->[MY_SOCKET_PROTOCOL]
637 )
638 ) {
639 0 $poe_kernel->yield(
640 $event_failure, 'socket', $!+0, $!, $self->[MY_UNIQUE_ID]
641 );
642 0 return $self;
643 }
644  
645 # o create a dummy socket
646 # o restore previous value of SO_OPENTYPE
647 # o close dummy socket
648 #
649 # This way we'd only be turning on the overlap attribute for
650 # the socket we created... and not all subsequent sockets.
651 104 ▶ if ( POE::Kernel::RUNNING_IN_HELL) {
Branch: 1 branch
BranchTrueFalseTotal
if (POE::Kernel::RUNNING_IN_HELL()) 0 104 104
652 0 eval {
653 0 ▶ socket(POE, AF_INET, SOCK_STREAM, IPPROTO_TCP)
Branch: 1 branch
BranchTrueFalseTotal
socket POE, 2, 1, 6 or die "socket failed: $!" 0 0 0
654 or die "socket failed: $!";
655 0 setsockopt(POE, SOL_SOCKET, SO_OPENTYPE(), $win32_socket_opt);
656 0 close POE;
657 };
658  
659 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
660 }
661 104 DEBUG && warn "socket";
662  
663 #------------------#
664 # Configure Socket #
665 #------------------#
666  
667 # Make the socket binary. It's wrapped in eval{} because tied
668 # filehandle classes may actually die in their binmode methods.
669 104 eval { binmode($socket_handle) };
670  
671 # Don't block on socket operations, because the socket will be
672 # driven by a select loop.
673 104 $socket_handle->blocking(0);
674  
675 # Make the socket reusable, if requested.
676 104 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"Reuse"} and lc $params{"Reuse"} eq "yes" || lc $params{"Reuse"} eq "on" || $params{"Reuse"} =~ /\d+/ && $params{"Reuse"}) 25 79 104
Condition33%4/12
defined $params{"Reuse"} and lc $params{"Reuse"} eq "yes" || lc $params{"Reuse"} eq "on" || $params{"Reuse"} =~ /\d+/ && $params{"Reuse"}
!ll&&!rl&&r
79025
lc $params{"Reuse"} eq "yes" or lc $params{"Reuse"} eq "on" or $params{"Reuse"} =~ /\d+/ and $params{"Reuse"}
l!l&&r!l&&!r
2500
lc $params{"Reuse"} eq "yes" or lc $params{"Reuse"} eq "on"
l!l&&r!l&&!r
2500
$params{"Reuse"} =~ /\d+/ and $params{"Reuse"}
!ll&&!rl&&r
000
MC/DC20%1/5
defined $params{"Reuse"} and lc $params{"Reuse"} eq "yes" || lc $params{"Reuse"} eq "on" || $params{"Reuse"} =~ /\d+/ && $params{"Reuse"}
defined $params{"Reuse"} lc $params{"Reuse"} eq "yes" lc $params{"Reuse"} eq "on" $params{"Reuse"} =~ /\d+/ $params{"Reuse"}
Truth table33%2/6
A and B or C or D and E
A= defined $params{"Reuse"}B= lc $params{"Reuse"} eq "yes"C= lc $params{"Reuse"} eq "on"D= $params{"Reuse"} =~ /\d+/E= $params{"Reuse"}
ABCDEresult
0XXXX0
1000X0
100100
11XXX1
101XX1
100111
677 (defined $params{Reuse})
678 and ( (lc($params{Reuse}) eq 'yes')
679 or (lc($params{Reuse}) eq 'on')
680 or ( ($params{Reuse} =~ /\d+/)
681 and $params{Reuse}
682 )
683 )
684 )
685 {
686 25 ▶ setsockopt($socket_handle, SOL_SOCKET, SO_REUSEADDR, 1) or do {
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $socket_handle, 1, 2, 1 or do { $POE::Wheel::SocketFactory::poe_kernel->yield($event_failure, "setsockopt", $! + 0, $!, $self->[1]); return $self } 25 0 25
687 0 $poe_kernel->yield(
688 $event_failure,
689 'setsockopt', $!+0, $!, $self->[MY_UNIQUE_ID]
690 );
691 0 return $self;
692 };
693 }
694  
695 #-------------#
696 # Bind Socket #
697 #-------------#
698  
699 104 my $bind_address;
700  
701 # Check SocketFactory /Bind.*/ parameters in an Internet socket
702 # context, and translate them into parameters that bind()
703 # understands.
704 104 ▶ if ($abstract_domain eq DOM_INET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($abstract_domain eq 'inet') { } 99 5 104
elsif ($abstract_domain eq 'inet6') { } 0 5 5
elsif ($abstract_domain eq 'unix') { } 5 0 5
705 # Don't bind if the creator doesn't specify a related parameter.
706 99 ▶ if ((defined $params{BindAddress}) or (defined $params{BindPort})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"BindAddress"} or defined $params{"BindPort"}) 24 75 99
Condition66%2/3
defined $params{"BindAddress"} or defined $params{"BindPort"}
l!l&&r!l&&!r
24075
MC/DC50%1/2
defined $params{"BindAddress"} or defined $params{"BindPort"}
defined $params{"BindAddress"} defined $params{"BindPort"}
Truth table66%2/3
A or B
A= defined $params{"BindAddress"}B= defined $params{"BindPort"}
ABresult
1X1
011
000
707  
708 # Set the bind address, or default to INADDR_ANY.
709 $bind_address = (
710 (defined $params{BindAddress})
711 ? $params{BindAddress}
712 24 ▶ : INADDR_ANY
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'BindAddress'} ? : 24 0 24
713 );
714  
715 # Need to check lengths in octets, not characters.
716 24 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 24 0 24
717  
718 # Resolve the bind address if it's not already packed.
719 24 ▶ unless (length($bind_address) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $bind_address == 4) 24 0 24
720 24 $bind_address = inet_aton($bind_address);
721 }
722  
723 24 ▶ unless (defined $bind_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bind_address) 0 24 24
724 0 $! = EADDRNOTAVAIL;
725 0 $poe_kernel->yield(
726 $event_failure,
727 "inet_aton", $!+0, $!, $self->[MY_UNIQUE_ID]
728 );
729 0 return $self;
730 }
731  
732 # Set the bind port, or default to 0 (any) if none specified.
733 # Resolve it to a number, if at all possible.
734 24 ▶ my $bind_port = (defined $params{BindPort}) ? $params{BindPort} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'BindPort'} ? : 24 0 24
735 24 ▶ if ($bind_port =~ /[^0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind_port =~ /[^0-9]/) 0 24 24
736 0 $bind_port = getservbyname($bind_port, $protocol_name);
737 0 ▶ unless (defined $bind_port) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bind_port) 0 0 0
738 0 $! = EADDRNOTAVAIL;
739 0 $poe_kernel->yield(
740 $event_failure,
741 'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
742 );
743 0 return $self;
744 }
745 }
746  
747 24 $bind_address = pack_sockaddr_in($bind_port, $bind_address);
748 24 ▶ unless (defined $bind_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bind_address) 0 24 24
749 0 $poe_kernel->yield(
750 $event_failure,
751 "pack_sockaddr_in", $!+0, $!, $self->[MY_UNIQUE_ID]
752 );
753 0 return $self;
754 }
755 }
756 }
757  
758 # Check SocketFactory /Bind.*/ parameters in an Internet socket
759 # context, and translate them into parameters that bind()
760 # understands.
761 elsif ($abstract_domain eq DOM_INET6) {
762  
763 # Don't bind if the creator doesn't specify a related parameter.
764 0 ▶ if ((defined $params{BindAddress}) or (defined $params{BindPort})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"BindAddress"} or defined $params{"BindPort"}) 0 0 0
Condition0%0/3
defined $params{"BindAddress"} or defined $params{"BindPort"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $params{"BindAddress"} or defined $params{"BindPort"}
defined $params{"BindAddress"} defined $params{"BindPort"}
Truth table0%0/3
A or B
A= defined $params{"BindAddress"}B= defined $params{"BindPort"}
ABresult
1X1
011
000
765  
766 # Set the bind address, or default to INADDR_ANY.
767 $bind_address = (
768 (defined $params{BindAddress})
769 ? $params{BindAddress}
770 0 ▶ : "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" # XXX - Only Socket6 has?
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'BindAddress'} ? : 0 0 0
771 );
772  
773 # Set the bind port, or default to 0 (any) if none specified.
774 # Resolve it to a number, if at all possible.
775 0 ▶ my $bind_port = (defined $params{BindPort}) ? $params{BindPort} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'BindPort'} ? : 0 0 0
776 0 ▶ if ($bind_port =~ /[^0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind_port =~ /[^0-9]/) 0 0 0
777 0 $bind_port = getservbyname($bind_port, $protocol_name);
778 0 ▶ unless (defined $bind_port) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bind_port) 0 0 0
779 0 $! = EADDRNOTAVAIL;
780 0 $poe_kernel->yield(
781 $event_failure,
782 'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
783 );
784 0 return $self;
785 }
786 }
787  
788 # Need to check lengths in octets, not characters.
789 24 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 24 0 24
790  
791 # Undef $bind_address if IN6ADDR_ANY and handle with AI_PASSIVE
792 0 ▶ if ( $bind_address eq '::' || $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind_address eq "::" or $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") 0 0 0
Condition0%0/3
$bind_address eq "::" or $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
l!l&&r!l&&!r
000
MC/DC0%0/2
$bind_address eq "::" or $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
$bind_address eq "::" $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
Truth table0%0/3
A or B
A= $bind_address eq "::"B= $bind_address eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
ABresult
1X1
011
000
793 0 $bind_address = undef;
794 }
795  
796 # Resolve the bind address.
797 0 ▶ my ($error, @addresses) = getaddrinfo(
Branch: 1 branch
BranchTrueFalseTotal
defined $bind_address ? : 0 0 0
798 $bind_address, $bind_port, {
799 family => $self->[MY_SOCKET_DOMAIN],
800 socktype => $self->[MY_SOCKET_TYPE],
801 ( defined $bind_address ? () : ( flags => 1 ) ),
802 }
803 );
804  
805 0 ▶ unless (@addresses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@addresses) 0 0 0
806 0 ▶ warn $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
807  
808 0 $! = EADDRNOTAVAIL;
809 0 $poe_kernel->yield(
810 $event_failure,
811 "getaddrinfo", $!+0, $!, $self->[MY_UNIQUE_ID]
812 );
813 0 return $self;
814 }
815  
816 0 $bind_address = $addresses[0]->{addr};
817 }
818 }
819  
820 # Check SocketFactory /Bind.*/ parameters in a Unix context, and
821 # translate them into parameters bind() understands.
822 elsif ($abstract_domain eq DOM_UNIX) {
823 5 ▶ carp 'BindPort ignored for Unix socket' if defined $params{BindPort};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"BindPort"} 0 5 5
824  
825 5 ▶ if (defined $params{BindAddress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"BindAddress"}) 3 2 5
826 # Is this necessary, or will bind() return EADDRINUSE?
827 3 ▶ if (defined $params{RemotePort}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"RemotePort"}) 0 3 3
828 0 $! = EADDRINUSE;
829 0 $poe_kernel->yield(
830 $event_failure,
831 'bind', $!+0, $!, $self->[MY_UNIQUE_ID]
832 );
833 0 return $self;
834 }
835  
836 3 $bind_address = &_condition_unix_address($params{BindAddress});
837 3 $bind_address = pack_sockaddr_un($bind_address);
838 3 ▶ unless ($bind_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bind_address) 0 3 3
839 0 $poe_kernel->yield(
840 $event_failure,
841 'pack_sockaddr_un', $!+0, $!, $self->[MY_UNIQUE_ID]
842 );
843 0 return $self;
844 }
845 }
846 }
847  
848 # This is an internal consistency error, and it should be hard
849 # trapped right away.
850 else {
851 0 die "Mail this error to the author of POE: Internal consistency error";
852 }
853  
854 # Perform the actual bind, if there's a bind address to bind to.
855 104 ▶ if (defined $bind_address) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bind_address) 27 77 104
856 27 ▶ unless (bind($socket_handle, $bind_address)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (bind $socket_handle, $bind_address) 0 27 27
857 0 $poe_kernel->yield(
858 $event_failure,
859 'bind', $!+0, $!, $self->[MY_UNIQUE_ID]
860 );
861 0 return $self;
862 }
863  
864 27 DEBUG && warn "bind";
865 }
866  
867 #---------#
868 # Connect #
869 #---------#
870  
871 104 my $connect_address;
872  
873 104 ▶ if (defined $params{RemoteAddress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'RemoteAddress'}) { } 77 27 104
874  
875 # Check SocketFactory /Remote.*/ parameters in an Internet socket
876 # context, and translate them into parameters that connect()
877 # understands.
878 77 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($abstract_domain eq 'inet' or $abstract_domain eq 'inet6') { } 75 2 77
elsif ($abstract_domain eq 'unix') { } 2 0 2
Condition66%2/3
$abstract_domain eq "inet" or $abstract_domain eq "inet6"
l!l&&r!l&&!r
7502
MC/DC50%1/2
$abstract_domain eq "inet" or $abstract_domain eq "inet6"
$abstract_domain eq "inet" $abstract_domain eq "inet6"
Truth table66%2/3
A or B
A= $abstract_domain eq "inet"B= $abstract_domain eq "inet6"
ABresult
1X1
011
000
879 $abstract_domain eq DOM_INET or
880 $abstract_domain eq DOM_INET6
881 ) {
882 # connecting if RemoteAddress
883 75 ▶ croak 'RemotePort required' unless (defined $params{RemotePort});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"RemotePort"} 0 75 75
884 75 ▶ carp 'ListenQueue ignored' if (defined $params{ListenQueue});
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"ListenQueue"} 0 75 75
885  
886 75 my $remote_port = $params{RemotePort};
887 75 ▶ if ($remote_port =~ /[^0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remote_port =~ /[^0-9]/) 0 75 75
888 0 ▶ unless ($remote_port = getservbyname($remote_port, $protocol_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($remote_port = getservbyname($remote_port, $protocol_name)) 0 0 0
889 0 $! = EADDRNOTAVAIL;
890 0 $poe_kernel->yield(
891 $event_failure,
892 'getservbyname', $!+0, $!, $self->[MY_UNIQUE_ID]
893 );
894 0 return $self;
895 }
896 }
897  
898 75 my $error_tag;
899 75 ▶ if ($abstract_domain eq DOM_INET) {
Branch: 2 branches
BranchTrueFalseTotal
if ($abstract_domain eq 'inet') { } 75 0 75
elsif ($abstract_domain eq 'inet6') { } 0 0 0
900 75 $connect_address = inet_aton($params{RemoteAddress});
901 75 $error_tag = "inet_aton";
902 }
903 elsif ($abstract_domain eq DOM_INET6) {
904 my ($error, @addresses) = getaddrinfo(
905 0 $params{RemoteAddress}, $remote_port, {
906 family => $self->[MY_SOCKET_DOMAIN],
907 socktype => $self->[MY_SOCKET_TYPE],
908 },
909 );
910  
911 0 ▶ unless (@addresses) {
Branch: 1 branch
BranchTrueFalseTotal
if (@addresses) { } 0 0 0
912 0 ▶ warn $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
913 0 $connect_address = undef;
914 }
915 else {
916 0 $connect_address = $addresses[0]->{addr};
917 }
918  
919 0 $error_tag = "getaddrinfo";
920 }
921 else {
922 0 die "unknown domain $abstract_domain";
923 }
924  
925 # TODO - If the gethostbyname2() code is removed, then we can
926 # combine the previous code with the following code, and perhaps
927 # remove one of these redundant $connect_address checks. The
928 # 0.29 release should tell us pretty quickly whether it's
929 # needed. If we reach 0.30 without incident, it's probably safe
930 # to remove the old gethostbyname2() code and clean this up.
931 75 ▶ unless (defined $connect_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $connect_address) 0 75 75
932 0 $! = EADDRNOTAVAIL;
933 0 $poe_kernel->yield(
934 $event_failure,
935 $error_tag, $!+0, $!, $self->[MY_UNIQUE_ID]
936 );
937 0 return $self;
938 }
939  
940 75 ▶ if ($abstract_domain eq DOM_INET) {
Branch: 2 branches
BranchTrueFalseTotal
if ($abstract_domain eq 'inet') { } 75 0 75
elsif ($abstract_domain eq 'inet6') { } 0 0 0
941 75 $connect_address = pack_sockaddr_in($remote_port, $connect_address);
942 75 $error_tag = "pack_sockaddr_in";
943 }
944 elsif ($abstract_domain eq DOM_INET6) {
945 0 $error_tag = "pack_sockaddr_in6";
946 }
947 else {
948 0 die "unknown domain $abstract_domain";
949 }
950  
951 75 ▶ unless ($connect_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($connect_address) 0 75 75
952 0 $! = EADDRNOTAVAIL;
953 0 $poe_kernel->yield(
954 $event_failure,
955 $error_tag, $!+0, $!, $self->[MY_UNIQUE_ID]
956 );
957 0 return $self;
958 }
959 }
960  
961 # Check SocketFactory /Remote.*/ parameters in a Unix socket
962 # context, and translate them into parameters connect()
963 # understands.
964 elsif ($abstract_domain eq DOM_UNIX) {
965  
966 2 $connect_address = _condition_unix_address($params{RemoteAddress});
967 2 $connect_address = pack_sockaddr_un($connect_address);
968 2 ▶ unless (defined $connect_address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $connect_address) 0 2 2
969 0 $poe_kernel->yield(
970 $event_failure,
971 'pack_sockaddr_un', $!+0, $!, $self->[MY_UNIQUE_ID]
972 );
973 0 return $self;
974 }
975 }
976  
977 # This is an internal consistency error, and it should be trapped
978 # right away.
979 else {
980 0 die "Mail this error to the author of POE: Internal consistency error";
981 }
982 }
983  
984 else {
985 carp "RemotePort ignored without RemoteAddress"
986 27 ▶ if defined $params{RemotePort};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"RemotePort"} 0 27 27
987 }
988  
989 # Perform the actual connection, if a connection was requested. If
990 # the connection can be established, then return the SocketFactory
991 # handle.
992 104 ▶ if (defined $connect_address) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connect_address) 77 27 104
993 77 ▶ unless (connect($socket_handle, $connect_address)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (connect $socket_handle, $connect_address) 75 2 77
994 75 ▶ if ($! and ($! != EINPROGRESS) and ($! != EWOULDBLOCK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! and $! != 115 and $! != 11) 0 75 75
Condition33%2/6
$! and $! != 115 and $! != 11
!ll&&!rl&&r
7500
$! and $! != 115
!ll&&!rl&&r
0750
MC/DC0%0/3
$! and $! != 115 and $! != 11
$! $! != 115 $! != 11
Truth table25%1/4
A and B and C
A= $!B= $! != 115C= $! != 11
ABCresult
0XX0
10X0
1100
1111
995 0 $poe_kernel->yield(
996 $event_failure,
997 'connect', $!+0, $!, $self->[MY_UNIQUE_ID]
998 );
999 0 return $self;
1000 }
1001 }
1002  
1003 77 DEBUG && warn "connect";
1004  
1005 77 $self->[MY_SOCKET_HANDLE] = $socket_handle;
1006 77 $self->_define_connect_state();
1007 $self->event(
1008 SuccessEvent => $params{SuccessEvent},
1009 FailureEvent => $params{FailureEvent},
1010 77 );
1011 77 return $self;
1012 }
1013  
1014 #---------------------#
1015 # Listen, or Whatever #
1016 #---------------------#
1017  
1018 # A connection wasn't requested, so this must be a server socket.
1019 # Do whatever it is that needs to be done for whatever type of
1020 # server socket this is.
1021 27 ▶ if (exists $supported_protocol{$abstract_domain}->{$protocol_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $supported_protocol{$abstract_domain}{$protocol_name}) { } 27 0 27
1022 27 my $protocol_op = $supported_protocol{$abstract_domain}->{$protocol_name};
1023  
1024 27 DEBUG && warn "$abstract_domain + $protocol_name = $protocol_op";
1025  
1026 27 ▶ if ($protocol_op eq SVROP_LISTENS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol_op eq 'listens') { } 23 4 27
1027 23 ▶ my $listen_queue = $params{ListenQueue} || SOMAXCONN;
Condition50%1/2
$params{'ListenQueue'} || 4096
l!l
023
MC/DC0%0/1
$params{'ListenQueue'} || 4096
$params{'ListenQueue'}
Truth table50%1/2
A
A= $params{'ListenQueue'}
Aresult
11
00
1028 # <rmah> In SocketFactory, you limit the ListenQueue parameter
1029 # to SOMAXCON (or is it SOCONNMAX?)...why?
1030 # <rmah> ah, here's czth, he'll have more to say on this issue
1031 # <czth> not really. just that SOMAXCONN can lie, notably on
1032 # Solaris and reportedly on BSDs too
1033 #
1034 # ($listen_queue > SOMAXCONN) && ($listen_queue = SOMAXCONN);
1035 23 ▶ unless (listen($socket_handle, $listen_queue)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (listen $socket_handle, $listen_queue) 0 23 23
1036 0 $poe_kernel->yield(
1037 $event_failure,
1038 'listen', $!+0, $!, $self->[MY_UNIQUE_ID]
1039 );
1040 0 return $self;
1041 }
1042  
1043 23 DEBUG && warn "listen";
1044  
1045 23 $self->[MY_SOCKET_HANDLE] = $socket_handle;
1046 23 $self->_define_accept_state();
1047 $self->event(
1048 SuccessEvent => $params{SuccessEvent},
1049 FailureEvent => $params{FailureEvent},
1050 23 );
1051 23 return $self;
1052 }
1053 else {
1054 carp "Ignoring ListenQueue parameter for non-listening socket"
1055 4 ▶ if defined $params{ListenQueue};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"ListenQueue"} 0 4 4
1056 4 ▶ if ($protocol_op eq SVROP_NOTHING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol_op eq 'nothing') { } 4 0 4
1057 # Do nothing. Duh. Fire off a success event immediately, and
1058 # return.
1059 4 $poe_kernel->yield(
1060 $event_success,
1061 $socket_handle, undef, undef, $self->[MY_UNIQUE_ID]
1062 );
1063 4 return $self;
1064 }
1065 else {
1066 0 die "Mail this error to the author of POE: Internal consistency error";
1067 }
1068 }
1069 }
1070 else {
1071 0 die "SocketFactory doesn't support $abstract_domain $protocol_name socket";
1072 }
1073  
1074 0 die "Mail this error to the author of POE: Internal consistency error";
1075 }
1076  
1077 # Pause and resume accept.
1078 24 ▶sub pause_accept {
Subroutine pause_accept: called CC=4 SCAR=17.1
Pod documented
1079 24 my $self = shift;
1080 24 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0] and defined $self->[5] and defined $self->[12]) 24 0 24
Condition33%2/6
defined $self->[0] and defined $self->[5] and defined $self->[12]
!ll&&!rl&&r
0024
defined $self->[0] and defined $self->[5]
!ll&&!rl&&r
0024
MC/DC0%0/3
defined $self->[0] and defined $self->[5] and defined $self->[12]
defined $self->[0] defined $self->[5] defined $self->[12]
Truth table25%1/4
A and B and C
A= defined $self->[0]B= defined $self->[5]C= defined $self->[12]
ABCresult
0XX0
10X0
1100
1111
1081 defined $self->[MY_SOCKET_HANDLE] and
1082 defined $self->[MY_STATE_ACCEPT] and
1083 defined $self->[MY_SOCKET_SELECTED]
1084 ) {
1085 24 $poe_kernel->select_pause_read($self->[MY_SOCKET_HANDLE]);
1086 }
1087 }
1088  
1089 22 ▶sub resume_accept {
Subroutine resume_accept: called CC=4 SCAR=17.1
Pod documented
1090 22 my $self = shift;
1091 22 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0] and defined $self->[5] and defined $self->[12]) 22 0 22
Condition33%2/6
defined $self->[0] and defined $self->[5] and defined $self->[12]
!ll&&!rl&&r
0022
defined $self->[0] and defined $self->[5]
!ll&&!rl&&r
0022
MC/DC0%0/3
defined $self->[0] and defined $self->[5] and defined $self->[12]
defined $self->[0] defined $self->[5] defined $self->[12]
Truth table25%1/4
A and B and C
A= defined $self->[0]B= defined $self->[5]C= defined $self->[12]
ABCresult
0XX0
10X0
1100
1111
1092 defined $self->[MY_SOCKET_HANDLE] and
1093 defined $self->[MY_STATE_ACCEPT] and
1094 defined $self->[MY_SOCKET_SELECTED]
1095 ) {
1096 22 $poe_kernel->select_resume_read($self->[MY_SOCKET_HANDLE]);
1097 }
1098 }
1099  
1100 #------------------------------------------------------------------------------
1101 # DESTROY and _shutdown pass things by reference because _shutdown is
1102 # called from the state() closures above. As a result, we can't
1103 # mention $self explicitly, or the wheel won't shut itself down
1104 # properly. Rather, it will form a circular reference on $self.
1105  
1106 104 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
1107 104 my $self = shift;
1108 104 _shutdown(
1109 \$self->[MY_SOCKET_SELECTED],
1110 \$self->[MY_SOCKET_HANDLE],
1111 \$self->[MY_STATE_ACCEPT],
1112 \$self->[MY_STATE_CONNECT],
1113 \$self->[MY_MINE_SUCCESS],
1114 \$self->[MY_EVENT_SUCCESS],
1115 \$self->[MY_MINE_FAILURE],
1116 \$self->[MY_EVENT_FAILURE],
1117 );
1118 104 &POE::Wheel::free_wheel_id($self->[MY_UNIQUE_ID]);
1119 }
1120  
1121 181 ▶sub _shutdown {
Subroutine _shutdown: called CC=6 SCAR=18.6
1122 my (
1123 181 $socket_selected, $socket_handle,
1124 $state_accept, $state_connect,
1125 $mine_success, $event_success,
1126 $mine_failure, $event_failure,
1127 ) = @_;
1128  
1129 181 ▶ if (defined $$socket_selected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$socket_selected) 100 81 181
1130 100 $poe_kernel->select($$socket_handle);
1131 100 $$socket_selected = undef;
1132 }
1133  
1134 181 ▶ if (defined $$state_accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$state_accept) 23 158 181
1135 23 $poe_kernel->state($$state_accept);
1136 23 $$state_accept = undef;
1137 }
1138  
1139 181 ▶ if (defined $$state_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$state_connect) 77 104 181
1140 77 $poe_kernel->state($$state_connect);
1141 77 $$state_connect = undef;
1142 }
1143  
1144 181 ▶ if (defined $$mine_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$mine_success) 0 181 181
1145 0 $poe_kernel->state($$event_success);
1146 0 $$mine_success = $$event_success = undef;
1147 }
1148  
1149 181 ▶ if (defined $$mine_failure) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$mine_failure) 0 181 181
1150 0 $poe_kernel->state($$event_failure);
1151 0 $$mine_failure = $$event_failure = undef;
1152 }
1153 }
1154  
1155 24 1;
1156  
1157 __END__
↑ Summary blib/lib/POE/Wheel/Run.pm »