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,17 +298,19 @@ fn new_tcp_socket() ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: sockfd 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 ? { $if trace_tcp ? {
eprintln(' new_tcp_socket | s.handle: ${s.handle:6}') 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 return s
} }
@ -316,17 +318,19 @@ fn tcp_socket_from_handle(sockfd int) ?TcpSocket {
mut s := TcpSocket{ mut s := TcpSocket{
handle: sockfd 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 ? { $if trace_tcp ? {
eprintln(' tcp_socket_from_handle | s.handle: ${s.handle:6}') 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 return s
} }

View File

@ -185,11 +185,13 @@ 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 windows { $if !net_blocking_sockets ? {
t := u32(1) // true $if windows {
socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ? t := u32(1) // true
} $else { socket_error(C.ioctlsocket(sockfd, fionbio, &t)) ?
socket_error(C.fcntl(sockfd, C.F_SETFD, C.O_NONBLOCK)) ? } $else {
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) ?