-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutils.h
385 lines (320 loc) · 8.13 KB
/
utils.h
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
#ifndef KCPTUN_UTILS_H
#define KCPTUN_UTILS_H
#include <algorithm>
#include "asio.hpp"
#include "asio/high_resolution_timer.hpp"
#include <assert.h>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <memory>
#include <random>
#include <stddef.h>
#include <stdint.h>
#include <streambuf>
#include <string>
#include <system_error>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <chrono>
#include <map>
#include <array>
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "zlib.h"
#include "encoding.h"
enum { nonce_size = 16, crc_size = 4 };
enum { mtu_limit = 1500 };
// support make_unique in c++ 11
template <typename T, typename... Args>
inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
my_make_unique(Args &&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T>
inline typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
std::unique_ptr<T>>::type
my_make_unique(size_t size) {
using U = typename std::remove_extent<T>::type;
return std::unique_ptr<T>(new U[size]());
}
template <typename T, typename... Args>
typename std::enable_if<std::extent<T>::value != 0, void>::type
my_make_unique(Args &&...) = delete;
using Handler = std::function<void(std::error_code, std::size_t)>;
using OutputHandler = std::function<void(char *, std::size_t, Handler)>;
static inline uint64_t
current_monotonic_usec()
{
auto now = std::chrono::steady_clock::now();
auto us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
return us;
}
static inline void
itimeofday(long *sec, long *usec)
{
auto us = current_monotonic_usec();
if (sec) {
*sec = us / 1000000;
}
if (usec) {
*usec = us % 1000000;
}
}
static inline uint64_t
iclock64(void) {
auto us = current_monotonic_usec();
auto ms = us / 1000;
return ms;
}
static uint32_t iclock() { return (uint32_t)(iclock64() & 0xfffffffful); }
struct Task {
Task() { reset(); }
Task(char *buf, std::size_t len, Handler handler)
: buf(buf), len(len), handler(handler) {}
char *buf;
std::size_t len;
Handler handler;
int c = 8;
void reset() {
buf = nullptr;
len = 0;
handler = nullptr;
}
bool check() { return buf != nullptr && len != 0; }
};
static inline uint32_t
crc32c_ieee(uint32_t crc, const unsigned char *buf, size_t len)
{
return crc32(crc, (const Bytef *)buf, len);
}
static inline std::error_code errc(int c) {
return std::error_code(c, std::generic_category());
}
#define TRACE
// std::cout << __func__ << " " << __LINE__ << " " << __FILE__ << std::endl;
class clean_ {
public:
clean_(const clean_ &) = delete;
clean_(clean_ &&) = delete;
clean_ &operator=(const clean_ &) = delete;
clean_() = default;
};
class DeferCaller final : clean_ {
public:
DeferCaller(std::function<void()> &&functor)
: functor_(std::move(functor)) {}
DeferCaller(const std::function<void()> &functor) : functor_(functor) {}
~DeferCaller() {
if (functor_)
functor_();
}
void cancel() { functor_ = nullptr; }
private:
std::function<void()> functor_;
};
class ConstructCaller final : clean_ {
public:
ConstructCaller(std::function<void()> &&functor) {
functor();
}
};
class Destroy : public clean_ {
public:
~Destroy() {
destroy();
}
void call_on_destroy(const std::function<void()> &h) {
destroy_handlers_.push_back(h);
}
void call_on_destroy(std::function<void()> &&h) {
destroy_handlers_.emplace_back(h);
}
void destroy() {
if (destroy_) {
return;
}
destroy_ = true;
call_this_on_destroy();
for (auto &h : destroy_handlers_) {
if (h) {
h();
}
}
destroy_handlers_.clear();
}
bool is_destroyed() const {
return destroy_;
}
protected:
virtual void call_this_on_destroy() {}
private:
bool destroy_ = false;
std::vector<std::function<void()>> destroy_handlers_;
};
class AsyncReadWriter {
public:
virtual ~AsyncReadWriter() = default;
virtual void async_read_some(char *buf, std::size_t len,
Handler handler) = 0;
virtual void async_write(char *buf, std::size_t len, Handler handler) = 0;
};
class AsyncInOutputer {
public:
AsyncInOutputer() = default;
AsyncInOutputer(OutputHandler o) : o_(o) {}
virtual ~AsyncInOutputer() = default;
void set_output_handler(OutputHandler o) { o_ = o; }
virtual void async_input(char *buf, std::size_t len, Handler handler) = 0;
protected:
void output(char *buf, std::size_t len, Handler handler) {
o_(buf, len, handler);
}
private:
OutputHandler o_;
};
class UsocketReadWriter : public AsyncReadWriter {
public:
UsocketReadWriter(asio::ip::udp::socket &&usocket,
asio::ip::udp::endpoint ep)
: usocket_(std::move(usocket)), ep_(ep) {}
UsocketReadWriter(asio::ip::udp::socket &&usocket)
: usocket_(std::move(usocket)), connected_(true) {}
void async_read_some(char *buf, std::size_t len, Handler handler) override {
usocket_.async_receive(asio::buffer(buf, len), handler);
}
void async_write(char *buf, std::size_t len, Handler handler) override {
if (connected_) {
usocket_.async_send(asio::buffer(buf, len), handler);
} else {
usocket_.async_send_to(asio::buffer(buf, len), ep_, handler);
}
}
private:
bool connected_ = false;
asio::ip::udp::socket usocket_;
asio::ip::udp::endpoint ep_;
};
static inline const char *get_bool_str(bool b) {
if (b) {
return "true";
}
return "false";
}
class Buffers final {
public:
explicit Buffers(std::size_t n = 2048) : n(n) {}
~Buffers() {
for (auto &buf : all_bufs_) {
free(buf);
}
}
Buffers(const Buffers &b) {
n = b.n;
}
Buffers &operator=(const Buffers &b) {
n = b.n;
return *this;
}
void reset(std::size_t n2) {
n = n2;
}
void push_back(char *buf);
char *get();
std::size_t capacity() const {
return all_bufs_.size();
}
std::size_t size() const {
return bufs_.size();
}
private:
std::size_t n;
std::unordered_set<char *> bufs_;
std::unordered_set<char *> all_bufs_;
};
class kvar final {
public:
explicit kvar(const std::string &name);
~kvar();
void add(int i) {
(*p) += i;
}
void sub(int i) {
(*p) -= i;
}
int get() {
return *p;
}
private:
int *p;
std::string name_;
};
class kvar_ : public clean_ {
public:
explicit kvar_(kvar &kv) : v_(kv) {
v_.add(1);
}
~kvar_() {
v_.sub(1);
}
private:
kvar &v_;
};
void printKvars();
void run_kvar_printer(asio::io_service &service);
struct buffer final {
public:
buffer();
~buffer();
buffer(const buffer &) = delete;
buffer &operator =(buffer &) = delete;
buffer(buffer &&other) noexcept;
std::size_t size() {
return len;
}
std::size_t aval() {
return cap - off - len;
}
char *start() {
return buf + off;
}
char *end() {
return buf + off + len;
}
void append(char *b, std::size_t sz) {
memcpy(end(), b, sz);
len += sz;
}
void retrieve(char *b, std::size_t sz) {
memcpy(b, start(), sz);
off += sz;
len -= sz;
}
char *buf = nullptr;
std::size_t off = 0;
std::size_t len = 0;
std::size_t cap = 0;
private:
void init();
};
class LinearBuffer final : public clean_ {
public:
LinearBuffer();
~LinearBuffer();
std::size_t size();
void append(char *buf, std::size_t len);
void retrieve(char *buf, std::size_t len);
private:
std::deque<buffer> bufs_;
};
#endif // KCPTUN_UTILS_H