1212#include " Common/Cpp/PrettyPrint.h"
1313#include " SerialPABotBase_Connection.h"
1414
15+ // #include <iostream>
16+ // using std::cout;
17+ // using std::endl;
18+
1519namespace PokemonAutomation {
1620namespace SerialPABotBase {
1721
@@ -35,6 +39,7 @@ class ControllerStatusThread{
3539 : m_connection(connection)
3640 , m_callback(callback)
3741 , m_stopping(false )
42+ , m_error(false )
3843 , m_status_thread(&ControllerStatusThread::status_thread, this )
3944 {}
4045 ~ControllerStatusThread (){
@@ -61,7 +66,10 @@ class ControllerStatusThread{
6166 std::thread watchdog ([&, this ]{
6267 WallClock next_ping = current_time ();
6368 while (true ){
64- if (m_stopping.load (std::memory_order_relaxed) || !m_connection.is_ready ()){
69+ if (m_stopping.load (std::memory_order_relaxed) ||
70+ m_error.load (std::memory_order_acquire) ||
71+ !m_connection.is_ready ()
72+ ){
6573 break ;
6674 }
6775
@@ -75,7 +83,10 @@ class ControllerStatusThread{
7583 }
7684
7785 std::unique_lock<std::mutex> lg (m_sleep_lock);
78- if (m_stopping.load (std::memory_order_relaxed) || !m_connection.is_ready ()){
86+ if (m_stopping.load (std::memory_order_relaxed) ||
87+ m_error.load (std::memory_order_acquire) ||
88+ !m_connection.is_ready ()
89+ ){
7990 break ;
8091 }
8192
@@ -101,8 +112,8 @@ class ControllerStatusThread{
101112 last_ack.store (current_time (), std::memory_order_relaxed);
102113 }catch (OperationCancelledException&){
103114 break ;
104- }catch (InvalidConnectionStateException&){
105- break ;
115+ }catch (InvalidConnectionStateException& e ){
116+ error = e. message () ;
106117 }catch (SerialProtocolException& e){
107118 error = e.message ();
108119 }catch (ConnectionException& e){
@@ -111,6 +122,7 @@ class ControllerStatusThread{
111122 error = " Unknown error." ;
112123 }
113124 if (!error.empty ()){
125+ m_error.store (true , std::memory_order_release);
114126 m_connection.set_status_line1 (error, COLOR_RED);
115127 m_callback.stop_with_error (std::move (error));
116128 }
@@ -140,6 +152,7 @@ class ControllerStatusThread{
140152 ControllerStatusThreadCallback& m_callback;
141153 CancellableHolder<CancellableScope> m_scope;
142154 std::atomic<bool > m_stopping;
155+ std::atomic<bool > m_error;
143156 std::mutex m_sleep_lock;
144157 std::condition_variable m_cv;
145158 std::thread m_status_thread;
0 commit comments