-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPABotBase2CC_ReliableStreamConnection.cpp
More file actions
548 lines (491 loc) · 17.6 KB
/
PABotBase2CC_ReliableStreamConnection.cpp
File metadata and controls
548 lines (491 loc) · 17.6 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* Reliable Stream Connection (CC)
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/CRC32/pabb_CRC32.h"
#include "Common/Cpp/PrettyPrint.h"
//#include "Common/Cpp/Exceptions.h"
#include "Common/PABotBase2/PABotBase2CC_MessageDumper.h"
//#include "PABotBase2_ConnectionDebug.h"
#include "PABotBase2CC_ReliableStreamConnection.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace PABotBase2{
ReliableStreamConnection::ReliableStreamConnection(
CancellableScope* parent,
Logger& logger, bool log_everything,
ThreadPool& thread_pool,
UnreliableStreamConnectionPushing& unreliable_connection,
WallDuration retransmit_timeout,
Mutex* print_lock
)
: m_logger(logger)
, m_unreliable_connection(unreliable_connection)
, m_retransmit_timeout(retransmit_timeout)
, m_print_lock(print_lock)
, m_reliable_sender(*this, 20)
, m_log_everything(log_everything)
// , m_version_verified(false)
, m_remote_protocol_compatible(false)
, m_remote_protocol(0)
, m_remote_slot_capacity(1)
, m_remote_buffer_capacity(PABB2_PacketSender_BUFFER_SIZE)
{
m_retransmit_thread = thread_pool.dispatch_now_blocking(
[this]{ retransmit_thread(); }
);
m_unreliable_connection.add_listener(*this);
if (parent){
attach(*parent);
}
}
ReliableStreamConnection::~ReliableStreamConnection(){
cancel(nullptr);
detach();
m_unreliable_connection.remove_listener(*this);
m_retransmit_thread.wait_and_ignore_exceptions();
}
bool ReliableStreamConnection::cancel(std::exception_ptr exception) noexcept{
if (Cancellable::cancel(std::move(exception))){
return true;
}
{
std::lock_guard<Mutex> lg(m_lock);
m_error = "Connection has been closed.";
}
m_cv.notify_all();
return false;
}
size_t ReliableStreamConnection::pending() const{
std::unique_lock<Mutex> lg(m_lock);
return m_reliable_sender.slots_used();
}
bool ReliableStreamConnection::wait_for_pending(WallDuration timeout){
std::unique_lock<Mutex> lg(m_lock);
if (timeout == WallDuration::max()){
m_cv.wait(lg, [this]{
return this->cancelled() || m_reliable_sender.slots_used() == 0;
});
}else{
m_cv.wait_for(lg, timeout, [this]{
return this->cancelled() || m_reliable_sender.slots_used() == 0;
});
}
throw_if_cancelled();
return m_reliable_sender.slots_used() == 0;
}
//
// StreamSender/StreamListener
//
size_t ReliableStreamConnection::reliable_send_blocking(const void* data, size_t bytes, WallDuration timeout){
WallClock deadline = timeout == WallDuration::max()
? WallClock::max()
: current_time() + timeout;
const char* ptr = (const char*)data;
std::unique_lock<Mutex> lg(m_lock);
size_t total_sent = 0;
while (bytes > 0 && current_time() < deadline){
throw_if_cancelled();
if (m_reliable_sender.slots_used() >= m_remote_slot_capacity){
m_cv.wait_until(lg, deadline);
}
size_t sent = m_reliable_sender.send_stream(ptr, bytes);
ptr += sent;
bytes -= sent;
total_sent += sent;
if (sent == 0){
m_cv.wait_until(lg, deadline);
}
}
return total_sent;
}
void ReliableStreamConnection::on_recv(const void* data, size_t bytes){
#if 0
if (m_print_lock){
std::lock_guard<Mutex> lg(*m_print_lock);
cout << "ReliableStreamConnection::on_recv(): " << bytes << endl;
}
#endif
m_parser.push_bytes(*this, (const uint8_t*)data, bytes);
}
//
// PABotBase2::StreamConnection
//
size_t ReliableStreamConnection::unreliable_send(const void* data, size_t bytes){
const PacketHeader* header = (const PacketHeader*)data;
uint8_t opcode = header->opcode & PABB2_CONNECTION_OPCODE_MASK;
bool retransmit = header->opcode & PABB2_CONNECTION_RETRANSMIT_FLAG;
bool always_log =
opcode != PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA &&
opcode != PABB2_CONNECTION_OPCODE_RET_STREAM_DATA;
if (retransmit){
m_logger.log(
"[RSC]: Re-send: (0x" + tostr_hex(header->opcode) + ") " + tostr(header),
COLOR_ORANGE
);
}else if (m_log_everything || always_log){
m_logger.log(
"[RSC]: Sending: (0x" + tostr_hex(header->opcode) + ") " + tostr(header),
COLOR_DARKGREEN
);
// PABotBase2::PacketHeader_print(header, true);
}
// cout << "ReliableStreamConnection::unreliable_send() - before send" << endl;
return m_unreliable_connection.unreliable_send(data, bytes);
}
//
// Send Path
//
bool ReliableStreamConnection::reset(WallDuration timeout){
{
std::lock_guard<Mutex> lg(m_lock);
m_reliable_sender.reset();
m_parser.reset();
m_stream_coalescer.reset();
throw_if_cancelled();
m_reliable_sender.send_packet(PABB2_CONNECTION_OPCODE_ASK_RESET, 0, nullptr);
}
m_cv.notify_all();
return wait_for_pending(timeout);
}
bool ReliableStreamConnection::try_send_request(uint8_t opcode){
std::lock_guard<Mutex> lg(m_lock);
// cout << "Sending: " << tostr_hex(opcode) << endl;
throw_if_cancelled();
if (m_reliable_sender.slots_used() >= m_remote_slot_capacity){
return 0;
}
return m_reliable_sender.send_packet(opcode, 0, nullptr);
}
void ReliableStreamConnection::send_request(uint8_t opcode){
std::unique_lock<Mutex> lg(m_lock);
while (true){
throw_if_cancelled();
if (m_reliable_sender.slots_used() < m_remote_slot_capacity &&
m_reliable_sender.send_packet(opcode, 0, nullptr)
){
return;
}
m_cv.wait(lg);
}
}
void ReliableStreamConnection::print() const{
std::unique_lock<Mutex> lg(m_lock);
m_reliable_sender.print(true);
// StreamCoalescer_print(&m_stream_coalescer, true);
}
void ReliableStreamConnection::send_ack(uint8_t seqnum, uint8_t opcode){
// Must call inside lock.
struct{
PacketHeader header;
uint8_t crc[sizeof(uint32_t)];
} packet;
packet.header.magic_number = PABB2_CONNECTION_MAGIC_NUMBER;
packet.header.seqnum = seqnum;
packet.header.packet_bytes = sizeof(packet);
packet.header.opcode = opcode;
pabb_crc32_write_to_message(&packet, sizeof(packet));
unreliable_send(&packet, sizeof(packet));
}
void ReliableStreamConnection::send_ack_u16(uint8_t seqnum, uint8_t opcode, uint16_t data){
// Must call inside lock.
struct{
PacketHeader_u16 header;
uint8_t crc[sizeof(uint32_t)];
} packet;
packet.header.magic_number = PABB2_CONNECTION_MAGIC_NUMBER;
packet.header.seqnum = seqnum;
packet.header.packet_bytes = sizeof(packet);
packet.header.opcode = opcode;
packet.header.data = data;
pabb_crc32_write_to_message(&packet, sizeof(packet));
unreliable_send(&packet, sizeof(packet));
}
void ReliableStreamConnection::retransmit_thread(){
WallClock next_retransmit = current_time() + m_retransmit_timeout;
while (true){
std::unique_lock<Mutex> lg(m_lock);
if (this->cancelled()){
break;
}
m_cv.wait_until(lg, next_retransmit);
if (this->cancelled()){
break;
}
if (m_reliable_sender.slots_used() == 0){
continue;
}
if (current_time() < next_retransmit){
continue;
}
// cout << "running retransmits" << endl;
#if 1
if (!m_reliable_sender.iterate_retransmits()){
// cout << "nothing to do" << endl;
next_retransmit = current_time() + m_retransmit_timeout;
}else{
// cout << "did something" << endl;
}
#else
next_retransmit = current_time() + m_retransmit_timeout;
#endif
}
}
//
// PABotBase2::PacketRunner
//
void ReliableStreamConnection::on_packet(const PacketHeader* packet){
uint8_t status = packet->magic_number;
// {
// std::lock_guard<std::mutex> lg(PokemonAutomation::print_lock);
// cout << "on_packet(): seqnum = [" << (int)packet->seqnum << "], opcode = " << (int)packet->opcode << endl;
// }
// cout << "on_packet(): ";
// PABotBase2::print_bytes(packet, packet->packet_bytes, false);
// cout << endl;
//
// Process the packet status.
//
switch (status){
case PABB2_PacketParser_RESULT_VALID:
break;
case PABB2_PacketParser_RESULT_INVALID:
m_logger.log(
"[RSC]: INVALID PACKET: Received invalid packet from device.",
COLOR_RED
);
return;
case PABB2_PacketParser_RESULT_CHECKSUM_FAIL:
m_logger.log(
"[RSC]: CHECKSUM MISMATCH: Received bad data from device: bytes = " + std::to_string(packet->packet_bytes),
// "[RSC]: CHECKSUM MISMATCH: Received bad data from device: bytes = " + tostr(packet),
COLOR_RED
);
return;
default:
m_logger.log(
"[RSC]: Internal Error: Packet parser returned invalid code: " + std::to_string(status),
COLOR_RED
);
return;
}
//
// Process the packet itself.
//
if (m_log_everything){
m_logger.log("[RSC]: Receive: (0x" + tostr_hex(packet->opcode) + ") " + tostr(packet), COLOR_PURPLE);
}
uint8_t opcode = packet->opcode & PABB2_CONNECTION_OPCODE_MASK;
switch (opcode){
case PABB2_CONNECTION_OPCODE_INVALID_LENGTH:
m_logger.log(
"[RSC]: PABB2_CONNECTION_OPCODE_INVALID_LENGTH: Device reported an invalid message length.",
COLOR_RED
);
return;
case PABB2_CONNECTION_OPCODE_INVALID_CHECKSUM_FAIL:
m_logger.log(
"[RSC]: PABB2_CONNECTION_OPCODE_INVALID_CHECKSUM_FAIL: Device reported a checksum mismatch.",
COLOR_RED
);
return;
case PABB2_CONNECTION_OPCODE_UNKNOWN_OPCODE:
process_UNKNOWN_OPCODE(packet);
return;
case PABB2_CONNECTION_OPCODE_ASK_STREAM_DATA:
process_ASK_STREAM_DATA(packet);
return;
case PABB2_CONNECTION_OPCODE_RET_STREAM_DATA:
process_RET_STREAM_DATA(packet);
return;
case PABB2_CONNECTION_OPCODE_RET_RESET:
process_RET_RESET(packet);
return;
case PABB2_CONNECTION_OPCODE_RET_VERSION:
process_RET_VERSION(packet);
return;
case PABB2_CONNECTION_OPCODE_RET_PACKET_SIZE:
process_RET_PACKET_SIZE(packet);
return;
case PABB2_CONNECTION_OPCODE_RET_BUFFER_SLOTS:
process_RET_BUFFER_SLOTS(packet);
return;
case PABB2_CONNECTION_OPCODE_INFO:
case PABB2_CONNECTION_OPCODE_INFO_U8:
case PABB2_CONNECTION_OPCODE_INFO_U16:
case PABB2_CONNECTION_OPCODE_INFO_H32:
case PABB2_CONNECTION_OPCODE_INFO_U32:
case PABB2_CONNECTION_OPCODE_INFO_I32:
case PABB2_CONNECTION_OPCODE_INFO_BINARY:
case PABB2_CONNECTION_OPCODE_INFO_STR:
case PABB2_CONNECTION_OPCODE_INFO_LABEL_H32:
case PABB2_CONNECTION_OPCODE_INFO_LABEL_U32:
case PABB2_CONNECTION_OPCODE_INFO_LABEL_I32:
// cout << "Received ack" << endl;
if (!m_log_everything){
m_logger.log("[RSC]: Receive: (0x" + tostr_hex(packet->opcode) + ") " + tostr(packet), COLOR_PURPLE);
}
return;
default:
m_logger.log(
"[RSC]: UNKNOWN OPCODE: Device sent an unknown opcode: 0x" + tostr_hex(packet->opcode),
COLOR_RED
);
return;
}
}
void ReliableStreamConnection::process_UNKNOWN_OPCODE(const PacketHeader* packet){
std::lock_guard<Mutex> lg(m_lock);
if (packet->packet_bytes < sizeof(PacketHeader_u8) + sizeof(uint32_t)){
m_error = "Unknown opcode packet is too small: " + std::to_string(packet->packet_bytes);
m_logger.log("[RSC]: " + m_error, COLOR_RED);
return;
}
const PacketHeader_u8* message = (const PacketHeader_u8*)packet;
m_logger.log(
"[RSC]: PABB2_CONNECTION_OPCODE_INVALID_OPCODE: Device reported an invalid opcode: " +
std::to_string(message->data),
COLOR_RED
);
}
void ReliableStreamConnection::process_RET_RESET(const PacketHeader* packet){
{
std::lock_guard<Mutex> lg(m_lock);
m_reliable_sender.remove(packet->seqnum);
}
m_cv.notify_all();
}
void ReliableStreamConnection::process_RET_VERSION(const PacketHeader* packet){
// m_logger.log(tostr(packet), COLOR_DARKGREEN);
do{
std::lock_guard<Mutex> lg(m_lock);
if (packet->packet_bytes < sizeof(PacketHeader_u32) + sizeof(uint32_t)){
m_error = "Version response is too small: " + std::to_string(packet->packet_bytes);
m_logger.log("[RSC]: " + m_error, COLOR_RED);
break;
}
m_reliable_sender.remove(packet->seqnum);
const PacketHeader_u32* message = (const PacketHeader_u32*)packet;
uint32_t protocol = message->data;
m_remote_protocol = protocol;
std::string str = "Remote Protocol: " + std::to_string(protocol);
uint32_t major_version = protocol / 100;
uint32_t minor_version = protocol % 100;
m_remote_protocol_compatible =
major_version == PABB2_CONNECTION_PROTOCOL_VERSION / 100 &&
minor_version >= PABB2_CONNECTION_PROTOCOL_VERSION % 100;
if (!m_remote_protocol_compatible){
m_error = str + " (incompatible)";
m_logger.log("[RSC]: " + m_error, COLOR_RED);
break;
}
m_logger.log("[RSC]: " + str + " (compatible)", COLOR_BLUE);
m_reliable_sender.remove(packet->seqnum);
}while (false);
m_cv.notify_all();
}
void ReliableStreamConnection::process_RET_PACKET_SIZE(const PacketHeader* packet){
if (packet->packet_bytes < sizeof(PacketHeader_u16) + sizeof(uint32_t)){
m_logger.log(
"[RSC]: Packet size response is too small: " + std::to_string(packet->packet_bytes),
COLOR_RED
);
return;
}
const PacketHeader_u16* message = (const PacketHeader_u16*)packet;
m_logger.log(
"[RSC]: Setting Packet Size to: " + std::to_string(message->data),
COLOR_BLUE
);
{
std::lock_guard<Mutex> lg(m_lock);
m_reliable_sender.remove(packet->seqnum);
m_reliable_sender.set_max_packet_size((uint8_t)message->data);
}
m_cv.notify_all();
}
void ReliableStreamConnection::process_RET_BUFFER_SLOTS(const PacketHeader* packet){
if (packet->packet_bytes < sizeof(PacketHeader_u8) + sizeof(uint32_t)){
m_logger.log(
"[RSC]: Buffer slot response is too small: " + std::to_string(packet->packet_bytes),
COLOR_RED
);
return;
}
const PacketHeader_u8* message = (const PacketHeader_u8*)packet;
m_logger.log(
"[RSC]: Setting Buffer Slots to: " + std::to_string(message->data),
COLOR_BLUE
);
{
std::lock_guard<Mutex> lg(m_lock);
m_reliable_sender.remove(packet->seqnum);
m_remote_slot_capacity = std::min<uint8_t>(message->data, PABB2_PacketSender_SLOTS);
}
m_cv.notify_all();
}
void ReliableStreamConnection::process_RET_BUFFER_BYTES(const PacketHeader* packet){
if (packet->packet_bytes < sizeof(PacketHeader_u16) + sizeof(uint32_t)){
m_logger.log(
"[RSC]: Buffer slot response is too small: " + std::to_string(packet->packet_bytes),
COLOR_RED
);
return;
}
const PacketHeader_u16* message = (const PacketHeader_u16*)packet;
m_logger.log(
"[RSC]: Setting Buffer Slots to: " + std::to_string(message->data),
COLOR_BLUE
);
{
std::lock_guard<Mutex> lg(m_lock);
m_reliable_sender.remove(packet->seqnum);
m_remote_buffer_capacity = std::min<uint16_t>(message->data, PABB2_PacketSender_BUFFER_SIZE);
}
m_cv.notify_all();
}
void ReliableStreamConnection::process_ASK_STREAM_DATA(const PacketHeader* packet){
// cout << "process_ASK_STREAM_DATA()" << endl;
if (packet->packet_bytes < sizeof(PacketHeaderData) + sizeof(uint32_t)){
m_logger.log(
"[RSC]: Received stream packet that is too small: " + std::to_string(packet->packet_bytes),
COLOR_RED
);
return;
}
{
std::lock_guard<Mutex> lg(m_lock);
if (!m_stream_coalescer.push_stream((const PacketHeaderData*)packet)){
// cout << "push_stream() failed" << endl;
return;
}
// cout << "Calling: send_ack_u16()" << endl;
send_ack_u16(
packet->seqnum,
PABB2_CONNECTION_OPCODE_RET_STREAM_DATA,
m_stream_coalescer.free_bytes()
);
char buffer[4096];
// m_stream_coalescer.print(false);
size_t bytes = m_stream_coalescer.read(buffer, sizeof(buffer));
// cout << "bytes = " << bytes << endl;
if (bytes != 0){
on_reliable_recv(buffer, bytes);
}
}
// m_cv.notify_all();
}
void ReliableStreamConnection::process_RET_STREAM_DATA(const PacketHeader* packet){
{
std::lock_guard<Mutex> lg(m_lock);
// pabb2_PacketSender_print(&m_reliable_sender, true);
m_reliable_sender.remove(packet->seqnum);
// pabb2_PacketSender_print(&m_reliable_sender, true);
}
m_cv.notify_all();
}
}
}