| 1 | package POE::Wheel::UDP; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | POE::Wheel::UDP - POE Wheel for UDP handling. | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | use POE; | ||||||||||||||||||||||||||||||||
| 10 | use POE::Wheel::UDP; | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | POE::Session->create( | ||||||||||||||||||||||||||||||||
| 13 | inline_states => { | ||||||||||||||||||||||||||||||||
| 14 | _start => sub { | ||||||||||||||||||||||||||||||||
| 15 | my $wheel = $_[HEAP]->{wheel} = POE::Wheel::UDP->new( | ||||||||||||||||||||||||||||||||
| 16 | LocalAddr => '10.0.0.1', | ||||||||||||||||||||||||||||||||
| 17 | LocalPort => 1234, | ||||||||||||||||||||||||||||||||
| 18 | PeerAddr => '10.0.0.2', | ||||||||||||||||||||||||||||||||
| 19 | PeerPort => 1235, | ||||||||||||||||||||||||||||||||
| 20 | InputEvent => 'input', | ||||||||||||||||||||||||||||||||
| 21 | Filter => POE::Filter::Stream->new, | ||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||
| 23 | $wheel->put( | ||||||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||||||
| 25 | payload => [ 'This datagram will go to the default address.' ], | ||||||||||||||||||||||||||||||||
| 26 | }, | ||||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||
| 28 | payload => [ 'This datagram will go to the explicit address and port I have paired with it.' ], | ||||||||||||||||||||||||||||||||
| 29 | addr => '10.0.0.3', | ||||||||||||||||||||||||||||||||
| 30 | port => 1236, | ||||||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||||
| 34 | input => sub { | ||||||||||||||||||||||||||||||||
| 35 | my ($wheel_id, $input) = @_[ARG0, ARG1]; | ||||||||||||||||||||||||||||||||
| 36 | print "Incoming datagram from $input->{addr}:$input->{port}: '$input->{payload}'\n"; | ||||||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | POE::Kernel->run; | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | POE Wheel for UDP handling. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | use 5.006; # I don't plan to support old perl | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | use base 'POE::Wheel'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | use POE; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 57 | 2 | ▶ | use Socket; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 58 | 2 | ▶ | use Fcntl; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||||||
| 61 | 2 | $VERSION = eval $VERSION; # see L<perlmodstyle> | |||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | =head1 Package Methods | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | =head2 $wheel = POE::Wheel::UDP->new( OPTIONS ); | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | Constructor for a new UDP Wheel object. OPTIONS is a key => value pair list specifying the following options: | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | =over | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | =item LocalAddr | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | =item LocalPort | ||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | (Required Pair) | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | Specify the local IP address and port for the created socket. LocalAddr should be in dotted-quad notation, | ||||||||||||||||||||||||||||||||
| 78 | and LocalPort should be an integer. This module will not resolve names to numbers at all. | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | =item PeerAddr | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | =item PeerPort | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | (Optional Pair) | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | Specify the remote IP address and port for the created socket. As above, PeerAddr should be in dotted-quad | ||||||||||||||||||||||||||||||||
| 87 | notation, and PeerPort should be an integer. These arguments are used to perform a C connect(2) on the socket, | ||||||||||||||||||||||||||||||||
| 88 | which means that outbound datagrams will be sent to this address by default AND inbound datagrams from sources | ||||||||||||||||||||||||||||||||
| 89 | other than this peer will be ignored. If you want to just set a default destination for packets, use the | ||||||||||||||||||||||||||||||||
| 90 | DefaultAddr and DefaultPort items instead. | ||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | =item DefaultAddr | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | =item DefaultPort | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | (Optional Pair) | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | Dotted quad, and integer (respectively) options for the default destination of datagrams originating from this | ||||||||||||||||||||||||||||||||
| 99 | wheel. This setting will override the PeerAddr and PeerPort on each put() method, but you can override this | ||||||||||||||||||||||||||||||||
| 100 | by passing arguments directly to the put() method. | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | =item InputEvent | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | (Optional) | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | Specify the event to be invoked via Kernel->yield when a packet arrives on this socket. Currently all incoming | ||||||||||||||||||||||||||||||||
| 107 | data is truncated to 1500 bytes. If you do not specify an event, the wheel will not ask the kernel to pass | ||||||||||||||||||||||||||||||||
| 108 | incoming datagrams to it, and therefore this wheel will not hold your session alive. | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | =item InputFilter | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | (Required if InputEvent defined) | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | Assign a POE::Filter object to the input side of this wheel. | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | =item OutputFilter | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | (Required if you want to call the put method) | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | Assign a POE::Filter object to the output side of this wheel. | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | =item Filter | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | Shorthand for assigning the same filter object to both the InputFilter and OutputFilter arguments. | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | =back | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=19 SCAR=35.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | carp( "Uneven set of options passed to ${class}->new." ) unless (@_ % 2 == 0); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 2 | my %opts = @_; | |||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | my $self = bless { }, (ref $class || $class); | ||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 2 | my %sockopts; | |||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 2 | foreach (qw(LocalAddr LocalPort PeerAddr PeerPort)) { | |||||||||||||||||||||||||||||||
| 140 | 8 | ▶ | $sockopts{$_} = delete( $opts{$_} ) if exists( $opts{$_} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | 2 | $self->_open( %sockopts ); | |||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 2 | my $id = $self->{id} = $self->SUPER::allocate_wheel_id(); | |||||||||||||||||||||||||||||||
| 146 | 2 | my $read_event = $self->{read_event} = ref($self) . "($id) -> select read"; | |||||||||||||||||||||||||||||||
| 147 | 2 | my $write_event = $self->{write_event} = ref($self) . "($id) -> select write"; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | if (exists( $opts{DefaultAddr} ) or exists( $opts{DefaultPort} )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $opts{"DefaultAddr"} or exists $opts{"DefaultPort"}
MC/DC0%0/2
exists $opts{"DefaultAddr"} or exists $opts{"DefaultPort"}
exists $opts{"DefaultAddr"}
exists $opts{"DefaultPort"}
Truth table33%1/3
A or B
A= exists $opts{"DefaultAddr"}B= exists $opts{"DefaultPort"}
| |||||||||||||||||||||||||||||||||
| 150 | croak "DefaultAddr is required if DefaultPort is specified." | ||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | unless exists( $opts{DefaultAddr} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | croak "DefaultPort is required if DefaultAddr is specified." | ||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | unless exists( $opts{DefaultPort} ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||
| 155 | my $addr = inet_aton( $opts{DefaultAddr} ) | ||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | or croak( "Supplied 'DefaultAddr' value '$opts{DefaultAddr}' caused inet_aton failure: $!" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | my $spec = pack_sockaddr_in( $opts{DefaultPort}, $addr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | or croak( "Supplied 'DefaultPort' value '$opts{DefaultPort}' caused pack_sockaddr_in failure: $!" ); | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | 0 | $self->{DefaultAddr} = delete $opts{DefaultAddr}; | |||||||||||||||||||||||||||||||
| 162 | 0 | $self->{DefaultPort} = delete $opts{DefaultPort}; | |||||||||||||||||||||||||||||||
| 163 | 0 | $self->{default_send} = $spec; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | if (exists( $opts{Filter} )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | 2 | my $filter = delete $opts{Filter}; | |||||||||||||||||||||||||||||||
| 168 | 2 | ▶ | $opts{InputFilter} ||= $filter; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$opts{"InputFilter"} ||= $filter
MC/DC0%0/2
$opts{"InputFilter"} ||= $filter
$opts{"InputFilter"}
$filter
Truth table33%1/3
A ||= B
A= $opts{"InputFilter"}B= $filter
| |||||||||||||||||||||||||||||||||
| 169 | 2 | ▶ | $opts{OutputFilter} ||= $filter; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$opts{"OutputFilter"} ||= $filter
MC/DC0%0/2
$opts{"OutputFilter"} ||= $filter
$opts{"OutputFilter"}
$filter
Truth table33%1/3
A ||= B
A= $opts{"OutputFilter"}B= $filter
| |||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | if (exists( $opts{InputFilter} )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 2 | $self->{InputFilter} = delete $opts{InputFilter}; | |||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 2 | ▶ | if (exists( $opts{OutputFilter} )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 2 | $self->{OutputFilter} = delete $opts{OutputFilter}; | |||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | if (exists( $opts{InputEvent} )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | croak "InputFilter option is required if InputEvent is defined." | ||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | unless exists($self->{InputFilter}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 1 | my $filter = \$self->{InputFilter}; | |||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 1 | my $input_event = $self->{InputEvent} = delete $opts{InputEvent}; | |||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 100 | ▶ | $poe_kernel->state( $read_event, sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.0
| |||||||||||||||||||||||||||||||||
| 189 | 100 | my ($kernel, $socket) = @_[KERNEL, ARG0]; | |||||||||||||||||||||||||||||||
| 190 | 100 | $! = undef; | |||||||||||||||||||||||||||||||
| 191 | 100 | ▶ | while( my $addr = recv( $socket, my $input = "", 1500, MSG_DONTWAIT ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 192 | 100 | ▶ | if (defined( $addr )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 100 | my %input_data; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | 100 | ▶ | if ($addr) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 196 | 100 | ▶ | my ($port, $addr) = unpack_sockaddr_in( $addr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 197 | or warn( "sockaddr_in failure: $!" ); | ||||||||||||||||||||||||||||||||
| 198 | 100 | $input_data{addr} = inet_ntoa( $addr ); | |||||||||||||||||||||||||||||||
| 199 | 100 | $input_data{port} = $port; | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | 100 | $input_data{bytes} = length( $input ); | |||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | 100 | local $POE::Filter::DATAGRAM = 1; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 100 | $$filter->get_one_start( [ $input ] ); | |||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||
| 208 | 100 | my @payload; | |||||||||||||||||||||||||||||||
| 209 | 100 | ▶ | while (my $records = $$filter->get_one) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 210 | 200 | ▶ | last unless @$records; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | 100 | push @payload, @$records; | |||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | 100 | $poe_kernel->yield( $input_event, { | |||||||||||||||||||||||||||||||
| 215 | payload => \@payload, | ||||||||||||||||||||||||||||||||
| 216 | %input_data, | ||||||||||||||||||||||||||||||||
| 217 | }, $id ); | ||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | else { | ||||||||||||||||||||||||||||||||
| 220 | 0 | warn "recv failure: $!"; | |||||||||||||||||||||||||||||||
| 221 | next | ||||||||||||||||||||||||||||||||
| 222 | 0 | } | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | 1 | } ); | |||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||
| 226 | 1 | $poe_kernel->select_read( $self->{sock}, $read_event ); | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | # Does anyone know if I should watch for writability on the socket at all? it's pretty hard to test | ||||||||||||||||||||||||||||||||
| 230 | # to see if UDP can ever return EAGAIN because I can't get it to go fast enough to blast past the buffers. | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | croak "Extra options passed to new(): " . join( ', ', map { "'$_'" } keys %opts ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 233 | if keys %opts; | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 2 | return $self; | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 2 | ▶ | sub _open { | ||||||||||||||||||||||||||||||
|
Subroutine
_open: called CC=12 SCAR=28.1
| |||||||||||||||||||||||||||||||||
| 239 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 240 | 2 | my %opts = @_; | |||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||
| 242 | 2 | my $proto = getprotobyname( "udp" ); | |||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | socket( my $sock, PF_INET, SOCK_DGRAM, $proto ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | or die( "socket() failure: $!" ); | ||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||
| 247 | 2 | ▶ | fcntl( $sock, F_SETFL, O_NONBLOCK | O_RDWR ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 248 | or die( "fcntl problem: $!" ); | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | setsockopt( $sock, SOL_SOCKET, SO_REUSEADDR, 1 ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | or die( "setsockopt SO_REUSEADDR failed: $!" ); | ||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||||||
| 254 | 2 | my $addr = inet_aton( $opts{LocalAddr} ) | |||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | or die( "inet_aton problem: $!" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | my $sockaddr = sockaddr_in( $opts{LocalPort}, $addr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | or die( "sockaddr_in problem: $!" ); | ||||||||||||||||||||||||||||||||
| 258 | 2 | ▶ | bind( $sock, $sockaddr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 259 | or die( "bind error: $!" ); | ||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||
| 262 | 2 | ▶ | if ($opts{PeerAddr} and $opts{PeerPort}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"PeerAddr"} and $opts{"PeerPort"}
MC/DC0%0/2
$opts{"PeerAddr"} and $opts{"PeerPort"}
$opts{"PeerAddr"}
$opts{"PeerPort"}
Truth table33%1/3
A and B
A= $opts{"PeerAddr"}B= $opts{"PeerPort"}
| |||||||||||||||||||||||||||||||||
| 263 | my $addr = inet_aton( $opts{PeerAddr} ) | ||||||||||||||||||||||||||||||||
| 264 | 2 | ▶ | or die( "inet_aton problem: $!" ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | my $sockaddr = sockaddr_in( $opts{PeerPort}, $addr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 266 | or die( "sockaddr_in problem: $!" ); | ||||||||||||||||||||||||||||||||
| 267 | 2 | ▶ | connect( $sock, $sockaddr ) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 268 | or die( "connect error: $!" ); | ||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||
| 271 | 2 | return $self->{sock} = $sock; | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | =head1 Object Methods | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | =head2 $wheel->put( LIST ) | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | Returns the total number of bytes sent in this call, which may not match the number of bytes | ||||||||||||||||||||||||||||||||
| 279 | you passed in for payloads due to send(2) semantics. Takes a list of hashrefs with the | ||||||||||||||||||||||||||||||||
| 280 | following useful keys in them: | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | =over | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | =item payload | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | An arrayref of records you wish to put through the filter and send in datagrams. The arrayref | ||||||||||||||||||||||||||||||||
| 287 | is used to allow more than one logical record per datagram. | ||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =item bytes | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | How many bytes were read from this datagram. Currently a maximum of 1500 will be read, and | ||||||||||||||||||||||||||||||||
| 292 | datagrams which are larger will be truncated. | ||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||
| 294 | =item addr | ||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||
| 296 | =item port | ||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||
| 298 | Specify a destination IP address and port for this specific packet. Optional if you specified | ||||||||||||||||||||||||||||||||
| 299 | a PeerAddr and PeerPort in the wheel constructor; Required if you did not. | ||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||
| 301 | =back | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | 100 | ▶ | sub put { | ||||||||||||||||||||||||||||||
|
Subroutine
put: called CC=15 SCAR=36.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 306 | 100 | my $self= shift; | |||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 100 | my $sock = $self->{sock}; | |||||||||||||||||||||||||||||||
| 309 | 100 | my $total_bytes = 0; | |||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 100 | ▶ | while (my $thing = shift) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 312 | 100 | ▶ | if (!defined( $thing )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 313 | 0 | warn "Undefined argument, ignoring"; | |||||||||||||||||||||||||||||||
| 314 | 0 | next; | |||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 100 | ▶ | if (ref( $thing ) ne 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 318 | 0 | warn "Non-hasref argument, ignoring"; | |||||||||||||||||||||||||||||||
| 319 | 0 | next; | |||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||
| 322 | 100 | ▶ | my $payload = $thing->{payload} or die; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | 100 | ▶ | die unless ref($payload) eq 'ARRAY'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 100 | my $filter = $self->{OutputFilter}; | |||||||||||||||||||||||||||||||
| 327 | 100 | my $records = $filter->put( $payload ); | |||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 100 | my $bytes; | |||||||||||||||||||||||||||||||
| 330 | 100 | ▶ | if (exists( $thing->{addr} ) or exists( $thing->{port} )) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
exists $thing->{"addr"} or exists $thing->{"port"}
MC/DC0%0/2
exists $thing->{"addr"} or exists $thing->{"port"}
exists $thing->{"addr"}
exists $thing->{"port"}
Truth table33%1/3
A or B
A= exists $thing->{"addr"}B= exists $thing->{"port"}
| |||||||||||||||||||||||||||||||||
| 331 | 0 | ▶ | my $addr = $thing->{addr} or die; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 332 | 0 | ▶ | my $port = $thing->{port} or die; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | 0 | foreach my $output (@$records) { | |||||||||||||||||||||||||||||||
| 335 | 0 | $bytes = send( $sock, $output, MSG_DONTWAIT, sockaddr_in( $port,inet_aton( $addr ) ) ); | |||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||
| 338 | elsif (exists( $self->{default_send} )) { | ||||||||||||||||||||||||||||||||
| 339 | 0 | my $default_send = $self->{default_send}; | |||||||||||||||||||||||||||||||
| 340 | 0 | foreach my $output (@$records) { | |||||||||||||||||||||||||||||||
| 341 | 0 | $bytes = send( $sock, $output, MSG_DONTWAIT, $default_send ); | |||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||
| 344 | else { | ||||||||||||||||||||||||||||||||
| 345 | 100 | foreach my $output (@$records) { | |||||||||||||||||||||||||||||||
| 346 | 100 | $bytes = send( $sock, $output, MSG_DONTWAIT ); | |||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | 100 | ▶ | if (!defined( $bytes )) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 351 | 0 | die( "send() failed: $!" ); | |||||||||||||||||||||||||||||||
| 352 | # if we ever remove fatal handling of this, do the following: | ||||||||||||||||||||||||||||||||
| 353 | # push current thing onto buffer. | ||||||||||||||||||||||||||||||||
| 354 | # last; | ||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||
| 356 | 100 | $total_bytes += $bytes; | |||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||
| 359 | # push rest of @_ onto buffer | ||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||
| 361 | 100 | return $total_bytes; | |||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||
| 364 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 365 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 366 | 2 | ▶ | if ($self->{read_event}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 367 | 2 | $poe_kernel->state( delete $self->{read_event} ); | |||||||||||||||||||||||||||||||
| 368 | 2 | $poe_kernel->select_read( $self->{sock} ); | |||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||
| 370 | 2 | $self->SUPER::free_wheel_id( delete $self->{id} ); | |||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||
| 373 | sub allocate_wheel_id; # try to cancel this method from being inhereted. | ||||||||||||||||||||||||||||||||
| 374 | sub free_wheel_id; | ||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||
| 376 | 2 | 1; | |||||||||||||||||||||||||||||||
| 377 | __END__ | ||||||||||||||||||||||||||||||||