diff --git a/client/controller.cpp b/client/controller.cpp index 85e1ec80..34b82f4d 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -60,7 +60,10 @@ void Controller::start(const PcmDevice& pcmDevice, const std::string& _ip, size_ void Controller::stop() { + cout << "Stopping\n"; active_ = false; + controllerThread->join(); + clientConnection->stop(); } @@ -77,16 +80,16 @@ void Controller::worker() clientConnection->start(); RequestMsg requestMsg(serversettings); shared_ptr serverSettings(NULL); - while (!(serverSettings = clientConnection->sendReq(&requestMsg))); + while (active_ && !(serverSettings = clientConnection->sendReq(&requestMsg))); cout << "ServerSettings buffer: " << serverSettings->bufferMs << "\n"; requestMsg.request = sampleformat; - while (!(sampleFormat = clientConnection->sendReq(&requestMsg))); + while (active_ && !(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))); + while (active_ && !(headerChunk = clientConnection->sendReq(&requestMsg))); cout << "Codec: " << headerChunk->codec << "\n"; if (headerChunk->codec == "ogg") decoder = new OggDecoder(); @@ -95,7 +98,7 @@ void Controller::worker() decoder->setHeader(headerChunk.get()); RequestMsg timeReq(timemsg); - for (size_t n=0; n<50; ++n) + for (size_t n=0; n<50 && active_; ++n) { shared_ptr reply = clientConnection->sendReq(&timeReq, chronos::msec(2000)); if (reply) @@ -115,7 +118,7 @@ void Controller::worker() CommandMsg startStream("startStream"); shared_ptr ackMsg(NULL); - while (!(ackMsg = clientConnection->sendReq(&startStream))); + while (active_ && !(ackMsg = clientConnection->sendReq(&startStream))); try { @@ -152,9 +155,11 @@ void Controller::worker() cout << "Stopping clientConnection\n"; clientConnection->stop(); cout << "done\n"; - usleep(1000000); + if (active_) + usleep(1000000); } } + cout << "Thread stopped\n"; } diff --git a/client/snapClient.cpp b/client/snapClient.cpp index 559a07b0..6a10cf49 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -11,6 +11,7 @@ #include "common/utils.h" #include "common/log.h" +#include "common/signalHandler.h" #include "controller.h" #include "alsaPlayer.h" @@ -18,6 +19,8 @@ using namespace std; namespace po = boost::program_options; +//bool g_terminated = false; +std::condition_variable terminateSignaled; PcmDevice getPcmDevice(const std::string& soundcard) { @@ -87,6 +90,10 @@ int main (int argc, char *argv[]) return 1; } + signal(SIGHUP, signal_handler); + signal(SIGTERM, signal_handler); + signal(SIGINT, signal_handler); + std::clog.rdbuf(new Log("snapclient", LOG_DAEMON)); if (runAsDaemon) { @@ -105,8 +112,11 @@ int main (int argc, char *argv[]) Controller controller; controller.start(pcmDevice, ip, port, latency); - while(true) - usleep(10000); + + std::mutex mtx; + std::unique_lock lck(mtx); + terminateSignaled.wait(lck); + controller.stop(); return 0; } diff --git a/common/signalHandler.h b/common/signalHandler.h index 4f2cd5c0..7c918a66 100644 --- a/common/signalHandler.h +++ b/common/signalHandler.h @@ -3,8 +3,9 @@ #include #include +#include -extern bool g_terminated; +extern std::condition_variable terminateSignaled; void signal_handler(int sig) { @@ -16,7 +17,11 @@ void signal_handler(int sig) break; case SIGTERM: syslog(LOG_WARNING, "Received SIGTERM signal."); - g_terminated = true; + terminateSignaled.notify_all(); + break; + case SIGINT: + syslog(LOG_WARNING, "Received SIGINT signal."); + terminateSignaled.notify_all(); break; default: syslog(LOG_WARNING, "Unhandled signal "); diff --git a/server/snapServer.cpp b/server/snapServer.cpp index b41a681a..d3302e19 100644 --- a/server/snapServer.cpp +++ b/server/snapServer.cpp @@ -8,7 +8,7 @@ #include "message/message.h" #include "pcmEncoder.h" #include "oggEncoder.h" -#include "controlServer.h" +c#include "controlServer.h" bool g_terminated = false; @@ -18,6 +18,7 @@ namespace po = boost::program_options; using namespace std; +std::condition_variable terminateSignaled; int main(int argc, char* argv[]) { @@ -76,6 +77,11 @@ int main(int argc, char* argv[]) encoder.reset(new OggEncoder(sampleFormat)); else if (codec == "pcm") encoder.reset(new PcmEncoder(sampleFormat)); + else if (codec == "flac") + { + cout << "Not yet supported\n"; + return 1; + } else { cout << "unknown codec: " << codec << "\n";