2020-08-21 00:01:37 +03:00
|
|
|
module net
|
|
|
|
|
2021-02-03 11:17:13 +03:00
|
|
|
// Well defined errors that are returned from socket functions
|
|
|
|
pub const (
|
2022-10-23 17:08:16 +03:00
|
|
|
errors_base = 0
|
2021-02-11 19:51:12 +03:00
|
|
|
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)
|
2022-08-07 10:40:05 +03:00
|
|
|
err_port_out_of_range = error_with_code('net: port out of range', errors_base + 5)
|
|
|
|
err_no_udp_remote = error_with_code('net: no udp remote', errors_base + 6)
|
2021-02-11 19:51:12 +03:00
|
|
|
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
|
2022-08-07 10:40:05 +03:00
|
|
|
err_connection_refused = error_with_code('net: connection refused', errors_base + 10)
|
2020-08-21 00:01:37 +03:00
|
|
|
)
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn socket_error_message(potential_code int, s string) !int {
|
2022-11-15 16:53:13 +03:00
|
|
|
return socket_error(potential_code) or { return error('${err.msg()}; ${s}') }
|
2022-01-08 18:02:03 +03:00
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn socket_error(potential_code int) !int {
|
2020-08-21 00:01:37 +03:00
|
|
|
$if windows {
|
|
|
|
if potential_code < 0 {
|
|
|
|
last_error_int := C.WSAGetLastError()
|
|
|
|
last_error := wsa_error(last_error_int)
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('net: socket error: (${last_error_int}) ${last_error}',
|
2021-02-11 19:51:12 +03:00
|
|
|
int(last_error))
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
2021-02-11 19:51:12 +03:00
|
|
|
} $else {
|
2020-08-21 00:01:37 +03:00
|
|
|
if potential_code < 0 {
|
|
|
|
last_error := error_code()
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('net: socket error: ${last_error}', last_error)
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return potential_code
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn wrap_error(error_code int) ! {
|
2022-08-07 10:40:05 +03:00
|
|
|
if error_code == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-21 00:01:37 +03:00
|
|
|
$if windows {
|
|
|
|
enum_error := wsa_error(error_code)
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('net: socket error: ${enum_error}', error_code)
|
2021-02-11 19:51:12 +03:00
|
|
|
} $else {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error_with_code('net: socket error: ${error_code}', error_code)
|
2020-08-21 00:01:37 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
|
|
|
|
// wrap_read_result takes a read result and sees if it is 0 for graceful
|
|
|
|
// connection termination and returns none
|
2021-06-13 23:53:38 +03:00
|
|
|
// e.g. res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))?
|
2020-11-15 23:54:47 +03:00
|
|
|
[inline]
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wrap_read_result(result int) !int {
|
2021-06-13 23:53:38 +03:00
|
|
|
if result == 0 {
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
return result
|
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
[inline]
|
2022-10-16 09:28:57 +03:00
|
|
|
fn wrap_write_result(result int) !int {
|
2021-06-13 23:53:38 +03:00
|
|
|
if result == 0 {
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
return result
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|