COROIO: coroio/backends/uring.hpp Source File
COROIO
 
Loading...
Searching...
No Matches
uring.hpp
1#pragma once
2
3#if __has_include(<liburing.h>)
4#define HAVE_URING
5
6#include <coroio/base.hpp>
7#include <coroio/socket.hpp>
8#include <coroio/poller.hpp>
9
10#include <liburing.h>
11#include <assert.h>
12#include <sys/eventfd.h>
13#include <sys/epoll.h>
14
15#include <system_error>
16#include <iostream>
17#include <tuple>
18#include <vector>
19#include <coroutine>
20#include <queue>
21
22namespace NNet {
23
60class TUring: public TPollerBase {
61public:
71 TUring(int queueSize = 256);
82 void Read(int fd, void* buf, int size, std::coroutine_handle<> handle);
91 void Write(int fd, const void* buf, int size, std::coroutine_handle<> handle);
100 void Recv(int fd, void* buf, int size, std::coroutine_handle<> handle);
109 void Send(int fd, const void* buf, int size, std::coroutine_handle<> handle);
118 void Accept(int fd, struct sockaddr* addr, socklen_t* len, std::coroutine_handle<> handle);
127 void Connect(int fd, const sockaddr* addr, socklen_t len, std::coroutine_handle<> handle);
133 void Cancel(int fd);
139 void Cancel(std::coroutine_handle<> h);
145 void Register(int fd);
152 int Wait(timespec ts = {10,0});
158 void Poll() {
159 Wait(GetTimeout());
160 }
166 int Result();
167
168private:
176 io_uring_sqe* GetSqe() {
177 io_uring_sqe* r = io_uring_get_sqe(&Ring_);
178 if (!r) {
179 Submit();
180 r = io_uring_get_sqe(&Ring_);
181 }
182 return r;
183 }
184
186 void Submit();
187
188 int RingFd_;
189 int EpollFd_;
190 struct io_uring Ring_;
191 std::queue<int> Results_;
192 std::vector<char> Buffer_;
193};
194
195} // namespace NNet
196
197#endif
Base class for pollers managing asynchronous I/O events and timers.
Definition poller.hpp:52
timespec GetTimeout() const
Computes the poll timeout based on scheduled timers.
Definition poller.hpp:286
Asynchronous file handle driven by the poller's implementation.
Definition socket.hpp:742
Socket type driven by the poller's implementation.
Definition socket.hpp:519
Poller implementation based on io_uring.
Definition uring.hpp:60
int Result()
Retrieves the result of the last completed I/O completion.
int Wait(timespec ts={10, 0})
Waits for I/O completions.
void Cancel(int fd)
Cancels pending operations on the specified file descriptor.
void Read(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous read operation.
void Recv(int fd, void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous receive operation.
~TUring()
Destructor cleans up the io_uring and related resources.
void Poll()
Polls for I/O completions.
Definition uring.hpp:158
void Send(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous send operation.
void Connect(int fd, const sockaddr *addr, socklen_t len, std::coroutine_handle<> handle)
Posts an asynchronous connect operation.
void Write(int fd, const void *buf, int size, std::coroutine_handle<> handle)
Posts an asynchronous write operation.
TUring(int queueSize=256)
Constructs a TUring instance.
void Cancel(std::coroutine_handle<> h)
Cancels pending operations associated with a specific coroutine handle.
void Accept(int fd, struct sockaddr *addr, socklen_t *len, std::coroutine_handle<> handle)
Posts an asynchronous accept operation.
void Register(int fd)
Registers a file descriptor with the IO_uring poller.