2020-08-20 23:01:37 +02:00
|
|
|
module net
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2020-12-15 16:39:11 +01:00
|
|
|
const (
|
|
|
|
tcp_default_read_timeout = 30 * time.second
|
|
|
|
tcp_default_write_timeout = 30 * time.second
|
|
|
|
)
|
|
|
|
|
2020-08-20 23:01:37 +02:00
|
|
|
pub struct TcpConn {
|
2021-01-20 11:11:01 +01:00
|
|
|
pub mut:
|
|
|
|
sock TcpSocket
|
2020-08-20 23:01:37 +02:00
|
|
|
mut:
|
|
|
|
write_deadline time.Time
|
2020-12-15 16:39:11 +01:00
|
|
|
read_deadline time.Time
|
|
|
|
read_timeout time.Duration
|
|
|
|
write_timeout time.Duration
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn dial_tcp(address string) ?&TcpConn {
|
|
|
|
mut s := new_tcp_socket() ?
|
2020-12-15 16:39:11 +01:00
|
|
|
s.connect(address) ?
|
2021-01-20 11:11:01 +01:00
|
|
|
return &TcpConn{
|
2020-08-20 23:01:37 +02:00
|
|
|
sock: s
|
2021-01-26 15:43:10 +01:00
|
|
|
read_timeout: net.tcp_default_read_timeout
|
|
|
|
write_timeout: net.tcp_default_write_timeout
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) close() ? {
|
2020-12-15 16:39:11 +01:00
|
|
|
c.sock.close() ?
|
2020-08-20 23:01:37 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_ptr blocks and attempts to write all data
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) write_ptr(b byteptr, len int) ? {
|
2020-12-29 14:41:46 +01:00
|
|
|
$if trace_tcp ? {
|
2021-01-26 15:43:10 +01:00
|
|
|
eprintln(
|
|
|
|
'>>> TcpConn.write_ptr | c.sock.handle: $c.sock.handle | b: ${ptr_str(b)} len: $len |\n' +
|
2020-12-29 16:51:10 +01:00
|
|
|
unsafe { b.vstring_with_len(len) })
|
2020-12-29 14:41:46 +01:00
|
|
|
}
|
2020-08-20 23:01:37 +02:00
|
|
|
unsafe {
|
|
|
|
mut ptr_base := byteptr(b)
|
|
|
|
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, msg_nosignal)
|
|
|
|
if sent < 0 {
|
|
|
|
code := error_code()
|
2021-01-08 17:41:52 +01:00
|
|
|
if code == int(error_ewouldblock) {
|
2021-01-26 15:43:10 +01:00
|
|
|
c.wait_for_write() ?
|
2021-01-08 17:41:52 +01:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
total_sent += sent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// write blocks and attempts to write all data
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) write(bytes []byte) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.write_ptr(bytes.data, bytes.len)
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:54:47 +01:00
|
|
|
// write_str blocks and attempts to write all data
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) write_str(s string) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.write_ptr(s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) read_ptr(buf_ptr byteptr, len int) ?int {
|
2020-12-15 16:39:11 +01:00
|
|
|
mut res := wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
|
2020-12-29 14:41:46 +01:00
|
|
|
$if trace_tcp ? {
|
|
|
|
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
if res > 0 {
|
2020-08-20 23:01:37 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
code := error_code()
|
2021-01-08 17:41:52 +01:00
|
|
|
if code == int(error_ewouldblock) {
|
|
|
|
c.wait_for_read() ?
|
|
|
|
res = wrap_read_result(C.recv(c.sock.handle, buf_ptr, len, 0)) ?
|
|
|
|
$if trace_tcp ? {
|
|
|
|
eprintln('<<< TcpConn.read_ptr | c.sock.handle: $c.sock.handle | buf_ptr: ${ptr_str(buf_ptr)} len: $len | res: $res')
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
2021-01-08 17:41:52 +01:00
|
|
|
return socket_error(res)
|
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
2021-01-12 11:43:55 +01:00
|
|
|
return none
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) read(mut buf []byte) ?int {
|
2020-11-15 21:54:47 +01:00
|
|
|
return c.read_ptr(buf.data, buf.len)
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) read_deadline() ?time.Time {
|
2020-08-20 23:01:37 +02:00
|
|
|
if c.read_deadline.unix == 0 {
|
|
|
|
return c.read_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c TcpConn) set_read_deadline(deadline time.Time) {
|
|
|
|
c.read_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) write_deadline() ?time.Time {
|
2020-08-20 23:01:37 +02:00
|
|
|
if c.write_deadline.unix == 0 {
|
|
|
|
return c.write_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c TcpConn) set_write_deadline(deadline time.Time) {
|
|
|
|
c.write_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpConn) read_timeout() time.Duration {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.read_timeout
|
|
|
|
}
|
|
|
|
|
2020-12-15 16:39:11 +01:00
|
|
|
pub fn (mut c TcpConn) set_read_timeout(t time.Duration) {
|
2020-08-20 23:01:37 +02:00
|
|
|
c.read_timeout = t
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpConn) write_timeout() time.Duration {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.write_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c TcpConn) set_write_timeout(t time.Duration) {
|
|
|
|
c.write_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) wait_for_read() ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpConn) wait_for_write() ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpConn) peer_addr() ?Addr {
|
2020-11-15 21:54:47 +01:00
|
|
|
mut addr := C.sockaddr{}
|
|
|
|
len := sizeof(C.sockaddr)
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.getpeername(c.sock.handle, &addr, &len)) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
return new_addr(addr)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpConn) peer_ip() ?string {
|
2020-11-15 21:54:47 +01:00
|
|
|
buf := [44]byte{}
|
|
|
|
peeraddr := C.sockaddr_in{}
|
|
|
|
speeraddr := sizeof(peeraddr)
|
2021-01-08 17:41:52 +01:00
|
|
|
socket_error(C.getpeername(c.sock.handle, unsafe { &C.sockaddr(&peeraddr) }, &speeraddr)) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
cstr := C.inet_ntop(C.AF_INET, &peeraddr.sin_addr, buf, sizeof(buf))
|
|
|
|
if cstr == 0 {
|
|
|
|
return error('net.peer_ip: inet_ntop failed')
|
|
|
|
}
|
|
|
|
res := cstring_to_vstring(cstr)
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:43:10 +01:00
|
|
|
pub fn (c TcpConn) str() string {
|
|
|
|
s := c.sock.str().replace('\n', ' ').replace(' ', ' ')
|
|
|
|
return 'TcpConn{ write_deadline: $c.write_deadline, read_deadline: $c.read_deadline, read_timeout: $c.read_timeout, write_timeout: $c.write_timeout, sock: $s }'
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TcpListener {
|
2021-01-20 11:11:01 +01:00
|
|
|
pub mut:
|
|
|
|
sock TcpSocket
|
2020-08-20 23:01:37 +02:00
|
|
|
mut:
|
2020-12-15 16:39:11 +01:00
|
|
|
accept_timeout time.Duration
|
2020-08-20 23:01:37 +02:00
|
|
|
accept_deadline time.Time
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn listen_tcp(port int) ?&TcpListener {
|
2020-12-15 16:39:11 +01:00
|
|
|
s := new_tcp_socket() ?
|
|
|
|
validate_port(port) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
mut addr := C.sockaddr_in{}
|
2021-01-08 17:41:52 +01:00
|
|
|
addr.sin_family = int(SocketFamily.inet)
|
2020-08-20 23:01:37 +02:00
|
|
|
addr.sin_port = C.htons(port)
|
|
|
|
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
|
|
|
|
size := sizeof(C.sockaddr_in)
|
|
|
|
// cast to the correct type
|
2021-01-08 17:41:52 +01:00
|
|
|
sockaddr := unsafe { &C.sockaddr(&addr) }
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.bind(s.handle, sockaddr, size)) ?
|
|
|
|
socket_error(C.listen(s.handle, 128)) ?
|
2021-01-20 11:11:01 +01:00
|
|
|
return &TcpListener{
|
2020-08-20 23:01:37 +02:00
|
|
|
sock: s
|
|
|
|
accept_deadline: no_deadline
|
2020-11-15 21:54:47 +01:00
|
|
|
accept_timeout: infinite_timeout
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut l TcpListener) accept() ?&TcpConn {
|
2020-08-20 23:01:37 +02:00
|
|
|
addr := C.sockaddr_storage{}
|
2020-12-29 14:41:46 +01:00
|
|
|
unsafe { C.memset(&addr, 0, sizeof(C.sockaddr_storage)) }
|
2020-08-20 23:01:37 +02:00
|
|
|
size := sizeof(C.sockaddr_storage)
|
|
|
|
// cast to correct type
|
2021-01-08 17:41:52 +01:00
|
|
|
sock_addr := unsafe { &C.sockaddr(&addr) }
|
2020-08-20 23:01:37 +02:00
|
|
|
mut new_handle := C.accept(l.sock.handle, sock_addr, &size)
|
|
|
|
if new_handle <= 0 {
|
2020-12-15 16:39:11 +01:00
|
|
|
l.wait_for_accept() ?
|
2020-08-20 23:01:37 +02:00
|
|
|
new_handle = C.accept(l.sock.handle, sock_addr, &size)
|
|
|
|
if new_handle == -1 || new_handle == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 16:39:11 +01:00
|
|
|
new_sock := tcp_socket_from_handle(new_handle) ?
|
2021-01-20 11:11:01 +01:00
|
|
|
return &TcpConn{
|
2020-08-20 23:01:37 +02:00
|
|
|
sock: new_sock
|
2021-01-26 15:43:10 +01:00
|
|
|
read_timeout: net.tcp_default_read_timeout
|
|
|
|
write_timeout: net.tcp_default_write_timeout
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpListener) accept_deadline() ?time.Time {
|
2020-08-20 23:01:37 +02:00
|
|
|
if c.accept_deadline.unix != 0 {
|
|
|
|
return c.accept_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c TcpListener) set_accept_deadline(deadline time.Time) {
|
|
|
|
c.accept_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpListener) accept_timeout() time.Duration {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.accept_timeout
|
|
|
|
}
|
|
|
|
|
2020-12-15 16:39:11 +01:00
|
|
|
pub fn (mut c TcpListener) set_accept_timeout(t time.Duration) {
|
2020-08-20 23:01:37 +02:00
|
|
|
c.accept_timeout = t
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpListener) wait_for_accept() ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return wait_for_read(c.sock.handle, c.accept_deadline, c.accept_timeout)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut c TcpListener) close() ? {
|
2020-12-15 16:39:11 +01:00
|
|
|
c.sock.close() ?
|
2020-08-20 23:01:37 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (c &TcpListener) address() ?Addr {
|
2020-08-20 23:01:37 +02:00
|
|
|
return c.sock.address()
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TcpSocket {
|
|
|
|
pub:
|
|
|
|
handle int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_tcp_socket() ?TcpSocket {
|
2020-12-15 16:39:11 +01:00
|
|
|
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.tcp, 0)) ?
|
2021-01-20 11:11:01 +01:00
|
|
|
mut s := TcpSocket{
|
2020-08-20 23:01:37 +02:00
|
|
|
handle: sockfd
|
|
|
|
}
|
2020-12-15 16:39:11 +01:00
|
|
|
// s.set_option_bool(.reuse_addr, true)?
|
|
|
|
s.set_option_int(.reuse_addr, 1) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
$if windows {
|
|
|
|
t := true
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
} $else {
|
2021-01-26 15:43:10 +01:00
|
|
|
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
|
2021-01-20 11:11:01 +01:00
|
|
|
mut s := TcpSocket{
|
2020-11-15 21:54:47 +01:00
|
|
|
handle: sockfd
|
|
|
|
}
|
2020-12-15 16:39:11 +01:00
|
|
|
// s.set_option_bool(.reuse_addr, true)?
|
|
|
|
s.set_option_int(.reuse_addr, 1) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
$if windows {
|
|
|
|
t := true
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
} $else {
|
2021-01-26 15:43:10 +01:00
|
|
|
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut s TcpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
// TODO reenable when this `in` operation works again
|
|
|
|
// if opt !in opts_can_set {
|
|
|
|
// return err_option_not_settable
|
|
|
|
// }
|
|
|
|
// if opt !in opts_bool {
|
|
|
|
// return err_option_wrong_type
|
|
|
|
// }
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (mut s TcpSocket) set_option_int(opt SocketOption, value int) ? {
|
2020-12-15 16:39:11 +01:00
|
|
|
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(int))) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
fn (mut s TcpSocket) close() ? {
|
2020-08-20 23:01:37 +02:00
|
|
|
return shutdown(s.handle)
|
|
|
|
}
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
fn (mut s TcpSocket) @select(test Select, timeout time.Duration) ?bool {
|
2020-08-20 23:01:37 +02:00
|
|
|
return @select(s.handle, test, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
connect_timeout = 5 * time.second
|
|
|
|
)
|
|
|
|
|
2021-01-20 11:11:01 +01:00
|
|
|
fn (mut s TcpSocket) connect(a string) ? {
|
2020-12-15 16:39:11 +01:00
|
|
|
addr := resolve_addr(a, .inet, .tcp) ?
|
2020-08-20 23:01:37 +02:00
|
|
|
res := C.connect(s.handle, &addr.addr, addr.len)
|
|
|
|
if res == 0 {
|
|
|
|
return none
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
_ := error_code()
|
2021-01-26 15:43:10 +01:00
|
|
|
write_result := s.@select(.write, net.connect_timeout) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
if write_result {
|
|
|
|
// succeeded
|
|
|
|
return none
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
2021-01-26 15:43:10 +01:00
|
|
|
except_result := s.@select(.except, net.connect_timeout) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
if except_result {
|
|
|
|
return err_connect_failed
|
|
|
|
}
|
|
|
|
// otherwise we timed out
|
|
|
|
return err_connect_timed_out
|
2020-08-20 23:01:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// address gets the address of a socket
|
2021-01-20 11:11:01 +01:00
|
|
|
pub fn (s &TcpSocket) address() ?Addr {
|
2020-08-20 23:01:37 +02:00
|
|
|
mut addr := C.sockaddr_in{}
|
|
|
|
size := sizeof(C.sockaddr_in)
|
|
|
|
// cast to the correct type
|
2021-01-08 17:41:52 +01:00
|
|
|
sockaddr := unsafe { &C.sockaddr(&addr) }
|
2020-08-20 23:01:37 +02:00
|
|
|
C.getsockname(s.handle, sockaddr, &size)
|
2020-11-15 21:54:47 +01:00
|
|
|
return new_addr(sockaddr)
|
2020-08-22 17:09:22 +02:00
|
|
|
}
|