From 1282ad4573b72e2e8372c3802d3b9c1caf9e52e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20W=2E=20Urba=C5=84czyk?= Date: Thu, 29 Sep 2011 13:42:07 +0000 Subject: [PATCH] Gracefully ending. --- server/CGameHandler.cpp | 6 ++++++ server/CVCMIServer.cpp | 18 ++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 177711f6b..04b7e4797 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -638,6 +638,12 @@ void CGameHandler::handleConnection(std::set players, CConnection &c) { pack = c.retreivePack(); int packType = typeList.getTypeID(pack); //get the id of type + if(packType == typeList.getTypeID()) + { + tlog0 << "Ending listening thread for side " << *players.begin() << std::endl; + break; + } + CBaseForGHApply *apply = applier->apps[packType]; //and appropriae applier object if(packType != typeList.getTypeID() &&(packType != typeList.getTypeID() || !isAllowedArrangePack((ArrangeStacks*)pack)) // for dialogs like garrison diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index 1de5f02b2..1643d7f8f 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -500,7 +500,9 @@ void CVCMIServer::loadGame() void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI) { + //we need three connections + boost::thread* threads[3] = {0}; CConnection *conns[3] = {0}; for (int i = 0; i < 3 ; i++) { @@ -545,7 +547,7 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI std::set pom; pom.insert(player); - boost::thread(boost::bind(&CGameHandler::handleConnection,gh,pom,boost::ref(*c))); + threads[player] = new boost::thread(boost::bind(&CGameHandler::handleConnection, gh, pom, boost::ref(*c))); } tlog0 << boost::format("Sending start info to connections!\n"); @@ -559,7 +561,13 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI tlog0 << "Battle over!\n"; delNull(gh); tlog0 << "Removed gh!\n"; - boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); + tlog0 << "Waiting for connections to close\n"; + BOOST_FOREACH(boost::thread *t, threads) + { + t->join(); + delNull(t); + } + tlog0 << "Dying...\n"; exit(0); } @@ -581,8 +589,10 @@ int main(int argc, char** argv) { io_service io_service; CVCMIServer server; - assert(argc == 4); - server.startDuel(argv[1], argv[2], argv[3]); + if(argc == 4) + server.startDuel(argv[1], argv[2], argv[3]); + else + server.startDuel("b1.json", "StupidAI", "StupidAI"); while(!end2) {