git-svn-id: svn://elaine/murooma/trunk@283 d8a302eb-03bc-478d-80e4-98257eca68ef
This commit is contained in:
(no author) 2014-09-21 08:23:44 +00:00
parent 001bfa6aba
commit 7467141a14
3 changed files with 47 additions and 2 deletions

View file

@ -20,6 +20,7 @@ void ControlServer::send(shared_ptr<BaseMessage> message)
if (!(*it)->active()) if (!(*it)->active())
{ {
cout << "Session inactive. Removing\n"; cout << "Session inactive. Removing\n";
(*it)->stop();
sessions.erase(it++); sessions.erase(it++);
} }
else else
@ -92,8 +93,8 @@ void ControlServer::acceptor()
ServerSession* session = new ServerSession(this, sock); ServerSession* session = new ServerSession(this, sock);
{ {
std::unique_lock<std::mutex> mlock(mutex); std::unique_lock<std::mutex> mlock(mutex);
sessions.insert(shared_ptr<ServerSession>(session));
session->start(); session->start();
sessions.insert(shared_ptr<ServerSession>(session));
} }
} }
} }

View file

@ -15,6 +15,12 @@ ServerSession::ServerSession(MessageReceiver* _receiver, std::shared_ptr<tcp::so
} }
ServerSession::~ServerSession()
{
stop();
}
void ServerSession::start() void ServerSession::start()
{ {
active_ = true; active_ = true;
@ -24,13 +30,49 @@ void ServerSession::start()
} }
void ServerSession::stop()
{
active_ = false;
try
{
boost::system::error_code ec;
if (socket)
{
socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
if (ec) cout << "Error in socket shutdown: " << ec << "\n";
socket->close(ec);
if (ec) cout << "Error in socket close: " << ec << "\n";
}
if (readerThread)
{
cout << "joining readerThread\n";
readerThread->join();
delete readerThread;
}
if (writerThread)
{
cout << "joining readerThread\n";
writerThread->join();
delete writerThread;
}
}
catch(...)
{
}
readerThread = NULL;
writerThread = NULL;
cout << "ServerSession stopped\n";
}
void ServerSession::socketRead(void* _to, size_t _bytes) void ServerSession::socketRead(void* _to, size_t _bytes)
{ {
size_t read = 0; size_t read = 0;
do do
{ {
boost::system::error_code error; boost::system::error_code error;
read += socket->read_some(boost::asio::buffer((char*)_to + read, _bytes - read), error); read += socket->read_some(boost::asio::buffer((char*)_to + read, _bytes - read));
} }
while (read < _bytes); while (read < _bytes);
} }

View file

@ -29,7 +29,9 @@ class ServerSession
{ {
public: public:
ServerSession(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket); ServerSession(MessageReceiver* _receiver, std::shared_ptr<tcp::socket> _socket);
~ServerSession();
void start(); void start();
void stop();
bool send(BaseMessage* message); bool send(BaseMessage* message);
void add(std::shared_ptr<BaseMessage> message); void add(std::shared_ptr<BaseMessage> message);
virtual bool connected() virtual bool connected()