Repository navigation
Expand file tree
/
Copy pathvector_io.wave
More file actions
58 lines (55 loc) · 2.32 KB
/
Copy pathvector_io.wave
File metadata and controls
58 lines (55 loc) · 2.32 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
pub struct NativeIoSlice { data: ptr<u8>; length: u64; }
pub struct NativeMessage {
name: ptr<i8>;
name_length: u32;
vectors: ptr<NativeIoSlice>;
vector_count: i32;
control: ptr<i8>;
control_length: u32;
flags: i32;
}
extern(c, "sendmsg") fun native_sendmsg(fd: i32, message: ptr<NativeMessage>, flags: i32) -> i64;
extern(c, "recvmsg") fun native_recvmsg(fd: i32, message: ptr<NativeMessage>, flags: i32) -> i64;
extern(c, "__error") fun native_error() -> ptr<i32>;
pub fun native_io_slice(data: ptr<u8>, length: i64, output: ptr<NativeIoSlice>) -> i64 {
if (length < 0 || (data == null && length > 0) || output == null) { return -22; }
deref output = NativeIoSlice { data: data, length: length as u64 };
return 0;
}
fun _message(vectors: ptr<NativeIoSlice>, count: i32) -> NativeMessage {
return NativeMessage {
name: null, name_length: 0, vectors: vectors, vector_count: count,
control: null, control_length: 0, flags: 0
};
}
fun _result(value: i64) -> i64 {
if (value >= 0) { return value; }
var error_pointer: ptr<i32> = native_error();
return -(deref error_pointer) as i64;
}
pub fun vector_send(fd: i64, vectors: ptr<NativeIoSlice>, count: i32, flags: i32) -> i64 {
var message: NativeMessage = _message(vectors, count);
return _result(native_sendmsg(fd as i32, &message, flags));
}
pub fun vector_recv(fd: i64, vectors: ptr<NativeIoSlice>, count: i32, flags: i32) -> i64 {
var message: NativeMessage = _message(vectors, count);
return _result(native_recvmsg(fd as i32, &message, flags));
}
// One datagram; report the bytes stored separately from the wire size.
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 || truncated == null || original_size == null) {
return -22;
}
var message: NativeMessage = _message(vectors, count);
var result: i64 = _result(native_recvmsg(fd as i32, &message, flags));
if (result < 0) {
return result;
}
var cut: bool = (message.flags & 16) != 0 || result > capacity;
deref truncated = cut;
if (cut) { deref original_size = -1; } else { deref original_size = result; }
if (result > capacity) {
return capacity;
}
return result;
}