-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactor.hh
More file actions
165 lines (141 loc) · 3.64 KB
/
reactor.hh
File metadata and controls
165 lines (141 loc) · 3.64 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* reactor.hh
*/
#ifndef REACTOR_HH_
#define REACTOR_HH_
#include <memory>
#include <libaio.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unordered_map>
#include <netinet/ip.h>
#include <cstring>
#include <cassert>
class socket_address;
class reactor;
class pollable_fd;
struct ipv4_addr {
uint8_t host[4];
uint16_t port;
};
socket_address make_ipv4_address(ipv4_addr addr);
class socket_address {
private:
union {
::sockaddr_storage sas;
::sockaddr sa;
::sockaddr_in in;
} u;
friend socket_address make_ipv4_address(ipv4_addr addr);
friend class reactor;
};
template <typename T>
class promise;
template <typename T>
struct future_state {
virtual ~future_state();
//promise<T>* promise;
bool value_valid = false;
bool ex_valid = false;
union {
T value;
std::exception_ptr ex;
} u;
void set(const T& value);
void set(T&& value);
void set_exception(std::exception_ptr ex);
T get() {
// while (promise) {
// promise->wait();
//}
if (u.ex) {
std::rethrow_exception(u.ex);
}
return std::move(u.value);
}
};
template <typename T>
class future {
std::unique_ptr<future_state<T>> _state;
public:
T get() {
return _state.get();
}
template <typename Func>
void then(Func func) {
}
};
class reactor {
class task;
public:
int _epollfd;
io_context_t _io_context;
private:
class task {
public:
virtual ~task() {}
virtual void run() = 0;
};
template <typename Func>
class lambda_task : public task {
Func _func;
public:
lambda_task(Func func) : _func(func) {}
virtual void run() { _func(); }
};
template <typename Func>
std::unique_ptr<task>
make_task(Func func) {
return std::make_unique<lambda_task<Func>>(func);
}
void epoll_add_in(pollable_fd& fd, std::unique_ptr<task> t);
void epoll_add_out(pollable_fd& fd, std::unique_ptr<task> t);
void abort_on_error(int ret);
public:
reactor();
~reactor();
std::unique_ptr<pollable_fd> listen(socket_address sa);
template <typename Func>
void accept(pollable_fd& listenfd, Func with_pfd_sockaddr);
future<std::unique_ptr<pollable_fd>> accept(pollable_fd& listen_fd);
future<size_t> read_some(pollable_fd& fd, void* buffer, size_t size);
template <typename Func>
void read_some(pollable_fd& fd, void* buffer, size_t len, Func with_len);
void run();
friend class pollable_fd;
};
class pollable_fd {
protected:
explicit pollable_fd(int fd) : fd(fd) {}
pollable_fd(const pollable_fd&) = delete;
void operator=(const pollable_fd&) = delete;
int fd;
int events = 0;
std::unique_ptr<reactor::task> pollin;
std::unique_ptr<reactor::task> pollout;
friend class reactor;
};
template <typename Func>
inline
void reactor::accept(pollable_fd& listenfd, Func with_pfd_sockaddr) {
auto lfd = listenfd.fd;
epoll_add_in(listenfd, make_task([=] {
socket_address sa;
socklen_t sl = sizeof(&sa.u.sas);
int fd = ::accept4(lfd, &sa.u.sa, &sl, SOCK_NONBLOCK | SOCK_CLOEXEC);
assert(fd != -1);
auto pfd = std::unique_ptr<pollable_fd>(new pollable_fd(fd));
with_pfd_sockaddr(std::move(pfd), sa);
}));
}
template <typename Func>
void reactor::read_some(pollable_fd& fd, void* buffer, size_t len, Func with_len) {
auto rfd = fd.fd;
epoll_add_in(fd, make_task([=] {
ssize_t r = ::recv(rfd, buffer, len, 0);
assert(r != -1);
with_len(len);
}));
}
#endif /* REACTOR_HH_ */