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#include <cstddef>
38
39namespace join
40{
41 // forward declaration.
42 class CompletionHandler;
43
47 struct alignas (64) IoOperation
48 {
52 enum class State : uint8_t
53 {
54 Idle,
55 Submitted,
56 Busy,
57 Suspended,
58 };
59
63 enum class Opcode : uint8_t
64 {
65 Poll,
66 Accept,
67 Connect,
68 Read,
69 Write,
70 ReadFixed,
72 RecvMsg,
73 SendMsg,
74 Recv,
75 Send,
76 };
77
81 IoOperation () noexcept
82 {
83 state.store (State::Idle, std::memory_order_release);
84 }
85
90 IoOperation (const IoOperation& other) noexcept;
91
97 IoOperation& operator= (const IoOperation& other) noexcept;
98
102 struct PollData
103 {
105 int fd;
106
108 uint32_t events;
109 };
110
118 static IoOperation makePoll (int fd, uint32_t events, CompletionHandler* handler) noexcept;
119
127 static IoOperation makePollMulti (int fd, uint32_t events, CompletionHandler* handler) noexcept;
128
133 {
135 int fd;
136
138 sockaddr* addr;
139
141 socklen_t* addrlen;
142
144 int flags;
145 };
146
156 static IoOperation makeAccept (int fd, sockaddr* addr, socklen_t* addrlen, int flags,
157 CompletionHandler* handler) noexcept;
158
166 static IoOperation makeAcceptMulti (int fd, int flags, CompletionHandler* handler) noexcept;
167
172 {
174 int fd;
175
177 const sockaddr* addr;
178
180 socklen_t addrlen;
181 };
182
191 static IoOperation makeConnect (int fd, const sockaddr* addr, socklen_t addrlen,
192 CompletionHandler* handler) noexcept;
193
197 struct RwData
198 {
200 int fd;
201
203 void* buf;
204
206 unsigned long len;
207
209 uint16_t index;
210
212 bool fixed;
213 };
214
224 static IoOperation makeRead (int fd, void* buf, uint32_t len, CompletionHandler* handler,
225 bool linked = false) noexcept;
226
236 static IoOperation makeWrite (int fd, const void* buf, uint32_t len, CompletionHandler* handler,
237 bool linked = false) noexcept;
238
249 static IoOperation makeReadFixed (int fd, void* buf, uint32_t len, uint16_t index, CompletionHandler* handler,
250 bool linked = false) noexcept;
251
262 static IoOperation makeWriteFixed (int fd, const void* buf, uint32_t len, uint16_t index,
263 CompletionHandler* handler, bool linked = false) noexcept;
264
268 struct MsgData
269 {
271 int fd;
272
274 msghdr* msg;
275
277 int flags;
278
280 uint32_t namelen;
281
283 uint32_t controllen;
284 };
285
295 static IoOperation makeRecvmsg (int fd, msghdr* msg, int flags, CompletionHandler* handler,
296 bool linked = false) noexcept;
297
307 static IoOperation makeRecvmsgMulti (int fd, uint16_t group, msghdr* msg, int flags,
308 CompletionHandler* handler) noexcept;
309
319 static IoOperation makeSendmsg (int fd, const msghdr* msg, int flags, CompletionHandler* handler,
320 bool linked = false) noexcept;
321
326 {
328 int fd;
329
331 void* buf;
332
334 unsigned long len;
335
337 int flags;
338 };
339
350 static IoOperation makeRecv (int fd, void* buf, uint32_t len, int flags, CompletionHandler* handler,
351 bool linked = false) noexcept;
352
361 static IoOperation makeRecvMulti (int fd, uint16_t group, int flags, CompletionHandler* handler) noexcept;
362
373 static IoOperation makeSend (int fd, const void* buf, uint32_t len, int flags, CompletionHandler* handler,
374 bool linked = false) noexcept;
375
385
390 int fd () const noexcept;
391
392#ifdef JOIN_HAS_IO_URING
394 uint32_t index = 0;
395#else
397 IoOperation* next = nullptr;
398#endif
399
401 uint8_t code = 0;
402
404 std::atomic<State> state;
405
408
410 bool linked = false;
411
413 bool multishot = false;
414
416 bool more = false;
417
419 uint16_t group = 0;
420
423
425 Data data = {};
426
428 IoRingBuffer* ring = nullptr;
429 };
430
431 static_assert (offsetof (IoOperation, state) >= sizeof (uint32_t),
432 "state must survive the arena free list link stored in the first bytes of a released chunk");
433}
434
435#endif
completion handler interface class.
Definition proactor.hpp:81
pool of buffers provided to the I/O backend.
Definition io_ring_buffer.hpp:49
Definition acceptor.hpp:32
payload for accept.
Definition io_operation.hpp:133
socklen_t * addrlen
peer address length.
Definition io_operation.hpp:141
int fd
file descriptor.
Definition io_operation.hpp:135
int flags
accept flags.
Definition io_operation.hpp:144
sockaddr * addr
peer address.
Definition io_operation.hpp:138
payload for connect.
Definition io_operation.hpp:172
const sockaddr * addr
remote address.
Definition io_operation.hpp:177
socklen_t addrlen
address length.
Definition io_operation.hpp:180
int fd
file descriptor.
Definition io_operation.hpp:174
payload for sendmsg / recvmsg.
Definition io_operation.hpp:269
int flags
sendmsg / recvmsg flags.
Definition io_operation.hpp:277
uint32_t namelen
reserved name area length.
Definition io_operation.hpp:280
msghdr * msg
message header.
Definition io_operation.hpp:274
uint32_t controllen
reserved control area length.
Definition io_operation.hpp:283
int fd
file descriptor.
Definition io_operation.hpp:271
payload for poll.
Definition io_operation.hpp:103
uint32_t events
requested events.
Definition io_operation.hpp:108
int fd
file descriptor.
Definition io_operation.hpp:105
payload for read / read fixed / write / write fixed.
Definition io_operation.hpp:198
unsigned long len
number of bytes to transfer.
Definition io_operation.hpp:206
bool fixed
use registered buffer (ignored with reactor backend).
Definition io_operation.hpp:212
uint16_t index
registered buffer index (ignored with reactor backend).
Definition io_operation.hpp:209
int fd
file descriptor.
Definition io_operation.hpp:200
void * buf
buffer.
Definition io_operation.hpp:203
payload for recv / send.
Definition io_operation.hpp:326
int fd
file descriptor.
Definition io_operation.hpp:328
unsigned long len
number of bytes to transfer.
Definition io_operation.hpp:334
int flags
send / recv flags.
Definition io_operation.hpp:337
void * buf
buffer.
Definition io_operation.hpp:331
Describes a single asynchronous operation submitted to the Proactor.
Definition io_operation.hpp:48
bool linked
link this SQE to the next one (next executes only if this succeeds, io_uring only).
Definition io_operation.hpp:410
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:178
bool more
true while a multishot operation stays armed.
Definition io_operation.hpp:416
uint16_t group
provided buffer group.
Definition io_operation.hpp:419
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:271
static IoOperation makeConnect(int fd, const sockaddr *addr, socklen_t addrlen, CompletionHandler *handler) noexcept
build a connect operation.
Definition io_operation.cpp:144
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:324
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:197
static IoOperation makeRecvMulti(int fd, uint16_t group, int flags, CompletionHandler *handler) noexcept
build a multishot receive operation.
Definition io_operation.cpp:306
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:216
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:160
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:95
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:251
static IoOperation makeAccept(int fd, sockaddr *addr, socklen_t *addrlen, int flags, CompletionHandler *handler) noexcept
build an accept operation.
Definition io_operation.cpp:110
Data data
operation code specific payload.
Definition io_operation.hpp:425
State
operation lifecycle state.
Definition io_operation.hpp:53
static IoOperation makeRecvmsg(int fd, msghdr *msg, int flags, CompletionHandler *handler, bool linked=false) noexcept
build a receive-message operation.
Definition io_operation.cpp:235
bool multishot
rearm the operation after each completion.
Definition io_operation.hpp:413
static IoOperation makeAcceptMulti(int fd, int flags, CompletionHandler *handler) noexcept
build a multishot accept operation.
Definition io_operation.cpp:127
CompletionHandler * handler
handler to dispatch to on completion.
Definition io_operation.hpp:422
std::atomic< State > state
operation state.
Definition io_operation.hpp:404
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:288
static IoOperation makePoll(int fd, uint32_t events, CompletionHandler *handler) noexcept
build a poll operation.
Definition io_operation.cpp:81
State resume
state to restore when the current hold is released.
Definition io_operation.hpp:407
IoOperation & operator=(const IoOperation &other) noexcept
copy assignment operator.
Definition io_operation.cpp:59
uint8_t code
operation code.
Definition io_operation.hpp:401
IoRingBuffer * ring
buffer ring bound to this operation.
Definition io_operation.hpp:428
IoOperation() noexcept
default constructor.
Definition io_operation.hpp:81
IoOperation * next
next operation queued on the same descriptor and direction.
Definition io_operation.hpp:397
Opcode
operation code.
Definition io_operation.hpp:64
int fd() const noexcept
return the file descriptor associated with this operation.
Definition io_operation.cpp:342
Definition io_operation.hpp:377
RwData rw
Definition io_operation.hpp:381
MsgData msg
Definition io_operation.hpp:382
PollData poll
Definition io_operation.hpp:378
AcceptData accept
Definition io_operation.hpp:379
StreamData stream
Definition io_operation.hpp:383
ConnectData connect
Definition io_operation.hpp:380