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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
#include <iostream>
#include <coroutine>
#include <span>
#include <optional>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <cassert>
#include <vector>
void load_flag()
{
char flag[400];
FILE* fp = fopen("flag", "rt");
fscanf(fp, "%s", flag);
fclose(fp);
}
struct io_context
{
struct entry
{
int fd;
std::coroutine_handle<> coroutine;
};
std::vector<entry> reads_;
std::vector<entry> writes_;
void run_until_done()
{
while (!reads_.empty() || !writes_.empty())
{
load_flag();
fd_set readfds;
FD_ZERO(&readfds);
fd_set writefds;
FD_ZERO(&writefds);
fd_set exceptfds;
FD_ZERO(&exceptfds);
int maxfd = 0;
for (auto& read : reads_)
{
maxfd = std::max(maxfd, read.fd);
FD_SET(read.fd, &readfds);
FD_SET(read.fd, &exceptfds);
}
for (auto& write : writes_)
{
maxfd = std::max(maxfd, write.fd);
FD_SET(write.fd, &writefds);
FD_SET(write.fd, &exceptfds);
}
int donefds = ::select(maxfd + 1, &readfds, &writefds, &exceptfds, /*timeout*/nullptr);
if (donefds == -1)
{
std::cerr << "Error performing select() errno: " << errno << '\n';
return;
}
// Take a copy as this may change
auto reads = reads_;
auto writes = writes_;
for (auto & read : reads)
{
if (FD_ISSET(read.fd, &readfds) || FD_ISSET(read.fd, &exceptfds))
{
auto coroutine = std::move(read.coroutine);
reads_.erase(std::remove_if(reads_.begin(), reads_.end(), [fd = read.fd](auto& item) { return item.fd == fd; }));
coroutine.resume();
continue;
}
}
for (auto& write : writes)
{
if (FD_ISSET(write.fd, &writefds) || FD_ISSET(write.fd, &exceptfds))
{
auto coroutine = std::move(write.coroutine);
writes_.erase(std::remove_if(writes_.begin(), writes_.end(), [fd = write.fd](auto& item) { return item.fd == fd; }));
coroutine.resume();
continue;
}
}
}
}
void add_read(int fd, std::coroutine_handle<> coroutine)
{
#ifdef _DEBUG
for (auto& read : reads_)
{
assert(read.fd != fd);
}
#endif
reads_.push_back({ fd, std::move(coroutine) });
}
void add_write(int fd, std::coroutine_handle<> coroutine)
{
#ifdef _DEBUG
for (auto& write: writes_)
{
assert(write.fd != fd);
}
#endif
writes_.push_back({ fd, std::move(coroutine) });
}
void cancel(int fd)
{
auto rit = reads_.begin();
while (rit != reads_.end())
{
if (rit->fd == fd)
{
reads_.erase(rit);
break;
}
}
auto wit = writes_.begin();
while (wit != writes_.end())
{
if (wit->fd == fd)
{
writes_.erase(wit);
break;
}
}
}
};
template<typename Result>
class Task {
public:
struct promise_type {
std::optional<Result> result_;
Task get_return_object() {
return Task{ this };
}
void unhandled_exception() noexcept {}
void return_value(Result result) noexcept { result_ = std::move(result); }
std::suspend_never initial_suspend() { return {}; }
struct FinalAwaiter {
constexpr bool await_ready() noexcept { return false; }
std::coroutine_handle<>
await_suspend(std::coroutine_handle<promise_type> coroutine) noexcept {
auto& promise = coroutine.promise();
return promise.continuation_ != nullptr ? promise.continuation_ : std::noop_coroutine();
}
// nothing to do, the coroutine will no longer be executed
void await_resume() noexcept { }
};
FinalAwaiter final_suspend() noexcept { return {}; }
void set_continuation(std::coroutine_handle<> continuation) {
continuation_ = continuation;
}
std::coroutine_handle<> continuation_;
};
explicit Task(promise_type* promise)
: handle_{ HandleT::from_promise(*promise) } {}
Task(Task&& other) : handle_{ std::exchange(other.handle_, nullptr) } { }
~Task() {
if (handle_) {
handle_.destroy();
}
}
Task & operator=(const Task&) = delete;
auto operator co_await() {
struct Awaiter {
std::coroutine_handle<promise_type> this_handle;
constexpr bool await_ready() {
assert(this_handle);
return this_handle.done();
}
void await_suspend(std::coroutine_handle<> awaiting_coroutine) {
this_handle.promise().set_continuation(awaiting_coroutine);
}
Result await_resume() {
return *this_handle.promise().result_;
}
};
return Awaiter{ handle_ };
}
using HandleT = std::coroutine_handle<promise_type>;
HandleT handle_;
};
struct NonCopyable
{
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};
int MakeNonBlocking(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
flags |= O_NONBLOCK;
return fcntl(fd, F_SETFL, flags);
}
class RecvAsync : NonCopyable {
public:
RecvAsync(io_context& ctx, int fd, std::span<std::byte> buffer) : NonCopyable(),
ctx_{ ctx },
fd_{ fd },
buffer_{ buffer }
{
}
auto operator co_await() {
struct Awaiter {
io_context& ctx_;
int fd_;
std::optional<int> result_;
std::span<std::byte> buffer_;
Awaiter(io_context& ctx, int fd, std::span<std::byte> buffer) :
ctx_{ ctx }, fd_{ fd }, buffer_{ buffer } {
}
bool await_ready() {
int result = ::recv(fd_, buffer_.data(), buffer_.size(), 0);
if (result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) )
{
return false;
}
result_ = result;
return true;
}
void await_suspend(std::coroutine_handle<> handle) noexcept {
ctx_.add_read(fd_, std::move(handle));
}
int await_resume() {
if (result_.has_value())
{
return result_.value();
}
int result = ::recv(fd_, buffer_.data(), buffer_.size(), 0);
if (result == -1)
{
assert(errno != EAGAIN && errno != EWOULDBLOCK);
}
return result;
}
};
return Awaiter{ ctx_, fd_, buffer_ };
}
private:
io_context& ctx_;
int fd_;
std::span<std::byte> buffer_;
};
class SendAsync : NonCopyable {
public:
SendAsync(io_context& ctx, int fd, std::span<std::byte> buffer) : NonCopyable(),
ctx_{ ctx },
fd_{ fd },
buffer_{ buffer }
{
}
auto operator co_await() {
struct Awaiter {
io_context& ctx_;
int fd_;
std::optional<int> result_;
std::span<std::byte> buffer_;
Awaiter(io_context& ctx, int fd, std::span<std::byte> buffer) :
ctx_{ ctx }, fd_{ fd }, buffer_{ buffer } {
}
bool await_ready() {
int result = ::send(fd_, buffer_.data(), buffer_.size(), 0);
if (result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK))
{
return false;
}
result_ = result;
return true;
}
void await_suspend(std::coroutine_handle<> handle) noexcept {
ctx_.add_write(fd_, std::move(handle));
}
int await_resume() {
if (result_.has_value())
{
return result_.value();
}
int result = ::send(fd_, buffer_.data(), buffer_.size(), 0);
if (result == -1)
{
assert(errno != EAGAIN && errno != EWOULDBLOCK);
}
return result;
}
};
return Awaiter{ ctx_, fd_, buffer_ };
}
private:
io_context& ctx_;
int fd_;
std::span<std::byte> buffer_;
};
Task<bool> SendAllAsync(io_context& ctx, int socket, std::span<std::byte> buffer)
{
int offset = 0;
while (offset < buffer.size())
{
int result = co_await SendAsync(ctx, socket, std::span(buffer.data() + offset, buffer.size() - offset));
if (result == -1)
{
co_return false;
}
offset += result;
}
co_return true;
}
Task<bool> SendAllAsyncNewline(io_context& ctx, int socket, std::span<std::byte> buffer)
{
std::byte buffer2[513];
std::copy(buffer.begin(), buffer.end(), buffer2);
buffer2[buffer.size()] = (std::byte)'\n';
return SendAllAsync(ctx, socket, std::span(buffer2, buffer.size()+1));
}
Task<bool> client_loop(io_context& ctx, int socket)
{
while (true)
{
std::byte buffer[512];
int recved = co_await RecvAsync(ctx, socket, buffer);
if (recved == 0)
{
std::cout << "Disconnected\n";
co_return true;
}
if (recved == -1)
{
std::cout << "Could not receive\n";
co_return false;
}
bool send_result = co_await SendAllAsyncNewline(ctx, socket, std::span(buffer, recved));
if (!send_result)
{
std::cout << "Could not send: " << errno << "\n";
co_return false;
}
}
}
Task<bool> server(io_context & ctx)
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock != -1);
int listen_result = listen(sock, 1);
assert(listen_result != -1);
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
int getsockname_result = ::getsockname(sock, (sockaddr*)&sin, &len);
assert(getsockname_result != -1);
std::cout << "port number " << ntohs(sin.sin_port) << "\n";
int client_size;
struct sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
int accept_result = accept(sock, (sockaddr*)&client_addr, &client_addr_size);
assert(accept_result != -1);
MakeNonBlocking(accept_result);
int sendbuff = 128;
setsockopt(accept_result, SOL_SOCKET, SO_SNDBUF, &sendbuff, sizeof(sendbuff)); // sendbuf 128
co_await client_loop(ctx, accept_result);
co_return true;
}
int main(int argc, char* argv[])
{
setbuf(stdout, nullptr);
io_context ctx;
auto s = server(ctx);
ctx.run_until_done();
return EXIT_SUCCESS;
}
|