From 8afca63dedbc9843c76ca77bc814285a3ddd0d37 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@d8a302eb-03bc-478d-80e4-98257eca68ef> Date: Sun, 21 Dec 2014 20:21:25 +0000 Subject: [PATCH] exception git-svn-id: svn://elaine/murooma/trunk@316 d8a302eb-03bc-478d-80e4-98257eca68ef --- client/clientConnection.cpp | 21 +++++++++------------ client/clientConnection.h | 7 ++++--- client/controller.cpp | 12 ++++++------ common/snapException.h | 26 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 common/snapException.h diff --git a/client/clientConnection.cpp b/client/clientConnection.cpp index 1b7a28b4..a20aafbc 100644 --- a/client/clientConnection.cpp +++ b/client/clientConnection.cpp @@ -4,13 +4,13 @@ #include "common/log.h" #include "clientConnection.h" #include "common/utils.h" - +#include "common/snapException.h" using namespace std; -ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : active_(false), connected_(false), messageReceiver(_receiver), reqId(1), ip(_ip), port(_port), readerThread(NULL), timeouts(0) +ClientConnection::ClientConnection(MessageReceiver* _receiver, const std::string& _ip, size_t _port) : active_(false), connected_(false), messageReceiver(_receiver), reqId(1), ip(_ip), port(_port), readerThread(NULL), sumTimeout(chronos::msec(0)) { } @@ -110,10 +110,10 @@ bool ClientConnection::send(BaseMessage* message) } -shared_ptr ClientConnection::sendRequest(BaseMessage* message, size_t timeout) +shared_ptr ClientConnection::sendRequest(BaseMessage* message, const chronos::msec& timeout) { shared_ptr response(NULL); - if (++reqId == 100) + if (++reqId == 10000) reqId = 1; message->id = reqId; //cout << "Req: " << reqId << "\n"; @@ -129,18 +129,15 @@ shared_ptr ClientConnection::sendRequest(BaseMessage* message if (pendingRequest->cv.wait_for(lck,std::chrono::milliseconds(timeout)) == std::cv_status::no_timeout) { response = pendingRequest->response; - timeouts = 0; + sumTimeout = chronos::msec(0); //cout << "Resp: " << pendingRequest->id << "\n"; } else { - ++timeouts; - cout << "timeout while waiting for response to: " << reqId << ", timeout " << timeouts << "\n"; - if (timeouts > 2*60) - { - std::exception e; - throw e; - } + sumTimeout += timeout; + cout << "timeout while waiting for response to: " << reqId << ", timeout " << sumTimeout.count() << "\n"; + if (sumTimeout > chronos::sec(10)) + throw snapException("sum timeout exceeded 10s"); } { std::unique_lock mlock(mutex_); diff --git a/client/clientConnection.h b/client/clientConnection.h index 8b157d03..686dc522 100644 --- a/client/clientConnection.h +++ b/client/clientConnection.h @@ -10,6 +10,7 @@ #include #include #include "message/message.h" +#include "common/timeDefs.h" using boost::asio::ip::tcp; @@ -44,10 +45,10 @@ public: virtual void start(); virtual void stop(); virtual bool send(BaseMessage* _message); - virtual std::shared_ptr sendRequest(BaseMessage* message, size_t timeout); + virtual std::shared_ptr sendRequest(BaseMessage* message, const chronos::msec& timeout = chronos::msec(1000)); template - std::shared_ptr sendReq(BaseMessage* message, size_t timeout) + std::shared_ptr sendReq(BaseMessage* message, const chronos::msec& timeout = chronos::msec(1000)) { std::shared_ptr reply = sendRequest(message, timeout); if (!reply) @@ -88,7 +89,7 @@ protected: std::string ip; size_t port; std::thread* readerThread; - int timeouts; + chronos::msec sumTimeout; }; diff --git a/client/controller.cpp b/client/controller.cpp index 822aee00..85e1ec80 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -77,16 +77,16 @@ void Controller::worker() clientConnection->start(); RequestMsg requestMsg(serversettings); shared_ptr serverSettings(NULL); - while (!(serverSettings = clientConnection->sendReq(&requestMsg, 1000))); + while (!(serverSettings = clientConnection->sendReq(&requestMsg))); cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n"; requestMsg.request = sampleformat; - while (!(sampleFormat = clientConnection->sendReq(&requestMsg, 1000))); + while (!(sampleFormat = clientConnection->sendReq(&requestMsg))); cout << "SampleFormat rate: " << sampleFormat->rate << ", bits: " << sampleFormat->bits << ", channels: " << sampleFormat->channels << "\n"; requestMsg.request = header; shared_ptr headerChunk(NULL); - while (!(headerChunk = clientConnection->sendReq(&requestMsg, 1000))); + while (!(headerChunk = clientConnection->sendReq(&requestMsg))); cout << "Codec: " << headerChunk->codec << "\n"; if (headerChunk->codec == "ogg") decoder = new OggDecoder(); @@ -97,7 +97,7 @@ void Controller::worker() RequestMsg timeReq(timemsg); for (size_t n=0; n<50; ++n) { - shared_ptr reply = clientConnection->sendReq(&timeReq, 2000); + shared_ptr reply = clientConnection->sendReq(&timeReq, chronos::msec(2000)); if (reply) { double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.; @@ -115,14 +115,14 @@ void Controller::worker() CommandMsg startStream("startStream"); shared_ptr ackMsg(NULL); - while (!(ackMsg = clientConnection->sendReq(&startStream, 1000))); + while (!(ackMsg = clientConnection->sendReq(&startStream))); try { while (active_) { usleep(500*1000); - shared_ptr reply = clientConnection->sendReq(&timeReq, 1000); + shared_ptr reply = clientConnection->sendReq(&timeReq); if (reply) { double latency = (reply->received.sec - reply->sent.sec) + (reply->received.usec - reply->sent.usec) / 1000000.; diff --git a/common/snapException.h b/common/snapException.h new file mode 100644 index 00000000..6b197f54 --- /dev/null +++ b/common/snapException.h @@ -0,0 +1,26 @@ +#ifndef SNAP_EXCEPTION_H +#define SNAP_EXCEPTION_H + +#include +#include + + +struct snapException : std::exception { + snapException(const std::string& what) noexcept + { + what_ = what; + } + + const char* what() const noexcept + { + return what_.c_str(); + } + +private: + std::string what_; +}; + + +#endif + +