aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/net/errors.v
blob: f6ada74a53f30e6cae7091f16b046fe1e5c96707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module net

const (
	errors_base = 0
)

// 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
)

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))
		}
	} $else {
		if potential_code < 0 {
			last_error := error_code()
			return error_with_code('net: socket error: $last_error', last_error)
		}
	}

	return potential_code
}

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 {
		if error_code == 0 {
			return
		}
		return error_with_code('net: socket error: $error_code', error_code)
	}
}

// wrap_read_result takes a read result and sees if it is 0 for graceful
// connection termination and returns none
// e.g. res := wrap_read_result(C.recv(c.sock.handle, voidptr(buf_ptr), len, 0))?
[inline]
fn wrap_read_result(result int) ?int {
	if result == 0 {
		return none
	}
	return result
}

[inline]
fn wrap_write_result(result int) ?int {
	if result == 0 {
		return none
	}
	return result
}