2020-08-20 23:01:37 +02:00
|
|
|
module net
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
// no_deadline should be given to functions when no deadline is wanted (i.e. all functions
|
|
|
|
// return instantly)
|
|
|
|
const no_deadline = time.Time{
|
|
|
|
unix: 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// no_timeout should be given to functions when no timeout is wanted (i.e. all functions
|
|
|
|
// return instantly)
|
|
|
|
pub const no_timeout = time.Duration(0)
|
|
|
|
|
|
|
|
// infinite_timeout should be given to functions when an infinite_timeout is wanted (i.e. functions
|
|
|
|
// only ever return with data)
|
2021-06-22 11:17:44 +02:00
|
|
|
pub const infinite_timeout = time.infinite
|
2021-06-13 22:53:38 +02:00
|
|
|
|
2020-08-20 23:01:37 +02:00
|
|
|
// Shutdown shutsdown a socket and closes it
|
|
|
|
fn shutdown(handle int) ? {
|
|
|
|
$if windows {
|
|
|
|
C.shutdown(handle, C.SD_BOTH)
|
2021-02-11 17:51:12 +01:00
|
|
|
socket_error(C.closesocket(handle)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
} $else {
|
|
|
|
C.shutdown(handle, C.SHUT_RDWR)
|
2021-02-11 17:51:12 +01:00
|
|
|
socket_error(C.close(handle)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select waits for an io operation (specified by parameter `test`) to be available
|
|
|
|
fn @select(handle int, test Select, timeout time.Duration) ?bool {
|
|
|
|
set := C.fd_set{}
|
|
|
|
|
|
|
|
C.FD_ZERO(&set)
|
|
|
|
C.FD_SET(handle, &set)
|
|
|
|
|
2021-06-22 11:17:44 +02:00
|
|
|
seconds := timeout / time.second
|
2021-06-13 22:53:38 +02:00
|
|
|
microseconds := time.Duration(timeout - (seconds * time.second)).microseconds()
|
2020-08-20 23:01:37 +02:00
|
|
|
|
|
|
|
mut tt := C.timeval{
|
|
|
|
tv_sec: u64(seconds)
|
|
|
|
tv_usec: u64(microseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
mut timeval_timeout := &tt
|
|
|
|
|
|
|
|
// infinite timeout is signaled by passing null as the timeout to
|
|
|
|
// select
|
2021-02-11 17:51:12 +01:00
|
|
|
if timeout == net.infinite_timeout {
|
2020-08-20 23:01:37 +02:00
|
|
|
timeval_timeout = &C.timeval(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
match test {
|
|
|
|
.read {
|
2021-02-11 17:51:12 +01:00
|
|
|
socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
.write {
|
2021-02-11 17:51:12 +01:00
|
|
|
socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
.except {
|
2021-02-11 17:51:12 +01:00
|
|
|
socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.FD_ISSET(handle, &set)
|
|
|
|
}
|
|
|
|
|
2021-06-16 03:07:24 +02:00
|
|
|
// select_with_retry will retry the select if select is failing
|
|
|
|
// due to interrupted system call. This can happen on signals
|
|
|
|
// for example the GC Boehm uses signals internally on garbage
|
|
|
|
// collection
|
|
|
|
[inline]
|
|
|
|
fn select_with_retry(handle int, test Select, timeout time.Duration) ?bool {
|
2021-06-17 09:41:26 +02:00
|
|
|
mut retries := 10
|
2021-06-16 03:07:24 +02:00
|
|
|
for retries > 0 {
|
|
|
|
ready := @select(handle, test, timeout) or {
|
|
|
|
if err.code == 4 {
|
2021-06-17 09:41:26 +02:00
|
|
|
// signal! lets retry max 10 times
|
|
|
|
// suspend thread with sleep to let the gc get
|
|
|
|
// cycles in the case the Bohem gc is interupting
|
|
|
|
time.sleep(1 * time.millisecond)
|
2021-06-16 03:07:24 +02:00
|
|
|
retries -= 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// we got other error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ready
|
|
|
|
}
|
|
|
|
return error('failed to @select more that three times due to interrupted system call')
|
|
|
|
}
|
|
|
|
|
2020-08-20 23:01:37 +02:00
|
|
|
// wait_for_common wraps the common wait code
|
2021-02-11 17:51:12 +01:00
|
|
|
fn wait_for_common(handle int, deadline time.Time, timeout time.Duration, test Select) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
if deadline.unix == 0 {
|
2021-06-22 11:17:44 +02:00
|
|
|
// do not accept negative timeout
|
|
|
|
if timeout < 0 {
|
2020-08-20 23:01:37 +02:00
|
|
|
return err_timed_out
|
|
|
|
}
|
2021-06-16 03:07:24 +02:00
|
|
|
ready := select_with_retry(handle, test, timeout) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
if ready {
|
2021-06-13 22:53:38 +02:00
|
|
|
return
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
return err_timed_out
|
|
|
|
}
|
|
|
|
// Convert the deadline into a timeout
|
|
|
|
// and use that
|
|
|
|
d_timeout := deadline.unix - time.now().unix
|
|
|
|
if d_timeout < 0 {
|
|
|
|
// deadline is in the past so this has already
|
|
|
|
// timed out
|
|
|
|
return err_timed_out
|
|
|
|
}
|
2021-06-16 03:07:24 +02:00
|
|
|
ready := select_with_retry(handle, test, timeout) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
if ready {
|
2021-06-13 22:53:38 +02:00
|
|
|
return
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
return err_timed_out
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait_for_write waits for a write io operation to be available
|
2021-02-11 17:51:12 +01:00
|
|
|
fn wait_for_write(handle int, deadline time.Time, timeout time.Duration) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return wait_for_common(handle, deadline, timeout, .write)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait_for_read waits for a read io operation to be available
|
2021-02-11 17:51:12 +01:00
|
|
|
fn wait_for_read(handle int, deadline time.Time, timeout time.Duration) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return wait_for_common(handle, deadline, timeout, .read)
|
|
|
|
}
|