From a5bf684493e0618276cbda2b0bfc071eb51f4a46 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 7 Aug 2010 04:49:06 +0000 Subject: [PATCH] - more network fixes to make lag and disconnects more stable - changed the way the game end logic happens to try to make it more stable --- source/glest_game/facilities/auto_test.cpp | 6 ++-- source/glest_game/game/game.cpp | 28 +++++++++++++------ source/glest_game/game/game.h | 5 ++-- source/glest_game/main/program.cpp | 9 +++++- source/glest_game/main/program.h | 4 ++- source/glest_game/network/network_message.cpp | 4 ++- .../sources/platform/posix/socket.cpp | 14 ++++++++-- 7 files changed, 53 insertions(+), 17 deletions(-) diff --git a/source/glest_game/facilities/auto_test.cpp b/source/glest_game/facilities/auto_test.cpp index 689aa2b7c..606ed21a9 100644 --- a/source/glest_game/facilities/auto_test.cpp +++ b/source/glest_game/facilities/auto_test.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2009 Martiņo Figueroa +// Copyright (C) 2001-2009 Martio Figueroa // // You can redistribute this code and/or modify it under // the terms of the GNU General Public License as published @@ -73,7 +73,9 @@ void AutoTest::updateGame(Game *game){ // quit if we've espend enough time in the game if(time(NULL)-gameStartTime>gameTime){ - game->quitGame(); + Program *program = game->getProgram(); + Stats endStats = game->quitGame(); + Game::exitGameState(program, endStats); } } diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index dfe73ddbf..a34640cf8 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -118,8 +118,11 @@ bool Game::quitTriggered() { return quitTriggeredIndicator; } -void Game::quitAndToggleState() { - quitGame(); +Stats Game::quitAndToggleState() { + //quitGame(); + //Program *program = game->getProgram(); + return quitGame(); + //Game::exitGameState(program, endStats); } // ==================== init and load ==================== @@ -1123,15 +1126,15 @@ void Game::keyPress(char c){ chatManager.keyPress(c); } -void Game::quitGame(){ +Stats Game::quitGame() { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //Stats stats = *(world.getStats()); - Stats *endStats = new Stats(); + Stats endStats; SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - *endStats = *(world.getStats()); + endStats = *(world.getStats()); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1140,16 +1143,25 @@ void Game::quitGame(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - ProgramState *newState = new BattleEnd(program, endStats); + //ProgramState *newState = new BattleEnd(program, endStats); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + //program->setState(newState); + + return endStats; +} + +void Game::exitGameState(Program *program, Stats &endStats) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - delete endStats; - endStats = NULL; + ProgramState *newState = new BattleEnd(program, &endStats); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); program->setState(newState); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } // ==================== PRIVATE ==================== diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 53f3f532b..628ad0962 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -128,8 +128,9 @@ public: virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); } virtual bool quitTriggered(); - virtual void quitAndToggleState(); - void quitGame(); + virtual Stats quitAndToggleState(); + Stats quitGame(); + static void exitGameState(Program *program, Stats &endStats); private: //render diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index ae138ca60..e9194409d 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -233,8 +233,15 @@ void Program::loopWorker() { if(this->programState->quitTriggered() == true) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - this->programState->quitAndToggleState(); + + Stats endStats = this->programState->quitAndToggleState(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + Game::exitGameState(this, endStats); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + return; } ProgramState *prevState = this->programState; diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index 8293fae97..fe59dfbee 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -19,6 +19,7 @@ #include "components.h" #include "window.h" #include "simple_threads.h" +#include "stats.h" using Shared::Platform::MouseButton; using Shared::Graphics::Context; @@ -77,7 +78,8 @@ public: virtual void restoreToStartXY() { SDL_WarpMouse(startX, startY); } virtual bool isInSpecialKeyCaptureEvent() { return false; } virtual bool quitTriggered() { return false; } - virtual void quitAndToggleState() {}; + virtual Stats quitAndToggleState() { return Stats(); }; + virtual Program * getProgram() { return program; } }; // =============================== diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 07a3abc18..e33d2f8b1 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -101,7 +101,9 @@ void NetworkMessage::send(Socket* socket, const void* data, int dataSize) const SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,socket,data,dataSize); if(socket != NULL) { + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,socket,data,dataSize); int sendResult = socket->send(data, dataSize); + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",__FILE__,__FUNCTION__,__LINE__,socket,data,dataSize); if(sendResult != dataSize) { if(socket != NULL && socket->getSocketId() > 0) { char szBuf[1024]=""; @@ -340,7 +342,7 @@ bool NetworkMessageCommandList::receive(Socket* socket) { } void NetworkMessageCommandList::send(Socket* socket) const{ - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtCommandList, frameCount = %d\n",__FILE__,__FUNCTION__,__LINE__,data.header.frameCount); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtCommandList, frameCount = %d, data.header.commandCount = %d, data.header.messageType = %d\n",__FILE__,__FUNCTION__,__LINE__,data.header.frameCount,data.header.commandCount,data.header.messageType); assert(data.header.messageType==nmtCommandList); int totalMsgSize = commandListHeaderSize + (sizeof(NetworkCommand) * data.header.commandCount); diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 8f05249ba..227d89c44 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -972,18 +972,25 @@ int Socket::getDataToRead(bool wantImmediateReply) { } int Socket::send(const void *data, int dataSize) { + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); ssize_t bytesSent= 0; if(isSocketValid() == true) { + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); errno = 0; MutexSafeWrapper safeMutex(&dataSynchAccessor); - bytesSent = ::send(sock, reinterpret_cast(data), dataSize, 0); + + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + bytesSent = ::send(sock, data, dataSize, MSG_NOSIGNAL); + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } // TEST errors //bytesSent = -1; // END TEST + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(bytesSent < 0 && getLastSocketError() != PLATFORM_SOCKET_TRY_AGAIN) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR WRITING SOCKET DATA, err = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText().c_str()); //throwException(szBuf); @@ -1001,13 +1008,16 @@ int Socket::send(const void *data, int dataSize) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d, sock = %d, dataSize = %d, data = %p\n",__FILE__,__FUNCTION__,__LINE__,attemptCount,sock,dataSize,data); MutexSafeWrapper safeMutex(&dataSynchAccessor); - bytesSent = ::send(sock, reinterpret_cast(data), dataSize, 0); + bytesSent = ::send(sock, data, dataSize, MSG_NOSIGNAL); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] #2 EAGAIN during send, trying again returned: %d\n",__FILE__,__FUNCTION__,bytesSent); } SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] attemptCount = %d\n",__FILE__,__FUNCTION__,__LINE__,attemptCount); } } + + //SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if(bytesSent <= 0) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR WRITING SOCKET DATA, err = %d error = %s\n",__FILE__,__FUNCTION__,__LINE__,bytesSent,getLastSocketErrorFormattedText().c_str());