2021-02-11 17:51:12 +01:00
|
|
|
module unix
|
|
|
|
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import net
|
|
|
|
|
|
|
|
const (
|
|
|
|
unix_default_read_timeout = 30 * time.second
|
|
|
|
unix_default_write_timeout = 30 * time.second
|
|
|
|
connect_timeout = 5 * time.second
|
|
|
|
msg_nosignal = 0x4000
|
|
|
|
)
|
|
|
|
|
|
|
|
struct StreamSocket {
|
|
|
|
pub:
|
|
|
|
handle int
|
|
|
|
mut:
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamConn {
|
|
|
|
pub mut:
|
|
|
|
sock StreamSocket
|
|
|
|
mut:
|
|
|
|
write_deadline time.Time
|
|
|
|
read_deadline time.Time
|
|
|
|
read_timeout time.Duration
|
|
|
|
write_timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StreamListener {
|
|
|
|
pub mut:
|
|
|
|
sock StreamSocket
|
|
|
|
mut:
|
|
|
|
accept_timeout time.Duration
|
|
|
|
accept_deadline time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
fn error_code() int {
|
|
|
|
return C.errno
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_stream_socket() ?StreamSocket {
|
2021-06-13 22:53:38 +02:00
|
|
|
sockfd := net.socket_error(C.socket(net.AddrFamily.unix, net.SocketType.tcp, 0)) ?
|
2021-02-11 17:51:12 +01:00
|
|
|
mut s := StreamSocket{
|
|
|
|
handle: sockfd
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut s StreamSocket) close() ? {
|
|
|
|
return shutdown(s.handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut s StreamSocket) @select(test Select, timeout time.Duration) ?bool {
|
|
|
|
return @select(s.handle, test, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut s StreamSocket) connect(a string) ? {
|
2021-02-11 21:34:38 +01:00
|
|
|
if a.len >= max_sun_path {
|
|
|
|
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
mut addr := C.sockaddr_un{}
|
|
|
|
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
|
2022-04-15 13:58:56 +02:00
|
|
|
addr.sun_family = u8(C.AF_UNIX)
|
2021-04-14 11:47:24 +02:00
|
|
|
unsafe { C.strncpy(&addr.sun_path[0], &char(a.str), max_sun_path) }
|
2021-02-11 17:51:12 +01:00
|
|
|
size := C.SUN_LEN(&addr)
|
2021-06-13 22:53:38 +02:00
|
|
|
res := C.connect(s.handle, voidptr(&addr), size)
|
2021-02-11 17:51:12 +01:00
|
|
|
// if res != 1 {
|
|
|
|
// return none
|
|
|
|
//}
|
|
|
|
if res == 0 {
|
2021-06-13 22:53:38 +02:00
|
|
|
return
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
_ := error_code()
|
|
|
|
write_result := s.@select(.write, unix.connect_timeout) ?
|
|
|
|
if write_result {
|
|
|
|
// succeeded
|
2021-06-13 22:53:38 +02:00
|
|
|
return
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
except_result := s.@select(.except, unix.connect_timeout) ?
|
|
|
|
if except_result {
|
|
|
|
return net.err_connect_failed
|
|
|
|
}
|
|
|
|
// otherwise we timed out
|
|
|
|
return net.err_connect_timed_out
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listen_stream(sock string) ?&StreamListener {
|
2021-02-11 21:34:38 +01:00
|
|
|
if sock.len >= max_sun_path {
|
|
|
|
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
mut s := new_stream_socket() ?
|
|
|
|
s.path = sock
|
|
|
|
mut addr := C.sockaddr_un{}
|
|
|
|
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_un)) }
|
2022-04-15 13:58:56 +02:00
|
|
|
addr.sun_family = u8(C.AF_UNIX)
|
2021-04-14 11:47:24 +02:00
|
|
|
unsafe { C.strncpy(&addr.sun_path[0], &char(sock.str), max_sun_path) }
|
2021-02-11 17:51:12 +01:00
|
|
|
size := C.SUN_LEN(&addr)
|
2021-08-22 12:38:02 +02:00
|
|
|
if os.exists(sock) {
|
|
|
|
os.rm(sock) ?
|
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
net.socket_error(C.bind(s.handle, voidptr(&addr), size)) ?
|
2021-08-28 08:37:24 +02:00
|
|
|
os.chmod(sock, 0o777) ?
|
2021-02-11 17:51:12 +01:00
|
|
|
net.socket_error(C.listen(s.handle, 128)) ?
|
|
|
|
return &StreamListener{
|
|
|
|
sock: s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect_stream(path string) ?&StreamConn {
|
|
|
|
mut s := new_stream_socket() ?
|
|
|
|
s.connect(path) ?
|
|
|
|
return &StreamConn{
|
|
|
|
sock: s
|
|
|
|
read_timeout: unix.unix_default_read_timeout
|
|
|
|
write_timeout: unix.unix_default_write_timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut l StreamListener) accept() ?&StreamConn {
|
|
|
|
mut new_handle := C.accept(l.sock.handle, 0, 0)
|
|
|
|
if new_handle <= 0 {
|
|
|
|
l.wait_for_accept() ?
|
|
|
|
new_handle = C.accept(l.sock.handle, 0, 0)
|
|
|
|
if new_handle == -1 || new_handle == 0 {
|
2021-06-13 22:53:38 +02:00
|
|
|
return error('accept failed')
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
new_sock := StreamSocket{
|
|
|
|
handle: new_handle
|
|
|
|
}
|
|
|
|
return &StreamConn{
|
|
|
|
sock: new_sock
|
|
|
|
read_timeout: unix.unix_default_read_timeout
|
|
|
|
write_timeout: unix.unix_default_write_timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &StreamListener) accept_deadline() ?time.Time {
|
|
|
|
if c.accept_deadline.unix != 0 {
|
|
|
|
return c.accept_deadline
|
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
return error('no deadline')
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamListener) set_accept_deadline(deadline time.Time) {
|
|
|
|
c.accept_deadline = deadline
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &StreamListener) accept_timeout() time.Duration {
|
|
|
|
return c.accept_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamListener) set_accept_timeout(t time.Duration) {
|
|
|
|
c.accept_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamListener) wait_for_accept() ? {
|
|
|
|
return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamListener) close() ? {
|
2021-08-22 12:38:02 +02:00
|
|
|
os.rm(c.sock.path) ?
|
2021-02-11 17:51:12 +01:00
|
|
|
c.sock.close() ?
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) close() ? {
|
|
|
|
c.sock.close() ?
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_ptr blocks and attempts to write all data
|
2021-04-14 11:47:24 +02:00
|
|
|
pub fn (mut c StreamConn) write_ptr(b &byte, len int) ?int {
|
2021-02-11 17:51:12 +01:00
|
|
|
$if trace_unix ? {
|
|
|
|
eprintln(
|
|
|
|
'>>> StreamConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
|
|
|
unsafe { b.vstring_with_len(len) })
|
|
|
|
}
|
|
|
|
unsafe {
|
2022-04-15 13:58:56 +02:00
|
|
|
mut ptr_base := &u8(b)
|
2021-02-11 17:51:12 +01:00
|
|
|
mut total_sent := 0
|
|
|
|
for total_sent < len {
|
|
|
|
ptr := ptr_base + total_sent
|
|
|
|
remaining := len - total_sent
|
|
|
|
mut sent := C.send(c.sock.handle, ptr, remaining, unix.msg_nosignal)
|
|
|
|
if sent < 0 {
|
|
|
|
code := error_code()
|
2021-02-12 19:10:06 +01:00
|
|
|
if code == int(error_ewouldblock) {
|
2021-02-11 17:51:12 +01:00
|
|
|
c.wait_for_write() ?
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
net.wrap_error(code) ?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
total_sent += sent
|
|
|
|
}
|
2021-02-27 09:29:18 +01:00
|
|
|
return total_sent
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// write blocks and attempts to write all data
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn (mut c StreamConn) write(bytes []u8) ?int {
|
2021-02-11 17:51:12 +01:00
|
|
|
return c.write_ptr(bytes.data, bytes.len)
|
|
|
|
}
|
|
|
|
|
2021-03-20 17:25:51 +01:00
|
|
|
// write_string blocks and attempts to write all data
|
|
|
|
pub fn (mut c StreamConn) write_string(s string) ?int {
|
2021-02-11 17:51:12 +01:00
|
|
|
return c.write_ptr(s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2021-04-14 11:47:24 +02:00
|
|
|
pub fn (mut c StreamConn) read_ptr(buf_ptr &byte, len int) ?int {
|
2021-06-13 22:53:38 +02:00
|
|
|
mut res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)) ?
|
2021-02-11 17:51:12 +01:00
|
|
|
$if trace_unix ? {
|
|
|
|
eprintln('<<< StreamConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
|
|
|
}
|
|
|
|
if res > 0 {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
code := error_code()
|
2021-02-12 19:10:06 +01:00
|
|
|
if code == int(error_ewouldblock) {
|
2021-02-11 17:51:12 +01:00
|
|
|
c.wait_for_read() ?
|
2021-06-13 22:53:38 +02:00
|
|
|
res = wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0)) ?
|
2021-02-11 17:51:12 +01:00
|
|
|
$if trace_unix ? {
|
|
|
|
eprintln('<<< StreamConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
|
|
|
}
|
|
|
|
return net.socket_error(res)
|
|
|
|
} else {
|
|
|
|
net.wrap_error(code) ?
|
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
return net.socket_error(code)
|
2021-02-11 17:51:12 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:35:35 +02:00
|
|
|
pub fn (mut c StreamConn) read(mut buf []u8) ?int {
|
2021-02-11 17:51:12 +01:00
|
|
|
return c.read_ptr(buf.data, buf.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) read_deadline() ?time.Time {
|
|
|
|
if c.read_deadline.unix == 0 {
|
|
|
|
return c.read_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) set_read_deadline(deadline time.Time) {
|
|
|
|
c.read_deadline = deadline
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) write_deadline() ?time.Time {
|
|
|
|
if c.write_deadline.unix == 0 {
|
|
|
|
return c.write_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) set_write_deadline(deadline time.Time) {
|
|
|
|
c.write_deadline = deadline
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &StreamConn) read_timeout() time.Duration {
|
|
|
|
return c.read_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) set_read_timeout(t time.Duration) {
|
|
|
|
c.read_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c &StreamConn) write_timeout() time.Duration {
|
|
|
|
return c.write_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c StreamConn) set_write_timeout(t time.Duration) {
|
|
|
|
c.write_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn (mut c StreamConn) wait_for_read() ? {
|
|
|
|
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
pub fn (mut c StreamConn) wait_for_write() ? {
|
|
|
|
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (c StreamConn) str() string {
|
|
|
|
s := c.sock.str().replace('\n', ' ').replace(' ', ' ')
|
|
|
|
return 'StreamConn{ write_deadline: $c.write_deadline, read_deadline: $c.read_deadline, read_timeout: $c.read_timeout, write_timeout: $c.write_timeout, sock: $s }'
|
|
|
|
}
|