-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathio_context.hh
More file actions
58 lines (48 loc) · 1.34 KB
/
io_context.hh
File metadata and controls
58 lines (48 loc) · 1.34 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
#pragma once
#include "lazy.hh"
#include <set>
#include <stdexcept>
#include <sys/epoll.h>
#include <vector>
#include "socket_accept_operation.hh"
#include "socket_recv_operation.hh"
#include "socket_send_operation.hh"
/* Just an epoll wrapper */
class Socket;
struct oneway_task {
struct promise_type {
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() { }
oneway_task get_return_object() { return {}; }
void return_void() { }
};
};
class IOContext {
public:
IOContext()
: fd_{epoll_create1(0)}
{
if (fd_ == -1)
throw std::runtime_error{"epoll_create1"};
}
void run();
void spawn(std::lazy<>&& task);
private:
constexpr static std::size_t max_events = 10;
const int fd_;
// Fill it by watchRead / watchWrite
std::set<Socket*> processedSockets_;
std::vector<std::lazy<>> delayDestructor_;
friend Socket;
friend SocketAcceptOperation;
friend SocketRecvOperation;
friend SocketSendOperation;
void attach(Socket* socket);
void watchRead(Socket* socket);
void unwatchRead(Socket* socket);
void watchWrite(Socket* socket);
void unwatchWrite(Socket* socket);
void detach(Socket* socket);
void cleanIO();
};