Skip to content

Commit be3e0f8

Browse files
committed
More work on PABotBase2.
1 parent 0608326 commit be3e0f8

8 files changed

Lines changed: 91 additions & 13 deletions

File tree

Common/Cpp/Concurrency/Backends/Thread_Qt.tpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
*
55
*/
66

7+
#include <iostream>
78
#include <QCoreApplication>
89
#include <QThread>
910
#include "Common/Cpp/Containers/Pimpl.tpp"
1011
#include "Common/Cpp/Concurrency/Qt6.9ThreadBugWorkaround.h"
1112
#include "Common/Cpp/Concurrency/Thread.h"
1213

13-
//#include <iostream>
1414
//using std::cout;
1515
//using std::endl;
1616

@@ -110,6 +110,7 @@ void Thread::join(){
110110
m_data->quit();
111111

112112
// But instead of waiting and joining (which will hang), we leak it intentionally.
113+
std::cout << "Intentionally leaking QThread to work around Qt6.9 thread adoption bug..." << std::endl;
113114
m_data.release();
114115
#endif
115116

Common/Cpp/StreamConnections/MockDevice.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ MockDevice::~MockDevice(){
5252
}
5353
void MockDevice::print() const{
5454
std::lock_guard<Mutex> lg(m_device_lock);
55+
std::lock_guard<Mutex> lg1(m_print_lock);
5556
pabb2_StreamCoalescer_print(&m_connection.stream_coalescer, true);
5657
}
5758

@@ -65,6 +66,7 @@ size_t MockDevice::device_send_serial(const void* data, size_t bytes, bool is_re
6566
}
6667

6768
if ((rand() % 100) / 100. < PABB2_DROP_DEVICE_TO_HOST){
69+
std::lock_guard<Mutex> lg1(m_print_lock);
6870
cout << "**Intentionally Dropping Packet: device -> host**" << endl;
6971
return 0;
7072
}
@@ -77,6 +79,7 @@ size_t MockDevice::device_send_serial(const void* data, size_t bytes, bool is_re
7779
);
7880

7981
if ((rand() % 100) / 100. < PABB2_DROP_DEVICE_TO_HOST){
82+
std::lock_guard<Mutex> lg1(m_print_lock);
8083
cout << "**Intentionally Corrupting Packet: device -> host**" << endl;
8184
m_device_to_host_line[rand() % m_device_to_host_line.size()] = 0;
8285
}
@@ -104,6 +107,7 @@ size_t MockDevice::send(const void* data, size_t bytes){
104107
// cout << "MockDevice::send(const void* data, size_t bytes)" << endl;
105108

106109
if ((rand() % 100) / 100. < PABB2_DROP_HOST_TO_DEVICE){
110+
std::lock_guard<Mutex> lg1(m_print_lock);
107111
cout << "**Intentionally Dropping Packet: host -> device**" << endl;
108112
return 0;
109113
}
@@ -119,6 +123,7 @@ size_t MockDevice::send(const void* data, size_t bytes){
119123
);
120124

121125
if ((rand() % 100) / 100. < PABB2_DROP_HOST_TO_DEVICE){
126+
std::lock_guard<Mutex> lg1(m_print_lock);
122127
cout << "**Intentionally Corrupting Packet: host -> device**" << endl;
123128
m_host_to_device_line[rand() % m_host_to_device_line.size()] = 0;
124129
}
@@ -131,6 +136,53 @@ size_t MockDevice::send(const void* data, size_t bytes){
131136
return bytes;
132137
}
133138

139+
void MockDevice::push_expected_stream_data(const void* data, size_t bytes){
140+
std::lock_guard<Mutex> lg(m_device_lock);
141+
142+
m_expected_host_to_device_stream.insert(
143+
m_expected_host_to_device_stream.end(),
144+
(const uint8_t*)data,
145+
(const uint8_t*)data + bytes
146+
);
147+
148+
bytes = m_expected_host_to_device_stream.size();
149+
150+
std::vector<uint8_t> actual(bytes);
151+
size_t read = pabb2_ReliableStreamConnection_read_stream(
152+
&m_connection,
153+
actual.data(), bytes
154+
);
155+
156+
{
157+
std::lock_guard<Mutex> lg1(m_print_lock);
158+
cout << "read = " << read << endl;
159+
}
160+
161+
162+
std::vector<uint8_t> expected(
163+
m_expected_host_to_device_stream.begin(),
164+
m_expected_host_to_device_stream.begin() + read
165+
);
166+
167+
m_expected_host_to_device_stream.erase(
168+
m_expected_host_to_device_stream.begin(),
169+
m_expected_host_to_device_stream.begin() + read
170+
);
171+
172+
if (memcmp(actual.data(), expected.data(), read) == 0){
173+
return;
174+
}
175+
176+
std::lock_guard<Mutex> lg1(m_print_lock);
177+
cout << "MISMATCH: Expected: "
178+
<< std::string((const char*)expected.data(), read)
179+
<< ", Actual: "
180+
<< std::string((const char*)actual.data(), read)
181+
<< endl;
182+
}
183+
184+
185+
134186

135187
void MockDevice::device_thread(){
136188
std::unique_lock<Mutex> lg(m_device_lock);

Common/Cpp/StreamConnections/MockDevice.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class MockDevice : public StreamConnection{
2727

2828
void print() const;
2929

30+
Mutex& print_lock() const{
31+
return m_print_lock;
32+
}
33+
3034

3135
private:
3236
// Call from device.
@@ -54,6 +58,8 @@ class MockDevice : public StreamConnection{
5458

5559
virtual size_t send(const void* data, size_t bytes) override;
5660

61+
void push_expected_stream_data(const void* data, size_t bytes);
62+
5763

5864
private:
5965
void device_thread();
@@ -71,6 +77,8 @@ class MockDevice : public StreamConnection{
7177
size_t m_host_to_device_capacity = 1024;
7278
std::deque<uint8_t> m_host_to_device_line;
7379

80+
std::deque<uint8_t> m_expected_host_to_device_stream;
81+
7482
std::atomic<bool> m_stopping;
7583

7684
mutable Mutex m_device_lock;
@@ -80,6 +88,8 @@ class MockDevice : public StreamConnection{
8088
mutable Mutex m_host_lock;
8189
ConditionVariable m_host_cv;
8290
AsyncTask m_host_thread;
91+
92+
mutable Mutex m_print_lock;
8393
};
8494

8595

Common/Cpp/StreamConnections/ReliableStreamConnection.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ ReliableStreamConnection::ReliableStreamConnection(
2525
Logger& logger, bool log_everything,
2626
ThreadPool& thread_pool,
2727
StreamConnection& unreliable_connection,
28-
WallDuration retransmit_timeout
28+
WallDuration retransmit_timeout,
29+
Mutex* print_lock
2930
)
3031
: m_logger(logger)
3132
, m_unreliable_connection(unreliable_connection)
3233
, m_retransmit_timeout(retransmit_timeout)
34+
, m_print_lock(print_lock)
3335
, m_log_everything(log_everything)
3436
// , m_version_verified(false)
3537
, m_remote_slot_capacity(1)
@@ -228,7 +230,10 @@ void ReliableStreamConnection::retransmit_thread(){
228230
//
229231

230232
void ReliableStreamConnection::on_recv(const void* data, size_t bytes){
231-
cout << "ReliableStreamConnection::on_recv(): " << bytes << endl;
233+
if (m_print_lock){
234+
std::lock_guard<Mutex> lg(*m_print_lock);
235+
cout << "ReliableStreamConnection::on_recv(): " << bytes << endl;
236+
}
232237
pabb2_PacketParser_push_bytes(
233238
&m_parser,
234239
this, &ReliableStreamConnection::on_packet,

Common/Cpp/StreamConnections/ReliableStreamConnection.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ReliableStreamConnection final
3434
Logger& logger, bool log_everything,
3535
ThreadPool& thread_pool,
3636
StreamConnection& unreliable_connection,
37-
WallDuration retransmit_timeout = Milliseconds(100)
37+
WallDuration retransmit_timeout = Milliseconds(100),
38+
Mutex* print_lock = nullptr
3839
);
3940
~ReliableStreamConnection();
4041

@@ -98,6 +99,7 @@ class ReliableStreamConnection final
9899
Logger& m_logger;
99100
StreamConnection& m_unreliable_connection;
100101
const WallDuration m_retransmit_timeout;
102+
Mutex* m_print_lock;
101103

102104
pabb2_PacketSender m_reliable_sender;
103105
pabb2_PacketParser m_parser;
@@ -113,6 +115,7 @@ class ReliableStreamConnection final
113115
mutable Mutex m_lock;
114116
ConditionVariable m_cv;
115117
AsyncTask m_retransmit_thread;
118+
116119
};
117120

118121

Common/PABotBase2/PABotBase2_StreamCoalescer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ bool pabb2_StreamCoalescer_push_stream(pabb2_StreamCoalescer* self, const pabb2_
144144
uint8_t diff = seqnum - slot_head;
145145
// printf("seqnum = %d, slot_head = %d\n", seqnum, slot_head);
146146
if (diff >= PABB2_StreamCoalescer_SLOTS){
147-
// printf("Device: In the past.\n");
147+
// printf("Device: In the past or too far ahead.\n");
148148
// Negative means we're in the past and we can just ack.
149149
return diff & 0x80;
150150
}
@@ -185,7 +185,7 @@ bool pabb2_StreamCoalescer_push_stream(pabb2_StreamCoalescer* self, const pabb2_
185185
size_t pabb2_StreamCoalescer_read(pabb2_StreamCoalescer* self, void* data, size_t max_bytes){
186186
if (self->stream_reset){
187187
self->stream_reset = false;
188-
return (size_t)-1;
188+
return 0;
189189
}
190190

191191
size_t read = 0;

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
309309
logger, true,
310310
GlobalThreadPools::unlimited_realtime(),
311311
device,
312-
1s
312+
1s,
313+
&device.print_lock()
313314
);
314315

315316
connection.reset();
@@ -324,19 +325,21 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
324325
connection.wait_for_pending();
325326

326327

327-
connection.send("asdf", 4);
328-
connection.send("qwer", 4);
329-
connection.send("zxcv", 4);
328+
connection.send("asdf", 4); device.push_expected_stream_data("asdf", 4);
329+
connection.send("qwer", 4); device.push_expected_stream_data("qwer", 4);
330+
connection.send("zxcv", 4); device.push_expected_stream_data("zxcv", 4);
331+
connection.wait_for_pending();
332+
330333
cout << "sent = " << connection.send("0123456789abcdef", 16) << endl;
331334
// connection.send("0123456789abcdef", 16);
332335
// connection.send("0123456789abcdef", 16);
333336
// connection.send("0123456789abcdef", 16);
334337

335338
connection.print();
339+
device.print();
336340

337341
connection.wait_for_pending();
338342

339-
device.print();
340343

341344
scope.wait_for(60s);
342345
}

SerialPrograms/Source/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,11 @@ bool set_fast_travel_menu_filter_routine(
368368
int target_filter_index = static_cast<int>(filter);
369369
WallClock deadline = current_time() + 30s;
370370
do {
371-
int selected_filter_index = get_current_selector_index(console, FAST_TRAVEL_FILTER_ARROW_BOX(), FAST_TRAVEL_FILTER_SPACING);
371+
int selected_filter_index = get_current_selector_index(
372+
console,
373+
FAST_TRAVEL_FILTER_ARROW_BOX(),
374+
FAST_TRAVEL_FILTER_SPACING
375+
);
372376
if (selected_filter_index == -1){
373377
return false;
374378
}
@@ -557,4 +561,4 @@ FastTravelState open_map_and_fly_to(ConsoleHandle& console, ProControllerContext
557561

558562
}
559563
}
560-
}
564+
}

0 commit comments

Comments
 (0)