fixed session shutdown

This commit is contained in:
badaix 2015-08-06 23:51:13 +02:00
parent 82c0070233
commit e35e724110
2 changed files with 7 additions and 6 deletions

View file

@ -38,7 +38,7 @@ void ControlServer::send(const msg::BaseMessage* message)
{ {
if (!(*it)->active()) if (!(*it)->active())
{ {
logO << "Session inactive. Removing\n"; logS(kLogErr) << "Session inactive. Removing\n";
// don't block: remove ServerSession in a thread // don't block: remove ServerSession in a thread
auto func = [](shared_ptr<ServerSession> s)->void{s->stop();}; auto func = [](shared_ptr<ServerSession> s)->void{s->stop();};
std::thread t(func, *it); std::thread t(func, *it);

View file

@ -62,13 +62,13 @@ void ServerSession::stop()
} }
if (readerThread_) if (readerThread_)
{ {
logD << "joining readerThread\n"; logO << "joining readerThread\n";
readerThread_->join(); readerThread_->join();
delete readerThread_; delete readerThread_;
} }
if (writerThread_) if (writerThread_)
{ {
logD << "joining writerThread\n"; logO << "joining writerThread\n";
writerThread_->join(); writerThread_->join();
delete writerThread_; delete writerThread_;
} }
@ -78,7 +78,8 @@ void ServerSession::stop()
} }
readerThread_ = NULL; readerThread_ = NULL;
writerThread_ = NULL; writerThread_ = NULL;
logD << "ServerSession stopped\n"; socket_ = NULL;
logO << "ServerSession stopped\n";
} }
@ -90,7 +91,7 @@ void ServerSession::socketRead(void* _to, size_t _bytes)
boost::system::error_code error; boost::system::error_code error;
read += socket_->read_some(boost::asio::buffer((char*)_to + read, _bytes - read)); read += socket_->read_some(boost::asio::buffer((char*)_to + read, _bytes - read));
} }
while (read < _bytes); while (active_ && (read < _bytes));
} }
@ -109,7 +110,7 @@ bool ServerSession::send(const msg::BaseMessage* message) const
{ {
// logO << "send: " << message->type << ", size: " << message->size << ", id: " << message->id << ", refers: " << message->refersTo << "\n"; // logO << "send: " << message->type << ", size: " << message->size << ", id: " << message->id << ", refers: " << message->refersTo << "\n";
std::unique_lock<std::mutex> mlock(mutex_); std::unique_lock<std::mutex> mlock(mutex_);
if (!socket_) if (!socket_ || !active_)
return false; return false;
boost::asio::streambuf streambuf; boost::asio::streambuf streambuf;
std::ostream stream(&streambuf); std::ostream stream(&streambuf);