Skip to content

Commit

Permalink
fix for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 committed Nov 10, 2023
1 parent acbba3d commit d4b3056
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions vlib/net/net_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module net

const is_windows = false

fn error_code() int {
pub fn error_code() int {
return C.errno
}

Expand All @@ -24,7 +24,7 @@ pub const (
msg_nosignal = 0x4000
)

const (
pub const (
error_ewouldblock = C.EWOULDBLOCK
error_einprogress = C.EINPROGRESS
)
Expand Down
4 changes: 2 additions & 2 deletions vlib/net/net_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ pub fn wsa_error(code int) WsaError {
return unsafe { WsaError(code) }
}

const (
pub const (
error_ewouldblock = WsaError.wsaewouldblock
error_einprogress = WsaError.wsaeinprogress
)
Expand All @@ -757,7 +757,7 @@ const (
)

// Error code returns the last socket error
fn error_code() int {
pub fn error_code() int {
return C.WSAGetLastError()
}

Expand Down
6 changes: 0 additions & 6 deletions vlib/net/unix/common.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@ const (
infinite_timeout = time.infinite
)

const error_ewouldblock = C.EWOULDBLOCK

fn C.strncpy(&char, &char, int)

// close a socket, given its file descriptor `handle`.
pub fn close(handle int) ! {
net.close(handle)!
}

fn error_code() int {
return C.errno
}

// shutdown shutsdown a socket, given its file descriptor `handle`.
// By default it shuts it down in both directions, both for reading
// and for writing. You can change that using `net.shutdown(handle, how: .read)`
Expand Down
30 changes: 16 additions & 14 deletions vlib/net/unix/stream.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub fn connect_stream(socket_path string) !&StreamConn {
if socket_path.len >= max_sun_path {
return error('Socket path too long! Max length: ${max_sun_path - 1} chars.')
}
mut s := new_stream_socket(socket_path)!
mut s := new_stream_socket(socket_path) or {
return error('${err.msg()}; could not create new unix socket')
}

s.connect(socket_path)!

Expand Down Expand Up @@ -68,16 +70,16 @@ pub fn (mut c StreamConn) write_ptr(b &u8, len int) !int {
ptr := ptr_base + total_sent
remaining := len - total_sent
mut sent := $if is_coroutine ? {
C.photon_send(c.sock.handle, ptr, remaining, unix.msg_nosignal, c.write_timeout)
C.photon_send(c.sock.handle, ptr, remaining, net.msg_nosignal, c.write_timeout)
} $else {
C.send(c.sock.handle, ptr, remaining, unix.msg_nosignal)
C.send(c.sock.handle, ptr, remaining, net.msg_nosignal)
}
$if trace_unix_data_write ? {
eprintln('>>> UnixConn.write_ptr | data chunk, total_sent: ${total_sent:6}, remaining: ${remaining:6}, ptr: ${voidptr(ptr):x} => sent: ${sent:6}')
}
if sent < 0 {
code := error_code()
if code == int(error_ewouldblock) {
code := net.error_code()
if code == int(net.error_ewouldblock) {
c.wait_for_write()!
continue
} else {
Expand Down Expand Up @@ -117,8 +119,8 @@ pub fn (mut c StreamConn) read_ptr(buf_ptr &u8, len int) !int {
}
return res
}
code := error_code()
if code == int(error_ewouldblock) {
code := net.error_code()
if code == int(net.error_ewouldblock) {
c.wait_for_read()!
res = $if is_coroutine ? {
wrap_read_result(C.photon_recv(c.sock.handle, voidptr(buf_ptr), len, 0, c.read_timeout))!
Expand Down Expand Up @@ -353,7 +355,7 @@ fn new_stream_socket(socket_path string) !StreamSocket {
$if !net_blocking_sockets ? {
$if windows {
t := u32(1) // true
net.socket_error(C.ioctlsocket(handle, fionbio, &t))!
net.socket_error(C.ioctlsocket(handle, net.fionbio, &t))!
} $else {
net.socket_error(C.fcntl(handle, C.F_SETFL, C.fcntl(handle, C.F_GETFL) | C.O_NONBLOCK))!
}
Expand Down Expand Up @@ -401,6 +403,7 @@ fn (mut s StreamSocket) connect(socket_path string) ! {
addr := addrs[0]
// cast to the correct type
alen := addr.len()
eprintln(addr)

$if !net_blocking_sockets ? {
res := $if is_coroutine ? {
Expand All @@ -411,12 +414,12 @@ fn (mut s StreamSocket) connect(socket_path string) ! {
if res == 0 {
return
}
ecode := error_code()
ecode := net.error_code()

// no need to check for einprogress on nix
// On windows we expect res == -1 && error_code() == ewouldblock
// On windows we expect res == -1 && net.error_code() == ewouldblock
$if windows {
if ecode == int(error_ewouldblock) {
if ecode == int(net.error_ewouldblock) {
// The socket is nonblocking and the connection cannot be completed
// immediately. Wait till the socket is ready to write
write_result := s.@select(.write, unix.connect_timeout)!
Expand All @@ -434,10 +437,9 @@ fn (mut s StreamSocket) connect(socket_path string) ! {
}
return
}
return err_timed_out
return net.err_timed_out
}
}

net.wrap_error(ecode)!
return
} $else {
Expand All @@ -462,7 +464,7 @@ pub fn stream_socket_from_handle(sockfd int) !&StreamSocket {
$if !net_blocking_sockets ? {
$if windows {
t := u32(1) // true
net.socket_error(C.ioctlsocket(sockfd, fionbio, &t))!
net.socket_error(C.ioctlsocket(sockfd, net.fionbio, &t))!
} $else {
net.socket_error(C.fcntl(sockfd, C.F_SETFL, C.fcntl(sockfd, C.F_GETFL) | C.O_NONBLOCK))!
}
Expand Down

0 comments on commit d4b3056

Please sign in to comment.