From 35f6a6665b42d243d98ede32473c8971e7ed0145 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 10 Nov 2012 09:22:28 +0000 Subject: [PATCH] - attempt to keep irc connection and just join / leave the irc channel as required to speed up irc in the lobby --- source/glest_game/game/game_constants.h | 1 + source/glest_game/global/config.cpp | 1 + source/glest_game/main/main.cpp | 15 +++++++ .../menu/menu_state_masterserver.cpp | 24 +++++++--- source/glest_game/network/network_message.cpp | 7 --- .../include/platform/posix/ircclient.h | 4 ++ .../sources/platform/posix/ircclient.cpp | 44 +++++++++++++++++++ 7 files changed, 84 insertions(+), 12 deletions(-) diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index d6d0aa90..c459ff5c 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -153,6 +153,7 @@ public: static const char *RANDOMFACTION_SLOTNAME; static const char *playerTextureCacheLookupKey; + static const char *ircClinetCacheLookupKey; static const char *factionPreviewTextureCacheLookupKey; static const char *characterMenuScreenPositionListCacheLookupKey; static const char *pathCacheLookupKey; diff --git a/source/glest_game/global/config.cpp b/source/glest_game/global/config.cpp index 3274ddf6..31c2a41c 100644 --- a/source/glest_game/global/config.cpp +++ b/source/glest_game/global/config.cpp @@ -50,6 +50,7 @@ const char *GameConstants::folder_path_screenshots = "screens/"; const char *GameConstants::OBSERVER_SLOTNAME = "*Observer*"; const char *GameConstants::RANDOMFACTION_SLOTNAME = "*Random*"; +const char *GameConstants::ircClinetCacheLookupKey = "ircClientCache"; const char *GameConstants::playerTextureCacheLookupKey = "playerTextureCache"; const char *GameConstants::factionPreviewTextureCacheLookupKey = "factionPreviewTextureCache"; const char *GameConstants::characterMenuScreenPositionListCacheLookupKey = "characterMenuScreenPositionListCache"; diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 75e59a45..cf49d67c 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -161,6 +161,21 @@ static void cleanupProcessObjects() { restoreVideoMode(true); } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 IRCCLient Cache SHUTDOWN\n"); + IRCThread * &ircClient = CacheManager::getCachedItem< IRCThread * >(GameConstants::ircClinetCacheLookupKey); + if(ircClient != NULL) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#2 IRCCLient Cache SHUTDOWN\n"); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + ircClient->setCallbackObj(NULL); + ircClient->signalQuit(); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + ircClient = NULL; + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#3 IRCCLient Cache SHUTDOWN\n"); + } + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#4 IRCCLient Cache SHUTDOWN\n"); + cleanupCRCThread(); //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 4f72b9de..2dd35868 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -26,7 +26,7 @@ #include "socket.h" #include "masterserver_info.h" #include - +#include "cache_manager.h" #include "leak_dumper.h" namespace Glest{ namespace Game{ @@ -305,9 +305,22 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen } MutexSafeWrapper safeMutexIRCPtr(&mutexIRCClient,string(extractFileFromDirectoryPath(__FILE__).c_str()) + "_" + intToStr(__LINE__)); - ircClient = new IRCThread(ircArgs,this); - ircClient->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); - ircClient->start(); + + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 IRCCLient Cache check\n"); + IRCThread * &ircThread = CacheManager::getCachedItem< IRCThread * >(GameConstants::ircClinetCacheLookupKey); + if(ircThread == NULL) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#2 IRCCLient Cache check\n"); + ircThread = new IRCThread(ircArgs,this); + ircClient = ircThread; + ircClient->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + ircClient->start(); + } + else { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#3 IRCCLient Cache check\n"); + ircClient = ircThread; + ircClient->setCallbackObj(this); + ircClient->joinChannel(); + } if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } @@ -433,8 +446,9 @@ void MenuStateMasterserver::cleanup() { if(ircClient != NULL) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + ircClient->leaveChannel(); ircClient->setCallbackObj(NULL); - ircClient->signalQuit(); + //ircClient->signalQuit(); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); ircClient = NULL; } diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index b18c9039..e2d40f0c 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -284,7 +284,6 @@ unsigned int NetworkMessagePing::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.pingTime = 0; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, @@ -383,7 +382,6 @@ unsigned int NetworkMessageReady::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtReady; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, @@ -1573,7 +1571,6 @@ unsigned int NetworkMessageQuit::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtQuit; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType); @@ -2530,7 +2527,6 @@ unsigned int PlayerIndexMessage::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtPlayerIndexMessage; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, @@ -2619,7 +2615,6 @@ unsigned int NetworkMessageLoadingStatus::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtLoadingStatusMessage; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, @@ -2838,7 +2833,6 @@ unsigned int NetworkMessageUnMarkCell::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtUnMarkCell; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, @@ -2942,7 +2936,6 @@ unsigned int NetworkMessageHighlightCell::getPackedSize() { static unsigned int result = 0; if(result == 0) { Data packedData; - packedData.messageType = nmtHighlightCell; unsigned char *buf = new unsigned char[sizeof(packedData)*3]; result = pack(buf, getPackedMessageFormat(), packedData.messageType, diff --git a/source/shared_lib/include/platform/posix/ircclient.h b/source/shared_lib/include/platform/posix/ircclient.h index 56c6d062..29c96f95 100644 --- a/source/shared_lib/include/platform/posix/ircclient.h +++ b/source/shared_lib/include/platform/posix/ircclient.h @@ -111,6 +111,10 @@ public: Mutex * getMutexIRCCB() { return &mutexIRCCB; } IRCCallbackInterface * getCallbackObj(bool lockObj=true); void setCallbackObj(IRCCallbackInterface *cb); + + void joinChannel(); + void leaveChannel(); + void connectToHost(); }; }}//end namespace diff --git a/source/shared_lib/sources/platform/posix/ircclient.cpp b/source/shared_lib/sources/platform/posix/ircclient.cpp index c6eb7f85..cc4747f5 100644 --- a/source/shared_lib/sources/platform/posix/ircclient.cpp +++ b/source/shared_lib/sources/platform/posix/ircclient.cpp @@ -706,4 +706,48 @@ void normalizeNick(char *nick) { } } +void IRCThread::connectToHost() { + bool connectRequired = false; + if(ircSession == NULL) { + connectRequired = true; + } + else { + int result = irc_is_connected(ircSession); + if(result != 1) { + connectRequired = true; + } + } + + if(connectRequired == false) { + if(irc_connect(ircSession, argv[0].c_str(), IRC_SERVER_PORT, 0, this->nick.c_str(), this->username.c_str(), "megaglest")) { + if(SystemFlags::VERBOSE_MODE_ENABLED || IRCThread::debugEnabled) printf ("===> IRC Could not connect: %s\n", irc_strerror (irc_errno(ircSession))); + return; + } + } +} + +void IRCThread::joinChannel() { + connectToHost(); + if(ircSession != NULL) { + IRCThread *ctx = (IRCThread *)irc_get_ctx(ircSession); + if(ctx != NULL) { + eventData.clear(); + eventDataDone = false; + hasJoinedChannel = false; + lastNickListUpdate = time(NULL); + + irc_cmd_join(ircSession, ctx->getChannel().c_str(), 0); + } + } +} + +void IRCThread::leaveChannel() { + if(ircSession != NULL) { + IRCThread *ctx = (IRCThread *)irc_get_ctx(ircSession); + if(ctx != NULL) { + irc_cmd_part(ircSession,ctx->getChannel().c_str()); + } + } +} + }}//end namespace