Skip to content

Commit

Permalink
Type IO::Socket::INET family/type/protocol values
Browse files Browse the repository at this point in the history
This changes the type of IO::Socket::INET.new's :$family parameter from
Int to ProtocolFamily:D(Int:D) (removing their invalid Int:U typing) and
gives the class' $!family/$!type/$!proto attributes explicit
ProtocolFamily:D/SocketType:D/ProtocolType:D typings (meaning they no
longer accept raw Int:D values corresponding to those types' enum
values).
  • Loading branch information
Kaiepi committed Nov 19, 2020
1 parent e074f3c commit 534cc54
Showing 1 changed file with 22 additions and 40 deletions.
62 changes: 22 additions & 40 deletions src/core.c/IO/Socket/INET.pm6
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
my class IO::Socket::INET does IO::Socket {
my module PIO {
constant SOCK_PACKET = 0;
constant SOCK_STREAM = 1;
constant SOCK_DGRAM = 2;
constant SOCK_RAW = 3;
constant SOCK_RDM = 4;
constant SOCK_SEQPACKET = 5;
constant SOCK_MAX = 6;
constant PROTO_TCP = 6;
constant PROTO_UDP = 17;
constant MIN_PORT = 0;
constant MAX_PORT = 65_535; # RFC 793: TCP/UDP port limit
constant MIN_PORT = 0;
constant MAX_PORT = 65_535; # RFC 793: TCP/UDP port limit
}

has ProtocolFamily:D $.family = PF_UNSPEC;
has SocketType:D $.type = SOCK_STREAM;
has ProtocolType:D $.proto = PROTO_TCP;

has Str $.host;
has Int $.port;
has Str $.localhost;
has Int $.localport;
has Int $.backlog;
has Bool $.listening;
has $.family = nqp::const::SOCKET_FAMILY_UNSPEC;
has $.proto = PIO::PROTO_TCP;
has $.type = PIO::SOCK_STREAM;

# XXX: this could be a bit smarter about how it deals with unspecified
# families...
my sub split-host-port(:$host is copy, :$port is copy, :$family) {
if ($host) {
my ($split-host, $split-port) = $family == nqp::const::SOCKET_FAMILY_INET6
my ($split-host, $split-port) = $family == PF_INET6
?? v6-split($host)
!! v4-split($host);

Expand All @@ -54,21 +46,16 @@ my class IO::Socket::INET does IO::Socket {

# Create new socket that listens on $localhost:$localport
multi method new(
Bool :$listen! where .so,
Str :$localhost is copy,
Int :$localport is copy,
Int :$family where {
$family == nqp::const::SOCKET_FAMILY_UNSPEC
|| $family == nqp::const::SOCKET_FAMILY_INET
|| $family == nqp::const::SOCKET_FAMILY_INET6
|| $family == nqp::const::SOCKET_FAMILY_UNIX
} = nqp::const::SOCKET_FAMILY_UNSPEC,
*%rest,
Bool :$listen! where .so,
Str :$localhost is copy,
Int :$localport is copy,
ProtocolFamily:D(Int:D) :$family = PF_UNSPEC,
*%rest,
--> IO::Socket::INET:D) {

($localhost, $localport) = (
split-host-port :host($localhost), :port($localport), :$family
orelse fail $_) unless $family == nqp::const::SOCKET_FAMILY_UNIX;
orelse fail $_) unless $family == PF_UNIX;

#TODO: Learn what protocols map to which socket types and then determine which is needed.
self.bless(
Expand All @@ -82,22 +69,17 @@ my class IO::Socket::INET does IO::Socket {

# Open new connection to socket on $host:$port
multi method new(
Str:D :$host! is copy,
Int :$port is copy,
Int :$family where {
$family == nqp::const::SOCKET_FAMILY_UNSPEC
|| $family == nqp::const::SOCKET_FAMILY_INET
|| $family == nqp::const::SOCKET_FAMILY_INET6
|| $family == nqp::const::SOCKET_FAMILY_UNIX
} = nqp::const::SOCKET_FAMILY_UNSPEC,
*%rest,
Str:D :$host! is copy,
Int :$port is copy,
ProtocolFamily:D(Int:D) :$family = PF_UNSPEC,
*%rest,
--> IO::Socket::INET:D) {

($host, $port) = split-host-port(
:$host,
:$port,
:$family,
) unless $family == nqp::const::SOCKET_FAMILY_UNIX;
) unless $family == PF_UNIX;

# TODO: Learn what protocols map to which socket types and then determine which is needed.
self.bless(
Expand All @@ -122,18 +104,18 @@ my class IO::Socket::INET does IO::Socket {
# which is derived from the protocol, is SOCK_STREAM then connect() is called.
if $!listening || $!localhost || $!localport {
nqp::bindsock($PIO, nqp::unbox_s($!localhost || "0.0.0.0"),
nqp::unbox_i($!localport || 0), nqp::unbox_i($!family),
nqp::unbox_i($!localport || 0), nqp::decont_i($!family),
nqp::unbox_i($!backlog || 128));
}

if $!listening {
#?if !js
$!localport = nqp::getport($PIO)
unless $!localport || ($!family == nqp::const::SOCKET_FAMILY_UNIX);
unless $!localport || ($!family == PF_UNIX);
#?endif
}
elsif $!type == PIO::SOCK_STREAM {
nqp::connect($PIO, nqp::unbox_s($!host), nqp::unbox_i($!port), nqp::unbox_i($!family));
elsif $!type == SOCK_STREAM {
nqp::connect($PIO, nqp::unbox_s($!host), nqp::unbox_i($!port), nqp::decont_i($!family));
}

nqp::bindattr(self, $?CLASS, '$!PIO', $PIO);
Expand Down

0 comments on commit 534cc54

Please sign in to comment.