From 310fadefdb001e79ac690fe990aab4848ed72f7f Mon Sep 17 00:00:00 2001 From: lotodore Date: Mon, 8 Oct 2007 20:14:44 +0000 Subject: [PATCH] Keep lobby open in the background during internet games. Show the lobby after the game finished. Re-open the game. Start and stop irc thread together with the client thread. --- .../gamelobbydialog/gamelobbydialogimpl.cpp | 5 -- src/gui/qt/guiwrapper.cpp | 2 +- src/gui/qt/mainwindow/mainwindowimpl.cpp | 37 ++++++---- src/gui/qt/mainwindow/mainwindowimpl.h | 4 +- src/net/common/servergamestate.cpp | 1 + src/net/common/serverlobbythread.cpp | 8 +++ src/net/serverlobbythread.h | 1 + src/session.cpp | 71 +++++++++---------- src/session.h | 7 +- 9 files changed, 77 insertions(+), 59 deletions(-) diff --git a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp index dd8c7d5a..5ad3cf72 100644 --- a/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp +++ b/src/gui/qt/gamelobbydialog/gamelobbydialogimpl.cpp @@ -49,10 +49,6 @@ void gameLobbyDialogImpl::exec() { hideShowGameDescription(FALSE); - assert(mySession); - mySession->terminateIrcClient(); - mySession->startIrcClient(); - if(myConfig->readConfigInt("UseIRCLobbyChat")) { groupBox_lobbyChat->show(); } @@ -61,7 +57,6 @@ void gameLobbyDialogImpl::exec() } QDialog::exec(); - mySession->terminateIrcClient(); } diff --git a/src/gui/qt/guiwrapper.cpp b/src/gui/qt/guiwrapper.cpp index d4a6c467..6521ec09 100644 --- a/src/gui/qt/guiwrapper.cpp +++ b/src/gui/qt/guiwrapper.cpp @@ -140,7 +140,7 @@ void GuiWrapper::SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned pla void GuiWrapper::SignalNetClientGameStart(boost::shared_ptr game) { myW->signalNetClientGameStart(game); } void GuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { myChat->signalChatMessage(QString::fromUtf8(playerName.c_str()), QString::fromUtf8(msg.c_str())); } -void GuiWrapper::SignalNetClientWaitDialog() { myW->signalShowNetworkStartDialog(); } +void GuiWrapper::SignalNetClientWaitDialog() { myW->signalShowClientDialog(); } void GuiWrapper::SignalNetServerSuccess(int actionID) { } void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { myW->signalNetServerError(errorID, osErrorID); } diff --git a/src/gui/qt/mainwindow/mainwindowimpl.cpp b/src/gui/qt/mainwindow/mainwindowimpl.cpp index fcbff561..210ff666 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.cpp +++ b/src/gui/qt/mainwindow/mainwindowimpl.cpp @@ -605,7 +605,7 @@ mainWindowImpl::mainWindowImpl(ConfigFile *c, QMainWindow *parent) //Nachrichten Thread-Save connect(this, SIGNAL(signalInitGui(int)), this, SLOT(initGui(int))); - connect(this, SIGNAL(signalShowNetworkStartDialog()), this, SLOT(showNetworkStartDialog())); + connect(this, SIGNAL(signalShowClientDialog()), this, SLOT(showClientDialog())); connect(this, SIGNAL(signalRefreshSet()), this, SLOT(refreshSet())); connect(this, SIGNAL(signalRefreshCash()), this, SLOT(refreshCash())); @@ -984,17 +984,7 @@ void mainWindowImpl::joinGameLobby() { } else { - myGameLobbyDialog->exec(); - - if (myGameLobbyDialog->result() == QDialog::Accepted) - { - //some gui modifications - networkGameModification(); - } - else - { - mySession->terminateNetworkClient(); - } + showLobbyDialog(); } } @@ -1149,6 +1139,14 @@ void mainWindowImpl::initGui(int speed) } } +void mainWindowImpl::showClientDialog() +{ + if (mySession->GetGameType() == Session::GAME_TYPE_NETWORK) + showNetworkStartDialog(); + else if (mySession->GetGameType() == Session::GAME_TYPE_INTERNET) + showLobbyDialog(); +} + void mainWindowImpl::showNetworkStartDialog() { myStartNetworkGameDialog->exec(); @@ -1165,6 +1163,21 @@ void mainWindowImpl::showNetworkStartDialog() } } +void mainWindowImpl::showLobbyDialog() +{ + myGameLobbyDialog->exec(); + + if (myGameLobbyDialog->result() == QDialog::Accepted) + { + //some gui modifications + networkGameModification(); + } + else + { + mySession->terminateNetworkClient(); + } +} + Session &mainWindowImpl::getSession() { assert(mySession.get()); return *mySession; } void mainWindowImpl::setSession(boost::shared_ptr session) { mySession = session; } diff --git a/src/gui/qt/mainwindow/mainwindowimpl.h b/src/gui/qt/mainwindow/mainwindowimpl.h index d00a54db..4b8e97dd 100755 --- a/src/gui/qt/mainwindow/mainwindowimpl.h +++ b/src/gui/qt/mainwindow/mainwindowimpl.h @@ -76,7 +76,7 @@ public: signals: void signalInitGui(int speed); - void signalShowNetworkStartDialog(); + void signalShowClientDialog(); void signalRefreshSet(); void signalRefreshCash(); @@ -154,7 +154,9 @@ public slots: void initGui(int speed); + void showClientDialog(); void showNetworkStartDialog(); + void showLobbyDialog(); //refresh-Funktionen void refreshSet(); diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index 7628f9b0..c605aa32 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -1126,6 +1126,7 @@ ServerGameStateNextGameDelay::Process(ServerGameThread &server) // Wait for the start of a new game. server.SetState(ServerGameStateInit::Instance()); + server.GetLobbyThread().NotifyReopeningGame(server.GetId()); } return retVal; diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 6ea37800..663e0423 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -162,6 +162,14 @@ ServerLobbyThread::NotifyStartingGame(unsigned gameId) m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game); } +void +ServerLobbyThread::NotifyReopeningGame(unsigned gameId) +{ + boost::shared_ptr packet = CreateNetPacketGameListUpdate(gameId, GAME_MODE_CREATED); + m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established); + m_gameSessionManager.SendToAllSessions(GetSender(), packet, SessionData::Game); +} + void ServerLobbyThread::HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket) { diff --git a/src/net/serverlobbythread.h b/src/net/serverlobbythread.h index d753bb43..6ea50dda 100644 --- a/src/net/serverlobbythread.h +++ b/src/net/serverlobbythread.h @@ -59,6 +59,7 @@ public: void NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId); void NotifyPlayerLeftGame(unsigned gameId, unsigned playerId); void NotifyStartingGame(unsigned gameId); + void NotifyReopeningGame(unsigned gameId); void HandleGameRetrievePlayerInfo(SessionWrapper session, const NetPacketRetrievePlayerInfo &tmpPacket); void HandleGameRetrieveAvatar(SessionWrapper session, const NetPacketRetrieveAvatar &tmpPacket); diff --git a/src/session.cpp b/src/session.cpp index 040138fc..ad0b304c 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -39,7 +39,8 @@ using namespace std; Session::Session(GuiInterface *g, ConfigFile *c) -: currentGameID(0), myNetClient(NULL), myNetServer(NULL), myIrcThread(NULL), myGui(g), myConfig(c) +: currentGameID(0), myNetClient(NULL), myNetServer(NULL), myIrcThread(NULL), + myGui(g), myConfig(c), myGameType(GAME_TYPE_NONE) { myAvatarManager.reset(new AvatarManager); } @@ -49,7 +50,6 @@ Session::~Session() { terminateNetworkClient(); terminateNetworkServer(); - terminateIrcClient(); delete myConfig; myConfig = 0; } @@ -61,6 +61,8 @@ bool Session::init() void Session::startLocalGame(const GameData &gameData, const StartData &startData) { + myGameType = GAME_TYPE_LOCAL; + currentGame.reset(); currentGameID++; @@ -96,7 +98,6 @@ void Session::startLocalGame(const GameData &gameData, const StartData &startDat currentGame->initHand(); currentGame->startHand(); // SPIEL-SCHLEIFE - } void Session::startClientGame(boost::shared_ptr game) @@ -116,13 +117,29 @@ GuiInterface *Session::getGui() return myGui; } +Session::GameType Session::GetGameType() +{ + return myGameType; +} + void Session::startInternetClient() { - if (myNetClient || !myGui) + if (myNetClient || myIrcThread || !myGui) { assert(false); return; } + myGameType = GAME_TYPE_INTERNET; + + myIrcThread = new IrcThread(*myGui); + myIrcThread->Init( + myConfig->readConfigString("IRCServerAddress"), + myConfig->readConfigInt("IRCServerPort"), + myConfig->readConfigInt("IRCServerUseIpv6") == 1, + myConfig->readConfigString("MyName"), + myConfig->readConfigString("IRCChannel")); + myIrcThread->Run(); + myNetClient = new ClientThread(*myGui, *myAvatarManager); myNetClient->Init( myConfig->readConfigString("InternetServerAddress"), @@ -142,6 +159,8 @@ void Session::startNetworkClient(const string &serverAddress, unsigned serverPor assert(false); return; } + myGameType = GAME_TYPE_NETWORK; + myNetClient = new ClientThread(*myGui, *myAvatarManager); myNetClient->Init( serverAddress, @@ -162,6 +181,8 @@ void Session::startNetworkClientForLocalServer(const GameData &gameData) assert(false); return; } + myGameType = GAME_TYPE_NETWORK; + myNetClient = new ClientThread(*myGui, *myAvatarManager); bool useIpv6 = myConfig->readConfigInt("ServerUseIpv6") == 1; const char *loopbackAddr = useIpv6 ? "::1" : "127.0.0.1"; @@ -182,13 +203,18 @@ void Session::terminateNetworkClient() if (!myNetClient) return; // already terminated myNetClient->SignalTermination(); - // Give the thread some time to terminate. + if (myIrcThread) + myIrcThread->SignalTermination(); + // Give the threads some time to terminate. if (myNetClient->Join(NET_CLIENT_TERMINATE_TIMEOUT_MSEC)) - { delete myNetClient; - } + if (myIrcThread && myIrcThread->Join(NET_IRC_TERMINATE_TIMEOUT_MSEC)) + delete myIrcThread; + // If termination fails, leave a memory leak to prevent a crash. myNetClient = 0; + myIrcThread = 0; + myGameType = GAME_TYPE_NONE; } void Session::clientCreateGame(const GameData &gameData, const string &name, const string &password) @@ -252,23 +278,6 @@ bool Session::waitForNetworkServer(unsigned timeoutMsec) return retVal; } -void Session::startIrcClient() -{ - if (myIrcThread || !myGui) - { - assert(false); - return; - } - myIrcThread = new IrcThread(*myGui); - myIrcThread->Init( - myConfig->readConfigString("IRCServerAddress"), - myConfig->readConfigInt("IRCServerPort"), - myConfig->readConfigInt("IRCServerUseIpv6") == 1, - myConfig->readConfigString("MyName"), - myConfig->readConfigString("IRCChannel")); - myIrcThread->Run(); -} - void Session::sendIrcChatMessage(const std::string &message) { if (!myIrcThread) @@ -276,20 +285,6 @@ void Session::sendIrcChatMessage(const std::string &message) myIrcThread->SendChatMessage(message); } -void Session::terminateIrcClient() -{ - if (!myIrcThread) - return; // already terminated - myIrcThread->SignalTermination(); - // Give the thread some time to terminate. - if (myIrcThread->Join(NET_IRC_TERMINATE_TIMEOUT_MSEC)) - delete myIrcThread; - else - assert(false); - // If termination fails, leave a memory leak to prevent a crash. - myIrcThread = 0; -} - void Session::sendLeaveCurrentGame() { if (!myNetClient) diff --git a/src/session.h b/src/session.h index 1b6f91fb..27b79eeb 100755 --- a/src/session.h +++ b/src/session.h @@ -40,6 +40,8 @@ public: ~Session(); + enum GameType { GAME_TYPE_NONE, GAME_TYPE_LOCAL, GAME_TYPE_NETWORK, GAME_TYPE_INTERNET }; + bool init(); void startLocalGame(const GameData &gameData, const StartData &startData); @@ -49,6 +51,8 @@ public: GuiInterface *getGui(); + GameType GetGameType(); + void startInternetClient(); void startNetworkClient(const std::string &serverAddress, unsigned serverPort, bool ipv6, bool sctp, const std::string &pwd); void startNetworkClientForLocalServer(const GameData &gameData); @@ -62,9 +66,7 @@ public: void terminateNetworkServer(); bool waitForNetworkServer(unsigned timeoutMsec); - void startIrcClient(); void sendIrcChatMessage(const std::string &message); - void terminateIrcClient(); void sendClientPlayerAction(); @@ -92,6 +94,7 @@ private: boost::shared_ptr currentGame; GuiInterface *myGui; ConfigFile *myConfig; + GameType myGameType; };