| 1 | package POE::Wheel::ListenAccept; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use vars qw($VERSION @ISA); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places) | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Carp qw( croak carp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Symbol qw( gensym ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use POSIX qw(:fcntl_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Errno qw(EWOULDBLOCK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use POE qw( Wheel ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | push @ISA, qw(POE::Wheel); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub SELF_HANDLE () { 0 } | ||||||||||||||||||||||||||||
| 17 | sub SELF_EVENT_ACCEPT () { 1 } | ||||||||||||||||||||||||||||
| 18 | sub SELF_EVENT_ERROR () { 2 } | ||||||||||||||||||||||||||||
| 19 | sub SELF_UNIQUE_ID () { 3 } | ||||||||||||||||||||||||||||
| 20 | sub SELF_STATE_ACCEPT () { 4 } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub CRIMSON_SCOPE_HACK ($) { 0 } | ||||||||||||||||||||||||||
|
Subroutine
CRIMSON_SCOPE_HACK: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.2
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 2 | my $type = shift; | |||||||||||||||||||||||||||
| 28 | 2 | my %params = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | croak "wheels no longer require a kernel reference as their first parameter" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ and ref $_[0] eq "POE::Kernel"
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"
| |||||||||||||||||||||||||||||
| 31 | if (@_ && (ref($_[0]) eq 'POE::Kernel')); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | ▶ | croak "$type requires a working Kernel" unless defined $poe_kernel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | croak "Handle required" unless defined $params{Handle}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | croak "AcceptEvent required" unless defined $params{AcceptEvent}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my $self = bless [ $params{Handle}, # SELF_HANDLE | ||||||||||||||||||||||||||||
| 39 | delete $params{AcceptEvent}, # SELF_EVENT_ACCEPT | ||||||||||||||||||||||||||||
| 40 | delete $params{ErrorEvent}, # SELF_EVENT_ERROR | ||||||||||||||||||||||||||||
| 41 | 2 | &POE::Wheel::allocate_wheel_id(), # SELF_UNIQUE_ID | |||||||||||||||||||||||||||
| 42 | undef, # SELF_STATE_ACCEPT | ||||||||||||||||||||||||||||
| 43 | ], $type; | ||||||||||||||||||||||||||||
| 44 | # register private event handlers | ||||||||||||||||||||||||||||
| 45 | 2 | $self->_define_accept_state(); | |||||||||||||||||||||||||||
| 46 | 2 | $poe_kernel->select($self->[SELF_HANDLE], $self->[SELF_STATE_ACCEPT]); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub event { | ||||||||||||||||||||||||||
|
Subroutine
event: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | push(@_, undef) if (scalar(@_) & 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 4 | my ($name, $event) = splice(@_, 0, 2); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if ($name eq 'AcceptEvent') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if (defined $event) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 2 | $self->[SELF_EVENT_ACCEPT] = $event; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||
| 65 | 0 | carp "AcceptEvent requires an event name. ignoring undef"; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | elsif ($name eq 'ErrorEvent') { | ||||||||||||||||||||||||||||
| 69 | 2 | $self->[SELF_EVENT_ERROR] = $event; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | 0 | carp "ignoring unknown ListenAccept parameter '$name'"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | sub _define_accept_state { | ||||||||||||||||||||||||||
|
Subroutine
_define_accept_state: called CC=1 SCAR=0.3
| |||||||||||||||||||||||||||||
| 80 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | # stupid closure trick | ||||||||||||||||||||||||||||
| 82 | 2 | my $event_accept = \$self->[SELF_EVENT_ACCEPT]; | |||||||||||||||||||||||||||
| 83 | 2 | my $event_error = \$self->[SELF_EVENT_ERROR]; | |||||||||||||||||||||||||||
| 84 | 2 | my $handle = $self->[SELF_HANDLE]; | |||||||||||||||||||||||||||
| 85 | 2 | my $unique_id = $self->[SELF_UNIQUE_ID]; | |||||||||||||||||||||||||||
| 86 | # register the select-read handler | ||||||||||||||||||||||||||||
| 87 | $poe_kernel->state | ||||||||||||||||||||||||||||
| 88 | ( $self->[SELF_STATE_ACCEPT] = ref($self) . "($unique_id) -> select read", | ||||||||||||||||||||||||||||
| 89 | 10 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=17.2
| |||||||||||||||||||||||||||||
| 90 | # prevents SEGV | ||||||||||||||||||||||||||||
| 91 | 10 | 0 && CRIMSON_SCOPE_HACK('<'); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # subroutine starts here | ||||||||||||||||||||||||||||
| 94 | 10 | my ($k, $me, $handle) = @_[KERNEL, SESSION, ARG0]; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 10 | my $new_socket = gensym; | |||||||||||||||||||||||||||
| 97 | 10 | my $peer = accept($new_socket, $handle); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 10 | ▶ | if ($peer) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 100 | 10 | $k->call($me, $$event_accept, $new_socket, $peer, $unique_id); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | elsif ($! != EWOULDBLOCK) { | ||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $$event_error && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | $k->call($me, $$event_error, 'accept', ($!+0), $!, $unique_id); | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 2 | ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 113 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | # remove tentacles from our owner | ||||||||||||||||||||||||||||
| 115 | 2 | $poe_kernel->select($self->[SELF_HANDLE]); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 2 | ▶ | if ($self->[SELF_STATE_ACCEPT]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 2 | $poe_kernel->state($self->[SELF_STATE_ACCEPT]); | |||||||||||||||||||||||||||
| 119 | 2 | undef $self->[SELF_STATE_ACCEPT]; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | &POE::Wheel::free_wheel_id($self->[SELF_UNIQUE_ID]); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub ID { | ||||||||||||||||||||||||||
|
Subroutine
ID: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 0 | return $_[0]->[SELF_UNIQUE_ID]; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 2 | 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||