From a832bb609a45a2b62c1fc685a85aa637ee483e77 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 23 Apr 2021 17:12:13 +0300 Subject: [PATCH] net: support `-d net_blocking_sockets` (workaround a sporadic vex failure) --- vlib/net/tcp.v | 36 ++++++++++++++++++++---------------- vlib/net/udp.v | 12 +++++++----- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/vlib/net/tcp.v b/vlib/net/tcp.v index 28bc992993..39e61fe467 100644 --- a/vlib/net/tcp.v +++ b/vlib/net/tcp.v @@ -298,17 +298,19 @@ fn new_tcp_socket() ?TcpSocket { mut s := TcpSocket{ handle: sockfd } - // s.set_option_bool(.reuse_addr, true)? - s.set_option_int(.reuse_addr, 1) ? - $if windows { - t := u32(1) // true - socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? - } $else { - socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? - } $if trace_tcp ? { eprintln(' new_tcp_socket | s.handle: ${s.handle:6}') } + // s.set_option_bool(.reuse_addr, true)? + s.set_option_int(.reuse_addr, 1) ? + $if !net_blocking_sockets ? { + $if windows { + t := u32(1) // true + socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? + } $else { + socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? + } + } return s } @@ -316,17 +318,19 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket { mut s := TcpSocket{ handle: sockfd } - // s.set_option_bool(.reuse_addr, true)? - s.set_option_int(.reuse_addr, 1) ? - $if windows { - t := u32(1) // true - socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? - } $else { - socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? - } $if trace_tcp ? { eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}') } + // s.set_option_bool(.reuse_addr, true)? + s.set_option_int(.reuse_addr, 1) ? + $if !net_blocking_sockets ? { + $if windows { + t := u32(1) // true + socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? + } $else { + socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? + } + } return s } diff --git a/vlib/net/udp.v b/vlib/net/udp.v index f0e81734b2..9e4976eb0d 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -185,11 +185,13 @@ fn new_udp_socket(local_port int) ?&UdpSocket { handle: sockfd } s.set_option_bool(.reuse_addr, true) ? - $if windows { - t := u32(1) // true - socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? - } $else { - socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ? + $if !net_blocking_sockets ? { + $if windows { + t := u32(1) // true + socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? + } $else { + socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ? + } } // In UDP we always have to bind to a port validate_port(local_port) ?