2010-03-16 22:37:11 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-03-17 14:22:08 +01:00
// Copyright (C) 2001-2008 Marti<74> o Figueroa
2010-03-16 22:37:11 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "client_interface.h"
# include <stdexcept>
# include <cassert>
# include "platform_util.h"
# include "game_util.h"
# include "conversion.h"
# include "config.h"
# include "lang.h"
# include "map.h"
# include "config.h"
# include "logger.h"
2011-01-20 09:19:14 +01:00
# include "window.h"
# include "leak_dumper.h"
2010-03-16 22:37:11 +01:00
using namespace std ;
using namespace Shared : : Platform ;
using namespace Shared : : Util ;
2010-08-28 01:04:59 +02:00
# ifdef WIN32
# define snprintf _snprintf
# endif
2010-03-16 22:37:11 +01:00
namespace Glest { namespace Game {
// =====================================================
// class ClientInterface
// =====================================================
const int ClientInterface : : messageWaitTimeout = 10000 ; //10 seconds
2010-04-15 03:19:00 +02:00
const int ClientInterface : : waitSleepTime = 10 ;
2010-06-28 02:21:12 +02:00
const int ClientInterface : : maxNetworkCommandListSendTimeWait = 4 ;
2010-03-16 22:37:11 +01:00
2011-01-02 10:33:37 +01:00
ClientInterface : : ClientInterface ( ) : GameNetworkInterface ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] constructor for %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , this ) ;
2010-08-26 18:03:54 +02:00
2010-03-16 22:37:11 +01:00
clientSocket = NULL ;
2010-08-23 17:40:43 +02:00
sessionKey = 0 ;
2010-03-16 22:37:11 +01:00
launchGame = false ;
introDone = false ;
playerIndex = - 1 ;
2010-04-22 01:13:39 +02:00
gameSettingsReceived = false ;
2010-05-12 17:25:56 +02:00
gotIntro = false ;
2010-06-15 07:36:07 +02:00
lastNetworkCommandListSendTime = 0 ;
currentFrameCount = 0 ;
2010-08-07 05:26:38 +02:00
clientSimulationLagStartTime = 0 ;
2010-03-16 22:37:11 +01:00
networkGameDataSynchCheckOkMap = false ;
networkGameDataSynchCheckOkTile = false ;
networkGameDataSynchCheckOkTech = false ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( " " ) ;
this - > setReceivedDataSynchCheck ( false ) ;
2010-03-16 22:37:11 +01:00
}
2010-12-25 09:14:35 +01:00
ClientInterface : : ~ ClientInterface ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] destructor for %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , this ) ;
2010-03-16 22:37:11 +01:00
2010-12-25 09:14:35 +01:00
if ( clientSocket ! = NULL & & clientSocket - > isConnected ( ) = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-02 04:50:20 +02:00
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string sQuitText = " has chosen to leave the game! " ;
if ( lang . hasString ( " PlayerLeftGame " , languageList [ i ] ) = = true ) {
sQuitText = lang . get ( " PlayerLeftGame " , languageList [ i ] ) ;
}
sendTextMessage ( sQuitText , - 1 , false , languageList [ i ] ) ;
}
2010-03-16 22:37:11 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-02 04:50:20 +02:00
close ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-02 04:50:20 +02:00
2010-03-16 22:37:11 +01:00
delete clientSocket ;
clientSocket = NULL ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
}
2010-12-25 09:14:35 +01:00
void ClientInterface : : connect ( const Ip & ip , int port ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
delete clientSocket ;
2010-12-25 09:14:35 +01:00
clientSocket = NULL ;
2010-03-16 22:37:11 +01:00
this - > ip = ip ;
this - > port = port ;
clientSocket = new ClientSocket ( ) ;
clientSocket - > setBlock ( false ) ;
clientSocket - > connect ( ip , port ) ;
2010-05-12 17:25:56 +02:00
connectedTime = time ( NULL ) ;
2010-03-16 22:37:11 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END - socket = %d \n " , __FILE__ , __FUNCTION__ , clientSocket - > getSocketId ( ) ) ;
2010-03-16 22:37:11 +01:00
}
2010-12-25 09:14:35 +01:00
void ClientInterface : : reset ( ) {
if ( getSocket ( ) ! = NULL ) {
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string sQuitText = " has chosen to leave the game! " ;
if ( lang . hasString ( " PlayerLeftGame " , languageList [ i ] ) = = true ) {
sQuitText = lang . get ( " PlayerLeftGame " , languageList [ i ] ) ;
}
sendTextMessage ( sQuitText , - 1 , false , languageList [ i ] ) ;
}
2010-03-16 22:37:11 +01:00
close ( ) ;
}
}
2010-12-25 09:14:35 +01:00
void ClientInterface : : update ( ) {
2011-02-21 02:34:31 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2011-02-21 02:34:31 +01:00
2010-06-15 07:36:07 +02:00
NetworkMessageCommandList networkMessageCommandList ( currentFrameCount ) ;
2010-03-16 22:37:11 +01:00
//send as many commands as we can
2010-12-25 09:14:35 +01:00
while ( requestedCommands . empty ( ) = = false ) {
if ( networkMessageCommandList . addCommand ( & requestedCommands . back ( ) ) ) {
2010-03-16 22:37:11 +01:00
requestedCommands . pop_back ( ) ;
}
2010-12-25 09:14:35 +01:00
else {
2010-03-16 22:37:11 +01:00
break ;
}
}
2010-06-15 07:36:07 +02:00
2011-01-11 23:09:46 +01:00
double lastSendElapsed = difftime ( time ( NULL ) , lastNetworkCommandListSendTime ) ;
2010-06-15 07:36:07 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 1 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-02-21 02:34:31 +01:00
2010-06-18 20:47:20 +02:00
if ( networkMessageCommandList . getCommandCount ( ) > 0 | |
( lastNetworkCommandListSendTime > 0 & & lastSendElapsed > = ClientInterface : : maxNetworkCommandListSendTimeWait ) ) {
2010-06-15 07:36:07 +02:00
lastNetworkCommandListSendTime = time ( NULL ) ;
2010-12-25 09:14:35 +01:00
sendMessage ( & networkMessageCommandList ) ;
2011-01-11 23:09:46 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 1 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-16 22:37:11 +01:00
}
2010-06-05 02:00:36 +02:00
// Possible cause of out of synch since we have more commands that need
// to be sent in this frame
2010-12-25 09:14:35 +01:00
if ( requestedCommands . empty ( ) = = false ) {
char szBuf [ 4096 ] = " " ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , requestedCommands . size ( ) ) ;
2010-06-05 02:00:36 +02:00
2010-08-21 15:04:52 +02:00
string sMsg = " may go out of synch: client requestedCommands.size() = " + intToStr ( requestedCommands . size ( ) ) ;
2011-04-05 20:39:47 +02:00
sendTextMessage ( sMsg , - 1 , true , " " ) ;
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2011-02-21 02:34:31 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 1 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-06-05 02:00:36 +02:00
}
2011-02-21 02:34:31 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 1 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-16 22:37:11 +01:00
}
2010-06-04 21:42:58 +02:00
std : : string ClientInterface : : getServerIpAddress ( ) {
return this - > ip . getString ( ) ;
}
2010-08-22 10:00:05 +02:00
void ClientInterface : : updateLobby ( ) {
2010-03-16 22:37:11 +01:00
NetworkMessageType networkMessageType = getNextMessageType ( true ) ;
switch ( networkMessageType )
{
case nmtInvalid :
break ;
case nmtIntro :
{
NetworkMessageIntro networkMessageIntro ;
2010-05-12 17:25:56 +02:00
if ( receiveMessage ( & networkMessageIntro ) ) {
gotIntro = true ;
2010-08-23 17:40:43 +02:00
sessionKey = networkMessageIntro . getSessionId ( ) ;
2010-08-05 07:58:14 +02:00
versionString = networkMessageIntro . getVersionString ( ) ;
2010-08-21 15:04:52 +02:00
playerIndex = networkMessageIntro . getPlayerIndex ( ) ;
serverName = networkMessageIntro . getName ( ) ;
2011-01-15 19:56:03 +01:00
serverFTPPort = networkMessageIntro . getFtpPort ( ) ;
2010-05-12 17:25:56 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d, playerIndex = %d, serverFTPPort = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageIntro . getGameState ( ) , versionString . c_str ( ) , sessionKey , playerIndex , serverFTPPort ) ;
2010-03-16 22:37:11 +01:00
//check consistency
2011-04-30 03:05:53 +02:00
bool compatible = checkVersionComptability ( networkMessageIntro . getVersionString ( ) , getNetworkVersionSVNString ( ) ) ;
2010-10-15 19:27:00 +02:00
if ( compatible = = false ) {
//if(networkMessageIntro.getVersionString() != getNetworkVersionString()) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
2010-04-30 03:32:37 +02:00
bool versionMatched = false ;
string platformFreeVersion = getNetworkPlatformFreeVersionString ( ) ;
string sErr = " " ;
if ( strncmp ( platformFreeVersion . c_str ( ) , networkMessageIntro . getVersionString ( ) . c_str ( ) , strlen ( platformFreeVersion . c_str ( ) ) ) ! = 0 ) {
2010-08-21 15:04:52 +02:00
string playerNameStr = getHumanPlayerName ( ) ;
2010-07-08 00:25:29 +02:00
sErr = " Server and client binary mismatch! \n You have to use the exactly same binaries! \n \n Server: " + networkMessageIntro . getVersionString ( ) +
2011-04-30 03:05:53 +02:00
" \n Client: " + getNetworkVersionSVNString ( ) + " player [ " + playerNameStr + " ] " ;
2010-04-30 03:32:37 +02:00
printf ( " %s \n " , sErr . c_str ( ) ) ;
2011-04-05 20:39:47 +02:00
sendTextMessage ( " Server and client binary mismatch!! " , - 1 , true , " " ) ;
sendTextMessage ( " Server: " + networkMessageIntro . getVersionString ( ) , - 1 , true , " " ) ;
2011-04-30 03:05:53 +02:00
sendTextMessage ( " Client: " + getNetworkVersionSVNString ( ) , - 1 , true , " " ) ;
2011-04-05 20:39:47 +02:00
sendTextMessage ( " Client player [ " + playerNameStr + " ] " , - 1 , true , " " ) ;
2010-04-30 03:32:37 +02:00
}
else {
versionMatched = true ;
2010-07-08 00:25:29 +02:00
2010-08-21 15:04:52 +02:00
string playerNameStr = getHumanPlayerName ( ) ;
2010-12-24 09:43:09 +01:00
sErr = " Warning, Server and client are using the same version but different platforms. \n \n Server: " + networkMessageIntro . getVersionString ( ) +
2011-04-30 03:05:53 +02:00
" \n Client: " + getNetworkVersionSVNString ( ) + " player [ " + playerNameStr + " ] " ;
2010-04-30 03:32:37 +02:00
printf ( " %s \n " , sErr . c_str ( ) ) ;
}
2010-05-12 17:25:56 +02:00
if ( Config : : getInstance ( ) . getBool ( " PlatformConsistencyChecks " , " true " ) & &
versionMatched = = false ) { // error message and disconnect only if checked
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( sErr ) ;
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-03-17 21:57:19 +01:00
quit = true ;
close ( ) ;
2010-03-17 14:22:08 +01:00
return ;
}
2010-03-16 22:37:11 +01:00
}
2010-03-17 21:57:19 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
2010-05-12 17:25:56 +02:00
if ( networkMessageIntro . getGameState ( ) = = nmgstOk ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
2010-05-12 17:25:56 +02:00
//send intro message
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2011-04-30 03:05:53 +02:00
NetworkMessageIntro sendNetworkMessageIntro ( sessionKey , getNetworkVersionSVNString ( ) , getHumanPlayerName ( ) , - 1 , nmgstOk , this - > getSocket ( ) - > getConnectedIPAddress ( ) , serverFTPPort , lang . getLanguage ( ) ) ;
2010-05-12 17:25:56 +02:00
sendMessage ( & sendNetworkMessageIntro ) ;
2010-03-16 22:37:11 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
if ( clientSocket = = NULL | | clientSocket - > isConnected ( ) = = false ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
string sErr = " Disconnected from server during intro handshake. " ;
DisplayErrorMessage ( sErr ) ;
quit = true ;
close ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
return ;
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-08 10:29:51 +02:00
assert ( playerIndex > = 0 & & playerIndex < GameConstants : : maxPlayers ) ;
introDone = true ;
}
2010-05-12 17:25:56 +02:00
}
else if ( networkMessageIntro . getGameState ( ) = = nmgstNoSlots ) {
string sErr = " Cannot join the server because there are no open slots for new players. " ;
DisplayErrorMessage ( sErr ) ;
quit = true ;
close ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
return ;
}
else {
string sErr = " Unknown response from server: " + intToStr ( networkMessageIntro . getGameState ( ) ) ;
DisplayErrorMessage ( sErr ) ;
quit = true ;
close ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
return ;
}
2010-03-16 22:37:11 +01:00
}
}
break ;
2010-07-09 17:01:49 +02:00
case nmtPing :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtPing \n " , __FILE__ , __FUNCTION__ ) ;
2010-07-09 17:01:49 +02:00
NetworkMessagePing networkMessagePing ;
if ( receiveMessage ( & networkMessagePing ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-09 19:12:57 +02:00
lastPingInfo = networkMessagePing ;
2010-07-09 17:01:49 +02:00
}
}
break ;
2010-03-16 22:37:11 +01:00
case nmtSynchNetworkGameData :
{
NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData ;
2010-08-22 10:00:05 +02:00
if ( receiveMessage ( & networkMessageSynchNetworkGameData ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got NetworkMessageSynchNetworkGameData, getTechCRCFileCount() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageSynchNetworkGameData . getTechCRCFileCount ( ) ) ;
2010-08-22 10:00:05 +02:00
networkGameDataSynchCheckOkMap = false ;
networkGameDataSynchCheckOkTile = false ;
networkGameDataSynchCheckOkTech = false ;
this - > setNetworkGameDataSynchCheckTechMismatchReport ( " " ) ;
this - > setReceivedDataSynchCheck ( false ) ;
2010-03-16 22:37:11 +01:00
2010-04-29 09:59:44 +02:00
int32 tilesetCRC = 0 ;
int32 techCRC = 0 ;
int32 mapCRC = 0 ;
2010-08-22 10:00:05 +02:00
vector < std : : pair < string , int32 > > vctFileList ;
2010-04-29 09:59:44 +02:00
try {
Config & config = Config : : getInstance ( ) ;
string scenarioDir = " " ;
if ( gameSettings . getScenarioDir ( ) ! = " " ) {
scenarioDir = gameSettings . getScenarioDir ( ) ;
if ( EndsWith ( scenarioDir , " .xml " ) = = true ) {
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - 4 , 4 ) ;
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - gameSettings . getScenario ( ) . size ( ) , gameSettings . getScenario ( ) . size ( ) + 1 ) ;
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , gameSettings . getScenarioDir ( ) . c_str ( ) , gameSettings . getScenario ( ) . c_str ( ) , scenarioDir . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
}
// check the checksum's
tilesetCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + networkMessageSynchNetworkGameData . getTileset ( ) + string ( " /* " ) , " .xml " , NULL ) ;
this - > setNetworkGameDataSynchCheckOkTile ( ( tilesetCRC = = networkMessageSynchNetworkGameData . getTilesetCRC ( ) ) ) ;
//if(this->getNetworkGameDataSynchCheckOkTile() == false)
//{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] tilesetCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTileset() = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , tilesetCRC , networkMessageSynchNetworkGameData . getTilesetCRC ( ) , networkMessageSynchNetworkGameData . getTileset ( ) . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
//}
//tech, load before map because of resources
techCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , string ( " / " ) + networkMessageSynchNetworkGameData . getTech ( ) + string ( " /* " ) , " .xml " , NULL ) ;
this - > setNetworkGameDataSynchCheckOkTech ( ( techCRC = = networkMessageSynchNetworkGameData . getTechCRC ( ) ) ) ;
2010-08-22 10:00:05 +02:00
if ( this - > getNetworkGameDataSynchCheckOkTech ( ) = = false ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-22 10:00:05 +02:00
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , string ( " / " ) + networkMessageSynchNetworkGameData . getTech ( ) + " /* " , " .xml " , NULL ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-22 10:00:05 +02:00
string report = networkMessageSynchNetworkGameData . getTechCRCFileMismatchReport ( vctFileList ) ;
this - > setNetworkGameDataSynchCheckTechMismatchReport ( report ) ;
}
2010-04-29 09:59:44 +02:00
//if(this->getNetworkGameDataSynchCheckOkTech() == false)
//{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] techCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTech() = [%s] \n " , __FILE__ , __FUNCTION__ , techCRC , networkMessageSynchNetworkGameData . getTechCRC ( ) , networkMessageSynchNetworkGameData . getTech ( ) . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
//}
//map
Checksum checksum ;
string file = Map : : getMapPath ( networkMessageSynchNetworkGameData . getMap ( ) , scenarioDir , false ) ;
if ( file ! = " " ) {
checksum . addFile ( file ) ;
mapCRC = checksum . getSum ( ) ;
}
this - > setNetworkGameDataSynchCheckOkMap ( ( mapCRC = = networkMessageSynchNetworkGameData . getMapCRC ( ) ) ) ;
2010-08-22 10:00:05 +02:00
this - > setReceivedDataSynchCheck ( true ) ;
2010-04-29 09:59:44 +02:00
//if(this->getNetworkGameDataSynchCheckOkMap() == false)
//{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] mapCRC info, local = %d, remote = %d, file = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , mapCRC , networkMessageSynchNetworkGameData . getMapCRC ( ) , file . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
//}
}
catch ( const runtime_error & ex ) {
2010-10-06 22:22:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-04-29 09:59:44 +02:00
string sErr = ex . what ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] error during processing, sErr = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
DisplayErrorMessage ( sErr ) ;
}
2010-08-22 10:00:05 +02:00
NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus ( mapCRC , tilesetCRC , techCRC , vctFileList ) ;
2010-04-29 09:59:44 +02:00
sendMessage ( & sendNetworkMessageSynchNetworkGameDataStatus ) ;
2010-03-16 22:37:11 +01:00
}
}
break ;
case nmtSynchNetworkGameDataFileCRCCheck :
{
NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck ;
if ( receiveMessage ( & networkMessageSynchNetworkGameDataFileCRCCheck ) )
{
2010-03-20 00:26:00 +01:00
Checksum checksum ;
string file = networkMessageSynchNetworkGameDataFileCRCCheck . getFileName ( ) ;
checksum . addFile ( file ) ;
int32 fileCRC = checksum . getSum ( ) ;
2010-03-16 22:37:11 +01:00
2010-03-20 00:26:00 +01:00
if ( fileCRC ! = networkMessageSynchNetworkGameDataFileCRCCheck . getFileCRC ( ) )
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck localCRC = %d, remoteCRC = %d, file [%s] \n " ,
2010-03-20 00:26:00 +01:00
__FILE__ , __FUNCTION__ , fileCRC ,
networkMessageSynchNetworkGameDataFileCRCCheck . getFileCRC ( ) ,
networkMessageSynchNetworkGameDataFileCRCCheck . getFileName ( ) . c_str ( ) ) ;
2010-03-16 22:37:11 +01:00
2010-03-20 00:26:00 +01:00
// Here we initiate a download of missing or mismatched content
2010-03-16 22:37:11 +01:00
2010-03-20 00:26:00 +01:00
NetworkMessageSynchNetworkGameDataFileGet sendNetworkMessageSynchNetworkGameDataFileGet ( networkMessageSynchNetworkGameDataFileCRCCheck . getFileName ( ) ) ;
sendMessage ( & sendNetworkMessageSynchNetworkGameDataFileGet ) ;
2010-03-16 22:37:11 +01:00
2010-03-20 00:26:00 +01:00
FileTransferInfo fileInfo ;
fileInfo . hostType = eClient ;
fileInfo . serverIP = this - > ip . getString ( ) ;
fileInfo . serverPort = this - > port ;
fileInfo . fileName = networkMessageSynchNetworkGameDataFileCRCCheck . getFileName ( ) ;
2010-03-16 22:37:11 +01:00
2010-03-20 00:26:00 +01:00
FileTransferSocketThread * fileXferThread = new FileTransferSocketThread ( fileInfo ) ;
fileXferThread - > start ( ) ;
}
if ( networkMessageSynchNetworkGameDataFileCRCCheck . getFileIndex ( ) < networkMessageSynchNetworkGameDataFileCRCCheck . getTotalFileCount ( ) )
{
NetworkMessageSynchNetworkGameDataFileCRCCheck sendNetworkMessageSynchNetworkGameDataFileCRCCheck (
networkMessageSynchNetworkGameDataFileCRCCheck . getTotalFileCount ( ) ,
networkMessageSynchNetworkGameDataFileCRCCheck . getFileIndex ( ) + 1 ,
0 ,
" " ) ;
sendMessage ( & sendNetworkMessageSynchNetworkGameDataFileCRCCheck ) ;
}
2010-03-16 22:37:11 +01:00
}
}
break ;
case nmtText :
{
NetworkMessageText networkMessageText ;
if ( receiveMessage ( & networkMessageText ) )
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtText \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
2011-04-05 20:39:47 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) , networkMessageText . getPlayerIndex ( ) , networkMessageText . getTargetLanguage ( ) ) ;
2010-07-01 02:08:59 +02:00
this - > addChatInfo ( msg ) ;
2010-03-16 22:37:11 +01:00
}
}
break ;
case nmtLaunch :
2010-08-27 22:09:55 +02:00
case nmtBroadCastSetup :
2010-03-16 22:37:11 +01:00
{
NetworkMessageLaunch networkMessageLaunch ;
2010-08-22 10:00:05 +02:00
if ( receiveMessage ( & networkMessageLaunch ) ) {
if ( networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got nmtLaunch \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-22 10:00:05 +02:00
}
2010-08-27 22:09:55 +02:00
else if ( networkMessageLaunch . getMessageType ( ) = = nmtBroadCastSetup ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got nmtBroadCastSetup \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-27 22:09:55 +02:00
}
2010-08-22 10:00:05 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageLaunch . getMessageType ( ) ) ;
2010-08-27 22:09:55 +02:00
char szBuf [ 1024 ] = " " ;
snprintf ( szBuf , 1023 , " In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageLaunch . getMessageType ( ) ) ;
throw runtime_error ( szBuf ) ;
2010-08-22 10:00:05 +02:00
}
2010-03-16 22:37:11 +01:00
networkMessageLaunch . buildGameSettings ( & gameSettings ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageLaunch . getMessageType ( ) ) ;
2010-03-16 22:37:11 +01:00
//replace server player by network
2010-08-22 10:00:05 +02:00
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i ) {
2010-03-16 22:37:11 +01:00
//replace by network
2010-08-22 10:00:05 +02:00
if ( gameSettings . getFactionControl ( i ) = = ctHuman ) {
2010-03-16 22:37:11 +01:00
gameSettings . setFactionControl ( i , ctNetwork ) ;
}
//set the faction index
2010-10-08 03:41:53 +02:00
if ( gameSettings . getStartLocationIndex ( i ) = = playerIndex ) {
2010-03-16 22:37:11 +01:00
gameSettings . setThisFactionIndex ( i ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] gameSettings.getThisFactionIndex(i) = %d, playerIndex = %d, i = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gameSettings . getThisFactionIndex ( ) , playerIndex , i ) ;
2010-03-16 22:37:11 +01:00
}
}
2010-08-22 10:00:05 +02:00
if ( networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
launchGame = true ;
}
2010-08-27 22:09:55 +02:00
else if ( networkMessageLaunch . getMessageType ( ) = = nmtBroadCastSetup ) {
gameSettingsReceived = true ;
2010-04-22 01:13:39 +02:00
}
}
}
break ;
2010-04-30 03:08:29 +02:00
case nmtPlayerIndexMessage :
{
PlayerIndexMessage playerIndexMessage ( - 1 ) ;
2010-10-08 19:17:22 +02:00
if ( receiveMessage ( & playerIndexMessage ) ) {
2010-04-30 03:08:29 +02:00
playerIndex = playerIndexMessage . getPlayerIndex ( ) ;
2010-10-08 19:17:22 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtPlayerIndexMessage, playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-04-30 03:08:29 +02:00
}
}
break ;
2010-03-16 22:37:11 +01:00
default :
2010-03-17 07:25:19 +01:00
{
string sErr = string ( __FILE__ ) + " :: " + string ( __FUNCTION__ ) + " Unexpected network message: " + intToStr ( networkMessageType ) ;
//throw runtime_error(string(__FILE__) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType));
2011-04-05 20:39:47 +02:00
sendTextMessage ( " Unexpected network message: " + intToStr ( networkMessageType ) , - 1 , true , " " ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( sErr ) ;
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-03-17 21:57:19 +01:00
quit = true ;
close ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-03-16 22:37:11 +01:00
}
2010-05-12 17:25:56 +02:00
2010-07-08 10:29:51 +02:00
if ( clientSocket ! = NULL & & clientSocket - > isConnected ( ) = = true & &
gotIntro = = false & & difftime ( time ( NULL ) , connectedTime ) > GameConstants : : maxClientConnectHandshakeSecs ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] difftime(time(NULL),connectedTime) = %f \n " , __FILE__ , __FUNCTION__ , __LINE__ , difftime ( time ( NULL ) , connectedTime ) ) ;
2010-05-12 17:25:56 +02:00
close ( ) ;
}
2010-03-16 22:37:11 +01:00
}
2010-12-25 09:14:35 +01:00
void ClientInterface : : updateKeyframe ( int frameCount ) {
2010-06-15 07:36:07 +02:00
currentFrameCount = frameCount ;
2011-02-21 02:34:31 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2011-02-21 02:34:31 +01:00
2011-02-15 04:32:14 +01:00
int simulateLag = Config : : getInstance ( ) . getInt ( " SimulateClientLag " , " 0 " ) ;
2010-03-16 22:37:11 +01:00
bool done = false ;
2010-05-15 20:59:17 +02:00
while ( done = = false ) {
2010-03-16 22:37:11 +01:00
//wait for the next message
2011-02-15 04:32:14 +01:00
NetworkMessageType networkMessageType = waitForMessage ( ) ;
2010-03-16 22:37:11 +01:00
2010-08-07 05:26:38 +02:00
// START: Test simulating lag for the client
2011-02-15 04:32:14 +01:00
if ( simulateLag > 0 ) {
2010-08-07 05:26:38 +02:00
if ( clientSimulationLagStartTime = = 0 ) {
clientSimulationLagStartTime = time ( NULL ) ;
}
if ( difftime ( time ( NULL ) , clientSimulationLagStartTime ) < = Config : : getInstance ( ) . getInt ( " SimulateClientLagDurationSeconds " , " 0 " ) ) {
sleep ( Config : : getInstance ( ) . getInt ( " SimulateClientLag " , " 0 " ) ) ;
}
}
// END: Test simulating lag for the client
2010-03-16 22:37:11 +01:00
//check we have an expected message
2011-02-15 04:32:14 +01:00
//NetworkMessageType networkMessageType= getNextMessageType(true);
2010-03-16 22:37:11 +01:00
switch ( networkMessageType )
{
case nmtCommandList :
{
2010-04-15 03:19:00 +02:00
int waitCount = 0 ;
2010-03-16 22:37:11 +01:00
//make sure we read the message
2010-08-20 20:06:03 +02:00
time_t receiveTimeElapsed = time ( NULL ) ;
2010-03-16 22:37:11 +01:00
NetworkMessageCommandList networkMessageCommandList ;
2011-04-30 08:13:23 +02:00
bool gotCmd = receiveMessage ( & networkMessageCommandList ) ;
if ( gotCmd = = false ) {
throw runtime_error ( " error retrieving nmtCommandList returned false! " ) ;
2010-03-16 22:37:11 +01:00
}
2011-04-30 08:13:23 +02:00
// while(receiveMessage(&networkMessageCommandList) == false &&
// isConnected() == true &&
// difftime(time(NULL),receiveTimeElapsed) <= (messageWaitTimeout / 2000)) {
// waitCount++;
// }
2010-04-15 03:19:00 +02:00
2011-04-30 08:13:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] receiveMessage took %lld msecs, waitCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , waitCount ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-03-16 22:37:11 +01:00
//check that we are in the right frame
2010-05-15 20:59:17 +02:00
if ( networkMessageCommandList . getFrameCount ( ) ! = frameCount ) {
2010-08-21 15:04:52 +02:00
string sErr = " Player: " + getHumanPlayerName ( ) +
2010-08-07 05:26:38 +02:00
" got a Network synchronization error, frame counts do not match, server frameCount = " +
intToStr ( networkMessageCommandList . getFrameCount ( ) ) + " , local frameCount = " +
intToStr ( frameCount ) ;
2011-04-05 20:39:47 +02:00
sendTextMessage ( sErr , - 1 , true , " " ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( sErr ) ;
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-03-17 21:57:19 +01:00
quit = true ;
close ( ) ;
2010-03-17 07:25:19 +01:00
return ;
2010-03-16 22:37:11 +01:00
}
// give all commands
2011-04-30 08:13:23 +02:00
for ( int i = 0 ; i < networkMessageCommandList . getCommandCount ( ) ; + + i ) {
2010-03-16 22:37:11 +01:00
pendingCommands . push_back ( * networkMessageCommandList . getCommand ( i ) ) ;
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] transfer network commands took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-04-15 03:19:00 +02:00
2010-03-16 22:37:11 +01:00
done = true ;
}
break ;
2010-07-09 17:01:49 +02:00
case nmtPing :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtPing \n " , __FILE__ , __FUNCTION__ ) ;
2010-07-09 17:01:49 +02:00
NetworkMessagePing networkMessagePing ;
if ( receiveMessage ( & networkMessagePing ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-09 19:12:57 +02:00
lastPingInfo = networkMessagePing ;
2010-07-09 17:01:49 +02:00
}
2011-02-21 02:34:31 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-07-09 17:01:49 +02:00
}
break ;
2010-03-16 22:37:11 +01:00
case nmtQuit :
{
2010-08-20 20:06:03 +02:00
time_t receiveTimeElapsed = time ( NULL ) ;
2010-03-16 22:37:11 +01:00
NetworkMessageQuit networkMessageQuit ;
2011-04-30 08:13:23 +02:00
// while(receiveMessage(&networkMessageQuit) == false &&
// isConnected() == true &&
// difftime(time(NULL),receiveTimeElapsed) <= (messageWaitTimeout / 2000)) {
// }
bool gotCmd = receiveMessage ( & networkMessageQuit ) ;
if ( gotCmd = = false ) {
throw runtime_error ( " error retrieving nmtQuit returned false! " ) ;
2010-03-16 22:37:11 +01:00
}
2011-04-30 08:13:23 +02:00
2010-05-15 20:59:17 +02:00
quit = true ;
2010-03-16 22:37:11 +01:00
done = true ;
}
break ;
case nmtText :
{
2010-08-20 20:06:03 +02:00
time_t receiveTimeElapsed = time ( NULL ) ;
2010-03-16 22:37:11 +01:00
NetworkMessageText networkMessageText ;
2011-04-30 08:13:23 +02:00
// while(receiveMessage(&networkMessageText) == false &&
// isConnected() == true &&
// difftime(time(NULL),receiveTimeElapsed) <= (messageWaitTimeout / 1000)) {
// }
bool gotCmd = receiveMessage ( & networkMessageText ) ;
if ( gotCmd = = false ) {
throw runtime_error ( " error retrieving nmtText returned false! " ) ;
2010-03-16 22:37:11 +01:00
}
2010-05-15 20:59:17 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2011-02-21 02:34:31 +01:00
2011-04-05 20:39:47 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) , networkMessageText . getPlayerIndex ( ) , networkMessageText . getTargetLanguage ( ) ) ;
2010-07-01 02:08:59 +02:00
this - > addChatInfo ( msg ) ;
2011-02-21 02:34:31 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-16 22:37:11 +01:00
}
break ;
2011-03-31 20:13:02 +02:00
case nmtLoadingStatusMessage :
break ;
case nmtInvalid :
2010-03-16 22:37:11 +01:00
break ;
default :
2010-03-17 07:25:19 +01:00
{
2011-04-05 20:39:47 +02:00
sendTextMessage ( " Unexpected message in client interface: " + intToStr ( networkMessageType ) , - 1 , true , " " ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( string ( __FILE__ ) + " :: " + string ( __FUNCTION__ ) + " Unexpected message in client interface: " + intToStr ( networkMessageType ) ) ;
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-03-17 21:57:19 +01:00
quit = true ;
close ( ) ;
2010-05-04 04:32:43 +02:00
done = true ;
2010-03-17 07:25:19 +01:00
}
2010-03-16 22:37:11 +01:00
}
2010-05-04 04:32:43 +02:00
if ( isConnected ( ) = = false & & quit = = true ) {
done = true ;
}
2010-03-16 22:37:11 +01:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
2010-03-16 22:37:11 +01:00
}
2010-05-04 04:32:43 +02:00
void ClientInterface : : waitUntilReady ( Checksum * checksum ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
Logger & logger = Logger : : getInstance ( ) ;
Chrono chrono ;
chrono . start ( ) ;
// FOR TESTING ONLY - delay to see the client count up while waiting
//sleep(5000);
2011-04-24 06:22:19 +02:00
//clientSocket->setBlock(true);
2010-03-16 22:37:11 +01:00
//send ready message
NetworkMessageReady networkMessageReady ;
2011-03-31 20:30:53 +02:00
sendMessage ( & networkMessageReady ) ;
2010-03-16 22:37:11 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-03-31 20:13:02 +02:00
NetworkMessageLoadingStatus networkMessageLoadingStatus ( nmls_NONE ) ;
Lang & lang = Lang : : getInstance ( ) ;
2010-03-16 22:37:11 +01:00
int64 lastMillisCheck = 0 ;
//wait until we get a ready message from the server
2010-05-04 04:32:43 +02:00
while ( true ) {
2011-03-31 20:13:02 +02:00
// FOR TESTING ONLY - delay to see the client count up while waiting
//sleep(2000);
2010-05-04 04:32:43 +02:00
2010-04-12 22:32:47 +02:00
if ( isConnected ( ) = = false ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
string sErr = " Error, Server has disconnected! " ;
DisplayErrorMessage ( sErr ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
quit = true ;
close ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
return ;
}
NetworkMessageType networkMessageType = getNextMessageType ( true ) ;
2010-07-06 07:30:34 +02:00
// consume old messages from the lobby
bool discarded = shouldDiscardNetworkMessage ( networkMessageType ) ;
if ( discarded = = false ) {
if ( networkMessageType = = nmtReady ) {
if ( receiveMessage ( & networkMessageReady ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-06 07:30:34 +02:00
break ;
}
2010-03-16 22:37:11 +01:00
}
2011-03-31 20:13:02 +02:00
else if ( networkMessageType = = nmtLoadingStatusMessage ) {
if ( receiveMessage ( & networkMessageLoadingStatus ) ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
}
else if ( networkMessageType = = nmtQuit ) {
NetworkMessageQuit networkMessageQuit ;
if ( receiveMessage ( & networkMessageQuit ) ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
DisplayErrorMessage ( lang . get ( " GameCancelledByUser " ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
quit = true ;
close ( ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return ;
}
}
2010-07-06 07:30:34 +02:00
else if ( networkMessageType = = nmtInvalid ) {
if ( chrono . getMillis ( ) > readyWaitTimeout ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string sErr = " Timeout waiting for server " ;
if ( lang . hasString ( " TimeoutWaitingForServer " , languageList [ i ] ) = = true ) {
sErr = lang . get ( " TimeoutWaitingForServer " , languageList [ i ] ) ;
}
bool echoLocal = lang . isLanguageLocal ( lang . getLanguage ( ) ) ;
sendTextMessage ( sErr , - 1 , echoLocal , languageList [ i ] ) ;
2010-04-12 22:32:47 +02:00
2011-04-05 20:39:47 +02:00
if ( echoLocal ) {
DisplayErrorMessage ( sErr ) ;
}
}
2010-04-12 22:32:47 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-07-06 07:30:34 +02:00
quit = true ;
close ( ) ;
2010-04-12 22:32:47 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-06 07:30:34 +02:00
return ;
}
else {
if ( chrono . getMillis ( ) / 1000 > lastMillisCheck ) {
lastMillisCheck = ( chrono . getMillis ( ) / 1000 ) ;
2010-03-16 22:37:11 +01:00
2010-07-06 07:30:34 +02:00
char szBuf [ 1024 ] = " " ;
2011-03-31 20:13:02 +02:00
string updateTextFormat = lang . get ( " NetworkGameClientLoadStatus " ) ;
if ( updateTextFormat = = " " | | updateTextFormat [ 0 ] = = ' ? ' ) {
updateTextFormat = " Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds) " ;
}
string waitForHosts = " " ;
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER1_CONNECTED ) = = nmls_PLAYER1_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER1_READY ) ! = nmls_PLAYER1_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 0 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER2_CONNECTED ) = = nmls_PLAYER2_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER2_READY ) ! = nmls_PLAYER2_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 1 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER3_CONNECTED ) = = nmls_PLAYER3_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER3_READY ) ! = nmls_PLAYER3_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 2 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER4_CONNECTED ) = = nmls_PLAYER4_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER4_READY ) ! = nmls_PLAYER4_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 3 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER5_CONNECTED ) = = nmls_PLAYER5_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER5_READY ) ! = nmls_PLAYER5_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 4 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER6_CONNECTED ) = = nmls_PLAYER6_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER6_READY ) ! = nmls_PLAYER6_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 5 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER7_CONNECTED ) = = nmls_PLAYER7_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER7_READY ) ! = nmls_PLAYER7_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 6 ) ;
2011-03-31 20:13:02 +02:00
}
}
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER8_CONNECTED ) = = nmls_PLAYER8_CONNECTED ) {
if ( ( networkMessageLoadingStatus . getStatus ( ) & nmls_PLAYER8_READY ) ! = nmls_PLAYER8_READY ) {
if ( waitForHosts ! = " " ) {
waitForHosts + = " , " ;
}
2011-03-31 20:30:53 +02:00
waitForHosts + = gameSettings . getNetworkPlayerNameByPlayerIndex ( 7 ) ;
2011-03-31 20:13:02 +02:00
}
}
sprintf ( szBuf , updateTextFormat . c_str ( ) , ( long long int ) lastMillisCheck , int ( readyWaitTimeout / 1000 ) ) ;
char szBuf1 [ 1024 ] = " " ;
string statusTextFormat = lang . get ( " NetworkGameStatusWaiting " ) ;
if ( statusTextFormat = = " " | | statusTextFormat [ 0 ] = = ' ? ' ) {
statusTextFormat = " Waiting for players: %s " ;
}
sprintf ( szBuf1 , statusTextFormat . c_str ( ) , waitForHosts . c_str ( ) ) ;
logger . add ( szBuf , true , szBuf1 ) ;
2010-07-06 07:30:34 +02:00
}
}
2010-03-16 22:37:11 +01:00
}
2010-07-06 07:30:34 +02:00
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-04-05 20:39:47 +02:00
sendTextMessage ( " Unexpected network message: " + intToStr ( networkMessageType ) , - 1 , true , " " ) ;
2010-04-12 22:32:47 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
DisplayErrorMessage ( string ( __FILE__ ) + " :: " + string ( __FUNCTION__ ) + " Unexpected network message: " + intToStr ( networkMessageType ) ) ;
2010-04-12 22:32:47 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-07-06 07:30:34 +02:00
quit = true ;
close ( ) ;
2010-04-12 22:32:47 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-06 07:30:34 +02:00
return ;
}
2011-01-20 09:19:14 +01:00
Window : : handleEvent ( ) ;
2010-07-06 07:30:34 +02:00
// sleep a bit
sleep ( waitSleepTime ) ;
2010-03-16 22:37:11 +01:00
}
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2010-03-16 22:37:11 +01:00
//check checksum
2010-05-04 04:32:43 +02:00
if ( networkMessageReady . getChecksum ( ) ! = checksum - > getSum ( ) ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string sErr = " Checksum error, you don't have the same data as the server " ;
if ( lang . hasString ( " CheckSumGameLoadError " , languageList [ i ] ) = = true ) {
sErr = lang . get ( " CheckSumGameLoadError " , languageList [ i ] ) ;
}
bool echoLocal = lang . isLanguageLocal ( lang . getLanguage ( ) ) ;
sendTextMessage ( sErr , - 1 , echoLocal , languageList [ i ] ) ;
string playerNameStr = " Player with error is: " + getHumanPlayerName ( ) ;
if ( lang . hasString ( " CheckSumGameLoadPlayer " , languageList [ i ] ) = = true ) {
playerNameStr = lang . get ( " CheckSumGameLoadPlayer " , languageList [ i ] ) + " " + getHumanPlayerName ( ) ;
}
sendTextMessage ( playerNameStr , - 1 , echoLocal , languageList [ i ] ) ;
2010-04-12 22:32:47 +02:00
2011-04-05 20:39:47 +02:00
string sErr1 = " Client Checksum: " + intToStr ( checksum - > getSum ( ) ) ;
if ( lang . hasString ( " CheckSumGameLoadClient " , languageList [ i ] ) = = true ) {
sErr1 = lang . get ( " CheckSumGameLoadClient " , languageList [ i ] ) + " " + intToStr ( checksum - > getSum ( ) ) ;
}
2010-07-08 00:25:29 +02:00
2011-04-05 20:39:47 +02:00
sendTextMessage ( sErr1 , - 1 , echoLocal , languageList [ i ] ) ;
2010-04-28 23:38:15 +02:00
2011-04-05 20:39:47 +02:00
string sErr2 = " Server Checksum: " + intToStr ( networkMessageReady . getChecksum ( ) ) ;
if ( lang . hasString ( " CheckSumGameLoadServer " , languageList [ i ] ) = = true ) {
sErr2 = lang . get ( " CheckSumGameLoadServer " , languageList [ i ] ) + " " + intToStr ( networkMessageReady . getChecksum ( ) ) ;
}
sendTextMessage ( sErr2 , - 1 , echoLocal , languageList [ i ] ) ;
2010-04-28 23:38:15 +02:00
2011-04-05 20:39:47 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d %s %s %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) , sErr1 . c_str ( ) , sErr2 . c_str ( ) ) ;
if ( echoLocal = = true ) {
if ( Config : : getInstance ( ) . getBool ( " NetworkConsistencyChecks " ) ) {
// error message and disconnect only if checked
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
string niceError = sErr + string ( " \n " ) + sErr1 + string ( " \n " ) + sErr2 ;
DisplayErrorMessage ( niceError ) ;
}
}
}
// string sErr = "Checksum error, you don't have the same data as the server";
// CheckSumGameLoadError
// sendTextMessage(sErr,-1, true);
//
// string playerNameStr = "Player with error is [" + getHumanPlayerName() + "]";
// sendTextMessage(playerNameStr,-1, true);
//
// string sErr1 = "Client Checksum: " + intToStr(checksum->getSum());
// sendTextMessage(sErr1,-1, true);
//
// string sErr2 = "Server Checksum: " + intToStr(networkMessageReady.getChecksum());
// sendTextMessage(sErr2,-1, true);
// if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d %s %s %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str(),sErr1.c_str(),sErr2.c_str());
2010-04-12 22:32:47 +02:00
if ( Config : : getInstance ( ) . getBool ( " NetworkConsistencyChecks " ) ) {
// error message and disconnect only if checked
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
2011-04-05 20:39:47 +02:00
//string niceError = sErr + string("\n") + sErr1 + string("\n") + sErr2;
//DisplayErrorMessage(niceError);
2010-04-12 22:32:47 +02:00
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2010-04-12 22:32:47 +02:00
quit = true ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-12 22:32:47 +02:00
close ( ) ;
}
2010-03-17 14:22:08 +01:00
return ;
2010-03-16 22:37:11 +01:00
}
2010-03-17 21:57:19 +01:00
2010-05-04 04:32:43 +02:00
//delay the start a bit, so clients have more room to get messages
2010-06-01 02:57:48 +02:00
//sleep(GameConstants::networkExtraLatency);
2010-03-16 22:37:11 +01:00
2010-06-28 02:21:12 +02:00
// This triggers LAG update packets to begin as required
lastNetworkCommandListSendTime = time ( NULL ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
}
2011-04-05 20:39:47 +02:00
void ClientInterface : : sendTextMessage ( const string & text , int teamIndex , bool echoLocal ,
string targetLanguage ) {
2010-10-08 19:17:22 +02:00
string humanPlayerName = getHumanPlayerName ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , humanPlayerName . c_str ( ) , playerIndex ) ;
2010-10-08 19:17:22 +02:00
2011-04-05 20:39:47 +02:00
NetworkMessageText networkMessageText ( text , teamIndex , playerIndex , targetLanguage ) ;
2010-03-16 22:37:11 +01:00
sendMessage ( & networkMessageText ) ;
2010-07-01 02:08:59 +02:00
if ( echoLocal = = true ) {
2011-04-05 20:39:47 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) , networkMessageText . getPlayerIndex ( ) , targetLanguage ) ;
2010-07-01 02:08:59 +02:00
this - > addChatInfo ( msg ) ;
}
2010-03-16 22:37:11 +01:00
}
2010-07-09 17:01:49 +02:00
void ClientInterface : : sendPingMessage ( int32 pingFrequency , int64 pingTime ) {
NetworkMessagePing networkMessagePing ( pingFrequency , pingTime ) ;
sendMessage ( & networkMessagePing ) ;
}
2010-06-04 21:42:58 +02:00
string ClientInterface : : getNetworkStatus ( ) {
std : : string label = Lang : : getInstance ( ) . get ( " Server " ) + " : " + serverName ;
2010-07-02 04:50:20 +02:00
//float pingTime = getThreadedPingMS(getServerIpAddress().c_str());
2010-06-04 21:42:58 +02:00
char szBuf [ 1024 ] = " " ;
2010-07-02 04:50:20 +02:00
//sprintf(szBuf,"%s, ping = %.2fms",label.c_str(),pingTime);
sprintf ( szBuf , " %s " , label . c_str ( ) ) ;
2010-06-04 21:42:58 +02:00
return szBuf ;
2010-03-16 22:37:11 +01:00
}
2011-02-15 04:32:14 +01:00
NetworkMessageType ClientInterface : : waitForMessage ( )
2010-03-16 22:37:11 +01:00
{
2010-05-29 08:56:32 +02:00
// Debug!
/*
sendTextMessage ( " Timeout waiting for message " , - 1 ) ;
DisplayErrorMessage ( " Timeout waiting for message " ) ;
quit = true ;
close ( ) ;
return ;
*/
2010-03-16 22:37:11 +01:00
Chrono chrono ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2010-03-16 22:37:11 +01:00
2011-02-15 04:32:14 +01:00
NetworkMessageType msg = nmtInvalid ;
2010-04-15 03:19:00 +02:00
int waitLoopCount = 0 ;
2011-02-15 04:32:14 +01:00
while ( msg = = nmtInvalid ) {
msg = getNextMessageType ( true ) ;
if ( msg = = nmtInvalid ) {
if ( chrono . getMillis ( ) % 150 = = 0 & & isConnected ( ) = = false ) {
if ( quit = = false ) {
//throw runtime_error("Disconnected");
//sendTextMessage("Server has Disconnected.",-1);
DisplayErrorMessage ( " Server has Disconnected. " ) ;
quit = true ;
}
close ( ) ;
return msg ;
2010-07-02 21:57:47 +02:00
}
2010-03-16 22:37:11 +01:00
2011-02-15 04:32:14 +01:00
if ( chrono . getMillis ( ) > messageWaitTimeout ) {
//if(1) {
//throw runtime_error("Timeout waiting for message");
2010-05-28 16:59:09 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 16:59:09 +02:00
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string msg = " Timeout waiting for message. " ;
if ( lang . hasString ( " TimeoutWaitingForMessage " , languageList [ i ] ) = = true ) {
msg = lang . get ( " TimeoutWaitingForMessage " , languageList [ i ] ) ;
}
sendTextMessage ( msg , - 1 , lang . isLanguageLocal ( languageList [ i ] ) , languageList [ i ] ) ;
if ( lang . isLanguageLocal ( languageList [ i ] ) = = true ) {
DisplayErrorMessage ( msg ) ;
}
}
2011-04-30 08:13:23 +02:00
sleep ( 1 ) ;
2011-02-15 04:32:14 +01:00
quit = true ;
close ( ) ;
return msg ;
}
// Sleep ever second we wait to let other threads work
//else if(chrono.getMillis() % 1000 == 0) {
//sleep(0);
//}
2010-03-16 22:37:11 +01:00
2011-02-15 04:32:14 +01:00
//sleep(waitSleepTime);
}
2010-04-15 03:19:00 +02:00
waitLoopCount + + ;
2010-03-16 22:37:11 +01:00
}
2010-04-15 03:19:00 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 1 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] waiting took %lld msecs, waitLoopCount = %d, msg = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , waitLoopCount , msg ) ;
2011-02-15 04:32:14 +01:00
return msg ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : quitGame ( bool userManuallyQuit )
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] userManuallyQuit = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , userManuallyQuit ) ;
2010-03-16 22:37:11 +01:00
if ( clientSocket ! = NULL & & userManuallyQuit = = true )
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-04-05 20:39:47 +02:00
Lang & lang = Lang : : getInstance ( ) ;
const vector < string > languageList = this - > gameSettings . getUniqueNetworkPlayerLanguages ( ) ;
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
string msg = " has chosen to leave the game! " ;
if ( lang . hasString ( " PlayerLeftGame " , languageList [ i ] ) = = true ) {
msg = lang . get ( " PlayerLeftGame " , languageList [ i ] ) ;
}
sendTextMessage ( msg , - 1 , lang . isLanguageLocal ( languageList [ i ] ) , languageList [ i ] ) ;
}
sleep ( 1 ) ;
2010-03-16 22:37:11 +01:00
close ( ) ;
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : close ( )
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] START, clientSocket = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , clientSocket ) ;
2010-03-16 22:37:11 +01:00
delete clientSocket ;
clientSocket = NULL ;
2010-05-12 17:25:56 +02:00
connectedTime = 0 ;
gotIntro = false ;
2010-07-08 10:29:51 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] END \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
}
2010-04-01 08:31:10 +02:00
2010-04-02 04:33:32 +02:00
void ClientInterface : : discoverServers ( DiscoveredServersInterface * cb ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-01 08:31:10 +02:00
2010-04-02 04:33:32 +02:00
ClientSocket : : discoverServers ( cb ) ;
2010-04-01 08:31:10 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-02 04:33:32 +02:00
}
void ClientInterface : : stopServerDiscovery ( ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-01 08:31:10 +02:00
2010-04-02 04:33:32 +02:00
ClientSocket : : stopBroadCastClientThread ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-01 08:31:10 +02:00
}
2010-08-23 06:33:21 +02:00
void ClientInterface : : sendSwitchSetupRequest ( string selectedFactionName , int8 currentFactionIndex ,
int8 toFactionIndex , int8 toTeam , string networkPlayerName ,
2011-04-05 20:39:47 +02:00
int8 networkPlayerStatus , int8 flags ,
string language ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] networkPlayerName [%s] flags = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkPlayerName . c_str ( ) , flags ) ;
2011-03-11 12:11:46 +01:00
SwitchSetupRequest message = SwitchSetupRequest ( selectedFactionName ,
currentFactionIndex , toFactionIndex , toTeam , networkPlayerName ,
2011-04-05 20:39:47 +02:00
networkPlayerStatus , flags , language ) ;
2010-04-29 01:59:37 +02:00
sendMessage ( & message ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-29 01:59:37 +02:00
}
2010-07-06 07:30:34 +02:00
bool ClientInterface : : shouldDiscardNetworkMessage ( NetworkMessageType networkMessageType ) {
bool discard = false ;
switch ( networkMessageType ) {
case nmtIntro :
{
discard = true ;
NetworkMessageIntro msg = NetworkMessageIntro ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
2010-07-09 17:01:49 +02:00
case nmtPing :
{
discard = true ;
NetworkMessagePing msg = NetworkMessagePing ( ) ;
this - > receiveMessage ( & msg ) ;
2010-07-09 19:12:57 +02:00
lastPingInfo = msg ;
2010-07-09 17:01:49 +02:00
}
break ;
2010-07-06 07:30:34 +02:00
case nmtLaunch :
{
discard = true ;
NetworkMessageLaunch msg = NetworkMessageLaunch ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
case nmtText :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtText \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-06 07:30:34 +02:00
discard = true ;
NetworkMessageText netMsg = NetworkMessageText ( ) ;
this - > receiveMessage ( & netMsg ) ;
2011-04-05 20:39:47 +02:00
ChatMsgInfo msg ( netMsg . getText ( ) . c_str ( ) , netMsg . getTeamIndex ( ) , netMsg . getPlayerIndex ( ) , netMsg . getTargetLanguage ( ) ) ;
2010-07-06 07:30:34 +02:00
this - > addChatInfo ( msg ) ;
}
break ;
case nmtSynchNetworkGameData :
{
discard = true ;
NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataStatus :
{
discard = true ;
NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataFileCRCCheck :
{
discard = true ;
NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataFileGet :
{
discard = true ;
NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
case nmtSwitchSetupRequest :
{
discard = true ;
SwitchSetupRequest msg = SwitchSetupRequest ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
2010-07-08 10:29:51 +02:00
case nmtBroadCastSetup :
{
discard = true ;
NetworkMessageLaunch msg = NetworkMessageLaunch ( ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
2010-07-06 07:30:34 +02:00
case nmtPlayerIndexMessage :
{
discard = true ;
PlayerIndexMessage msg = PlayerIndexMessage ( 0 ) ;
this - > receiveMessage ( & msg ) ;
}
break ;
}
return discard ;
2010-03-16 22:37:11 +01:00
}
2010-07-06 07:30:34 +02:00
2010-08-21 15:04:52 +02:00
string ClientInterface : : getHumanPlayerName ( int index ) {
string result = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) ;
2010-10-09 00:32:23 +02:00
if ( index > = 0 | | gameSettings . getThisFactionIndex ( ) > = 0 ) {
2010-08-21 15:04:52 +02:00
if ( index < 0 ) {
2010-10-09 00:32:23 +02:00
index = gameSettings . getThisFactionIndex ( ) ;
2010-08-21 15:04:52 +02:00
}
if ( gameSettings . getNetworkPlayerName ( index ) ! = " " ) {
result = gameSettings . getNetworkPlayerName ( index ) ;
}
}
return result ;
}
2010-03-16 22:37:11 +01:00
} } //end namespace