diff --git a/server/controlServer.cpp b/server/controlServer.cpp index f977e408..4264887d 100644 --- a/server/controlServer.cpp +++ b/server/controlServer.cpp @@ -34,12 +34,13 @@ ControlServer::ControlServer(unsigned short port) : port_(port), headerChunk_(NU void ControlServer::send(shared_ptr message) { - std::unique_lock mlock(mutex); + std::unique_lock mlock(mutex_); for (auto it = sessions_.begin(); it != sessions_.end(); ) { if (!(*it)->active()) { logO << "Session inactive. Removing\n"; + // don't block: remove ServerSession in a thread auto func = [](shared_ptr s)->void{s->stop();}; std::thread t(func, *it); t.detach(); @@ -74,16 +75,19 @@ void ControlServer::onMessageReceived(ServerSession* connection, const msg::Base } else if (requestMsg.request == kServerSettings) { + std::unique_lock mlock(mutex_); serverSettings_->refersTo = requestMsg.id; connection->send(serverSettings_); } else if (requestMsg.request == kSampleFormat) { + std::unique_lock mlock(mutex_); sampleFormat_->refersTo = requestMsg.id; connection->send(sampleFormat_); } else if (requestMsg.request == kHeader) { + std::unique_lock mlock(mutex_); headerChunk_->refersTo = requestMsg.id; connection->send(headerChunk_); } @@ -118,7 +122,7 @@ void ControlServer::acceptor() logS(kLogNotice) << "ControlServer::NewConnection: " << sock->remote_endpoint().address().to_string() << endl; ServerSession* session = new ServerSession(this, sock); { - std::unique_lock mlock(mutex); + std::unique_lock mlock(mutex_); session->start(); sessions_.insert(shared_ptr(session)); } diff --git a/server/publishAvahi.cpp b/server/publishAvahi.cpp index 901057b1..6b689a63 100644 --- a/server/publishAvahi.cpp +++ b/server/publishAvahi.cpp @@ -134,7 +134,7 @@ void PublishAvahi::create_services(AvahiClient *c) { if (!group) { - if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) { + if (!(group = avahi_entry_group_new(c, entry_group_callback, this))) { logE << "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)) << "\n"; goto fail; } diff --git a/server/serverSession.cpp b/server/serverSession.cpp index 726b4a5b..05ac032d 100644 --- a/server/serverSession.cpp +++ b/server/serverSession.cpp @@ -16,10 +16,10 @@ along with this program. If not, see . ***/ -#include "serverSession.h" #include #include #include +#include "serverSession.h" #include "common/log.h" using namespace std; @@ -69,7 +69,7 @@ void ServerSession::stop() } if (writerThread_) { - logD << "joining readerThread\n"; + logD << "joining writerThread\n"; writerThread_->join(); delete writerThread_; } @@ -155,7 +155,7 @@ void ServerSession::reader() } catch (const std::exception& e) { - logS(kLogErr) << "Exception: " << e.what() << ", trying to reconnect" << endl; + logS(kLogErr) << "Exception in ServerSession::reader(): " << e.what() << endl; } active_ = false; } @@ -176,9 +176,9 @@ void ServerSession::writer() send(message.get()); } } - catch (std::exception& e) + catch (const std::exception& e) { - logE << "Exception in thread: " << e.what() << "\n"; + logS(kLogErr) << "Exception in ServerSession::writer(): " << e.what() << endl; } active_ = false; }