diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 329912f5..500d43a4 100755 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -41,6 +41,7 @@ const int ClientInterface::maxNetworkCommandListSendTimeWait = 4; ClientInterface::ClientInterface(){ clientSocket= NULL; + sessionKey = 0; launchGame= false; introDone= false; playerIndex= -1; @@ -164,11 +165,12 @@ void ClientInterface::updateLobby() { NetworkMessageIntro networkMessageIntro; if(receiveMessage(&networkMessageIntro)) { gotIntro = true; + sessionKey = networkMessageIntro.getSessionId(); versionString = networkMessageIntro.getVersionString(); playerIndex= networkMessageIntro.getPlayerIndex(); serverName= networkMessageIntro.getName(); - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s]\n",__FILE__,__FUNCTION__,__LINE__,networkMessageIntro.getGameState(),versionString.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageIntro.getGameState(),versionString.c_str(),sessionKey); //check consistency if(networkMessageIntro.getVersionString() != getNetworkVersionString()) { @@ -218,7 +220,7 @@ void ClientInterface::updateLobby() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //send intro message - NetworkMessageIntro sendNetworkMessageIntro(getNetworkVersionString(), getHumanPlayerName(), -1, nmgstOk); + NetworkMessageIntro sendNetworkMessageIntro(sessionKey,getNetworkVersionString(), getHumanPlayerName(), -1, nmgstOk); sendMessage(&sendNetworkMessageIntro); SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index c7e6242a..3e82af34 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -54,6 +54,7 @@ private: time_t clientSimulationLagStartTime; string versionString; + int sessionKey; public: ClientInterface(); diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index ad7c27da..d0e60f9e 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -150,6 +150,7 @@ ConnectionSlot::ConnectionSlot(ServerInterface* serverInterface, int playerIndex SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + this->sessionKey = 0; this->serverInterface = serverInterface; this->playerIndex = playerIndex; this->currentFrameCount = 0; @@ -239,13 +240,15 @@ void ConnectionSlot::update(bool checkForNewClients) { //send intro message when connected if(socket != NULL) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getOpenSlotCount()); + RandomGen random; + sessionKey = random.randRange(-100000, 100000); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getOpenSlotCount(),sessionKey); if(hasOpenSlots == false) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] !!!!!!!!WARNING - no open slots, disconnecting client\n",__FILE__,__FUNCTION__,__LINE__); if(socket != NULL) { - NetworkMessageIntro networkMessageIntro(getNetworkVersionString(), getHostName(), playerIndex, nmgstNoSlots); + NetworkMessageIntro networkMessageIntro(sessionKey,getNetworkVersionString(), getHostName(), playerIndex, nmgstNoSlots); sendMessage(&networkMessageIntro); } @@ -255,7 +258,7 @@ void ConnectionSlot::update(bool checkForNewClients) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] client will be assigned to the next open slot\n",__FILE__,__FUNCTION__,__LINE__); if(socket != NULL) { - NetworkMessageIntro networkMessageIntro(getNetworkVersionString(), getHostName(), playerIndex, nmgstOk); + NetworkMessageIntro networkMessageIntro(sessionKey,getNetworkVersionString(), getHostName(), playerIndex, nmgstOk); sendMessage(&networkMessageIntro); } } @@ -347,10 +350,21 @@ void ConnectionSlot::update(bool checkForNewClients) { NetworkMessageIntro networkMessageIntro; if(receiveMessage(&networkMessageIntro)) { + int msgSessionId = networkMessageIntro.getSessionId(); name= networkMessageIntro.getName(); versionString = networkMessageIntro.getVersionString(); - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got name [%s] versionString [%s]\n",__FILE__,__FUNCTION__,name.c_str(),versionString.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got name [%s] versionString [%s], msgSessionId = %d\n",__FILE__,__FUNCTION__,name.c_str(),versionString.c_str(),msgSessionId); + + if(msgSessionId != sessionKey) { + string playerNameStr = name; + string sErr = "Client gave invalid sessionid for player [" + playerNameStr + "]"; + printf("%s\n",sErr.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); + + close(); + return; + } //check consistency if(networkMessageIntro.getVersionString() != getNetworkVersionString()) { @@ -378,6 +392,7 @@ void ConnectionSlot::update(bool checkForNewClients) { sErr = "Warning, Server and client are using the same version but different platforms.\n\nServer: " + getNetworkVersionString() + "\nClient: " + networkMessageIntro.getVersionString() + " player [" + playerNameStr + "]"; printf("%s\n",sErr.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); //sendTextMessage("Server and client have different platform mismatch.",-1, true); //sendTextMessage(" Server:" + networkMessageIntro.getVersionString(),-1, true); diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index a9093e80..2df56371 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -110,6 +110,7 @@ private: time_t lastReceiveCommandListTime; bool gotLagCountWarning; string versionString; + int sessionKey; public: ConnectionSlot(ServerInterface* serverInterface, int playerIndex); diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index cb93d726..3874abda 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -125,12 +125,16 @@ void NetworkMessage::send(Socket* socket, const void* data, int dataSize) const NetworkMessageIntro::NetworkMessageIntro(){ data.messageType= -1; + data.sessionId= -1; data.playerIndex= -1; data.gameState = nmgstInvalid; } -NetworkMessageIntro::NetworkMessageIntro(const string &versionString, const string &name, int playerIndex, NetworkGameStateType gameState) { +NetworkMessageIntro::NetworkMessageIntro(int32 sessionId,const string &versionString, + const string &name, int playerIndex, + NetworkGameStateType gameState) { data.messageType = nmtIntro; + data.sessionId = sessionId; data.versionString = versionString; data.name = name; data.playerIndex = static_cast(playerIndex); @@ -141,11 +145,12 @@ bool NetworkMessageIntro::receive(Socket* socket){ bool result = NetworkMessage::receive(socket, &data, sizeof(data)); data.name.nullTerminate(); data.versionString.nullTerminate(); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] get nmtIntro, data.playerIndex = %d, data.sessionId = %d\n",__FILE__,__FUNCTION__,__LINE__,data.playerIndex,data.sessionId); return result; } void NetworkMessageIntro::send(Socket* socket) const{ - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtIntro\n",__FILE__,__FUNCTION__,__LINE__); + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending nmtIntro, data.playerIndex = %d, data.sessionId = %d\n",__FILE__,__FUNCTION__,__LINE__,data.playerIndex,data.sessionId); assert(data.messageType==nmtIntro); NetworkMessage::send(socket, &data, sizeof(data)); } diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index 3b15025f..5e1eae6d 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -85,6 +85,7 @@ private: private: struct Data{ int8 messageType; + int32 sessionId; NetworkString versionString; NetworkString name; int16 playerIndex; @@ -96,8 +97,9 @@ private: public: NetworkMessageIntro(); - NetworkMessageIntro(const string &versionString, const string &name, int playerIndex, NetworkGameStateType gameState); + NetworkMessageIntro(int32 sessionId, const string &versionString, const string &name, int playerIndex, NetworkGameStateType gameState); + int32 getSessionId() const { return data.sessionId;} string getVersionString() const { return data.versionString.getString(); } string getName() const { return data.name.getString(); } int getPlayerIndex() const { return data.playerIndex; }