Skip to content
Permalink
Browse files

Add type constraints to port numbers in IO::Socket::Async

Also remove todo to improve error message for invalid port numbers.
  • Loading branch information...
jmaslak committed Oct 29, 2018
1 parent d73272e commit 547f28baf614bb958751b033f0fb8d30639a6b63
Showing with 19 additions and 18 deletions.
  1. +13 −9 src/core/IO/Socket/Async.pm6
  2. +6 −9 t/05-messages/02-errors.t
@@ -8,11 +8,13 @@ my class IO::Socket::Async {
has $!close-promise;
has $!close-vow;

subset Port-Number of Int where { !defined($_) or $_ ~~ ^65536 };

has Str $.peer-host;
has Int $.peer-port;
has Port-Number $.peer-port;

has Str $.socket-host;
has Int $.socket-port;
has Port-Number $.socket-port;

method new() {
die "Cannot create an asynchronous socket directly; please use\n" ~
@@ -165,7 +167,7 @@ my class IO::Socket::Async {
$!close-vow.keep(True);
}

method connect(IO::Socket::Async:U: Str() $host, Int() $port,
method connect(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
@@ -290,8 +292,8 @@ my class IO::Socket::Async {
method serial(--> True) { }
}

method listen(IO::Socket::Async:U: Str() $host, Int() $port, Int() $backlog = 128,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
method listen(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
Int() $backlog = 128, :$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $encoding = Encoding::Registry.find($enc);
Supply.new: SocketListenerTappable.new:
:$host, :$port, :$backlog, :$encoding, :$scheduler
@@ -329,8 +331,8 @@ my class IO::Socket::Async {
await $p
}

method bind-udp(IO::Socket::Async:U: Str() $host, Int() $port, :$broadcast,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
method bind-udp(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
:$broadcast, :$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $encoding = Encoding::Registry.find($enc);
nqp::asyncudp(
@@ -355,11 +357,13 @@ my class IO::Socket::Async {
await $p
}

method print-to(IO::Socket::Async:D: Str() $host, Int() $port, Str() $str, :$scheduler = $*SCHEDULER) {
method print-to(IO::Socket::Async:D: Str() $host, Int() $port where * ~~ Port-Number,
Str() $str, :$scheduler = $*SCHEDULER) {
self.write-to($host, $port, $!encoder.encode-chars($str), :$scheduler)
}

method write-to(IO::Socket::Async:D: Str() $host, Int() $port, Blob $b, :$scheduler = $*SCHEDULER) {
method write-to(IO::Socket::Async:D: Str() $host, Int() $port where * ~~ Port-Number,
Blob $b, :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
nqp::asyncwritebytesto(
@@ -16,15 +16,12 @@ throws-like { for [:a] X [:b] -> ($i, $j) { } },
message => / '<anon>' /,
"anonymous subs get '<anon>' in arity error messages";

todo 'needs better error message';

skip 'crashes: https://github.com/rakudo/rakudo/issues/2402';
#throws-like {
# sub l { IO::Socket::Async.listen: "localhost", 111390 }
# react whenever l() {
# whenever l() {} # try to listen on already open sock
# }
#}, X::AdHoc, message => /'something good'/;
throws-like {
sub l { IO::Socket::Async.listen: "localhost", 111390 }
react whenever l() {
whenever l() {} # try to listen on already open sock
}
}, X::TypeCheck::Binding::Parameter, message => /'type check failed'/;

# RT #132283
is-deeply class { has $.bar }.^methods».name.sort, <BUILDALL bar>,

2 comments on commit 547f28b

@b2gills

This comment has been minimized.

Copy link
Contributor

replied Nov 10, 2018

Int() $port where * ~~ Port-Number is probably better written as Int() $port where Port-Number as the where clause already does smartmatching.

@jmaslak

This comment has been minimized.

Copy link
Contributor Author

replied Nov 10, 2018

@b2gills - I put your suggestion in #2481

Please sign in to comment.
You can’t perform that action at this time.