net: fix non-blocking read/write (#20438)

This commit is contained in:
kbkpbot 2024-02-09 02:18:29 +08:00 committed by GitHub
parent 410bd9db71
commit a9ebab06da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 260 additions and 94 deletions

View file

@ -9,3 +9,20 @@ pub:
pub fn (s &Socket) address() !Addr {
return addr_from_socket_handle(s.handle)
}
// set_blocking will change the state of the socket to either blocking,
// when state is true, or non blocking (false).
pub fn set_blocking(handle int, state bool) ! {
$if windows {
t := if state { u32(0) } else { u32(1) }
socket_error(C.ioctlsocket(handle, fionbio, &t))!
} $else {
mut flags := C.fcntl(handle, C.F_GETFL, 0)
if state {
flags &= ~C.O_NONBLOCK
} else {
flags |= C.O_NONBLOCK
}
socket_error(C.fcntl(handle, C.F_SETFL, flags))!
}
}