forked from rbeeli/websocketclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.hpp
329 lines (277 loc) · 7.79 KB
/
socket.hpp
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
#pragma once
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <variant>
#include "poller.hpp"
#include "corochain.hpp"
namespace NNet
{
class TAddress
{
public:
TAddress(const std::string& addr, int port);
TAddress(sockaddr_in addr);
TAddress(sockaddr_in6 addr);
TAddress(sockaddr* addr, socklen_t len);
TAddress() = default;
const std::variant<sockaddr_in, sockaddr_in6>& Addr() const;
std::pair<const sockaddr*, int> RawAddr() const;
bool operator==(const TAddress& other) const;
int Domain() const;
TAddress WithPort(int port) const;
std::string ToString() const;
private:
std::variant<sockaddr_in, sockaddr_in6> Addr_ = {};
};
class TSocketOps
{
public:
TSocketOps(TPollerBase& poller, int domain, int type);
TSocketOps(int fd, TPollerBase& poller);
TSocketOps() = default;
TPollerBase* Poller()
{
return Poller_;
}
protected:
int Create(int domain, int type);
int Setup(int s);
TPollerBase* Poller_ = nullptr;
int Fd_ = -1;
};
template <typename TSockOps>
class TSocketBase : public TSocketOps
{
public:
TSocketBase(TPollerBase& poller, int domain, int type) : TSocketOps(poller, domain, type)
{
}
TSocketBase(int fd, TPollerBase& poller) : TSocketOps(fd, poller)
{
}
TSocketBase() = default;
auto ReadSome(void* buf, size_t size)
{
struct TAwaitableRead : public TAwaitable<TAwaitableRead>
{
void run()
{
this->ret = static_cast<int>(TSockOps::read(this->fd, this->b, this->s));
}
void await_suspend(std::coroutine_handle<> h)
{
this->poller->AddRead(this->fd, h);
}
};
return TAwaitableRead{Poller_, Fd_, buf, size, 0, false};
}
// force read on next loop iteration
auto ReadSomeYield(void* buf, size_t size)
{
struct TAwaitableRead : public TAwaitable<TAwaitableRead>
{
bool await_ready()
{
return (this->ready = false);
}
void run()
{
this->ret = TSockOps::read(this->fd, this->b, this->s);
}
void await_suspend(std::coroutine_handle<> h)
{
this->poller->AddRead(this->fd, h);
}
};
return TAwaitableRead{Poller_, Fd_, buf, size, 0, false};
}
auto WriteSome(const void* buf, size_t size)
{
struct TAwaitableWrite : public TAwaitable<TAwaitableWrite>
{
void run()
{
this->ret = static_cast<int>(TSockOps::write(this->fd, this->b, this->s));
}
void await_suspend(std::coroutine_handle<> h)
{
this->poller->AddWrite(this->fd, h);
}
};
return TAwaitableWrite{Poller_, Fd_, const_cast<void*>(buf), size, 0, false};
}
auto WriteSomeYield(const void* buf, size_t size)
{
struct TAwaitableWrite : public TAwaitable<TAwaitableWrite>
{
bool await_ready()
{
return (this->ready = false);
}
void run()
{
this->ret = TSockOps::write(this->fd, this->b, this->s);
}
void await_suspend(std::coroutine_handle<> h)
{
this->poller->AddWrite(this->fd, h);
}
};
return TAwaitableWrite{Poller_, Fd_, const_cast<void*>(buf), size, 0, false};
}
protected:
template <typename T>
struct TAwaitable
{
bool await_ready()
{
SafeRun();
return (ready = (ret >= 0));
}
int await_resume()
{
if (!ready)
{
SafeRun();
}
return ret;
}
void SafeRun()
{
((T*)this)->run();
if (ret < 0 && !(errno == EINTR || errno == EAGAIN || errno == EINPROGRESS))
{
throw std::system_error(errno, std::generic_category());
}
}
TPollerBase* poller;
int fd;
void* b;
size_t s;
int ret;
bool ready;
};
};
class TFileOps
{
public:
static auto read(int fd, void* buf, size_t count)
{
return ::read(fd, buf, count);
}
static auto write(int fd, const void* buf, size_t count)
{
return ::write(fd, buf, count);
}
};
class TFileHandle : public TSocketBase<TFileOps>
{
public:
TFileHandle(int fd, TPollerBase& poller) : TSocketBase(fd, poller)
{
}
TFileHandle() = default;
};
class TSockOps
{
public:
static auto read(int fd, void* buf, size_t count)
{
return ::recv(fd, buf, count, 0);
}
static auto write(int fd, const void* buf, size_t count)
{
return ::send(fd, buf, count, 0);
}
};
class TSocket : public TSocketBase<TSockOps>
{
public:
using TPoller = TPollerBase;
TSocket(TAddress&& addr, TPollerBase& poller, int type = SOCK_STREAM);
TSocket(const TAddress& addr, int fd, TPollerBase& poller);
TSocket(const TAddress& addr, TPollerBase& poller, int type = SOCK_STREAM);
TSocket(TSocket&& other);
~TSocket();
TSocket() = default;
TSocket(const TSocket& other) = delete;
TSocket& operator=(TSocket& other) const = delete;
TSocket& operator=(TSocket&& other);
TValueTask<void> Close();
auto Connect(TTime deadline = TTime::max())
{
struct TAwaitable
{
bool await_ready()
{
int ret = connect(fd, addr.first, addr.second);
if (ret < 0 && !(errno == EINTR || errno == EAGAIN || errno == EINPROGRESS))
{
throw std::system_error(errno, std::generic_category(), "connect");
}
return ret >= 0;
}
void await_suspend(std::coroutine_handle<> h)
{
poller->AddWrite(fd, h);
if (deadline != TTime::max())
{
poller->AddTimer(fd, deadline, h);
}
}
void await_resume()
{
if (deadline != TTime::max() && poller->RemoveTimer(fd, deadline))
{
throw std::system_error(std::make_error_code(std::errc::timed_out));
}
}
TPollerBase* poller;
int fd;
std::pair<const sockaddr*, int> addr;
TTime deadline;
};
return TAwaitable{Poller_, Fd_, Addr_.RawAddr(), deadline};
}
auto Accept()
{
struct TAwaitable
{
bool await_ready() const
{
return false;
}
void await_suspend(std::coroutine_handle<> h)
{
poller->AddRead(fd, h);
}
TSocket await_resume()
{
char clientaddr[sizeof(sockaddr_in6)];
socklen_t len = sizeof(sockaddr_in6);
int clientfd = accept(fd, reinterpret_cast<sockaddr*>(&clientaddr[0]), &len);
if (clientfd < 0)
{
throw std::system_error(errno, std::generic_category(), "accept");
}
return TSocket{
TAddress{reinterpret_cast<sockaddr*>(&clientaddr[0]), len}, clientfd, *poller};
}
TPollerBase* poller;
int fd;
};
return TAwaitable{Poller_, Fd_};
}
void Bind();
void Listen(int backlog = 128);
const TAddress& Addr() const;
int Fd() const;
protected:
TAddress Addr_;
};
} // namespace NNet