diff --git a/client/client_connection.cpp b/client/client_connection.cpp index 6ffbfd0d..61bef856 100644 --- a/client/client_connection.cpp +++ b/client/client_connection.cpp @@ -211,13 +211,10 @@ void ClientConnection::reader() getNextMessage(); } } - catch (const std::exception& e) - { - if (messageReceiver_ != nullptr) - messageReceiver_->onException(this, make_shared(e.what())); - } catch (...) { + if (messageReceiver_ != nullptr) + messageReceiver_->onException(this, std::current_exception()); } active_ = false; } diff --git a/client/client_connection.hpp b/client/client_connection.hpp index 0cf511e6..28c72aec 100644 --- a/client/client_connection.hpp +++ b/client/client_connection.hpp @@ -78,17 +78,13 @@ private: }; -/// would be nicer to use std::exception_ptr -/// but not supported on all plattforms -typedef std::shared_ptr shared_exception_ptr; - /// Interface: callback for a received message and error reporting class MessageReceiver { public: virtual ~MessageReceiver() = default; virtual void onMessageReceived(ClientConnection* connection, const msg::BaseMessage& baseMessage, char* buffer) = 0; - virtual void onException(ClientConnection* connection, shared_exception_ptr exception) = 0; + virtual void onException(ClientConnection* connection, std::exception_ptr exception) = 0; }; diff --git a/client/controller.cpp b/client/controller.cpp index ecdf51dc..58d9ee5c 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -46,9 +46,9 @@ Controller::Controller(const ClientSettings& settings, std::unique_ptrwhat() << "\n"; + LOG(ERROR) << "Controller::onException\n"; async_exception_ = exception; } @@ -159,16 +159,16 @@ void Controller::onMessageReceived(ClientConnection* /*connection*/, const msg:: } } - if (baseMessage.type != message_type::kTime) - if (sendTimeSyncMessage(1000)) - LOG(DEBUG) << "time sync onMessageReceived\n"; + // if (baseMessage.type != message_type::kTime) + // if (sendTimeSyncMessage(1000)) + // LOG(DEBUG) << "time sync onMessageReceived\n"; } -bool Controller::sendTimeSyncMessage(long after) +bool Controller::sendTimeSyncMessage(const std::chrono::milliseconds& after) { - static long lastTimeSync(0); - long now = chronos::getTickCount(); + static chronos::time_point_clk lastTimeSync(chronos::clk::now()); + auto now = chronos::clk::now(); if (lastTimeSync + after > now) return false; @@ -227,8 +227,8 @@ void Controller::worker() { if (async_exception_) { - LOG(DEBUG) << "Async exception: " << async_exception_->what() << "\n"; - throw SnapException(async_exception_->what()); + LOG(DEBUG) << "Async exception\n"; + std::rethrow_exception(async_exception_); } auto reply = clientConnection_->sendReq(&timeReq, chronos::msec(2000)); @@ -249,12 +249,12 @@ void Controller::worker() chronos::sleep(100); if (async_exception_) { - LOG(DEBUG) << "Async exception: " << async_exception_->what() << "\n"; - throw SnapException(async_exception_->what()); + LOG(DEBUG) << "Async exception\n"; + std::rethrow_exception(async_exception_); } } - if (sendTimeSyncMessage(5000)) + if (sendTimeSyncMessage(1000ms)) LOG(DEBUG) << "time sync main loop\n"; } } diff --git a/client/controller.hpp b/client/controller.hpp index 777ed75a..380414f4 100644 --- a/client/controller.hpp +++ b/client/controller.hpp @@ -42,6 +42,7 @@ #include "metadata.hpp" #include "stream.hpp" +using namespace std::chrono_literals; /// Forwards PCM data to the audio player /** @@ -64,11 +65,11 @@ public: /// Implementation of MessageReceiver. /// Used for async exception reporting - void onException(ClientConnection* connection, shared_exception_ptr exception) override; + void onException(ClientConnection* connection, std::exception_ptr exception) override; private: void worker(); - bool sendTimeSyncMessage(long after = 1000); + bool sendTimeSyncMessage(const std::chrono::milliseconds& after = 1000ms); ClientSettings settings_; std::string meta_callback_; std::atomic active_; @@ -83,7 +84,7 @@ private: std::unique_ptr headerChunk_; std::mutex receiveMutex_; - shared_exception_ptr async_exception_; + std::exception_ptr async_exception_; }; diff --git a/client/stream.cpp b/client/stream.cpp index 067e7287..95c32543 100644 --- a/client/stream.cpp +++ b/client/stream.cpp @@ -117,16 +117,10 @@ void Stream::clearChunks() void Stream::addChunk(unique_ptr chunk) { - // drop chunks that should have been played 10s before. Just in case, this shouldn't happen. - while (!chunks_.empty()) - { - auto first_chunk = chunks_.front(); - cs::usec age = std::chrono::duration_cast(TimeProvider::serverNow() - first_chunk->start()); - if (age > 10s + bufferMs_) - chunks_.pop(); - else - break; - } + // drop chunk if it's too old. Just in case, this shouldn't happen. + cs::usec age = std::chrono::duration_cast(TimeProvider::serverNow() - chunk->start()); + if (age > 5s + bufferMs_) + return; // LOG(DEBUG, LOG_TAG) << "new chunk: " << chunk->durationMs() << " ms, Chunks: " << chunks_.size() << "\n";