2020-08-21 00:01:37 +03:00
|
|
|
module net
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
2020-12-15 19:31:39 +03:00
|
|
|
const (
|
2021-02-02 11:36:51 +03:00
|
|
|
udp_default_read_timeout = time.second / 10
|
|
|
|
udp_default_write_timeout = time.second / 10
|
2020-12-15 19:31:39 +03:00
|
|
|
)
|
|
|
|
|
2021-02-02 11:36:51 +03:00
|
|
|
struct UdpSocket {
|
|
|
|
handle int
|
|
|
|
l Addr
|
|
|
|
r ?Addr
|
|
|
|
}
|
|
|
|
|
2020-08-21 00:01:37 +03:00
|
|
|
pub struct UdpConn {
|
2021-01-20 13:11:01 +03:00
|
|
|
pub mut:
|
|
|
|
sock UdpSocket
|
2020-08-21 00:01:37 +03:00
|
|
|
mut:
|
|
|
|
write_deadline time.Time
|
2021-01-08 19:41:52 +03:00
|
|
|
read_deadline time.Time
|
|
|
|
read_timeout time.Duration
|
|
|
|
write_timeout time.Duration
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn dial_udp(laddr string, raddr string) ?&UdpConn {
|
2021-01-08 19:41:52 +03:00
|
|
|
local := resolve_addr(laddr, .inet, .udp) ?
|
|
|
|
sbase := new_udp_socket(local.port) ?
|
|
|
|
sock := UdpSocket{
|
2020-08-21 00:01:37 +03:00
|
|
|
handle: sbase.handle
|
|
|
|
l: local
|
|
|
|
r: resolve_wrapper(raddr)
|
|
|
|
}
|
2021-01-20 13:11:01 +03:00
|
|
|
return &UdpConn{
|
2020-08-31 23:17:59 +03:00
|
|
|
sock: sock
|
2021-01-26 17:43:10 +03:00
|
|
|
read_timeout: net.udp_default_read_timeout
|
|
|
|
write_timeout: net.udp_default_write_timeout
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:11:05 +03:00
|
|
|
fn resolve_wrapper(raddr string) ?Addr {
|
|
|
|
// Dont have to do this when its fixed
|
|
|
|
// this just allows us to store this `none` optional in a struct
|
|
|
|
x := resolve_addr(raddr, .inet, .udp) or { return none }
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write_ptr(b byteptr, len int) ? {
|
2021-01-08 19:41:52 +03:00
|
|
|
remote := c.sock.remote() or { return err_no_udp_remote }
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(remote, b, len)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write(buf []byte) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_ptr(buf.data, buf.len)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write_str(s string) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_ptr(s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write_to_ptr(addr Addr, b byteptr, len int) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
res := C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)
|
|
|
|
if res >= 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
code := error_code()
|
2021-01-08 19:41:52 +03:00
|
|
|
if code == int(error_ewouldblock) {
|
|
|
|
c.wait_for_write() ?
|
|
|
|
socket_error(C.sendto(c.sock.handle, b, len, 0, &addr.addr, addr.len)) ?
|
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_to blocks and writes the buf to the remote addr specified
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write_to(addr Addr, buf []byte) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(addr, buf.data, buf.len)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write_to_string blocks and writes the buf to the remote addr specified
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) write_to_string(addr Addr, s string) ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_to_ptr(addr, s.str, s.len)
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
// read reads from the socket into buf up to buf.len returning the number of bytes read
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) read(mut buf []byte) ?(int, Addr) {
|
2020-08-21 00:01:37 +03:00
|
|
|
mut addr_from := C.sockaddr{}
|
|
|
|
len := sizeof(C.sockaddr)
|
2021-01-08 19:41:52 +03:00
|
|
|
mut res := wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from,
|
|
|
|
&len)) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
if res > 0 {
|
2021-01-08 19:41:52 +03:00
|
|
|
addr := new_addr(addr_from) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
return res, addr
|
|
|
|
}
|
|
|
|
code := error_code()
|
2021-01-08 19:41:52 +03:00
|
|
|
if code == int(error_ewouldblock) {
|
|
|
|
c.wait_for_read() ?
|
|
|
|
// same setup as in tcp
|
|
|
|
res = wrap_read_result(C.recvfrom(c.sock.handle, buf.data, buf.len, 0, &addr_from,
|
|
|
|
&len)) ?
|
|
|
|
res2 := socket_error(res) ?
|
|
|
|
addr := new_addr(addr_from) ?
|
|
|
|
return res2, addr
|
|
|
|
} else {
|
|
|
|
wrap_error(code) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) read_deadline() ?time.Time {
|
2020-08-21 00:01:37 +03:00
|
|
|
if c.read_deadline.unix == 0 {
|
|
|
|
return c.read_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_read_deadline(deadline time.Time) {
|
|
|
|
c.read_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) write_deadline() ?time.Time {
|
2020-08-21 00:01:37 +03:00
|
|
|
if c.write_deadline.unix == 0 {
|
|
|
|
return c.write_deadline
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_write_deadline(deadline time.Time) {
|
|
|
|
c.write_deadline = deadline
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) read_timeout() time.Duration {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.read_timeout
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:41:52 +03:00
|
|
|
pub fn (mut c UdpConn) set_read_timeout(t time.Duration) {
|
2020-08-21 00:01:37 +03:00
|
|
|
c.read_timeout = t
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) write_timeout() time.Duration {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.write_timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut c UdpConn) set_write_timeout(t time.Duration) {
|
|
|
|
c.write_timeout = t
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) wait_for_read() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return wait_for_read(c.sock.handle, c.read_deadline, c.read_timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) wait_for_write() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return wait_for_write(c.sock.handle, c.write_deadline, c.write_timeout)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (c &UdpConn) str() string {
|
2020-08-21 00:01:37 +03:00
|
|
|
// TODO
|
|
|
|
return 'UdpConn'
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut c UdpConn) close() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return c.sock.close()
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn listen_udp(port int) ?&UdpConn {
|
2021-01-08 19:41:52 +03:00
|
|
|
s := new_udp_socket(port) ?
|
2021-01-20 13:11:01 +03:00
|
|
|
return &UdpConn{
|
2020-08-21 00:01:37 +03:00
|
|
|
sock: s
|
2021-01-26 17:43:10 +03:00
|
|
|
read_timeout: net.udp_default_read_timeout
|
|
|
|
write_timeout: net.udp_default_write_timeout
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn new_udp_socket(local_port int) ?&UdpSocket {
|
2021-01-08 19:41:52 +03:00
|
|
|
sockfd := socket_error(C.socket(SocketFamily.inet, SocketType.udp, 0)) ?
|
2021-01-20 13:11:01 +03:00
|
|
|
mut s := &UdpSocket{
|
2020-08-21 00:01:37 +03:00
|
|
|
handle: sockfd
|
|
|
|
}
|
2021-01-08 19:41:52 +03:00
|
|
|
s.set_option_bool(.reuse_addr, true) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
$if windows {
|
|
|
|
t := true
|
2021-01-08 19:41:52 +03:00
|
|
|
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
} $else {
|
2021-01-26 17:43:10 +03:00
|
|
|
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
// In UDP we always have to bind to a port
|
2021-01-08 19:41:52 +03:00
|
|
|
validate_port(local_port) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
mut addr := C.sockaddr_in{}
|
2021-01-08 19:41:52 +03:00
|
|
|
addr.sin_family = int(SocketFamily.inet)
|
2020-08-21 00:01:37 +03:00
|
|
|
addr.sin_port = C.htons(local_port)
|
|
|
|
addr.sin_addr.s_addr = C.htonl(C.INADDR_ANY)
|
|
|
|
size := sizeof(C.sockaddr_in)
|
|
|
|
// cast to the correct type
|
2021-01-08 19:41:52 +03:00
|
|
|
sockaddr := unsafe { &C.sockaddr(&addr) }
|
|
|
|
socket_error(C.bind(s.handle, sockaddr, size)) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (s &UdpSocket) remote() ?Addr {
|
2020-08-21 00:01:37 +03:00
|
|
|
return s.r
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
pub fn (mut s UdpSocket) set_option_bool(opt SocketOption, value bool) ? {
|
2020-08-21 00:01:37 +03: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
|
|
|
|
// }
|
2021-01-08 19:41:52 +03:00
|
|
|
socket_error(C.setsockopt(s.handle, C.SOL_SOCKET, int(opt), &value, sizeof(bool))) ?
|
2020-08-21 00:01:37 +03:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn (mut s UdpSocket) close() ? {
|
2020-08-21 00:01:37 +03:00
|
|
|
return shutdown(s.handle)
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:11:01 +03:00
|
|
|
fn (mut s UdpSocket) @select(test Select, timeout time.Duration) ?bool {
|
2020-08-21 00:01:37 +03:00
|
|
|
return @select(s.handle, test, timeout)
|
|
|
|
}
|