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 "leak_dumper.h"
# include "map.h"
# include "config.h"
# include "logger.h"
using namespace std ;
using namespace Shared : : Platform ;
using namespace Shared : : Util ;
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
ClientInterface : : ClientInterface ( ) {
clientSocket = NULL ;
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-03-16 22:37:11 +01:00
networkGameDataSynchCheckOkMap = false ;
networkGameDataSynchCheckOkTile = false ;
networkGameDataSynchCheckOkTech = false ;
}
ClientInterface : : ~ ClientInterface ( )
{
2010-07-02 04:50:20 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
if ( clientSocket ! = NULL & & clientSocket - > isConnected ( ) = = true )
{
2010-07-02 04:50:20 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-24 00:32:25 +01:00
string sQuitText = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " has chosen to leave the game! " ;
2010-03-16 22:37:11 +01:00
sendTextMessage ( sQuitText , - 1 ) ;
}
2010-07-02 04:50:20 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
close ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
delete clientSocket ;
clientSocket = NULL ;
2010-07-02 04:50:20 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : connect ( const Ip & ip , int port )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
delete clientSocket ;
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
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END - socket = %d \n " , __FILE__ , __FUNCTION__ , clientSocket - > getSocketId ( ) ) ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : reset ( )
{
if ( getSocket ( ) ! = NULL )
{
2010-03-24 00:32:25 +01:00
string sQuitText = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " has chosen to leave the game! " ;
2010-03-16 22:37:11 +01:00
sendTextMessage ( sQuitText , - 1 ) ;
close ( ) ;
}
}
void ClientInterface : : update ( )
{
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
while ( ! requestedCommands . empty ( ) ) {
if ( networkMessageCommandList . addCommand ( & requestedCommands . back ( ) ) ) {
requestedCommands . pop_back ( ) ;
}
else {
break ;
}
}
2010-06-15 07:36:07 +02:00
int lastSendElapsed = difftime ( time ( NULL ) , lastNetworkCommandListSendTime ) ;
2010-06-23 16:49:20 +02:00
if ( lastNetworkCommandListSendTime > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] lastSendElapsed = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , lastSendElapsed ) ;
2010-06-15 07:36:07 +02:00
2010-06-18 20:47:20 +02:00
if ( networkMessageCommandList . getCommandCount ( ) > 0 | |
( lastNetworkCommandListSendTime > 0 & & lastSendElapsed > = ClientInterface : : maxNetworkCommandListSendTimeWait ) ) {
2010-03-16 22:37:11 +01:00
sendMessage ( & networkMessageCommandList ) ;
2010-06-15 07:36:07 +02:00
lastNetworkCommandListSendTime = time ( NULL ) ;
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
if ( ! requestedCommands . empty ( ) ) {
char szBuf [ 1024 ] = " " ;
2010-06-15 07:36:07 +02:00
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
string sMsg = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " may go out of synch: client requestedCommands.size() = " + intToStr ( requestedCommands . size ( ) ) ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( sMsg , - 1 , true ) ;
2010-06-05 02:00:36 +02:00
}
2010-03-16 22:37:11 +01:00
//clear chat variables
2010-07-01 02:08:59 +02:00
//chatText.clear();
//chatSender.clear();
//chatTeamIndex= -1;
clearChatInfo ( ) ;
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-03-16 22:37:11 +01:00
void ClientInterface : : updateLobby ( )
{
//clear chat variables
2010-07-01 02:08:59 +02:00
//chatText.clear();
//chatSender.clear();
//chatTeamIndex= -1;
clearChatInfo ( ) ;
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-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageIntro . getGameState ( ) ) ;
2010-03-16 22:37:11 +01:00
//check consistency
2010-05-12 17:25:56 +02:00
if ( networkMessageIntro . getVersionString ( ) ! = getNetworkVersionString ( ) ) {
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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-07-08 00:25:29 +02:00
string playerNameStr = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) ;
sErr = " Server and client binary mismatch! \n You have to use the exactly same binaries! \n \n Server: " + networkMessageIntro . getVersionString ( ) +
" \n Client: " + getNetworkVersionString ( ) + " player [ " + playerNameStr + " ] " ;
2010-04-30 03:32:37 +02:00
printf ( " %s \n " , sErr . c_str ( ) ) ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( " Server and client binary mismatch!! " , - 1 , true ) ;
sendTextMessage ( " Server: " + networkMessageIntro . getVersionString ( ) , - 1 , true ) ;
sendTextMessage ( " Client: " + getNetworkVersionString ( ) , - 1 , true ) ;
2010-07-08 00:25:29 +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
string playerNameStr = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) ;
sErr = " Warning, Server and client are using the same version but different platforms. \n \n Server: " + networkMessageIntro . getVersionString ( ) +
" \n Client: " + getNetworkVersionString ( ) + " player [ " + playerNameStr + " ] " ;
2010-04-30 03:32:37 +02:00
printf ( " %s \n " , sErr . c_str ( ) ) ;
2010-07-08 00:25:29 +02:00
//sendTextMessage("Server and client have different platform mismatch.",-1, true);
//sendTextMessage(" Server:" + networkMessageIntro.getVersionString(),-1, true);
//sendTextMessage(" Client: "+ getNetworkVersionString(),-1, true);
//sendTextMessage(" Client player [" + playerNameStr + "]",-1, true);
2010-04-30 03:32:37 +02:00
}
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 ) ;
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
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
if ( networkMessageIntro . getGameState ( ) = = nmgstOk ) {
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
//send intro message
NetworkMessageIntro sendNetworkMessageIntro ( getNetworkVersionString ( ) , Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) , - 1 , nmgstOk ) ;
2010-03-16 22:37:11 +01:00
2010-05-12 17:25:56 +02:00
playerIndex = networkMessageIntro . getPlayerIndex ( ) ;
serverName = networkMessageIntro . getName ( ) ;
sendMessage ( & sendNetworkMessageIntro ) ;
2010-03-16 22:37:11 +01:00
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( clientSocket = = NULL | | clientSocket - > isConnected ( ) = = false ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
string sErr = " Disconnected from server during intro handshake. " ;
DisplayErrorMessage ( sErr ) ;
quit = true ;
close ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ( ) ;
2010-07-08 10:29:51 +02:00
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 ( ) ;
2010-07-08 10:29:51 +02:00
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 :
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtPing \n " , __FILE__ , __FUNCTION__ ) ;
NetworkMessagePing networkMessagePing ;
if ( receiveMessage ( & networkMessagePing ) ) {
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 ;
if ( receiveMessage ( & networkMessageSynchNetworkGameData ) )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got NetworkMessageSynchNetworkGameData \n " , __FILE__ , __FUNCTION__ ) ;
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 ;
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 ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " 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 ( ) ) ;
}
// check the checksum's
//int32 tilesetCRC = getFolderTreeContentsCheckSumRecursively(string(GameConstants::folder_path_tilesets) + "/" + networkMessageSynchNetworkGameData.getTileset() + "/*", ".xml", NULL);
tilesetCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + networkMessageSynchNetworkGameData . getTileset ( ) + string ( " /* " ) , " .xml " , NULL ) ;
this - > setNetworkGameDataSynchCheckOkTile ( ( tilesetCRC = = networkMessageSynchNetworkGameData . getTilesetCRC ( ) ) ) ;
//if(this->getNetworkGameDataSynchCheckOkTile() == false)
//{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] tilesetCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTileset() = [%s] \n " , __FILE__ , __FUNCTION__ , tilesetCRC , networkMessageSynchNetworkGameData . getTilesetCRC ( ) , networkMessageSynchNetworkGameData . getTileset ( ) . c_str ( ) ) ;
//}
//tech, load before map because of resources
//int32 techCRC = getFolderTreeContentsCheckSumRecursively(string(GameConstants::folder_path_techs) + "/" + networkMessageSynchNetworkGameData.getTech() + "/*", ".xml", NULL);
techCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , string ( " / " ) + networkMessageSynchNetworkGameData . getTech ( ) + string ( " /* " ) , " .xml " , NULL ) ;
this - > setNetworkGameDataSynchCheckOkTech ( ( techCRC = = networkMessageSynchNetworkGameData . getTechCRC ( ) ) ) ;
//if(this->getNetworkGameDataSynchCheckOkTech() == false)
//{
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 ( ) ) ;
//}
//map
Checksum checksum ;
string file = Map : : getMapPath ( networkMessageSynchNetworkGameData . getMap ( ) , scenarioDir , false ) ;
if ( file ! = " " ) {
checksum . addFile ( file ) ;
mapCRC = checksum . getSum ( ) ;
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] file = [%s] checksum = %d\n",__FILE__,__FUNCTION__,file.c_str(),mapCRC);
this - > setNetworkGameDataSynchCheckOkMap ( ( mapCRC = = networkMessageSynchNetworkGameData . getMapCRC ( ) ) ) ;
//if(this->getNetworkGameDataSynchCheckOkMap() == false)
//{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] mapCRC info, local = %d, remote = %d, file = [%s] \n " , __FILE__ , __FUNCTION__ , mapCRC , networkMessageSynchNetworkGameData . getMapCRC ( ) , file . c_str ( ) ) ;
//}
}
catch ( const runtime_error & ex ) {
string sErr = ex . what ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] error during processing, sErr = [%s] \n " , __FILE__ , __FUNCTION__ , sErr . c_str ( ) ) ;
DisplayErrorMessage ( sErr ) ;
}
NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus ( mapCRC , tilesetCRC , techCRC ) ;
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 ( ) )
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck localCRC = %d, remoteCRC = %d, file [%s] \n " ,
__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 ) )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtText \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
2010-07-01 02:08:59 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getSender ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) ) ;
this - > addChatInfo ( msg ) ;
2010-03-16 22:37:11 +01:00
}
}
break ;
case nmtLaunch :
{
NetworkMessageLaunch networkMessageLaunch ;
if ( receiveMessage ( & networkMessageLaunch ) )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got NetworkMessageLaunch \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
networkMessageLaunch . buildGameSettings ( & gameSettings ) ;
//replace server player by network
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i )
{
//replace by network
if ( gameSettings . getFactionControl ( i ) = = ctHuman )
{
gameSettings . setFactionControl ( i , ctNetwork ) ;
}
//set the faction index
if ( gameSettings . getStartLocationIndex ( i ) = = playerIndex )
{
gameSettings . setThisFactionIndex ( i ) ;
}
}
launchGame = true ;
}
}
break ;
2010-04-22 01:13:39 +02:00
case nmtBroadCastSetup :
{
NetworkMessageLaunch networkMessageLaunch ;
if ( receiveMessage ( & networkMessageLaunch ) )
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got NetworkMessageLaunch \n " , __FILE__ , __FUNCTION__ ) ;
networkMessageLaunch . buildGameSettings ( & gameSettings ) ;
//replace server player by network
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i )
{
//replace by network
if ( gameSettings . getFactionControl ( i ) = = ctHuman )
{
gameSettings . setFactionControl ( i , ctNetwork ) ;
}
//set the faction index
if ( gameSettings . getStartLocationIndex ( i ) = = playerIndex )
{
gameSettings . setThisFactionIndex ( i ) ;
}
}
gameSettingsReceived = true ;
}
}
break ;
2010-04-30 03:08:29 +02:00
case nmtPlayerIndexMessage :
{
PlayerIndexMessage playerIndexMessage ( - 1 ) ;
if ( receiveMessage ( & playerIndexMessage ) )
{
playerIndex = playerIndexMessage . getPlayerIndex ( ) ;
}
}
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));
2010-07-01 02:08:59 +02:00
sendTextMessage ( " Unexpected network message: " + intToStr ( networkMessageType ) , - 1 , true ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( sErr ) ;
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 ) {
2010-07-06 07:30:34 +02:00
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
}
void ClientInterface : : updateKeyframe ( int frameCount )
{
2010-06-15 07:36:07 +02:00
currentFrameCount = frameCount ;
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
waitForMessage ( ) ;
//check we have an expected message
NetworkMessageType networkMessageType = getNextMessageType ( true ) ;
switch ( networkMessageType )
{
case nmtCommandList :
{
2010-04-15 03:19:00 +02:00
Chrono chrono ;
chrono . start ( ) ;
int waitCount = 0 ;
2010-03-16 22:37:11 +01:00
//make sure we read the message
NetworkMessageCommandList networkMessageCommandList ;
2010-05-15 20:59:17 +02:00
while ( receiveMessage ( & networkMessageCommandList ) = = false & &
isConnected ( ) = = true ) {
//sleep(waitSleepTime);
sleep ( 0 ) ;
2010-04-15 03:19:00 +02:00
waitCount + + ;
2010-03-16 22:37:11 +01:00
}
2010-07-06 07:30:34 +02:00
if ( 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 ) ;
2010-04-15 03:19:00 +02:00
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-06-08 09:40:32 +02:00
string sErr = " Network synchronization error, frame counts do not match, server frameCount = " + intToStr ( networkMessageCommandList . getFrameCount ( ) ) + " , local frameCount = " + intToStr ( frameCount ) ;
2010-03-17 07:25:19 +01:00
//throw runtime_error("Network synchronization error, frame counts do not match");
2010-07-01 02:08:59 +02:00
sendTextMessage ( sErr , - 1 , true ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( sErr ) ;
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
2010-05-15 20:59:17 +02:00
for ( int i = 0 ; i < networkMessageCommandList . getCommandCount ( ) ; + + i ) {
2010-03-16 22:37:11 +01:00
pendingCommands . push_back ( * networkMessageCommandList . getCommand ( i ) ) ;
}
2010-07-06 07:30:34 +02:00
if ( 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 :
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtPing \n " , __FILE__ , __FUNCTION__ ) ;
NetworkMessagePing networkMessagePing ;
if ( receiveMessage ( & networkMessagePing ) ) {
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 nmtQuit :
{
NetworkMessageQuit networkMessageQuit ;
2010-05-15 20:59:17 +02:00
//if(receiveMessage(&networkMessageQuit)) {
while ( receiveMessage ( & networkMessageQuit ) = = false & &
isConnected ( ) = = true ) {
sleep ( 0 ) ;
2010-03-16 22:37:11 +01:00
}
2010-05-15 20:59:17 +02:00
quit = true ;
2010-03-16 22:37:11 +01:00
done = true ;
}
break ;
case nmtText :
{
NetworkMessageText networkMessageText ;
2010-05-15 20:59:17 +02:00
while ( receiveMessage ( & networkMessageText ) = = false & &
isConnected ( ) = = true ) {
sleep ( 0 ) ;
2010-03-16 22:37:11 +01:00
}
2010-05-15 20:59:17 +02:00
2010-07-01 02:08:59 +02:00
//chatText = networkMessageText.getText();
//chatSender = networkMessageText.getSender();
//chatTeamIndex = networkMessageText.getTeamIndex();
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getSender ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) ) ;
this - > addChatInfo ( msg ) ;
2010-03-16 22:37:11 +01:00
}
break ;
case nmtInvalid :
break ;
default :
2010-03-17 07:25:19 +01:00
{
//throw runtime_error(string(__FILE__) + "::" + string(__FUNCTION__) + " Unexpected message in client interface: " + intToStr(networkMessageType));
2010-07-01 02:08:59 +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 ) ) ;
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
}
}
2010-05-04 04:32:43 +02:00
void ClientInterface : : waitUntilReady ( Checksum * checksum ) {
2010-04-12 22:32:47 +02:00
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);
//send ready message
NetworkMessageReady networkMessageReady ;
sendMessage ( & networkMessageReady ) ;
2010-04-12 22:32:47 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ) {
2010-04-12 22:32:47 +02:00
if ( isConnected ( ) = = false ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
string sErr = " Error, Server has disconnected! " ;
2010-05-04 04:32:43 +02:00
//sendTextMessage(sErr,-1);
2010-04-12 22:32:47 +02:00
2010-05-04 04:32:43 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-03-16 22:37:11 +01:00
2010-04-12 22:32:47 +02:00
DisplayErrorMessage ( sErr ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
quit = true ;
close ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2010-03-16 22:37:11 +01:00
}
2010-07-06 07:30:34 +02:00
else if ( networkMessageType = = nmtInvalid ) {
if ( chrono . getMillis ( ) > readyWaitTimeout ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//throw runtime_error("Timeout waiting for server");
string sErr = " Timeout waiting for server " ;
sendTextMessage ( sErr , - 1 , true ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
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 ( sErr ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
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
quit = true ;
close ( ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ] = " " ;
2010-07-17 08:54:04 +02:00
sprintf ( szBuf , " Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds) " , lastMillisCheck , int ( readyWaitTimeout / 1000 ) ) ;
2010-07-06 07:30:34 +02:00
logger . add ( szBuf , true ) ;
}
}
2010-03-16 22:37:11 +01:00
}
2010-07-06 07:30:34 +02:00
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//throw runtime_error(string(__FILE__) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType) );
sendTextMessage ( " Unexpected network message: " + intToStr ( networkMessageType ) , - 1 , true ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
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
2010-07-06 07:30:34 +02:00
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
quit = true ;
close ( ) ;
2010-04-12 22:32:47 +02:00
2010-07-06 07:30:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return ;
}
// sleep a bit
sleep ( waitSleepTime ) ;
2010-03-16 22:37:11 +01:00
}
}
2010-04-12 22:32:47 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
//check checksum
2010-05-04 04:32:43 +02:00
if ( networkMessageReady . getChecksum ( ) ! = checksum - > getSum ( ) ) {
2010-04-12 22:32:47 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 14:22:08 +01:00
string sErr = " Checksum error, you don't have the same data as the server " ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( sErr , - 1 , true ) ;
2010-04-12 22:32:47 +02:00
2010-07-08 00:25:29 +02:00
string playerNameStr = " Player with error is [ " + Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " ] " ;
sendTextMessage ( playerNameStr , - 1 , true ) ;
2010-04-28 23:38:15 +02:00
string sErr1 = " Client Checksum: " + intToStr ( checksum - > getSum ( ) ) ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( sErr1 , - 1 , true ) ;
2010-04-28 23:38:15 +02:00
string sErr2 = " Server Checksum: " + intToStr ( networkMessageReady . getChecksum ( ) ) ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( sErr2 , - 1 , true ) ;
2010-04-28 23:38:15 +02:00
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
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-28 23:38:15 +02:00
string niceError = sErr + string ( " \n " ) + sErr1 + string ( " \n " ) + sErr2 ;
DisplayErrorMessage ( niceError ) ;
2010-04-12 22:32:47 +02:00
quit = true ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ) ;
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-16 22:37:11 +01:00
}
2010-07-01 02:08:59 +02:00
void ClientInterface : : sendTextMessage ( const string & text , int teamIndex , bool echoLocal ) {
2010-03-16 22:37:11 +01:00
NetworkMessageText networkMessageText ( text , getHostName ( ) , teamIndex ) ;
sendMessage ( & networkMessageText ) ;
2010-07-01 02:08:59 +02:00
if ( echoLocal = = true ) {
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getSender ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) ) ;
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
}
void ClientInterface : : waitForMessage ( )
{
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 ;
chrono . start ( ) ;
2010-04-15 03:19:00 +02:00
int waitLoopCount = 0 ;
2010-05-15 20:59:17 +02:00
while ( getNextMessageType ( true ) = = nmtInvalid ) {
if ( isConnected ( ) = = false ) {
2010-07-02 21:57:47 +02:00
if ( quit = = false ) {
//throw runtime_error("Disconnected");
//sendTextMessage("Server has Disconnected.",-1);
DisplayErrorMessage ( " Server has Disconnected. " ) ;
quit = true ;
}
2010-03-17 21:57:19 +01:00
close ( ) ;
2010-03-17 07:25:19 +01:00
return ;
2010-03-16 22:37:11 +01:00
}
2010-05-15 20:59:17 +02:00
if ( chrono . getMillis ( ) > messageWaitTimeout ) {
2010-05-29 08:56:32 +02:00
//if(1) {
2010-03-17 07:25:19 +01:00
//throw runtime_error("Timeout waiting for message");
2010-05-28 16:59:09 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-01 02:08:59 +02:00
sendTextMessage ( " Timeout waiting for message " , - 1 , true ) ;
2010-03-17 07:25:19 +01:00
DisplayErrorMessage ( " Timeout waiting for message " ) ;
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
}
2010-04-30 04:34:19 +02: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
2010-07-06 07:30:34 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] waiting took %lld msecs, waitLoopCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , waitLoopCount ) ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : quitGame ( bool userManuallyQuit )
{
2010-05-28 16:59:09 +02:00
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 )
{
2010-05-28 16:59:09 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-24 00:32:25 +01:00
string sQuitText = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " has chosen to leave the game! " ;
2010-03-16 22:37:11 +01:00
sendTextMessage ( sQuitText , - 1 ) ;
close ( ) ;
}
2010-05-28 16:59:09 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-16 22:37:11 +01:00
}
void ClientInterface : : close ( )
{
2010-07-08 10:29:51 +02:00
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
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 ) {
2010-04-01 08:31:10 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-02 04:33:32 +02:00
ClientSocket : : discoverServers ( cb ) ;
2010-04-01 08:31:10 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-02 04:33:32 +02:00
}
void ClientInterface : : stopServerDiscovery ( ) {
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 ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-01 08:31:10 +02:00
}
2010-04-29 01:59:37 +02:00
void ClientInterface : : sendSwitchSetupRequest ( string selectedFactionName , int8 currentFactionIndex , int8 toFactionIndex , int8 toTeam )
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//printf("string-cuf-tof-team= %s-%d-%d-%d\n",selectedFactionName.c_str(),currentFactionIndex,toFactionIndex,toTeam);
SwitchSetupRequest message = SwitchSetupRequest ( selectedFactionName , currentFactionIndex , toFactionIndex , toTeam ) ;
sendMessage ( & message ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
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 :
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtText \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
discard = true ;
NetworkMessageText netMsg = NetworkMessageText ( ) ;
this - > receiveMessage ( & netMsg ) ;
ChatMsgInfo msg ( netMsg . getText ( ) . c_str ( ) , netMsg . getSender ( ) . c_str ( ) , netMsg . getTeamIndex ( ) ) ;
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-03-16 22:37:11 +01:00
} } //end namespace