- Added better error handling for in-game errors

This commit is contained in:
Mark Vejvoda
2010-05-29 06:56:32 +00:00
parent 7753435b3a
commit 8b6af02ece
5 changed files with 69 additions and 23 deletions

View File

@@ -32,6 +32,8 @@ using namespace Shared::Platform;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
Game *thisGamePtr = NULL;
// ===================================================== // =====================================================
// class Game // class Game
// ===================================================== // =====================================================
@@ -41,6 +43,9 @@ namespace Glest{ namespace Game{
Game::Game(Program *program, const GameSettings *gameSettings): Game::Game(Program *program, const GameSettings *gameSettings):
ProgramState(program), lastMousePos(0) ProgramState(program), lastMousePos(0)
{ {
originalDisplayMsgCallback = NULL;
thisGamePtr = this;
this->gameSettings= *gameSettings; this->gameSettings= *gameSettings;
scrollSpeed = Config::getInstance().getFloat("UiScrollSpeed","1.5"); scrollSpeed = Config::getInstance().getFloat("UiScrollSpeed","1.5");
@@ -80,12 +85,30 @@ Game::~Game(){
gui.end(); //selection must be cleared before deleting units gui.end(); //selection must be cleared before deleting units
world.end(); //must die before selection because of referencers world.end(); //must die before selection because of referencers
thisGamePtr = NULL;
if(originalDisplayMsgCallback != NULL) {
NetworkInterface::setDisplayMessageFunction(originalDisplayMsgCallback);
}
} }
// ==================== init and load ==================== // ==================== init and load ====================
int Game::ErrorDisplayMessage(const char *msg, bool exitApp) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,msg);
if(thisGamePtr != NULL) {
string text = msg;
thisGamePtr->showMessageBox(text, "Error detected", false);
}
return 0;
}
void Game::load(){ void Game::load(){
originalDisplayMsgCallback = NetworkInterface::getDisplayMessageFunction();
NetworkInterface::setDisplayMessageFunction(ErrorDisplayMessage);
Logger &logger= Logger::getInstance(); Logger &logger= Logger::getInstance();
string mapName= gameSettings.getMap(); string mapName= gameSettings.getMap();
@@ -410,7 +433,8 @@ void Game::update(){
chatManager.updateNetwork(); chatManager.updateNetwork();
//check for quiting status //check for quiting status
if(NetworkManager::getInstance().getGameNetworkInterface()->getQuit()){ if(NetworkManager::getInstance().getGameNetworkInterface()->getQuit() && mainMessageBox.getEnabled() == false) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
quitGame(); quitGame();
} }
@@ -419,6 +443,8 @@ void Game::update(){
AutoTest::getInstance().updateGame(this); AutoTest::getInstance().updateGame(this);
} }
//throw runtime_error("Test!");
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
} }
@@ -539,15 +565,14 @@ void Game::mouseDownLeft(int x, int y){
//exit message box, has to be the last thing to do in this function //exit message box, has to be the last thing to do in this function
if(mainMessageBox.getEnabled()){ if(mainMessageBox.getEnabled()){
int button= 1; int button= 1;
if(mainMessageBox.mouseClick(x, y, button)) if(mainMessageBox.mouseClick(x, y, button)) {
{ if(button==1) {
if(button==1) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
{
networkManager.getGameNetworkInterface()->quitGame(true); networkManager.getGameNetworkInterface()->quitGame(true);
quitGame(); quitGame();
} }
else else {
{ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//close message box //close message box
mainMessageBox.setEnabled(false); mainMessageBox.setEnabled(false);
} }
@@ -849,6 +874,7 @@ void Game::keyPress(char c){
} }
void Game::quitGame(){ void Game::quitGame(){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
program->setState(new BattleEnd(program, world.getStats())); program->setState(new BattleEnd(program, world.getStats()));
} }

View File

@@ -23,6 +23,7 @@
#include "script_manager.h" #include "script_manager.h"
#include "game_settings.h" #include "game_settings.h"
#include "simple_threads.h" #include "simple_threads.h"
#include "network_interface.h"
using std::vector; using std::vector;
using namespace Shared::PlatformCommon; using namespace Shared::PlatformCommon;
@@ -81,6 +82,7 @@ private:
GameSettings gameSettings; GameSettings gameSettings;
Vec2i lastMousePos; Vec2i lastMousePos;
time_t lastRenderLog2d; time_t lastRenderLog2d;
DisplayMessageFunction originalDisplayMsgCallback;
public: public:
Game(Program *program, const GameSettings *gameSettings); Game(Program *program, const GameSettings *gameSettings);
@@ -140,6 +142,7 @@ private:
void showWinMessageBox(); void showWinMessageBox();
void showMessageBox(const string &text, const string &header, bool toggle); void showMessageBox(const string &text, const string &header, bool toggle);
void renderWorker(); void renderWorker();
static int ErrorDisplayMessage(const char *msg, bool exitApp);
}; };
}}//end namespace }}//end namespace

