-
Notifications
You must be signed in to change notification settings - Fork 8
/
Network.cpp
398 lines (350 loc) · 10.7 KB
/
Network.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "Network.hpp"
#include "common.h"
#include <unistd.h>
#include <fstream>
#include <thread>
#include <algorithm>
using namespace Network;
static State state = CONNECTING;
static bool quit = false;
static int ds_sock = -1;
static int udp_sock = -1;
static int input_sock = -1;
static struct sockaddr_in ds_addr;
static int connect_attempts = 0;
void Network::ConnectDS(const std::string host) {
int ret;
if (ds_sock < 0) {
ds_sock = socket(AF_INET, SOCK_STREAM, 0);
if (ds_sock < 0) {
NetworkError("Couldn't make socket");
return;
}
}
ds_addr = (struct sockaddr_in) {
.sin_family = AF_INET,
.sin_port = htons(8000),
.sin_addr = {0},
.sin_zero = {0},
}; //TODO
ret = inet_pton(ds_addr.sin_family, host.c_str(), &(ds_addr.sin_addr));
if (ret <= 0) {
NetworkErrorF("Address %s invalid - check your config file", host.c_str());
if (ds_sock >= 0) {
close(ds_sock);
ds_sock = -1;
}
state = ERR_BAD_IP;
return;
}
state = CONNECTING;
ret = connect(ds_sock, (struct sockaddr*)&ds_addr, sizeof(ds_addr));
if (ret < 0) {
NetworkErrorF("Can't connect to DS (%s)", host.c_str());
if (ds_sock >= 0) {
close(ds_sock);
ds_sock = -1;
}
return;
}
}
void Network::ListenUDP() {
int ret;
if (udp_sock < 0) {
udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
struct sockaddr_in listen_addr = {
.sin_family = AF_INET,
.sin_port = htons(8001),
.sin_addr = {
.s_addr = htonl(INADDR_ANY),
},
.sin_zero = {0},
};
ret = bind(udp_sock, (struct sockaddr*)&listen_addr, sizeof(listen_addr));
if (ret < 0) {
NetworkError("Can't bind to UDP");
if (udp_sock >= 0) {
close(udp_sock);
udp_sock = -1;
}
return;
}
}
typedef struct jpeg {
uint8_t id;
std::array<bool, 256> seq;
uint8_t seq_last;
bool pending;
bool good;
std::vector<uint8_t> data;
} jpeg;
static std::array<jpeg, 256> receivingFrames_top;
static std::array<jpeg, 256> receivingFrames_btm;
static void initJpeg(jpeg& jpeg) {
jpeg.seq.fill(false);
jpeg.seq_last = 254;
jpeg.pending = false;
jpeg.good = false;
jpeg.data.reserve(30000);
jpeg.data.clear();
}
static jpeg& getJpegForID(uint8_t id, int isTop) {
auto& receivingFrames = (isTop) ? receivingFrames_top : receivingFrames_btm;
auto& jpeg = receivingFrames[id];
if (jpeg.good) {
//this will never be hit for recent frames, only old (>127) ones
initJpeg(jpeg);
}
jpeg.id = id; //TODO refactor this out
return jpeg;
}
static uint8_t lastGoodID_top = 255;
static uint8_t lastGoodID_btm = 255;
static void checkGoodJpeg(jpeg& jpeg, int isTop) {
auto& lastGoodID = (isTop) ? lastGoodID_top : lastGoodID_btm;
auto& receivingFrames = (isTop) ? receivingFrames_top : receivingFrames_btm;
if (jpeg.pending) {
//check we actually got a full frame
bool all_seqs = std::all_of(jpeg.seq.cbegin(),
std::next(jpeg.seq.cbegin(), jpeg.seq_last),
[](bool b){ return b; }
);
//all seqs OK, ready to display
if (all_seqs) {
//printf("frame %d OK\n", jpeg.id);
jpeg.good = true;
//only interested in data for frames after this one
lastGoodID = jpeg.id;
//let display know we're good
state = CONNECTED_STREAMING;
//hack: set the last few frames as good, too. Helps getJpegForID
//clear out old state.
//TODO: come up with a less flaky solution for this
//did you know? C++'s % operator does not affect negative numbers.
//nice OOB write, that one. adding 256 as a quick fix, since
//this whole code is just a quick fix
int ndx = lastGoodID - 1 + 256;
receivingFrames[ndx-- % 256].good = true;
receivingFrames[ndx-- % 256].good = true;
receivingFrames[ndx % 256].good = true;
}
}
}
//https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-addition-and-subtraction
static int8_t diffFrameIDs(uint8_t incoming, uint8_t old) {
int diff = (int)incoming - (int)old;
uint8_t mdiff = diff % 256;
return (int8_t)mdiff;
}
void Network::RecieveUDP() {
int ret;
uint8_t buf[UDP_PACKET_SIZE];
struct sockaddr_storage remote_addr;
socklen_t remote_addr_len = sizeof(remote_addr);
ret = recvfrom(udp_sock, buf, UDP_PACKET_SIZE, 0, (struct sockaddr*)&remote_addr, &remote_addr_len);
if (ret <= 0) {
if (quit) return;
NetworkError("RecieveUDP failed");
return;
}
uint8_t id = buf[0];
uint8_t flags = buf[1];
uint8_t fmt = buf[2];
uint8_t seq = buf[3];
int isTop = flags & 1;
int lastPacket = flags & 0x10;
uint8_t* jpeg_data = buf + 4;
uint jpeg_size = ret - 4;
uint jpeg_offset = seq * (UDP_PACKET_SIZE - 4);
auto& lastGoodID = (isTop) ? lastGoodID_top : lastGoodID_btm;
if (diffFrameIDs(id, lastGoodID) < 1) {
if (diffFrameIDs(id, lastGoodID) == 0) {
printf("[Network] BUG: received seq for previous frame\n");
}
printf("[Network] Discarding late seq %d for frame %d, last frame is %d\n", seq, id, lastGoodID);
return;
}
auto& jpeg = getJpegForID(id, isTop);
jpeg.seq[seq] = true;
if (jpeg.data.size() < (jpeg_offset + jpeg_size)) {
jpeg.data.resize(jpeg_offset + jpeg_size);
//printf("resizing jpeg to %ld bytes for frame %d\n", jpeg.data.size(), id);
}
std::copy_n(jpeg_data, jpeg_size, std::next(jpeg.data.begin(), jpeg_offset));
if (lastPacket) {
//printf("pending frame %d on seq %d (last: %d)\n", id, seq, lastGoodID);
jpeg.seq_last = seq;
jpeg.pending = true;
}
checkGoodJpeg(jpeg, isTop);
}
void Network::SendRemotePlay(uint8_t priority, uint8_t priorityFactor, uint8_t jpegQuality, uint8_t QoS) {
Packet pac = {
.magic = NativeToLE(0x12345678),
.seq = NativeToLE(1),
.type = NativeToLE(0),
.cmd = NativeToLE(901),
.args = { .RemotePlay = {
.mode = NativeToLE(priority << 8 | priorityFactor),
.quality = NativeToLE(jpegQuality),
.qos = NativeToLE(QoS*2 << 16),
}},
.length = NativeToLE(0),
};
send(ds_sock, &pac, sizeof(pac), 0);
}
int Network::SendInputRedirection(Input::InputState input) {
int ret;
if (input_sock < 0) {
input_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (input_sock < 0) return errno;
}
uint32_t packet[] = {
[0] = NativeToLE(input.buttons.data),
[1] = NativeToLE(input.touch.data),
[2] = NativeToLE(input.circle.data),
[3] = NativeToLE(input.pro.data),
[4] = NativeToLE(input.buttons_sys.data),
};
struct sockaddr_in ds_addr_input = ds_addr;
ds_addr_input.sin_port = htons(4950);
ret = sendto(input_sock, packet, sizeof(packet), 0, (struct sockaddr*)&ds_addr_input, sizeof(ds_addr_input));
/*printf("input: %08X-%08X-%08X-%08X-%08X %d %d\n", packet[0], packet[1], packet[2], packet[3], packet[4],
ret, errno
);*/
if (ret < 0) {
return errno;
} else return 0;
}
int Network::SendHeartbeat() {
Packet pac = {
.magic = NativeToLE(0x12345678),
.seq = NativeToLE(1),
.type = NativeToLE(0),
.cmd = NativeToLE(0),
.args = { .raw = { 0 } },
.length = NativeToLE(0),
};
int ret = send(ds_sock, &pac, sizeof(pac), 0);
if (ret < 0) {
return errno;
} else return 0;
}
static void heartbeatLoop() {
while (!quit) {
SendHeartbeat();
sleep(1);
}
}
static std::mutex lastInputMtx;
static Input::InputState lastInput;
static bool lastInputDirty = false;
static void inputLoop(int input_ratelimit_us, int input_pollrate_us) {
while (!quit) {
if (lastInputDirty) {
{
const std::lock_guard<std::mutex> lock(lastInputMtx);
SendInputRedirection(lastInput);
lastInputDirty = false;
} //mutex release
usleep(input_ratelimit_us);
} else {
usleep(input_pollrate_us);
}
}
}
void Network::mainLoop(const Config::NetworkConfig* config) {
//init bits
for (auto& jpeg : receivingFrames_top) {
initJpeg(jpeg);
}
for (auto& jpeg : receivingFrames_btm) {
initJpeg(jpeg);
}
//connect
ListenUDP();
while (udp_sock < 0 && !quit) {
sleep(1);
connect_attempts++;
ListenUDP();
}
ConnectDS(config->host);
while (ds_sock < 0 && !quit) {
sleep(5);
connect_attempts++;
ConnectDS(config->host);
}
if (quit) {
printf("[Network] quit requested\n");
if (ds_sock >= 0) {
printf("[Network] Tearing down ds sock\n");
close(ds_sock);
}
if (udp_sock >= 0) {
printf("[Network] Tearing down udp sock\n");
close(udp_sock);
}
printf("[Network] bye!\n");
return;
}
state = CONNECTED_WAIT;
sleep(2); //I know, I know
SendRemotePlay(config->priority, config->priorityFactor, config->jpegQuality, config->QoS);
for (int i = 0; i < 2 && !quit; i++) {
SendHeartbeat();
sleep(1);
}
std::thread heartbeatThread(heartbeatLoop);
std::thread inputThread(inputLoop, config->input_ratelimit_us, config->input_pollrate_us);
while (!quit) {
RecieveUDP();
}
inputThread.join();
heartbeatThread.join();
if (ds_sock >= 0) {
close(ds_sock);
ds_sock = -1;
}
if (udp_sock >= 0) {
close(udp_sock);
udp_sock = -1;
}
}
void Network::Quit() {
if (ds_sock >= 0) {
close(ds_sock);
ds_sock = -1;
}
if (udp_sock >= 0) {
close(udp_sock);
udp_sock = -1;
}
quit = true;
}
State Network::GetNetworkState() {
return state;
}
int Network::GetConnectionAttempts() {
return connect_attempts;
}
void Network::Input(Input::InputState input) {
if (lastInput != input) {
const std::lock_guard<std::mutex> lock(lastInputMtx);
lastInput = input;
lastInputDirty = true;
}
}
//there's a lot of race conditions here.
uint8_t Network::GetTopJPEGID() {
return lastGoodID_top;
}
std::vector<uint8_t>& Network::GetTopJPEG(uint8_t id) {
return receivingFrames_top[id].data;
}
uint8_t Network::GetBtmJPEGID() {
return lastGoodID_btm;
}
std::vector<uint8_t>& Network::GetBtmJPEG(uint8_t id) {
return receivingFrames_btm[id].data;
}