diff --git a/client/controller.cpp b/client/controller.cpp index 7c07abaa..14272214 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -36,7 +36,7 @@ using namespace std; -Controller::Controller() : MessageReceiver(), active_(false), sampleFormat_(NULL), decoder_(NULL) +Controller::Controller() : MessageReceiver(), active_(false), sampleFormat_(NULL), decoder_(NULL), sendTimeSyncMsg_(false), asyncException_(false) { } @@ -44,6 +44,8 @@ Controller::Controller() : MessageReceiver(), active_(false), sampleFormat_(NULL void Controller::onException(ClientConnection* connection, const std::exception& exception) { logE << "onException: " << exception.what() << "\n"; + exception_ = exception; + asyncException_ = true; } @@ -66,6 +68,22 @@ void Controller::onMessageReceived(ClientConnection* connection, const msg::Base delete pcmChunk; } } + else if (baseMessage.type == message_type::kTime) + { + msg::Time reply; + reply.deserialize(baseMessage, buffer); + double latency = (reply.received.sec - reply.sent.sec) + (reply.received.usec - reply.sent.usec) / 1000000.; +// logO << "diff to server [ms]: " << (float)TimeProvider::getInstance().getDiffToServer().count() / 1000.f << "\n"; +// logO << "timeMsg: " << latency << "\n"; + TimeProvider::getInstance().setDiffToServer((reply.latency - latency) * 1000 / 2); + } + + if (sendTimeSyncMsg_) + { + msg::Request timeReq(kTime); + clientConnection_->send(&timeReq); + sendTimeSyncMsg_ = false; + } } @@ -147,17 +165,25 @@ void Controller::worker() while (active_) { - usleep(500*1000); - shared_ptr reply = clientConnection_->sendReq(&timeReq); - if (reply) - { - double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.; - TimeProvider::getInstance().setDiffToServer((reply->latency - latency) * 1000 / 2); - } + for (size_t n=0; n<10 && active_; ++n) + { + usleep(100*1000); + if (asyncException_) + throw exception_; + } + + sendTimeSyncMsg_ = true; +// shared_ptr reply = clientConnection_->sendReq(&timeReq); +// if (reply) +// { +// double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.; +// TimeProvider::getInstance().setDiffToServer((reply->latency - latency) * 1000 / 2); +// } } } catch (const std::exception& e) { + asyncException_ = false; logS(kLogErr) << "Exception in Controller::worker(): " << e.what() << endl; logO << "Stopping clientConnection" << endl; clientConnection_->stop(); diff --git a/client/controller.h b/client/controller.h index 730e2b6d..93dc51bd 100644 --- a/client/controller.h +++ b/client/controller.h @@ -60,6 +60,9 @@ private: Decoder* decoder_; PcmDevice pcmDevice_; size_t latency_; + bool sendTimeSyncMsg_; + std::exception exception_; + bool asyncException_; };