-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathstep12.cpp
128 lines (106 loc) · 3.02 KB
/
step12.cpp
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
#include "co_async/debug.hpp"
#include "co_async/task.hpp"
#include "co_async/timer_loop.hpp"
#include "co_async/when_any.hpp"
#include "co_async/when_all.hpp"
#include "co_async/and_then.hpp"
#include <system_error>
#include <cerrno>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#include <unistd.h>
namespace co_async {
auto checkError(auto res) {
// 把C语言错误码转换为C++异常
if (res == -1) [[unlikely]] {
throw std::system_error(errno, std::system_category());
}
return res;
}
struct EpollFilePromise : Promise<void> {
auto get_return_object() {
return std::coroutine_handle<EpollFilePromise>::from_promise(*this);
}
EpollFilePromise &operator=(EpollFilePromise &&) = delete;
int mFileNo;
uint32_t mEvents;
};
struct EpollLoop {
void addListener(EpollFilePromise &promise) {
struct epoll_event event;
event.events = promise.mEvents;
event.data.ptr = &promise;
checkError(epoll_ctl(mEpoll, EPOLL_CTL_ADD, promise.mFileNo, &event));
}
void tryRun() {
struct epoll_event ebuf[10];
int res = checkError(epoll_wait(mEpoll, ebuf, 10, -1));
for (int i = 0; i < res; i++) {
auto &event = ebuf[i];
auto &promise = *(EpollFilePromise *)event.data.ptr;
checkError(epoll_ctl(mEpoll, EPOLL_CTL_DEL, promise.mFileNo, NULL));
std::coroutine_handle<EpollFilePromise>::from_promise(promise).resume();
}
}
EpollLoop &operator=(EpollLoop &&) = delete;
~EpollLoop() {
close(mEpoll);
}
int mEpoll = checkError(epoll_create1(0));
};
struct EpollFileAwaiter {
bool await_ready() const noexcept {
return false;
}
void
await_suspend(std::coroutine_handle<EpollFilePromise> coroutine) const {
auto &promise = coroutine.promise();
promise.mFileNo = mFileNo;
promise.mEvents = mEvents;
loop.addListener(promise);
}
void await_resume() const noexcept {}
using ClockType = std::chrono::system_clock;
EpollLoop &loop;
int mFileNo;
uint32_t mEvents;
};
inline Task<void, EpollFilePromise>
wait_file(EpollLoop &loop, int fileNo, uint32_t events) {
co_await EpollFileAwaiter(loop, fileNo, events);
}
}
co_async::EpollLoop loop;
co_async::Task<std::string> reader() {
co_await wait_file(loop, 0, EPOLLIN);
std::string s;
while (true) {
char c;
ssize_t len = read(0, &c, 1);
if (len == -1) {
if (errno != EWOULDBLOCK) [[unlikely]] {
throw std::system_error(errno, std::system_category());
}
break;
}
s.push_back(c);
}
co_return s;
}
co_async::Task<void> async_main() {
while (true) {
auto s = co_await reader();
debug(), "读到了", s;
if (s == "quit\n") break;
}
}
int main() {
int attr = 1;
ioctl(0, FIONBIO, &attr);
auto t = async_main();
t.mCoroutine.resume();
while (!t.mCoroutine.done()) {
loop.tryRun();
}
return 0;
}