22 using port_t = uint16_t;
23 using address_t = uint32_t;
25 using con_t = uint64_t;
31 tag NONE = 0x0, WG_HANDSHAKE = 0x1, SYS = 0x2, WG_COOKIE = 0x3, WG_TRANSPORT = 0x4;
59 return std::to_string(c.pair.a) +
":" + std::to_string(c.pair.p);
77 clock_gettime(CLOCK_REALTIME, &ts);
78 uint64_t seconds = ts.tv_sec; uint32_t nano = ts.tv_nsec;
81 memcpy(time.
bytes(),
reinterpret_cast<char*
>(&seconds),
sizeof(uint64_t));
82 memcpy(time.
bytes() + 8,
reinterpret_cast<char*
>(&nano),
sizeof(uint32_t));
98 sec_a = *
reinterpret_cast<const uint64_t*
>(a.
bytes()),
99 sec_b = *
reinterpret_cast<const uint64_t*
>(b.
bytes());
100 if (sec_a > sec_b)
return true;
101 if (sec_a < sec_b)
return false;
105 nan_a = *
reinterpret_cast<const uint32_t*
>(a.
bytes() + 8),
106 nan_b = *
reinterpret_cast<const uint32_t*
>(b.
bytes() + 8);
109 if (nan_a >= nan_b)
return true;
122 uint64_t as = *
reinterpret_cast<const uint64_t*
>(a.
bytes()), bs = *
reinterpret_cast<const uint64_t*
>(b.bytes());
123 return as < bs ? bs - as : as - bs;
133 clock_gettime(CLOCK_REALTIME, &ts);
134 uint64_t seconds = ts.tv_sec; uint32_t nano = ts.tv_nsec;
141 connection self = {.pair {.a = inet_addr(
"127.0.0.1"), .p = 5000}};
161 unsigned char pub[32] = {0};
170 typedef enum {DEAD, INIT, READY, TERMINATE}
status;
173 std::atomic<status> stat = DEAD;
174 std::atomic<bool> verbose =
false;
175 std::atomic<bool> log =
false;
176 std::string logfile =
"main.log";
179 std::random_device dev;
180 std::mt19937 rng(dev());
186 typedef enum {STANDARD, INFO, WARN, ERROR, SUCCESS} message_type;
188 END[] =
"\e[0m", RED[] =
"\e[31m", YELLOW[] =
"\e[1;33m",
189 GREEN[] =
"\e[32m", BLUE[] =
"\e[34m", VIOLET[] =
"\e[35m";
198 void sleep(
const size_t& milliseconds=100) {std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));}
208 void output(
const std::string& message,
const std::string& thread=
"SYS",
const message_type& t=STANDARD) {
209 std::lock_guard<std::mutex> guard(io_lock);
212 if (log && t != STANDARD) {
213 auto outfile = std::ofstream(logfile, std::ios_base::app);
215 outfile << thread <<
": " << message << std::endl;
220 if (t == STANDARD) std::cout << message << std::endl;
221 else if (t == ERROR) std::cerr << RED << thread <<
"! " << message << END << std::endl;
222 else if (t == SUCCESS) std::cerr << GREEN << thread <<
": " << message << END << std::endl;
226 if (t == INFO) std::cout << thread <<
": " << message << std::endl;
227 else if (t == WARN) std::cout << YELLOW << thread <<
": " << message << END << std::endl;
233 #define prompt_return(msg) {prompt(msg); return;}
235 #define prompt_break(msg) {prompt(msg); break;}
237 #define prompt_continue(msg) {prompt(msg); continue;}
257 template <
typename T =
bool>
inline T
input(
const std::string& title,
const T& error_ret = T()) {
265 auto f = std::cin.fail();
272 std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
284 inline void prompt(
const std::string& message) {
286 output(
"Press Enter to Continue");
292 std::vector<std::vector<std::string>> titles = {
297 " __ ______ ____ _ ________",
298 " / / / / __ \\/ __ \\ | | / / ____/",
299 " / / / / / / / /_/ /____| | /| / / / __",
300 "/ /_/ / /_/ / ____/_____/ |/ |/ / /_/ /",
301 "\\____/_____/_/ |__/|__/\\____/"
304 "__/\\\\\\________/\\\\\\__/\\\\\\\\\\\\\\\\\\\\\\\\_____/\\\\\\\\\\\\\\\\\\\\\\\\\\__________________/\\\\\\______________/\\\\\\_____/\\\\\\\\\\\\\\\\\\\\\\\\_ ",
305 " _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\////////\\\\\\__\\/\\\\\\/////////\\\\\\_______________\\/\\\\\\_____________\\/\\\\\\___/\\\\\\//////////__ ",
306 " _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\______\\//\\\\\\_\\/\\\\\\_______\\/\\\\\\_______________\\/\\\\\\_____________\\/\\\\\\__/\\\\\\_____________ ",
307 " _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\\\\\\\\\\\\\\\\\\\\\/___/\\\\\\\\\\\\\\\\\\\\\\_\\//\\\\\\____/\\\\\\____/\\\\\\__\\/\\\\\\____/\\\\\\\\\\\\\\_ ",
308 " _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\/////////____\\///////////___\\//\\\\\\__/\\\\\\\\\\__/\\\\\\___\\/\\\\\\___\\/////\\\\\\_ ",
309 " _\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\_______\\/\\\\\\_\\/\\\\\\_____________________________\\//\\\\\\/\\\\\\/\\\\\\/\\\\\\____\\/\\\\\\_______\\/\\\\\\_ ",
310 " _\\//\\\\\\______/\\\\\\__\\/\\\\\\_______/\\\\\\__\\/\\\\\\______________________________\\//\\\\\\\\\\\\//\\\\\\\\\\_____\\/\\\\\\_______\\/\\\\\\_ ",
311 " __\\///\\\\\\\\\\\\\\\\\\/___\\/\\\\\\\\\\\\\\\\\\\\\\\\/___\\/\\\\\\_______________________________\\//\\\\\\__\\//\\\\\\______\\//\\\\\\\\\\\\\\\\\\\\\\\\/__ ",
312 " ____\\/////////_____\\////////////_____\\///_________________________________\\///____\\///________\\////////////____",
315 "$$\\ $$\\ $$$$$$$\\ $$$$$$$\\ $$\\ $$\\ $$$$$$\\ ",
316 "$$ | $$ |$$ __$$\\ $$ __$$\\ $$ | $\\ $$ |$$ __$$\\ ",
317 "$$ | $$ |$$ | $$ |$$ | $$ | $$ |$$$\\ $$ |$$ / \\__|",
318 "$$ | $$ |$$ | $$ |$$$$$$$ |$$$$$$\\ $$ $$ $$\\$$ |$$ |$$$$\\",
319 "$$ | $$ |$$ | $$ |$$ ____/ \\______|$$$$ _$$$$ |$$ |\\_$$ |",
320 "$$ | $$ |$$ | $$ |$$ | $$$ / \\$$$ |$$ | $$ |",
321 "\\$$$$$$ |$$$$$$$ |$$ | $$ / \\$$ |\\$$$$$$ |",
322 " \\______/ \\_______/ \\__| \\__/ \\__| \\______/",
325 "888 888 8888888b. 8888888b. 888 888 .d8888b.",
326 "888 888 888 \"Y88b 888 Y88b 888 o 888 d88P Y88b",
327 "888 888 888 888 888 888 888 d8b 888 888 888",
328 "888 888 888 888 888 d88P 888 d888b 888 888",
329 "888 888 888 888 8888888P\" 888d88888b888 888 88888",
330 "888 888 888 888 888 888888 88888P Y88888 888 888",
331 "Y88b. .d88P 888 .d88P 888 8888P Y8888 Y88b d88P",
332 " \"Y88888P\" 8888888P\" 888 888P Y888 \"Y8888P88",
336 size_t title = 0, mode = 0;
344 std::uniform_int_distribution<std::mt19937::result_type> mode_dist(1,6);
345 mode = mode_dist(rng);
346 }
while (mode == old);
357 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
358 auto width = w.ws_col;
361 if (retry == 100) title = 0;
362 std::uniform_int_distribution<std::mt19937::result_type> title_dist(0,titles.size() - 1);
363 title = title_dist(rng);
365 }
while (titles[title][0].length() > width || title == old);
369 std::string print_title(
const uint64_t& cycle) {
370 std::stringstream ret;
371 std::vector<std::string> lookup = {RED, YELLOW, GREEN, BLUE, VIOLET};
373 for (
size_t x = 0; x < titles[title].size(); ++x) {
374 for (
size_t y = 0; y < titles[title][x].length(); ++y) {
375 size_t h = titles[title][x].length(), v = titles[title].size();
378 case 1: ret << lookup[(x + y + cycle) % 5];
break;
379 case 2: ret << lookup[(x + cycle) % 5];
break;
380 case 3: ret << lookup[(y + cycle) % 5];
break;
381 case 4: ret << lookup[((v - x) + (h - y) + cycle) % 5];
break;
382 case 5: ret << lookup[((v - x) + cycle) % 5];
break;
383 case 6: ret << lookup[((h - y) + cycle) % 5];
break;
386 ret << titles[title][x][y];
T input(const std::string &title, const T &error_ret=T())
std::cin can be a little difficult to use, particularly handling bad input. This sanitized it.
Definition shared.h:257
void output(const std::string &message, const std::string &thread="SYS", const message_type &t=STANDARD)
Output to STDOUT, thread safe.
Definition shared.h:208