forked from DylanZA/netbench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.cpp
179 lines (158 loc) · 4.33 KB
/
control.cpp
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
#include "control.h"
#include <poll.h>
#include <atomic>
#include <cstring>
#include <thread>
#include <vector>
#include "socket.h"
#include "util.h"
namespace {
template <class T>
void addT(T i, std::vector<uint8_t>& data) {
size_t was = data.size();
data.resize(data.size() + sizeof(i));
memcpy(data.data() + was, &i, sizeof(i));
}
void addS(std::string const& s, std::vector<uint8_t>& data) {
addT<uint32_t>(s.size(), data);
size_t was = data.size();
data.resize(data.size() + s.size());
memcpy(data.data() + was, s.data(), s.size());
}
template <class T>
T readT(uint8_t*& at, size_t& len) {
if (len < sizeof(T)) {
die("not enough data len=", len);
}
T ret;
memcpy(&ret, at, sizeof(T));
len -= sizeof(T);
at += sizeof(T);
return ret;
}
std::string readS(uint8_t*& at, size_t& len) {
uint32_t str_len = readT<uint32_t>(at, len);
if (len < str_len) {
die("not enough data len=", len, " str_len=", str_len);
}
std::string ret;
ret.resize(str_len);
memcpy(ret.data(), at, str_len);
len -= str_len;
at += str_len;
return ret;
}
std::vector<uint8_t> encode(std::unordered_map<uint16_t, std::string> data) {
std::vector<uint8_t> ret;
addT<uint32_t>(data.size(), ret);
for (auto const& kv : data) {
addT<uint16_t>(kv.first, ret);
addS(kv.second, ret);
}
return ret;
}
std::vector<uint8_t> payload(std::vector<uint8_t> data) {
uint32_t size = data.size();
std::vector<uint8_t> ret;
ret.resize(data.size() + sizeof(size));
memcpy(ret.data(), &size, sizeof(size));
memcpy(ret.data() + sizeof(size), data.data(), data.size());
return ret;
}
std::unordered_map<uint16_t, std::string> decode(std::vector<uint8_t> data) {
std::unordered_map<uint16_t, std::string> ret;
uint8_t* at = data.data();
size_t len = data.size();
uint32_t count = readT<uint32_t>(at, len);
for (uint32_t i = 0; i < count; i++) {
uint16_t port = readT<uint16_t>(at, len);
std::string str = readS(at, len);
ret[port] = std::move(str);
}
return ret;
}
} // namespace
class ControlServer : public IControlServer {
public:
ControlServer(std::vector<uint8_t> data, uint16_t port, bool ipv6)
: data_(std::move(data)) {
fd_ = checkedErrno(mkBoundSock(port, ipv6), "mkBoundSock control server");
thread_ = std::thread(wrapThread("ControlServer", [this]() { go(); }));
}
void go() {
checkedErrno(listen(fd_, 5), "listen control server");
while (!done_) {
pollfd p{};
p.fd = fd_;
p.events = POLLIN;
// polls[1].fd = eventfd_;
// polls[1].events = POLLIN;
checkedErrno(poll(&p, 1, 250), "poll control fd");
if (done_) {
break;
}
if (!(p.revents & POLLIN)) {
continue;
}
int nextfd = accept(fd_, NULL, NULL);
if (nextfd < 0) {
break;
}
uint8_t* at = data_.data();
int togo = data_.size();
int ret = 1;
while (togo > 0 && ret > 0) {
ret = send(nextfd, at, togo, 0);
if (ret > 0) {
at += ret;
togo -= ret;
} else if (ret < 0 && errno == -EINTR) {
ret = 1;
}
}
close(nextfd);
}
vlog("ControlServer done");
}
~ControlServer() override {
vlog("end control server");
done_ = true;
close(fd_);
thread_.join();
}
private:
std::vector<uint8_t> data_;
std::atomic<bool> done_{false};
int fd_;
std::thread thread_;
};
std::unique_ptr<IControlServer> makeControlServer(
std::unordered_map<uint16_t, std::string> data,
uint16_t port,
bool ipv6) {
return std::make_unique<ControlServer>(payload(encode(data)), port, ipv6);
}
std::unordered_map<uint16_t, std::string>
getPortNameMap(std::string host, uint16_t port, bool ipv6) {
int fd = checkedErrno(mkBasicSock(ipv6), "mksock");
struct sockaddr_storage addr;
socklen_t addrLen;
getAddress(host, ipv6, port, &addr, &addrLen);
checkedErrno(
::connect(fd, (const struct sockaddr*)&addr, addrLen), "control connect");
uint32_t total_length;
int ret = recv(fd, &total_length, 4, 0);
if (ret != 4) {
die("bad recv, wanted 4 got ", ret);
}
std::vector<uint8_t> data;
data.resize(total_length);
uint8_t* at = data.data();
int size = data.size();
while (size > 0) {
ret = checkedErrno(recv(fd, at, size, 0), "receive control data");
size -= ret;
at += ret;
}
return decode(std::move(data));
}