|
9 | 9 | //#include "CommonFramework/Logging/Logger.h" |
10 | 10 | #include "CommonFramework/GlobalSettingsPanel.h" |
11 | 11 | #include "CommonFramework/Options/Environment/ThemeSelectorOption.h" |
| 12 | +#include "NintendoSwitch/NintendoSwitch_Settings.h" |
12 | 13 | #include "SysbotBase_Connection.h" |
13 | 14 |
|
14 | 15 | //#include <iostream> |
15 | 16 | //using std::cout; |
16 | 17 | //using std::endl; |
17 | 18 |
|
| 19 | + |
| 20 | + |
18 | 21 | namespace PokemonAutomation{ |
19 | 22 | namespace SysbotBase{ |
20 | 23 |
|
@@ -139,7 +142,18 @@ void TcpSysbotBase_Connection::thread_loop(){ |
139 | 142 | } |
140 | 143 |
|
141 | 144 | m_last_ping_send = current_time(); |
142 | | - write_data("getVersion\r\n"); |
| 145 | + if (supports_command_queue() && NintendoSwitch::ConsoleSettings::instance().ENABLE_SBB3_PINGS){ |
| 146 | + // If we're more than 60 pings behind, just start clearing them. |
| 147 | + while (m_active_pings.size() > 60){ |
| 148 | + m_active_pings.erase(m_active_pings.begin()); |
| 149 | + } |
| 150 | + |
| 151 | + m_active_pings[m_ping_seqnum] = m_last_ping_send; |
| 152 | + write_data("ping " + std::to_string(m_ping_seqnum) + "\r\n"); |
| 153 | + m_ping_seqnum++; |
| 154 | + }else{ |
| 155 | + write_data("getVersion\r\n"); |
| 156 | + } |
143 | 157 | m_cv.wait_for(lg, std::chrono::seconds(1)); |
144 | 158 | } |
145 | 159 | } |
@@ -229,16 +243,50 @@ void TcpSysbotBase_Connection::process_message(const std::string& message, WallC |
229 | 243 | } |
230 | 244 | } |
231 | 245 |
|
| 246 | + size_t pos = str.find("ping"); |
| 247 | + if (pos != std::string::npos){ |
| 248 | + const char* ptr = &str[pos]; |
| 249 | + while (true){ |
| 250 | + char ch = ptr[0]; |
| 251 | + if (ch < 32 || ch == ' ' || ch == '\t'){ |
| 252 | + ptr++; |
| 253 | + continue; |
| 254 | + } |
| 255 | + break; |
| 256 | + } |
| 257 | + size_t ping_seqnum = atoll(ptr); |
| 258 | + |
| 259 | + std::lock_guard<std::mutex> lg(m_lock); |
| 260 | + m_last_ping_receive = timestamp; |
| 261 | + auto iter = m_active_pings.find(ping_seqnum); |
| 262 | + if (iter == m_active_pings.end()){ |
| 263 | + m_logger.log("Received Unexpected Ping: " + std::to_string(ping_seqnum)); |
| 264 | + return; |
| 265 | + } |
| 266 | + |
| 267 | + std::chrono::microseconds latency = std::chrono::duration_cast<std::chrono::microseconds>(timestamp - iter->second); |
| 268 | + std::string text = "Response Time: " + pretty_print(latency.count()) + " ms"; |
| 269 | + if (latency < 10ms){ |
| 270 | + set_status_line1(text, COLOR_BLUE); |
| 271 | + }else if (latency < 50ms){ |
| 272 | + set_status_line1(text, COLOR_DARKGREEN); |
| 273 | + }else{ |
| 274 | + set_status_line1(text, COLOR_ORANGE); |
| 275 | + } |
| 276 | + |
| 277 | + m_active_pings.erase(iter); |
| 278 | + } |
| 279 | + |
232 | 280 | } |
233 | 281 | void TcpSysbotBase_Connection::set_mode(const std::string& sbb_version){ |
234 | 282 | if (sbb_version.rfind("2.", 0) == 0){ |
235 | 283 | m_logger.log("Detected sbb2. Using old (slow) command set.", COLOR_ORANGE); |
236 | 284 | write_data("configure mainLoopSleepTime 0\r\n"); |
237 | | - m_supports_command_queue = false; |
| 285 | + m_supports_command_queue.store(false, std::memory_order_relaxed); |
238 | 286 | }else if (PreloadSettings::instance().DEVELOPER_MODE && sbb_version.rfind("3.", 0) == 0){ |
239 | 287 | m_logger.log("Detected sbb3. Using CC command queue.", COLOR_BLUE); |
240 | 288 | write_data("configure enablePA 1\r\n"); |
241 | | - m_supports_command_queue = true; |
| 289 | + m_supports_command_queue.store(true, std::memory_order_relaxed); |
242 | 290 | }else{ |
243 | 291 | m_logger.log("Unrecognized sbb version: " + sbb_version, COLOR_RED); |
244 | 292 | return; |
|
0 commit comments