2021-02-11 19:51:12 +03:00
|
|
|
module unix
|
|
|
|
|
|
|
|
import time
|
|
|
|
import net
|
|
|
|
|
2023-01-25 13:32:05 +03:00
|
|
|
const error_ewouldblock = C.EWOULDBLOCK
|
2021-02-12 21:10:06 +03:00
|
|
|
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.SUN_LEN(ptr &C.sockaddr_un) int
|
2021-02-11 19:51:12 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
fn C.strncpy(&char, &char, int)
|
2021-02-11 19:51:12 +03:00
|
|
|
|
2023-01-25 13:32:05 +03:00
|
|
|
// shutdown shutsdown a socket, given its file descriptor `handle`.
|
|
|
|
// By default it shuts it down in both directions, both for reading
|
|
|
|
// and for writing. You can change that using `net.shutdown(handle, how: .read)`
|
|
|
|
// or `net.shutdown(handle, how: .write)`
|
|
|
|
pub fn shutdown(handle int, config net.ShutdownConfig) int {
|
|
|
|
return net.shutdown(handle, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close a socket, given its file descriptor `handle`.
|
|
|
|
pub fn close(handle int) ! {
|
|
|
|
net.close(handle)!
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Select waits for an io operation (specified by parameter `test`) to be available
|
2022-10-16 09:28:57 +03:00
|
|
|
fn @select(handle int, test Select, timeout time.Duration) !bool {
|
2021-02-11 19:51:12 +03:00
|
|
|
set := C.fd_set{}
|
|
|
|
|
|
|
|
C.FD_ZERO(&set)
|
|
|
|
C.FD_SET(handle, &set)
|
|
|
|
|
2021-06-22 12:17:44 +03:00
|
|
|
seconds := timeout / time.second
|
|
|
|
microseconds := time.Duration(timeout - (seconds * time.second)).microseconds()
|
2021-02-11 19:51:12 +03: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
|
|
|
|
if timeout == unix.infinite_timeout {
|
|
|
|
timeval_timeout = &C.timeval(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
match test {
|
|
|
|
.read {
|
2022-10-16 09:28:57 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout))!
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
.write {
|
2022-10-16 09:28:57 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout))!
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
.except {
|
2022-10-16 09:28:57 +03:00
|
|
|
net.socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout))!
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.FD_ISSET(handle, &set)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait_for_common wraps the common wait code
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wait_for_common(handle int, deadline time.Time, timeout time.Duration, test Select) ! {
|
2021-02-11 19:51:12 +03:00
|
|
|
if deadline.unix == 0 {
|
2021-06-22 12:17:44 +03:00
|
|
|
// do not accept negative timeout
|
|
|
|
if timeout < 0 {
|
2021-02-11 19:51:12 +03:00
|
|
|
return net.err_timed_out
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
ready := @select(handle, test, timeout)!
|
2021-02-11 19:51:12 +03:00
|
|
|
if ready {
|
2021-06-13 23:53:38 +03:00
|
|
|
return
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
return net.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 net.err_timed_out
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
ready := @select(handle, test, d_timeout)!
|
2021-02-11 19:51:12 +03:00
|
|
|
if ready {
|
2021-06-13 23:53:38 +03:00
|
|
|
return
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|
|
|
|
return net.err_timed_out
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait_for_write waits for a write io operation to be available
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wait_for_write(handle int, deadline time.Time, timeout time.Duration) ! {
|
2021-02-11 19:51:12 +03:00
|
|
|
return wait_for_common(handle, deadline, timeout, .write)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait_for_read waits for a read io operation to be available
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wait_for_read(handle int, deadline time.Time, timeout time.Duration) ! {
|
2021-02-11 19:51:12 +03:00
|
|
|
return wait_for_common(handle, deadline, timeout, .read)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
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)
|
|
|
|
const (
|
2021-06-22 12:17:44 +03:00
|
|
|
infinite_timeout = time.infinite
|
2021-02-11 19:51:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
[inline]
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wrap_read_result(result int) !int {
|
2021-02-11 19:51:12 +03:00
|
|
|
if result != 0 {
|
|
|
|
return result
|
|
|
|
}
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2021-02-11 19:51:12 +03:00
|
|
|
}
|