| 1 | # Declare our package | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package Games::AssaultCube::ServerQuery; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | # import the Moose stuff | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | # Initialize our version | ||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # get some utility stuff | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Games::AssaultCube::Utils qw( default_port tostr get_ac_pingport ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Games::AssaultCube::ServerQuery::Response; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # TODO make validation so we accept either hostname or IPv4/6... | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | has 'server' => ( | |||||||||||||||||||||||||||||||||||||||||
| 20 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 22 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # <mst> Apocalypse: { my $port_spec = subtype as Int => where { ... }; has 'attr' => (isa => $port_spec, ...); } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | ▶ | my $port_type = subtype as 'Int' => where { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.3
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | if ( $_ <= 0 or $_ > 65535 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_ <= 0 or $_ > 65535
MC/DC0%0/2
$_ <= 0 or $_ > 65535
$_ <= 0
$_ > 65535
Truth table33%1/3
A or B
A= $_ <= 0B= $_ > 65535
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | has 'port' => ( | |||||||||||||||||||||||||||||||||||||||||
| 36 | isa => $port_type, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 38 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 39 | default => default_port(), | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | has 'timeout' => ( | |||||||||||||||||||||||||||||||||||||||||
| 44 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | default => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | has 'get_players' => ( | |||||||||||||||||||||||||||||||||||||||||
| 50 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=5 SCAR=25.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | if ( @_ == 1 && ! ref $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
@_ == 1 and not ref $_[0]
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | # set the server as the first argument | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | return { server => $_[0] }; | |||||||||||||||||||||||||||||||||||||||||
| 61 | ▶ | } elsif ( @_ == 2 && ! ref $_[0] ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
@_ == 2 and not ref $_[0]
MC/DC0%0/2
@_ == 2 and not ref $_[0]
@_ == 2
not ref $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 2B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | # server/port argument | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return { server => $_[0], port => $_[1] }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # normal hash/hashref way | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | return $class->SUPER::BUILDARGS(@_); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | # Ok, get our socket and send off the PING! | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $sock = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | Proto => 'udp', | ||||||||||||||||||||||||||||||||||||||||||
| 76 | PeerPort => get_ac_pingport( $self->port ), | ||||||||||||||||||||||||||||||||||||||||||
| 77 | PeerAddr => $self->server, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ) or die "Could not create socket: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | binmode $sock, ":utf8" or die "Unable to set binmode: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # generate the PING packet | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # TODO support the EXT_XYZ options | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $pingpacket; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( $self->get_players ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | $pingpacket = tostr('1') . tostr('1'); | |||||||||||||||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $pingpacket = tostr('1') . tostr('0'); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # send it! | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $sock->send( $pingpacket ) or die "Unable to send: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | # set the alarm, and wait for the response | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my( $datagram, $result ); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 96 | # perldoc -f alarm says I need to put \n in the die... weird! | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | local $SIG{ALRM} = sub { die "alarm\n" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | alarm $self->timeout; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $result = $sock->recv( $datagram, 1024 ) or die "Unable to recv: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | alarm 0; | |||||||||||||||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( $@ =~ /^alarm/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | die "Unable to query server: Timed out"; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | die "Unable to query server: $@"; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ( defined $result and defined $datagram and length( $datagram ) > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $result and defined $datagram and length $datagram > 0
defined $result and defined $datagram
MC/DC0%0/3
defined $result and defined $datagram and length $datagram > 0
defined $result
defined $datagram
length $datagram > 0
Truth table0%0/4
A and B and C
A= defined $resultB= defined $datagramC= length $datagram > 0
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return Games::AssaultCube::ServerQuery::Response->new( $self, $datagram ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # from Moose::Manual::BestPractices | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | no Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||||||||||||||||