Skip to content

Commit a986520

Browse files
committed
Fix PABotBase2 retransmits.
1 parent 522c3eb commit a986520

4 files changed

Lines changed: 60 additions & 23 deletions

File tree

Common/Cpp/StreamConnections/MockDevice.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <string.h>
8+
#include "Common/Cpp/Exceptions.h"
89
#include "Common/PABotBase2/PABotBase2_ConnectionDebug.h"
910
#include "MockDevice.h"
1011

@@ -13,7 +14,7 @@
1314
using std::cout;
1415
using std::endl;
1516

16-
#if 0
17+
#if 1
1718
#define PABB2_DROP_HOST_TO_DEVICE 0.2
1819
#define PABB2_DROP_DEVICE_TO_HOST 0.2
1920
#else
@@ -158,10 +159,17 @@ size_t MockDevice::verify_stream_data(){
158159
actual.data(), bytes
159160
);
160161

162+
if (read == 0){
163+
return bytes;
164+
}
165+
166+
167+
#if 0
161168
{
162169
std::lock_guard<Mutex> lg1(m_print_lock);
163170
cout << "read = " << read << endl;
164171
}
172+
#endif
165173

166174

167175
std::vector<uint8_t> expected(
@@ -178,17 +186,20 @@ size_t MockDevice::verify_stream_data(){
178186

179187
std::lock_guard<Mutex> lg1(m_print_lock);
180188
if (matched){
189+
#if 0
181190
cout << "Matched: Bytes = " << read
182191
<< ", Remaining = " << m_expected_host_to_device_stream.size()
183192
<< ", Matched = "
184193
<< std::string((const char*)actual.data(), read)
185194
<< endl;
195+
#endif
186196
}else{
187197
cout << "MISMATCH: Expected = "
188198
<< std::string((const char*)expected.data(), read)
189199
<< ", Actual = "
190200
<< std::string((const char*)actual.data(), read)
191201
<< endl;
202+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Mismatch");
192203
}
193204
return m_expected_host_to_device_stream.size();
194205
}
@@ -200,7 +211,7 @@ void MockDevice::device_thread(){
200211
std::unique_lock<Mutex> lg(m_device_lock);
201212
while (!m_stopping.load(std::memory_order_relaxed)){
202213
pabb2_ReliableStreamConnection_run_events(&m_connection);
203-
m_device_cv.wait(lg);
214+
m_device_cv.wait_for(lg, Milliseconds(10));
204215
}
205216
}
206217
void MockDevice::host_recv_thread(){

Common/Cpp/StreamConnections/ReliableStreamConnection.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
#include "Common/Cpp/StreamConnections/PABotBase2_MessageDumper.h"
1212
#include "ReliableStreamConnection.h"
1313

14-
// REMOVE
15-
#include <iostream>
16-
using std::cout;
17-
using std::endl;
14+
//#include <iostream>
15+
//using std::cout;
16+
//using std::endl;
1817

1918
namespace PokemonAutomation{
2019

@@ -234,10 +233,12 @@ void ReliableStreamConnection::retransmit_thread(){
234233
//
235234

236235
void ReliableStreamConnection::on_recv(const void* data, size_t bytes){
236+
#if 0
237237
if (m_print_lock){
238238
std::lock_guard<Mutex> lg(*m_print_lock);
239239
cout << "ReliableStreamConnection::on_recv(): " << bytes << endl;
240240
}
241+
#endif
241242
pabb2_PacketParser_push_bytes(
242243
&m_parser,
243244
this, &ReliableStreamConnection::on_packet,

Common/PABotBase2/PABotBase2_PacketSender.c

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -286,29 +286,42 @@ bool pabb2_PacketSender_iterate_retransmits(pabb2_PacketSender* self){
286286
size_t offset = ~self->offsets[head & PABB2_ConnectionSender_SLOTS_MASK];
287287
pabb2_PacketHeader* packet = (pabb2_PacketHeader*)(self->buffer + offset);
288288

289-
// Retransmit if it hasn't been acked already and is old enough.
290-
if (packet->opcode != PABB2_CONNECTION_OPCODE_INVALID &&
291-
seqnum - packet->magic_number >= PABB2_ConnectionSender_RETRANSMIT_COUNTER
292-
){
293-
packet->magic_number = PABB2_CONNECTION_MAGIC_NUMBER;
294-
uint8_t packet_bytes = packet->packet_bytes;
295-
289+
// Already acked.
290+
if (packet->opcode == PABB2_CONNECTION_OPCODE_INVALID){
296291
#if 0
297-
printf("Retransmitting: %u\n", packet->seqnum);
292+
printf("Already Acked\n");
298293
fflush(stdout);
299294
#endif
295+
head++;
296+
continue;
297+
}
300298

301-
self->unreliable_sender_send(
302-
self->unreliable_sender_context,
303-
packet,
304-
packet_bytes == 0 ? (size_t)256 : (size_t)packet_bytes,
305-
true
306-
);
307-
packet->magic_number = seqnum;
308-
return true;
299+
// Not old enough.
300+
if ((uint8_t)(seqnum - packet->magic_number) < PABB2_ConnectionSender_RETRANSMIT_COUNTER){
301+
#if 0
302+
printf("Not old enough.\n");
303+
fflush(stdout);
304+
#endif
305+
head++;
306+
continue;
309307
}
310308

311-
head++;
309+
packet->magic_number = PABB2_CONNECTION_MAGIC_NUMBER;
310+
uint8_t packet_bytes = packet->packet_bytes;
311+
312+
#if 0
313+
printf("Retransmitting: %u\n", packet->seqnum);
314+
fflush(stdout);
315+
#endif
316+
317+
self->unreliable_sender_send(
318+
self->unreliable_sender_context,
319+
packet,
320+
packet_bytes == 0 ? (size_t)256 : (size_t)packet_bytes,
321+
true
322+
);
323+
packet->magic_number = seqnum;
324+
return true;
312325
}
313326

314327
// Increment counter only if we did nothing.

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,29 @@ void stress_test(Logger& logger, CancellableScope& scope){
316316
connection.send_request(PABB2_CONNECTION_OPCODE_ASK_BUFFER_SLOTS);
317317
connection.wait_for_pending();
318318

319+
uint64_t bytes_sent = 0;
320+
WallClock last_print = current_time();
321+
319322
while (true){
320323
scope.throw_if_cancelled();
321324

322325
std::string data = random_string(20);
323326
const char* ptr = data.data();
324327
size_t left = data.size();
325328
while (left > 0){
329+
// scope.wait_for(Milliseconds(rand() % 100));
326330
size_t sent = connection.send(ptr, left);
331+
if (sent == 0){
332+
device.verify_stream_data();
333+
}
327334
ptr += sent;
328335
left -= sent;
329336
}
337+
bytes_sent += data.size();
338+
if (current_time() - last_print > Seconds(1)){
339+
cout << "Bytes Sent = " << bytes_sent << endl;
340+
last_print = current_time();
341+
}
330342
device.push_expected_stream_data(data.data(), data.size());
331343
}
332344
}

0 commit comments

Comments
 (0)