-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboilerplate.h
144 lines (120 loc) · 3.83 KB
/
boilerplate.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
#ifndef __BOILERPLATE__
#define __BOILERPLATE__
#include <ctime>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <vector>
time_t strtotime_t(std::string const &str) {
std::tm date = {};
std::istringstream range(str);
range >> std::get_time(&date, "%Y-%m-%d %H:%M:%S");
return std::mktime(&date);
}
time_t min_year() {
static time_t date = strtotime_t("1717-01-01 00:00:00");
return date;
}
time_t max_year() {
static time_t date = strtotime_t("4243-01-01 00:00:00");
return date;
}
std::string const addr_humanreadable(struct sockaddr_in *ipv4) {
char ipcstr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(ipv4->sin_addr), ipcstr, INET_ADDRSTRLEN);
return std::string(ipcstr) + ":" + std::to_string(ntohs(ipv4->sin_port));
}
class Message {
private:
time_t timestamp;
uint64_t decimal_timestamp;
char c;
std::string file_viscera;
bool error = false;
static std::string s_buffer;
public:
static size_t const MAX_ENTRAILS_SIZE = 65536;
// For client creating the message
Message(std::string const ×tamp, char c) : c(c) {
std::istringstream iss(timestamp);
iss >> this->decimal_timestamp;
if (iss.fail() || timestamp.compare(std::to_string(this->decimal_timestamp)) != 0) {
error = true;
return;
}
this->timestamp = static_cast<time_t>(this->decimal_timestamp);
}
// Decode message sent on the net
Message(std::string const &serialized) {
if (serialized.length() > MAX_ENTRAILS_SIZE || serialized.length() < 9) {
error = true;
return;
}
this->c = serialized[8];
file_viscera = serialized.substr(9);
this->decimal_timestamp = *reinterpret_cast<uint64_t const*>(serialized.c_str());
if (!is_big_endian()) {
this->decimal_timestamp = bswap(this->decimal_timestamp);
}
this->timestamp = static_cast<time_t>(this->decimal_timestamp);
}
bool failed() const {
return error;
}
bool is_correct() const {
return (!error && std::difftime(max_year(), timestamp) > 0
&& std::difftime(timestamp, min_year()) >= 0);
}
char const *serialize() const {
uint64_t ts = this->decimal_timestamp;
if (!is_big_endian()) {
ts = bswap(ts);
}
char *ts_str = reinterpret_cast<char *>(&ts);
for (size_t i = 0; i < 8; i++) {
s_buffer[i] = *(ts_str + i);
}
s_buffer[8] = this->c;
return s_buffer.c_str();
}
size_t const ssize() const {
return s_buffer.length();
}
static uint64_t bswap(uint64_t number) {
uint64_t const every2b = UINT64_C(0xFF00FF00FF00FF00);
uint64_t const every2bo = UINT64_C(0xFF00FF00FF00FF);
uint64_t const every4b = UINT64_C(0xFFFF0000FFFF0000);
uint64_t const every4bo = UINT64_C(0xFFFF0000FFFF);
number = ((number << 8) & every2b) | ((number >> 8) & every2bo);
number = ((number << 16) & every4b) | ((number >> 16) & every4bo);
number = (number << 32) | (number >> 32);
return number;
}
static bool is_big_endian() {
int const guinea_pig = 1;
return !(*reinterpret_cast<char const*>(&guinea_pig));
}
static void initialize_serializer(std::string const &file_content) {
s_buffer = std::string(9, ' ') + file_content;
}
friend std::ostream &operator<<(std::ostream &os, Message const &ms);
};
std::ostream& operator<<(std::ostream &os, Message const &ms) {
if (ms.is_correct()) {
os << ms.decimal_timestamp << " " << ms.c << " " << ms.file_viscera;
}
return os;
}
std::string Message::s_buffer;
#endif /* __BOILERPLATE__ */