diff --git a/src/util/SocketIoClient.cpp b/src/util/SocketIoClient.cpp index e8cda254..8d4a5a1b 100644 --- a/src/util/SocketIoClient.cpp +++ b/src/util/SocketIoClient.cpp @@ -197,8 +197,7 @@ void SocketIoClient::handleEngineIoMessage_(char* ptr, int length) // "ping" -- send pong connection_->send("3"); - pingTimer_.stop(); - pingTimer_.start(); + pingTimer_.restart(); break; } case '4': diff --git a/src/util/ThreadedTimer.cpp b/src/util/ThreadedTimer.cpp index f4214d31..0709011d 100644 --- a/src/util/ThreadedTimer.cpp +++ b/src/util/ThreadedTimer.cpp @@ -28,6 +28,7 @@ ThreadedTimer::ThreadedTimer() : isDestroying_(false) + , isRestarting_(false) , repeat_(false) , timeoutMilliseconds_(0) { @@ -88,6 +89,19 @@ void ThreadedTimer::stop() } } +void ThreadedTimer::restart() +{ + if (objectThread_.joinable()) + { + isRestarting_ = true; + timerCV_.notify_one(); + } + else + { + start(); + } +} + void ThreadedTimer::eventLoop_() { #if defined(__APPLE__) @@ -98,9 +112,10 @@ void ThreadedTimer::eventLoop_() do { std::unique_lock lk(timerMutex_); - if (!timerCV_.wait_for(lk, std::chrono::milliseconds(timeoutMilliseconds_), [&]() { return isDestroying_; }) && fn_) + isRestarting_ = false; + if (!timerCV_.wait_for(lk, std::chrono::milliseconds(timeoutMilliseconds_), [&]() { return isDestroying_ || isRestarting_; }) && fn_) { fn_(*this); } - } while (!isDestroying_ && repeat_); + } while (!isDestroying_ && (isRestarting_ || repeat_)); } diff --git a/src/util/ThreadedTimer.h b/src/util/ThreadedTimer.h index a698a990..a79aa814 100644 --- a/src/util/ThreadedTimer.h +++ b/src/util/ThreadedTimer.h @@ -44,11 +44,13 @@ public: void start(); void stop(); + void restart(); bool isRunning(); private: bool isDestroying_; + bool isRestarting_; std::thread objectThread_; std::mutex timerMutex_; std::condition_variable timerCV_; @@ -59,4 +61,4 @@ private: void eventLoop_(); }; -#endif // THREADED_TIMER_H \ No newline at end of file +#endif // THREADED_TIMER_H