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