net: support `-d net_blocking_sockets` (workaround a sporadic vex failure)

pull/9862/head
Delyan Angelov 2021-04-23 17:12:13 +03:00
parent 6f50157abc
commit a832bb609a
2 changed files with 27 additions and 21 deletions

View File

@ -298,16 +298,18 @@ fn new_tcp_socket() ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: sockfd handle: sockfd
} }
$if trace_tcp ? {
eprintln(' new_tcp_socket | s.handle: ${s.handle:6}')
}
// s.set_option_bool(.reuse_addr, true)? // s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1) ? s.set_option_int(.reuse_addr, 1) ?
$if !net_blocking_sockets ? {
$if windows { $if windows {
t := u32(1) // true t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? 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}')
} }
return s return s
} }
@ -316,16 +318,18 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: sockfd handle: sockfd
} }
$if trace_tcp ? {
eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}')
}
// s.set_option_bool(.reuse_addr, true)? // s.set_option_bool(.reuse_addr, true)?
s.set_option_int(.reuse_addr, 1) ? s.set_option_int(.reuse_addr, 1) ?
$if !net_blocking_sockets ? {
$if windows { $if windows {
t := u32(1) // true t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK)) ? 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}')
} }
return s return s
} }

View File

@ -185,12 +185,14 @@ fn new_udp_socket(local_port int) ?&UdpSocket {
handle: sockfd handle: sockfd
} }
s.set_option_bool(.reuse_addr, true) ? s.set_option_bool(.reuse_addr, true) ?
$if !net_blocking_sockets ? {
$if windows { $if windows {
t := u32(1) // true t := u32(1) // true
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
} $else { } $else {
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ? socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ?
} }
}
// In UDP we always have to bind to a port // In UDP we always have to bind to a port
validate_port(local_port) ? validate_port(local_port) ?
mut addr := C.sockaddr_in{} mut addr := C.sockaddr_in{}