forked from yawut/ntrview-wiiu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.cpp
345 lines (299 loc) · 9.02 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
#include "Network.hpp"
#include <unistd.h>
#include <fstream>
#include <thread>
#include <algorithm>
#ifdef __WIIU__
//cool
#else
static int socketlasterr() {
return errno;
}
#endif
using namespace Network;
State state = CONNECTING;
bool quit = false;
int ds_sock = -1;
int udp_sock = -1;
int connect_attempts = 0;
void Network::ConnectDS(const std::string host) {
int ret;
ds_sock = socket(AF_INET, SOCK_STREAM, 0);
if (ds_sock < 0) {
NetworkError("Couldn't make socket");
return;
}
struct sockaddr_in ds_addr = {
.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) {
shutdown(ds_sock, 2); //HACK wiiu compat
ds_sock = -1;
}
return;
}
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) {
shutdown(ds_sock, 2); //HACK wiiu compat
ds_sock = -1;
}
return;
}
}
void Network::ListenUDP() {
int ret;
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) {
shutdown(udp_sock, 2); //HACK wiiu compat
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;
}
uint8_t lastGoodID_top = 255;
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 = Swap(0x12345678),
.seq = Swap(1),
.type = Swap(0),
.cmd = Swap(901),
.args = { .RemotePlay = {
.mode = Swap(priority << 8 | priorityFactor),
.quality = Swap(jpegQuality),
.qos = Swap(QoS*2 << 16),
}},
.length = Swap(0),
};
send(ds_sock, &pac, sizeof(pac), 0);
}
int Network::SendHeartbeat() {
Packet pac = {
.magic = Swap(0x12345678),
.seq = Swap(1),
.type = Swap(0),
.cmd = Swap(0),
.args = { .raw = { 0 } },
.length = Swap(0),
};
int ret = send(ds_sock, &pac, sizeof(pac), 0);
if (ret < 0) {
return socketlasterr();
} else return 0;
}
static void heartbeatLoop() {
while (!quit) {
SendHeartbeat();
sleep(1);
}
}
void Network::mainLoop(const std::string host, uint8_t priority, uint8_t priorityFactor, uint8_t jpegQuality, uint8_t QoS) {
//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(host);
while (ds_sock < 0 && !quit) {
sleep(1);
connect_attempts++;
ConnectDS(host);
}
if (quit) {
printf("[Network] quit requested\n");
if (ds_sock >= 0) {
printf("[Network] Tearing down ds sock\n");
shutdown(ds_sock, 2); //HACK wiiu compat
}
if (udp_sock >= 0) {
printf("[Network] Tearing down udp sock\n");
shutdown(udp_sock, 2); //HACK wiiu compat
}
printf("[Network] bye!\n");
return;
}
state = CONNECTED_WAIT;
sleep(2); //I know, I know
SendRemotePlay(priority, priorityFactor, jpegQuality, QoS);
for (int i = 0; i < 2 && !quit; i++) {
SendHeartbeat();
sleep(1);
}
std::thread heartbeatThread(heartbeatLoop);
while (!quit) {
RecieveUDP();
}
heartbeatThread.join();
if (ds_sock >= 0) {
shutdown(ds_sock, 2); //HACK wiiu compat
ds_sock = -1;
}
if (udp_sock >= 0) {
shutdown(udp_sock, 2); //HACK wiiu compat
udp_sock = -1;
}
}
void Network::Quit() {
if (ds_sock >= 0) {
shutdown(ds_sock, 2); //HACK wiiu compat
ds_sock = -1;
}
if (udp_sock >= 0) {
shutdown(udp_sock, 2); //HACK wiiu compat
udp_sock = -1;
}
quit = true;
}
State Network::GetNetworkState() {
return state;
}
int Network::GetConnectionAttempts() {
return connect_attempts;
}
//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;
}