Repository navigation
Expand file tree
/
Copy pathSysbotBase_Connection.cpp
More file actions
295 lines (238 loc) · 8.88 KB
/
Copy pathSysbotBase_Connection.cpp
File metadata and controls
295 lines (238 loc) · 8.88 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
/* sys-botbase Connection
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <QEventLoop>
#include "Common/Cpp/Time.h"
//#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "NintendoSwitch/NintendoSwitch_Settings.h"
#include "SysbotBase_Connection.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace SysbotBase{
using namespace std::chrono_literals;
bool parse_ip_and_port(const std::string& str, QHostAddress& address, int& port){
// IPv4
QStringList parts = QString::fromStdString(str).split(":");
if (parts.size() != 2){
return false;
}
address = QHostAddress(parts[0]);
bool ok;
port = parts[1].toUInt(&ok);
if (!ok){
return false;
}
// TODO: IPv6
return true;
}
TcpSysbotBase_Connection::TcpSysbotBase_Connection(
Logger& logger,
const std::string& url
)
: m_logger(logger)
, m_socket(GlobalThreadPools::unlimited_realtime())
, m_last_ping_send(WallClock::min())
, m_last_ping_receive(WallClock::min())
{
m_controller_list.emplace_back(ControllerType::NintendoSwitch_WiredController);
m_current_controller = ControllerType::NintendoSwitch_WiredController;
QHostAddress address;
int port;
if (!parse_ip_and_port(url, address, port)){
set_status_line0(html_color_text("Invalid IP address + port.", COLOR_RED));
return;
}
set_status_line0(html_color_text("Connecting...", COLOR_DARKGREEN));
m_connecting_message =
"Connecting To: " + address.toString().toStdString() +
" - Port: " + std::to_string(port);
m_logger.log(m_connecting_message);
// Attach ourselves as listener first since it might immediately return as
// connected.
m_socket.add_listener(*this);
try{
m_socket.connect(address.toString().toStdString(), port);
}catch (...){
m_socket.remove_listener(*this);
throw;
}
}
TcpSysbotBase_Connection::~TcpSysbotBase_Connection(){
try{
write_data("detachController\r\n");
}catch (...){}
m_socket.remove_listener(*this);
m_socket.close();
{
std::lock_guard<Mutex> lg(m_lock);
m_cv.notify_all();
}
m_thread.wait_and_ignore_exceptions();
}
void TcpSysbotBase_Connection::write_data(const std::string& data){
WriteSpinLock lg(m_send_lock, "TcpSysbotBase_Connection::write_data()");
// cout << "Sending: " << data << endl;
m_socket.send(data.data(), data.size());
}
std::string pretty_print(uint64_t x){
auto ms = x / 1000;
auto us = x - ms * 1000;
std::string str_ms = std::to_string(ms);
std::string str_us = std::to_string(us);
return str_ms + "." + std::string(3 - str_us.size(), '0') + str_us;
}
void TcpSysbotBase_Connection::thread_loop(){
std::unique_lock<Mutex> lg(m_lock);
m_last_ping_send = current_time();
while (true){
ClientSocket::State state = m_socket.state();
if (state == ClientSocket::State::DESTRUCTING || state == ClientSocket::State::NOT_RUNNING){
break;
}
WallClock now = current_time();
std::chrono::milliseconds time_since = std::chrono::duration_cast<std::chrono::milliseconds>(now - m_last_ping_receive);
if (time_since > std::chrono::seconds(5)){
std::string str = "Last Ack: " + pretty_print(time_since.count()) + " seconds ago";
set_status_line1(str, COLOR_RED);
}
m_last_ping_send = current_time();
if (NintendoSwitch::ConsoleSettings::instance().ENABLE_SBB3_PINGS){
// If we're more than 60 pings behind, just start clearing them.
while (m_active_pings.size() > 60){
m_active_pings.erase(m_active_pings.begin());
}
m_active_pings[m_ping_seqnum] = m_last_ping_send;
write_data("ping " + std::to_string(m_ping_seqnum) + "\r\n");
m_ping_seqnum++;
}else{
write_data("getVersion\r\n");
}
m_cv.wait_for(lg, std::chrono::seconds(1));
}
}
void TcpSysbotBase_Connection::on_connect_finished(const std::string& error_message){
try{
if (!error_message.empty()){
m_logger.log(m_connecting_message, COLOR_RED);
set_status_line0(m_connecting_message, COLOR_RED);
return;
}
m_logger.log(m_connecting_message + " (Success)", COLOR_BLUE);
write_data("configure echoCommands 0\r\n");
write_data("getVersion\r\n");
}catch (...){}
}
void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){
// cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl;
WallClock now = current_time();
try{
const char* ptr = (const char*)data;
for (size_t c = 0; c < bytes; c++){
char ch = ptr[c];
if (ch == '\r'){
continue;
}
if (ch != '\n'){
m_receive_buffer.emplace_back(ch);
continue;
}
process_message(
std::string(
m_receive_buffer.begin(),
m_receive_buffer.end()
),
now
);
m_receive_buffer.clear();
}
// m_listeners.run_method(&Listener::on_receive_data, data, bytes);
}catch (...){}
}
void TcpSysbotBase_Connection::process_message(const std::string& message, WallClock timestamp){
if (GlobalSettings::instance().LOG_EVERYTHING){
m_logger.log("Received: " + message, COLOR_DARKGREEN);
}
m_listeners.run_method(&Listener::on_message, message);
// Version #
std::string str = message;
if (str.find('.') != std::string::npos){
while (!str.empty() && str.back() <= 32){
str.pop_back();
}
set_status_line0("sys-botbase: Version " + str, COLOR_BLUE);
std::lock_guard<Mutex> lg(m_lock);
m_last_ping_receive = timestamp;
if (m_last_ping_send != WallClock::min()){
std::chrono::microseconds latency = std::chrono::duration_cast<std::chrono::microseconds>(timestamp - m_last_ping_send);
std::string text = "Response Time: " + pretty_print(latency.count()) + " ms";
if (latency < 10ms){
set_status_line1(text, COLOR_BLUE);
}else if (latency < 50ms){
set_status_line1(text, COLOR_DARKGREEN);
}else{
set_status_line1(text, COLOR_ORANGE);
}
}
if (!m_thread){
set_mode(str);
}
}
size_t pos = str.find("ping");
if (pos != std::string::npos){
const char* ptr = &str[pos + 4];
while (true){
char ch = ptr[0];
if (ch < 32 || ch == ' ' || ch == '\t'){
ptr++;
continue;
}
break;
}
size_t ping_seqnum = atoll(ptr);
std::lock_guard<Mutex> lg(m_lock);
m_last_ping_receive = timestamp;
auto iter = m_active_pings.find(ping_seqnum);
if (iter == m_active_pings.end()){
m_logger.log("Received Unexpected Ping: " + std::to_string(ping_seqnum));
return;
}
std::chrono::microseconds latency = std::chrono::duration_cast<std::chrono::microseconds>(timestamp - iter->second);
std::string text = "Response Time: " + pretty_print(latency.count()) + " ms";
if (latency < 10ms){
set_status_line1(text, COLOR_BLUE);
}else if (latency < 50ms){
set_status_line1(text, COLOR_DARKGREEN);
}else{
set_status_line1(text, COLOR_ORANGE);
}
m_active_pings.erase(iter);
}
}
void TcpSysbotBase_Connection::set_mode(const std::string& sbb_version){
if (sbb_version.rfind("2.", 0) == 0){
set_status_line1("sbb2 is no longer supported. Please upgrade to sbb3.", COLOR_RED);
return;
}else if (sbb_version.rfind("3.", 0) == 0){
m_logger.log("Detected sbb3. Using CC command queue.", COLOR_BLUE);
if (NintendoSwitch::ConsoleSettings::instance().ENABLE_SBB3_LOGGING){
write_data("configure enableLogs 1\r\n");
}
write_data("configure enablePA 1\r\n");
}else{
m_logger.log("Unrecognized sbb version: " + sbb_version, COLOR_RED);
return;
}
m_thread = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking(
[this]{ thread_loop(); }
);
declare_ready();
}
}
}