Skip to content

Commit

Permalink
net: move unix socket code to net.unix and skip net/unix/unix_test.v … (
Browse files Browse the repository at this point in the history
  • Loading branch information
refrogerator authored Feb 11, 2021
1 parent f8db44b commit 9b4f2ed
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 425 deletions.
3 changes: 2 additions & 1 deletion cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
'vlib/net/tcp_simple_client_server_test.v',
'vlib/net/tcp_test.v',
'vlib/net/udp_test.v',
'vlib/net/unix_test.v',
'vlib/net/unix/unix_test.v',
'vlib/orm/orm_test.v',
'vlib/os/os_test.v',
'vlib/dl/dl_test.v',
Expand Down Expand Up @@ -189,6 +189,7 @@ const (
'vlib/orm/orm_test.v',
'vlib/v/tests/orm_sub_struct_test.v',
'vlib/net/websocket/ws_test.v',
'vlib/net/unix/unix_test.v',
'vlib/x/websocket/websocket_test.v',
'vlib/vweb/tests/vweb_test.v',
]
Expand Down
1 change: 0 additions & 1 deletion vlib/net/aasocket.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub enum SocketType {
// SocketFamily are the available address families
pub enum SocketFamily {
inet = C.AF_INET
unix = C.AF_UNIX
}

struct C.in_addr {
Expand Down
38 changes: 15 additions & 23 deletions vlib/net/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import time
fn shutdown(handle int) ? {
$if windows {
C.shutdown(handle, C.SD_BOTH)
socket_error(C.closesocket(handle))?
socket_error(C.closesocket(handle)) ?
} $else {
C.shutdown(handle, C.SHUT_RDWR)
socket_error(C.close(handle))?
socket_error(C.close(handle)) ?
}

return none
Expand All @@ -34,38 +34,34 @@ fn @select(handle int, test Select, timeout time.Duration) ?bool {

// infinite timeout is signaled by passing null as the timeout to
// select
if timeout == infinite_timeout {
if timeout == net.infinite_timeout {
timeval_timeout = &C.timeval(0)
}

match test {
.read {
socket_error(C.@select(handle+1, &set, C.NULL, C.NULL, timeval_timeout))?
socket_error(C.@select(handle + 1, &set, C.NULL, C.NULL, timeval_timeout)) ?
}
.write {
socket_error(C.@select(handle+1, C.NULL, &set, C.NULL, timeval_timeout))?
socket_error(C.@select(handle + 1, C.NULL, &set, C.NULL, timeval_timeout)) ?
}
.except {
socket_error(C.@select(handle+1, C.NULL, C.NULL, &set, timeval_timeout))?
socket_error(C.@select(handle + 1, C.NULL, C.NULL, &set, timeval_timeout)) ?
}
}

return C.FD_ISSET(handle, &set)
}

// wait_for_common wraps the common wait code
fn wait_for_common(
handle int,
deadline time.Time,
timeout time.Duration,
test Select) ? {
fn wait_for_common(handle int, deadline time.Time, timeout time.Duration, test Select) ? {
if deadline.unix == 0 {
// only accept infinite_timeout as a valid
// negative timeout - it is handled in @select however
if timeout < 0 && timeout != infinite_timeout {
if timeout < 0 && timeout != net.infinite_timeout {
return err_timed_out
}
ready := @select(handle, test, timeout)?
ready := @select(handle, test, timeout) ?
if ready {
return none
}
Expand All @@ -80,33 +76,29 @@ fn wait_for_common(
return err_timed_out
}

ready := @select(handle, test, d_timeout)?
ready := @select(handle, test, d_timeout) ?
if ready {
return none
}
return err_timed_out
}

// wait_for_write waits for a write io operation to be available
fn wait_for_write(
handle int,
deadline time.Time,
timeout time.Duration) ? {
fn wait_for_write(handle int, deadline time.Time, timeout time.Duration) ? {
return wait_for_common(handle, deadline, timeout, .write)
}

// wait_for_read waits for a read io operation to be available
fn wait_for_read(
handle int,
deadline time.Time,
timeout time.Duration) ? {
fn wait_for_read(handle int, deadline time.Time, timeout time.Duration) ? {
return wait_for_common(handle, deadline, timeout, .read)
}

// no_deadline should be given to functions when no deadline is wanted (i.e. all functions
// return instantly)
const (
no_deadline = time.Time{unix: 0}
no_deadline = time.Time{
unix: 0
}
)

// no_timeout should be given to functions when no timeout is wanted (i.e. all functions
Expand Down
31 changes: 16 additions & 15 deletions vlib/net/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ const (

// Well defined errors that are returned from socket functions
pub const (
err_new_socket_failed = error_with_code('net: new_socket failed to create socket', errors_base+1)
err_option_not_settable = error_with_code('net: set_option_xxx option not settable', errors_base+2)
err_option_wrong_type = error_with_code('net: set_option_xxx option wrong type', errors_base+3)
err_port_out_of_range = error_with_code('', errors_base+5)
err_no_udp_remote = error_with_code('', errors_base+6)
err_connect_failed = error_with_code('net: connect failed', errors_base+7)
err_connect_timed_out = error_with_code('net: connect timed out', errors_base+8)

err_timed_out = error_with_code('net: op timed out', errors_base+9)
err_timed_out_code = errors_base+9
err_new_socket_failed = error_with_code('net: new_socket failed to create socket',
errors_base + 1)
err_option_not_settable = error_with_code('net: set_option_xxx option not settable',
errors_base + 2)
err_option_wrong_type = error_with_code('net: set_option_xxx option wrong type',
errors_base + 3)
err_port_out_of_range = error_with_code('', errors_base + 5)
err_no_udp_remote = error_with_code('', errors_base + 6)
err_connect_failed = error_with_code('net: connect failed', errors_base + 7)
err_connect_timed_out = error_with_code('net: connect timed out', errors_base + 8)
err_timed_out = error_with_code('net: op timed out', errors_base + 9)
err_timed_out_code = errors_base + 9
)

pub fn socket_error(potential_code int) ?int {
$if windows {
if potential_code < 0 {
last_error_int := C.WSAGetLastError()
last_error := wsa_error(last_error_int)
return error_with_code('net: socket error: ($last_error_int) $last_error', int(last_error))
return error_with_code('net: socket error: ($last_error_int) $last_error',
int(last_error))
}
}
$else {
} $else {
if potential_code < 0 {
last_error := error_code()
return error_with_code('net: socket error: $last_error', last_error)
Expand All @@ -40,8 +42,7 @@ pub fn wrap_error(error_code int) ? {
$if windows {
enum_error := wsa_error(error_code)
return error_with_code('net: socket error: $enum_error', error_code)
}
$else {
} $else {
if error_code == 0 {
return
}
Expand Down
Loading

0 comments on commit 9b4f2ed

Please sign in to comment.