View File

@@ -671,6 +671,15 @@ string ClientInterface::getNetworkStatus() const{
void ClientInterface::waitForMessage() void ClientInterface::waitForMessage()
{ {
// Debug!
/*
sendTextMessage("Timeout waiting for message",-1);
DisplayErrorMessage("Timeout waiting for message");
quit= true;
close();
return;
*/
Chrono chrono; Chrono chrono;
chrono.start(); chrono.start();
@@ -686,6 +695,7 @@ void ClientInterface::waitForMessage()
} }
if(chrono.getMillis() > messageWaitTimeout) { if(chrono.getMillis() > messageWaitTimeout) {
//if(1) {
//throw runtime_error("Timeout waiting for message"); //throw runtime_error("Timeout waiting for message");
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

View File

@@ -69,6 +69,8 @@ public:
virtual void close()= 0; virtual void close()= 0;
static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; } static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; }
static DisplayMessageFunction getDisplayMessageFunction() { return pCB_DisplayMessage; }
string getIp() const {return getSocket()->getIp();} string getIp() const {return getSocket()->getIp();}
string getHostName() const {return getSocket()->getHostName();} string getHostName() const {return getSocket()->getHostName();}

View File

@@ -691,28 +691,33 @@ void ServerInterface::broadcastMessage(const NetworkMessage* networkMessage, int
//serverSynchAccessor.p(); //serverSynchAccessor.p();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
for(int i= 0; i<GameConstants::maxPlayers; ++i) { try {
ConnectionSlot* connectionSlot= slots[i]; for(int i= 0; i<GameConstants::maxPlayers; ++i) {
ConnectionSlot* connectionSlot= slots[i];
if(i != excludeSlot && connectionSlot != NULL) { if(i != excludeSlot && connectionSlot != NULL) {
if(connectionSlot->isConnected()) { if(connectionSlot->isConnected()) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] before sendMessage\n",__FILE__,__FUNCTION__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] before sendMessage\n",__FILE__,__FUNCTION__);
connectionSlot->sendMessage(networkMessage); connectionSlot->sendMessage(networkMessage);
}
else if(gameHasBeenInitiated == true) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 before removeSlot for slot# %d\n",__FILE__,__FUNCTION__,i);
removeSlot(i);
}
} }
else if(gameHasBeenInitiated == true) { else if(i == excludeSlot && gameHasBeenInitiated == true &&
connectionSlot != NULL && connectionSlot->isConnected() == false) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #1 before removeSlot for slot# %d\n",__FILE__,__FUNCTION__,i); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 before removeSlot for slot# %d\n",__FILE__,__FUNCTION__,i);
removeSlot(i); removeSlot(i);
} }
} }
else if(i == excludeSlot && gameHasBeenInitiated == true && }
connectionSlot != NULL && connectionSlot->isConnected() == false) { catch(const exception &ex) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 before removeSlot for slot# %d\n",__FILE__,__FUNCTION__,i); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what());
removeSlot(i); throw runtime_error(ex.what());
} }
}
//serverSynchAccessor.v(); //serverSynchAccessor.v();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);