join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
io_operation.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_IO_OPERATION_HPP
26#define JOIN_CORE_IO_OPERATION_HPP
27
28// libjoin.
30
31// C++.
32#include <atomic>
33
34// C.
35#include <sys/socket.h>
36#include <cstdint>
37
38namespace join
39{
40 // forward declaration.
41 class CompletionHandler;
42
46 struct alignas (64) IoOperation
47 {
51 enum class State : uint8_t
52 {
53 Idle,
54 Submitted,
55 Busy,
56 Suspended,
57 };
58
62 enum class Opcode : uint8_t
63 {
64 Poll,
65 Accept,
66 Connect,
67 Read,
68 Write,
69 ReadFixed,
71 RecvMsg,
72 SendMsg,
73 Recv,
74 Send,
75 };
76
80 IoOperation () = default;
81
86 IoOperation (const IoOperation& other) noexcept;
87
93 IoOperation& operator= (const IoOperation& other) noexcept;
94
98 struct PollData
99 {
101 int fd;
102
104 uint32_t events;
105 };
106
114 static IoOperation makePoll (int fd, uint32_t events, CompletionHandler* handler) noexcept;
115
123 static IoOperation makePollMulti (int fd, uint32_t events, CompletionHandler* handler) noexcept;
124
129 {
131 int fd;
132
134 sockaddr* addr;
135
137 socklen_t* addrlen;
138
140 int flags;
141 };
142
152 static IoOperation makeAccept (int fd, sockaddr* addr, socklen_t* addrlen, int flags,
153 CompletionHandler* handler) noexcept;
154
162 static IoOperation makeAcceptMulti (int fd, int flags, CompletionHandler* handler) noexcept;
163
168 {
170 int fd;
171
173 const sockaddr* addr;
174
176 socklen_t addrlen;
177 };
178
187 static IoOperation makeConnect (int fd, const sockaddr* addr, socklen_t addrlen,
188 CompletionHandler* handler) noexcept;
189
193 struct RwData
194 {
196 int fd;
197
199 void* buf;
200
202 unsigned long len;
203
205 uint16_t index;
206
208 bool fixed;
209 };
210
220 static IoOperation makeRead (int fd, void* buf, uint32_t len, CompletionHandler* handler,
221 bool linked = false) noexcept;
222
232 static IoOperation makeWrite (int fd, const void* buf, uint32_t len, CompletionHandler* handler,
233 bool linked = false) noexcept;
234
245 static IoOperation makeReadFixed (int fd, void* buf, uint32_t len, uint16_t index, CompletionHandler* handler,
246 bool linked = false) noexcept;
247
258 static IoOperation makeWriteFixed (int fd, const void* buf, uint32_t len, uint16_t index,
259 CompletionHandler* handler, bool linked = false) noexcept;
260
264 struct MsgData
265 {
267 int fd;
268
270 msghdr* msg;
271
273 int flags;
274
276 uint32_t namelen;
277
279 uint32_t controllen;
280 };
281
291 static IoOperation makeRecvmsg (int fd, msghdr* msg, int flags, CompletionHandler* handler,
292 bool linked = false) noexcept;
293
303 static IoOperation makeRecvmsgMulti (int fd, uint16_t group, msghdr* msg, int flags,
304 CompletionHandler* handler) noexcept;
305
315 static IoOperation makeSendmsg (int fd, const msghdr* msg, int flags, CompletionHandler* handler,
316 bool linked = false) noexcept;
317
322 {
324 int fd;
325
327 void* buf;
328
330 unsigned long len;
331
333 int flags;
334 };
335
346 static IoOperation makeRecv (int fd, void* buf, uint32_t len, int flags, CompletionHandler* handler,
347 bool linked = false) noexcept;
348
357 static IoOperation makeRecvMulti (int fd, uint16_t group, int flags, CompletionHandler* handler) noexcept;
358
369 static IoOperation makeSend (int fd, const void* buf, uint32_t len, int flags, CompletionHandler* handler,
370 bool linked = false) noexcept;
371
381
386 int fd () const noexcept;
387
389 uint8_t code = 0;
390
393
396
398 uint32_t index = 0;
399
401 bool linked = false;
402
404 bool multishot = false;
405
407 bool more = false;
408
410 uint16_t group = 0;
411
414
416 Data data = {};
417
419 IoRingBuffer* ring = nullptr;
420 };
421}
422
423#endif
completion handler interface class.
Definition proactor.hpp:79
pool of buffers provided to the I/O backend.
Definition io_ring_buffer.hpp:49
Definition acceptor.hpp:32
Definition error.hpp:144
payload for accept.
Definition io_operation.hpp:129
socklen_t * addrlen
peer address length.
Definition io_operation.hpp:137
int fd
file descriptor.
Definition io_operation.hpp:131
int flags
accept flags.
Definition io_operation.hpp:140
sockaddr * addr
peer address.
Definition io_operation.hpp:134
payload for connect.
Definition io_operation.hpp:168
const sockaddr * addr
remote address.
Definition io_operation.hpp:173
socklen_t addrlen
address length.
Definition io_operation.hpp:176
int fd
file descriptor.
Definition io_operation.hpp:170
payload for sendmsg / recvmsg.
Definition io_operation.hpp:265
int flags
sendmsg / recvmsg flags.
Definition io_operation.hpp:273
uint32_t namelen
reserved name area length.
Definition io_operation.hpp:276
msghdr * msg
message header.
Definition io_operation.hpp:270
uint32_t controllen
reserved control area length.
Definition io_operation.hpp:279
int fd
file descriptor.
Definition io_operation.hpp:267
payload for poll.
Definition io_operation.hpp:99
uint32_t events
requested events.
Definition io_operation.hpp:104
int fd
file descriptor.
Definition io_operation.hpp:101
payload for read / read fixed / write / write fixed.
Definition io_operation.hpp:194
unsigned long len
number of bytes to transfer.
Definition io_operation.hpp:202
bool fixed
use registered buffer (ignored with reactor backend).
Definition io_operation.hpp:208
uint16_t index
registered buffer index (ignored with reactor backend).
Definition io_operation.hpp:205
int fd
file descriptor.
Definition io_operation.hpp:196
void * buf
buffer.
Definition io_operation.hpp:199
payload for recv / send.
Definition io_operation.hpp:322
int fd
file descriptor.
Definition io_operation.hpp:324
unsigned long len
number of bytes to transfer.
Definition io_operation.hpp:330
int flags
send / recv flags.
Definition io_operation.hpp:333
void * buf
buffer.
Definition io_operation.hpp:327
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:47
bool linked
link this SQE to the next one (next executes only if this succeeds, io_uring only).
Definition io_operation.hpp:401
static IoOperation makeWrite(int fd, const void *buf, uint32_t len, CompletionHandler *handler, bool linked=false) noexcept
build a regular write operation.
Definition io_operation.cpp:170
bool more
true while a multishot operation stays armed.
Definition io_operation.hpp:407
uint16_t group
provided buffer group.
Definition io_operation.hpp:410
static IoOperation makeSendmsg(int fd, const msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a send-message operation.
Definition io_operation.cpp:263
static IoOperation makeConnect(int fd, const sockaddr *addr, socklen_t addrlen, CompletionHandler *handler) noexcept
build a connect operation.
Definition io_operation.cpp:136
static IoOperation makeSend(int fd, const void *buf, uint32_t len, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a send operation.
Definition io_operation.cpp:316
static IoOperation makeReadFixed(int fd, void *buf, uint32_t len, uint16_t index, CompletionHandler *handler, bool linked=false) noexcept
build a fixed-buffer read operation.
Definition io_operation.cpp:189
static IoOperation makeRecvMulti(int fd, uint16_t group, int flags, CompletionHandler *handler) noexcept
build a multishot receive operation.
Definition io_operation.cpp:298
static IoOperation makeWriteFixed(int fd, const void *buf, uint32_t len, uint16_t index, CompletionHandler *handler, bool linked=false) noexcept
build a fixed-buffer write operation.
Definition io_operation.cpp:208
uint32_t index
index of this operation in the proactor pending ops (io_uring only).
Definition io_operation.hpp:398
static IoOperation makeRead(int fd, void *buf, uint32_t len, CompletionHandler *handler, bool linked=false) noexcept
build a regular read operation.
Definition io_operation.cpp:152
static IoOperation makePollMulti(int fd, uint32_t events, CompletionHandler *handler) noexcept
build a multishot poll operation, staying armed until cancelled or failed.
Definition io_operation.cpp:87
static IoOperation makeRecvmsgMulti(int fd, uint16_t group, msghdr *msg, int flags, CompletionHandler *handler) noexcept
build a multishot receive-message operation.
Definition io_operation.cpp:243
static IoOperation makeAccept(int fd, sockaddr *addr, socklen_t *addrlen, int flags, CompletionHandler *handler) noexcept
build an accept operation.
Definition io_operation.cpp:102
Data data
operation code specific payload.
Definition io_operation.hpp:416
State
operation lifecycle state.
Definition io_operation.hpp:52
static IoOperation makeRecvmsg(int fd, msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a receive-message operation.
Definition io_operation.cpp:227
bool multishot
rearm the operation after each completion.
Definition io_operation.hpp:404
static IoOperation makeAcceptMulti(int fd, int flags, CompletionHandler *handler) noexcept
build a multishot accept operation.
Definition io_operation.cpp:119
CompletionHandler * handler
handler to dispatch to on completion.
Definition io_operation.hpp:413
std::atomic< State > state
operation state.
Definition io_operation.hpp:392
static IoOperation makeRecv(int fd, void *buf, uint32_t len, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a receive operation.
Definition io_operation.cpp:280
static IoOperation makePoll(int fd, uint32_t events, CompletionHandler *handler) noexcept
build a poll operation.
Definition io_operation.cpp:73
State resume
state to restore when the current hold is released.
Definition io_operation.hpp:395
IoOperation & operator=(const IoOperation &other) noexcept
copy assignment operator.
Definition io_operation.cpp:55
uint8_t code
operation code.
Definition io_operation.hpp:389
IoRingBuffer * ring
buffer ring bound to this operation.
Definition io_operation.hpp:419
IoOperation()=default
default constructor.
Opcode
operation code.
Definition io_operation.hpp:63
int fd() const noexcept
return the file descriptor associated with this operation.
Definition io_operation.cpp:334
Definition io_operation.hpp:373
RwData rw
Definition io_operation.hpp:377
MsgData msg
Definition io_operation.hpp:378
PollData poll
Definition io_operation.hpp:374
AcceptData accept
Definition io_operation.hpp:375
StreamData stream
Definition io_operation.hpp:379
ConnectData connect
Definition io_operation.hpp:376