Repository navigation
Expand file tree
/
Copy pathvector_io.wave
More file actions
62 lines (59 loc) · 2.58 KB
/
Copy pathvector_io.wave
File metadata and controls
62 lines (59 loc) · 2.58 KB
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
import("std::sys::windows::socket")::{socket_last_error};
pub struct NativeIoSlice { length: u32; data: ptr<u8>; }
extern(system, "WSASend") fun native_wsa_send(
fd: i64, vectors: ptr<NativeIoSlice>, count: u32, transferred: ptr<u32>,
flags: u32, overlapped: ptr<i8>, completion: ptr<i8>
) -> i32;
extern(system, "WSARecv") fun native_wsa_recv(
fd: i64, vectors: ptr<NativeIoSlice>, count: u32, transferred: ptr<u32>,
flags: ptr<u32>, overlapped: ptr<i8>, completion: ptr<i8>
) -> i32;
pub fun native_io_slice(data: ptr<u8>, length: i64, output: ptr<NativeIoSlice>) -> i64 {
if (length < 0 || length > 4294967295 || (data == null && length > 0) || output == null) {
return -22;
}
deref output = NativeIoSlice { length: length as u32, data: data };
return 0;
}
pub fun vector_send(fd: i64, vectors: ptr<NativeIoSlice>, count: i32, flags: i32) -> i64 {
if (count < 0) { return -22; }
var transferred: u32 = 0;
var result: i32 = native_wsa_send(
fd, vectors, count as u32, &transferred, flags as u32, null, null
);
if (result != 0) { return socket_last_error(); }
return transferred as i64;
}
pub fun vector_recv(fd: i64, vectors: ptr<NativeIoSlice>, count: i32, flags: i32) -> i64 {
if (count < 0) { return -22; }
var transferred: u32 = 0;
var receive_flags: u32 = flags as u32;
var result: i32 = native_wsa_recv(
fd, vectors, count as u32, &transferred, &receive_flags, null, null
);
if (result != 0) { return socket_last_error(); }
return transferred as i64;
}
// UDP WSAEMSGSIZE fills the available buffers and discards the excess bytes.
// Do not rely on the transferred output when WSARecv returned an error.
pub fun vector_recv_datagram(fd: i64, vectors: ptr<NativeIoSlice>, count: i32, flags: i32, capacity: i64, truncated: ptr<bool>, original_size: ptr<i64>) -> i64 {
if (count <= 0 || capacity < 0 || capacity > 4294967295 || truncated == null || original_size == null) {
return -22;
}
var transferred: u32 = 0;
var receive_flags: u32 = flags as u32;
var result: i32 = native_wsa_recv(fd, vectors, count as u32, &transferred, &receive_flags, null, null);
if (result != 0) {
var failure: i64 = socket_last_error();
if (failure != -10040) {
return failure;
}
deref truncated = true;
deref original_size = -1;
return capacity;
}
var cut: bool = (receive_flags & 32768) != 0;
deref truncated = cut;
if (cut) { deref original_size = -1; } else { deref original_size = transferred as i64; }
return transferred as i64;
}