/
msg.d
83 lines (68 loc) · 1.87 KB
/
msg.d
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
71
72
73
74
75
76
77
78
79
80
81
82
83
module tests.msg;
import during;
import tests.base;
import core.stdc.stdlib;
import core.sys.linux.errno;
import core.sys.posix.sys.socket;
import core.sys.posix.sys.uio : iovec;
import core.sys.posix.unistd;
import std.algorithm : copy, equal, map;
import std.range;
@("send/recv")
unittest
{
if (!checkKernelVersion(5, 3)) return;
int[2] fd;
int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
assert(ret == 0, "socketpair()");
Uring io;
auto res = io.setup();
assert(res >= 0, "Error initializing IO");
// 0 - read, 1 - write
iovec[2] v;
msghdr[2] msg;
foreach (i; 0..2)
{
v[i].iov_base = malloc(256);
v[i].iov_len = 256;
msg[i].msg_iov = &v[i];
msg[i].msg_iovlen = 1;
}
scope (exit) foreach (i; 0..2) free(v[i].iov_base);
iota(0, 256)
.map!(a => cast(ubyte)a)
.copy((cast(ubyte*)v[1].iov_base)[0..256]);
// add recvmsg
io.putWith!(
(ref SubmissionEntry e, int fd, ref msghdr m)
{
e.prepRecvMsg(fd, m);
e.user_data = 0;
})(fd[0], msg[0]);
// add sendmsg
io.putWith!(
(ref SubmissionEntry e, int fd, ref msghdr m)
{
e.prepSendMsg(fd, m);
e.user_data = 1;
})(fd[1], msg[1]);
ret = io.submit(2);
assert(ret == 2);
assert(io.length == 2);
foreach (i; 0..2)
{
scope (exit) io.popFront();
if (io.front.res == -EINVAL)
{
version (D_BetterC)
{
errmsg = "kernel doesn't support SEND/RECVMSG";
return;
}
else throw new Exception("kernel doesn't support SEND/RECVMSG");
}
assert(io.front.res >= 0);
if (io.front.user_data == 1) continue; // write done
else assert((cast(ubyte*)v[0].iov_base)[0..256].equal(iota(0, 256)));
}
}