2010-03-17 07:25:19 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-17 07:25:19 +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 "connection_slot.h"
# include <stdexcept>
# include "conversion.h"
# include "game_util.h"
# include "config.h"
# include "server_interface.h"
# include "network_message.h"
# include "leak_dumper.h"
# include "platform_util.h"
# include "map.h"
using namespace std ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
// =====================================================
2010-05-28 01:46:38 +02:00
// class ConnectionSlotThread
// =====================================================
2011-11-27 06:27:50 +01:00
ConnectionSlotThread : : ConnectionSlotThread ( int slotIndex ) : BaseThread ( ) {
2011-12-02 17:07:59 +01:00
this - > triggerIdMutex = new Mutex ( ) ;
2011-11-27 06:27:50 +01:00
this - > slotIndex = slotIndex ;
2010-05-28 01:46:38 +02:00
this - > slotInterface = NULL ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2011-02-15 08:49:40 +01:00
eventList . reserve ( 100 ) ;
2010-05-28 01:46:38 +02:00
}
2011-11-27 06:27:50 +01:00
ConnectionSlotThread : : ConnectionSlotThread ( ConnectionSlotCallbackInterface * slotInterface , int slotIndex ) : BaseThread ( ) {
2011-12-02 17:07:59 +01:00
this - > triggerIdMutex = new Mutex ( ) ;
2011-11-27 06:27:50 +01:00
this - > slotIndex = slotIndex ;
2010-05-28 01:46:38 +02:00
this - > slotInterface = slotInterface ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2010-05-28 01:46:38 +02:00
}
2011-12-02 17:07:59 +01:00
ConnectionSlotThread : : ~ ConnectionSlotThread ( ) {
delete triggerIdMutex ;
triggerIdMutex = NULL ;
}
2010-05-28 01:46:38 +02:00
void ConnectionSlotThread : : setQuitStatus ( bool value ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d value = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , value ) ;
2010-05-28 01:46:38 +02:00
BaseThread : : setQuitStatus ( value ) ;
if ( value = = true ) {
signalUpdate ( 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-05-28 01:46:38 +02:00
}
void ConnectionSlotThread : : signalUpdate ( ConnectionSlotEvent * event ) {
if ( event ! = NULL ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 09:45:58 +01:00
eventList . push_back ( * event ) ;
2010-06-02 03:37:45 +02:00
safeMutex . ReleaseLock ( ) ;
2010-05-28 01:46:38 +02:00
}
2010-08-21 00:59:32 +02:00
semTaskSignalled . signal ( ) ;
2010-05-28 01:46:38 +02:00
}
2011-01-11 09:45:58 +01:00
void ConnectionSlotThread : : setTaskCompleted ( int eventId ) {
if ( eventId > 0 ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 09:45:58 +01:00
//event->eventCompleted = true;
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventId = = eventId ) {
//eventList.erase(eventList.begin() + i);
slotEvent . eventCompleted = true ;
break ;
}
}
2010-06-02 03:37:45 +02:00
safeMutex . ReleaseLock ( ) ;
2010-05-28 01:46:38 +02:00
}
}
2011-01-11 21:02:07 +01:00
void ConnectionSlotThread : : purgeAllEvents ( ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 21:02:07 +01:00
eventList . clear ( ) ;
safeMutex . ReleaseLock ( ) ;
}
void ConnectionSlotThread : : setAllEventsCompleted ( ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 21:02:07 +01:00
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = false ) {
slotEvent . eventCompleted = true ;
}
}
safeMutex . ReleaseLock ( ) ;
}
2011-01-11 09:45:58 +01:00
void ConnectionSlotThread : : purgeCompletedEvents ( ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 09:45:58 +01:00
//event->eventCompleted = true;
for ( int i = eventList . size ( ) - 1 ; i > = 0 ; i - - ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = true ) {
eventList . erase ( eventList . begin ( ) + i ) ;
}
}
safeMutex . ReleaseLock ( ) ;
}
2011-01-11 19:39:18 +01:00
bool ConnectionSlotThread : : canShutdown ( bool deleteSelfIfShutdownDelayed ) {
bool ret = ( getExecutingTask ( ) = = false ) ;
if ( ret = = false & & deleteSelfIfShutdownDelayed = = true ) {
setDeleteSelfOnExecutionDone ( deleteSelfIfShutdownDelayed ) ;
signalQuit ( ) ;
}
return ret ;
}
2010-12-25 09:14:35 +01:00
bool ConnectionSlotThread : : isSignalCompleted ( ConnectionSlotEvent * event ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2011-01-11 09:45:58 +01:00
bool result = false ;
if ( event ! = NULL ) {
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventId = = event - > eventId ) {
result = slotEvent . eventCompleted ;
break ;
}
}
}
2011-11-23 09:00:09 +01:00
//safeMutex.ReleaseLock();
2010-05-28 01:46:38 +02:00
return result ;
}
2011-11-23 09:00:09 +01:00
void ConnectionSlotThread : : slotUpdateTask ( ConnectionSlotEvent * event ) {
if ( event ! = NULL ) {
if ( event - > eventType = = eSendSocketData ) {
if ( event - > connectionSlot ! = NULL ) {
event - > connectionSlot - > sendMessage ( event - > networkMessage ) ;
}
}
else if ( event - > eventType = = eReceiveSocketData ) {
//updateSlot(event);
if ( event - > connectionSlot ! = NULL ) {
event - > connectionSlot - > updateSlot ( event ) ;
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
}
}
2010-05-28 01:46:38 +02:00
void ConnectionSlotThread : : execute ( ) {
2010-12-22 01:15:32 +01:00
RunningStatusSafeWrapper runningStatus ( this ) ;
2010-05-28 01:46:38 +02:00
try {
2010-12-22 01:15:32 +01:00
//setRunningStatus(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-05-28 01:46:38 +02:00
2011-09-01 03:11:23 +02:00
//unsigned int idx = 0;
2010-05-28 01:46:38 +02:00
for ( ; this - > slotInterface ! = NULL ; ) {
if ( getQuitStatus ( ) = = true ) {
2011-03-28 05:54:23 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 01:46:38 +02:00
break ;
}
2011-11-25 22:56:36 +01:00
semTaskSignalled . waitTillSignalled ( ) ;
2010-05-28 01:46:38 +02:00
2011-11-25 22:56:36 +01:00
if ( getQuitStatus ( ) = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2010-05-28 01:46:38 +02:00
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
2010-12-25 09:14:35 +01:00
int eventCount = eventList . size ( ) ;
if ( eventCount > 0 ) {
2011-01-11 21:02:07 +01:00
ConnectionSlotEvent eventCopy ;
eventCopy . eventId = - 1 ;
2011-01-11 09:45:58 +01:00
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = false ) {
2011-01-11 21:02:07 +01:00
eventCopy = slotEvent ;
2011-01-11 09:45:58 +01:00
break ;
}
}
2010-12-25 09:14:35 +01:00
safeMutex . ReleaseLock ( ) ;
2011-01-11 21:02:07 +01:00
if ( getQuitStatus ( ) = = 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-01-11 21:02:07 +01:00
break ;
}
if ( eventCopy . eventId > 0 ) {
2011-01-11 19:39:18 +01:00
ExecutingTaskSafeWrapper safeExecutingTaskMutex ( this ) ;
2011-11-23 09:00:09 +01:00
//this->slotInterface->slotUpdateTask(&eventCopy);
this - > slotUpdateTask ( & eventCopy ) ;
2011-01-11 21:02:07 +01:00
setTaskCompleted ( eventCopy . eventId ) ;
2011-01-11 09:45:58 +01:00
}
2010-12-25 23:38:00 +01:00
}
else {
safeMutex . ReleaseLock ( ) ;
2010-12-25 09:14:35 +01:00
}
2010-05-28 01:46:38 +02:00
if ( getQuitStatus ( ) = = 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-05-28 01:46:38 +02:00
break ;
}
}
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 01:46:38 +02:00
}
catch ( const exception & ex ) {
2010-12-22 01:15:32 +01:00
//setRunningStatus(false);
2010-05-28 01:46:38 +02:00
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 ( ) ) ;
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 01:46:38 +02:00
throw runtime_error ( 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 \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 01:46:38 +02:00
}
// =====================================================
// class ConnectionSlot
2010-03-17 07:25:19 +01:00
// =====================================================
2010-05-28 16:59:09 +02:00
ConnectionSlot : : ConnectionSlot ( ServerInterface * serverInterface , int playerIndex ) {
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-12-02 17:07:59 +01:00
this - > mutexSocket = new Mutex ( ) ;
this - > mutexCloseConnection = new Mutex ( ) ;
this - > mutexPendingNetworkCommandList = new Mutex ( ) ;
this - > socketSynchAccessor = new Mutex ( ) ;
2010-12-30 16:56:02 +01:00
this - > connectedRemoteIPAddress = 0 ;
2010-08-23 17:40:43 +02:00
this - > sessionKey = 0 ;
2010-05-28 01:46:38 +02:00
this - > serverInterface = serverInterface ;
this - > playerIndex = playerIndex ;
2011-03-11 12:11:46 +01:00
this - > playerStatus = 0 ;
2011-04-05 20:39:47 +02:00
this - > playerLanguage = " " ;
2010-06-15 07:36:07 +02:00
this - > currentFrameCount = 0 ;
this - > currentLagCount = 0 ;
2010-06-17 02:08:27 +02:00
this - > gotLagCountWarning = false ;
2010-06-15 07:36:07 +02:00
this - > lastReceiveCommandListTime = 0 ;
2011-09-01 03:11:23 +02:00
this - > receivedNetworkGameStatus = false ;
2012-03-20 21:31:41 +01:00
this - > canAcceptConnections = true ;
2011-09-01 03:11:23 +02:00
2011-11-23 09:00:09 +01:00
this - > setSocket ( NULL ) ;
2010-05-28 01:46:38 +02:00
this - > slotThreadWorker = NULL ;
2011-11-27 06:27:50 +01:00
this - > slotThreadWorker = new ConnectionSlotThread ( this - > serverInterface , playerIndex ) ;
2010-06-18 22:04:05 +02:00
this - > slotThreadWorker - > setUniqueID ( __FILE__ ) ;
2010-05-28 01:46:38 +02:00
this - > slotThreadWorker - > start ( ) ;
2010-05-28 16:59:09 +02:00
this - > ready = false ;
this - > gotIntro = false ;
2010-08-20 22:03:06 +02:00
this - > connectedTime = 0 ;
2010-03-17 07:25:19 +01:00
networkGameDataSynchCheckOkMap = false ;
networkGameDataSynchCheckOkTile = false ;
networkGameDataSynchCheckOkTech = false ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( " " ) ;
this - > setReceivedDataSynchCheck ( false ) ;
2010-07-01 02:08:59 +02:00
this - > clearChatInfo ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-12-25 09:14:35 +01:00
ConnectionSlot : : ~ ConnectionSlot ( ) {
2011-11-23 09:00:09 +01:00
//printf("===> Destructor for ConnectionSlot = %d\n",playerIndex);
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] START \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2011-01-11 23:09:46 +01: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__ ) ;
2011-01-11 23:09:46 +01:00
2010-12-25 09:14:35 +01:00
if ( BaseThread : : shutdownAndWait ( slotThreadWorker ) = = 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-12-25 09:14:35 +01:00
delete slotThreadWorker ;
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-12-25 09:14:35 +01:00
}
2010-05-28 01:46:38 +02:00
slotThreadWorker = NULL ;
2011-12-02 17:07:59 +01:00
delete socketSynchAccessor ;
socketSynchAccessor = NULL ;
delete mutexPendingNetworkCommandList ;
mutexPendingNetworkCommandList = NULL ;
delete mutexCloseConnection ;
mutexCloseConnection = NULL ;
delete mutexSocket ;
mutexSocket = 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-17 07:25:19 +01:00
}
2011-11-25 22:56:36 +01:00
void ConnectionSlot : : updateSlot ( ConnectionSlotEvent * event ) {
2011-11-23 09:00:09 +01:00
Chrono chrono ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled ) chrono . start ( ) ;
2011-11-25 22:56:36 +01:00
if ( event ! = NULL ) {
bool & socketTriggered = event - > socketTriggered ;
2011-11-25 10:12:53 +01:00
bool checkForNewClients = ( serverInterface - > getGameHasBeenInitiated ( ) = = false ) ;
2011-11-23 09:00:09 +01: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 ( ) ) ;
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] MUTEX LOCK held for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-25 22:56:36 +01:00
if ( ( serverInterface - > getGameHasBeenInitiated ( ) = = false | |
2011-11-25 06:37:55 +01:00
//(this->getSocket() != NULL && socketTriggered == true))) {
2011-11-25 22:56:36 +01:00
socketTriggered = = true ) ) {
if ( socketTriggered = = true | |
( serverInterface - > getGameHasBeenInitiated ( ) = = false & & this - > isConnected ( ) = = false ) ) {
2011-11-23 09:00:09 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] MUTEX LOCK held for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-25 22:56:36 +01:00
this - > update ( checkForNewClients , event - > triggerId ) ;
2011-11-23 09:00:09 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugPerformance ) . enabled & & chrono . getMillis ( ) > 4 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] took %lld msecs \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) ) ;
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] MUTEX LOCK held for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
// This means no clients are trying to connect at the moment
2011-11-25 00:15:21 +01:00
//if(this->getSocket() == NULL) {
// checkForNewClients = false;
//}
2011-11-25 22:56:36 +01:00
}
2011-11-23 09:00:09 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] MUTEX LOCK held for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-25 22:56:36 +01:00
}
}
2011-11-23 09:00:09 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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-11-25 22:56:36 +01:00
void ConnectionSlot : : update ( bool checkForNewClients , int lockedSlotIndex ) {
2011-02-08 19:23:41 +01:00
//Chrono chrono;
//chrono.start();
2011-01-13 09:17:18 +01:00
try {
clearThreadErrorList ( ) ;
2010-03-17 07:25:19 +01:00
2011-11-25 22:56:36 +01:00
if ( slotThreadWorker ! = NULL ) {
slotThreadWorker - > purgeCompletedEvents ( ) ;
}
2010-05-25 20:06:42 +02:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-25 22:56:36 +01:00
pair < bool , Socket * > socketInfo = this - > getSocketInfo ( ) ;
2011-11-25 06:37:55 +01:00
if ( socketInfo . second = = NULL ) {
2010-05-25 20:06:42 +02:00
if ( networkGameDataSynchCheckOkMap ) networkGameDataSynchCheckOkMap = false ;
if ( networkGameDataSynchCheckOkTile ) networkGameDataSynchCheckOkTile = false ;
if ( networkGameDataSynchCheckOkTech ) networkGameDataSynchCheckOkTech = false ;
2010-08-22 10:00:05 +02:00
this - > setReceivedDataSynchCheck ( false ) ;
2010-05-25 20:06:42 +02:00
// Is the listener socket ready to be read?
2012-03-20 21:31:41 +01:00
if ( checkForNewClients = = true & & this - > canAcceptConnections = = true ) {
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] BEFORE accept new client connection, serverInterface->getOpenSlotCount() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getOpenSlotCount ( ) ) ;
2010-05-25 20:06:42 +02:00
bool hasOpenSlots = ( serverInterface - > getOpenSlotCount ( ) > 0 ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
bool hasData = ( serverInterface - > getServerSocket ( ) ! = NULL & & serverInterface - > getServerSocket ( ) - > hasDataToRead ( ) = = true ) ;
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
if ( hasData = = true ) {
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] about to accept new client connection playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2011-11-25 06:37:55 +01:00
2012-01-20 05:15:13 +01:00
Socket * newSocket = serverInterface - > getServerSocket ( ) - > accept ( false ) ;
2011-11-25 06:37:55 +01:00
2012-01-20 05:15:13 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] called accept new client connection playerIndex = %d newSocket = %p \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex , newSocket ) ;
2011-02-06 08:01:54 +01:00
if ( newSocket ! = NULL ) {
// Set Socket as non-blocking
newSocket - > setBlock ( false ) ;
2011-11-23 09:00:09 +01:00
//printf("Got new connection for slot = %d\n",playerIndex);
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( mutexCloseConnection , CODE_AT_LINE ) ;
2011-11-23 09:00:09 +01:00
this - > setSocket ( newSocket ) ;
safeMutex . ReleaseLock ( ) ;
2011-02-06 08:01:54 +01:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-08-21 04:59:21 +02:00
this - > connectedTime = time ( NULL ) ;
this - > clearChatInfo ( ) ;
this - > name = " " ;
2011-03-11 12:11:46 +01:00
this - > playerStatus = npst_PickSettings ;
2011-04-05 20:39:47 +02:00
this - > playerLanguage = " " ;
2010-08-21 04:59:21 +02:00
this - > ready = false ;
this - > vctFileList . clear ( ) ;
this - > receivedNetworkGameStatus = false ;
this - > gotIntro = false ;
2010-12-24 09:43:09 +01:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot1 ( mutexPendingNetworkCommandList , CODE_AT_LINE ) ;
2010-08-21 04:59:21 +02:00
this - > vctPendingNetworkCommandList . clear ( ) ;
2011-11-23 09:00:09 +01:00
safeMutexSlot1 . ReleaseLock ( ) ;
2010-12-24 09:43:09 +01:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-08-21 04:59:21 +02:00
this - > currentFrameCount = 0 ;
this - > currentLagCount = 0 ;
this - > lastReceiveCommandListTime = 0 ;
this - > gotLagCountWarning = false ;
this - > versionString = " " ;
2011-01-11 21:02:07 +01:00
//if(this->slotThreadWorker == NULL) {
// this->slotThreadWorker = new ConnectionSlotThread(this->serverInterface,playerIndex);
// this->slotThreadWorker->setUniqueID(__FILE__);
// this->slotThreadWorker->start();
//}
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-06-28 05:17:50 +02:00
serverInterface - > updateListen ( ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-06-28 05:17:50 +02:00
}
2012-01-20 05:15:13 +01:00
else {
close ( ) ;
return ;
}
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2012-01-20 05:15:13 +01:00
//}
2010-06-29 08:50:35 +02:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-05-25 20:06:42 +02:00
//send intro message when connected
2012-01-20 05:15:13 +01:00
//if(hasData == true && this->isConnected() == true) {
if ( this - > isConnected ( ) = = true ) {
2011-09-25 07:38:35 +02:00
//RandomGen random;
//sessionKey = random.randRange(-100000, 100000);
2011-10-01 03:40:00 +02:00
srand ( time ( NULL ) / ( this - > playerIndex + 1 ) ) ;
2011-09-25 07:38:35 +02:00
sessionKey = rand ( ) % 1000000 ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getOpenSlotCount ( ) , sessionKey ) ;
2010-05-12 17:25:56 +02:00
2010-05-25 20:06:42 +02:00
if ( hasOpenSlots = = false ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] !!!!!!!!WARNING - no open slots, disconnecting client \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
2011-11-25 00:15:21 +01:00
//if(this->getSocket() != NULL) {
2011-04-30 03:05:53 +02:00
NetworkMessageIntro networkMessageIntro ( sessionKey , getNetworkVersionSVNString ( ) , getHostName ( ) , playerIndex , nmgstNoSlots , 0 , ServerSocket : : getFTPServerPort ( ) , " " ) ;
2010-08-20 22:51:25 +02:00
sendMessage ( & networkMessageIntro ) ;
2011-11-25 00:15:21 +01:00
//}
2010-05-12 17:25:56 +02:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-05-25 20:06:42 +02:00
close ( ) ;
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] client will be assigned to the next open slot \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2011-11-25 00:15:21 +01:00
//if(this->getSocket() != NULL) {
2011-04-30 03:05:53 +02:00
NetworkMessageIntro networkMessageIntro ( sessionKey , getNetworkVersionSVNString ( ) , getHostName ( ) , playerIndex , nmgstOk , 0 , ServerSocket : : getFTPServerPort ( ) , " " ) ;
2010-08-20 22:51:25 +02:00
sendMessage ( & networkMessageIntro ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-25 00:15:21 +01:00
//}
2010-05-25 20:06:42 +02:00
}
2012-01-20 05:15:13 +01:00
}
2010-05-12 17:25:56 +02:00
}
}
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-05-25 20:06:42 +02:00
}
else {
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
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 01:46:38 +02:00
2011-11-25 06:37:55 +01:00
if ( socketInfo . first = = true ) {
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2011-11-27 06:27:50 +01:00
this - > clearChatInfo ( ) ;
2010-03-17 07:25:19 +01:00
2011-11-27 06:27:50 +01:00
bool gotTextMsg = true ;
for ( ; this - > hasDataToRead ( ) = = true & & gotTextMsg = = true ; ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] polling for networkMessageType... \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-07 05:26:38 +02:00
2011-11-27 06:27:50 +01:00
NetworkMessageType networkMessageType = getNextMessageType ( ) ;
2010-03-17 07:25: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] networkMessageType = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageType ) ;
2010-05-28 01:46:38 +02:00
2011-11-27 06:27:50 +01:00
gotTextMsg = false ;
2010-06-29 08:50:35 +02:00
//process incoming commands
switch ( networkMessageType ) {
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
case nmtInvalid :
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtInvalid \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-29 08:50:35 +02:00
break ;
2010-03-17 07:25:19 +01:00
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
2010-08-20 22:03:06 +02:00
// client REQUIRES a ping before completing intro
// authentication
//if(gotIntro == true) {
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-08-20 22:03:06 +02:00
lastPingInfo = networkMessagePing ;
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2010-08-20 22:03:06 +02:00
//}
2010-07-09 17:01:49 +02:00
}
break ;
2010-06-29 08:50:35 +02:00
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 gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageText networkMessageText ;
if ( receiveMessage ( & networkMessageText ) ) {
2011-04-05 20:39:47 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) , networkMessageText . getPlayerIndex ( ) , networkMessageText . getTargetLanguage ( ) ) ;
2010-08-20 22:03:06 +02:00
this - > addChatInfo ( msg ) ;
2011-11-27 06:27:50 +01:00
gotTextMsg = true ;
2010-08-20 22:03:06 +02:00
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-06-29 08:50:35 +02:00
}
2010-05-25 20:06:42 +02:00
}
2010-06-29 08:50:35 +02:00
break ;
2010-05-25 20:06:42 +02:00
2010-06-29 08:50:35 +02:00
//command list
case nmtCommandList : {
2010-03-17 07:25: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] got nmtCommandList gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
//throw runtime_error("test");
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageCommandList networkMessageCommandList ;
if ( receiveMessage ( & networkMessageCommandList ) ) {
currentFrameCount = networkMessageCommandList . getFrameCount ( ) ;
lastReceiveCommandListTime = time ( NULL ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] currentFrameCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , currentFrameCount ) ;
2010-06-15 07:36:07 +02:00
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexPendingNetworkCommandList , CODE_AT_LINE ) ;
2011-11-27 06:27:50 +01:00
for ( int i = 0 ; i < networkMessageCommandList . getCommandCount ( ) ; + + i ) {
vctPendingNetworkCommandList . push_back ( * networkMessageCommandList . getCommand ( i ) ) ;
2010-08-20 22:03:06 +02:00
}
2011-11-27 06:27:50 +01:00
safeMutexSlot . ReleaseLock ( ) ;
2010-06-29 08:50:35 +02:00
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-05-25 20:06:42 +02:00
}
2010-03-17 07:25:19 +01:00
}
2010-06-29 08:50:35 +02:00
break ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
//process intro messages
case nmtIntro :
2010-05-25 20:06:42 +02:00
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtIntro \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
NetworkMessageIntro networkMessageIntro ;
2010-08-21 04:59:21 +02:00
if ( receiveMessage ( & networkMessageIntro ) ) {
2010-08-23 17:40:43 +02:00
int msgSessionId = networkMessageIntro . getSessionId ( ) ;
2011-04-05 20:39:47 +02:00
this - > name = networkMessageIntro . getName ( ) ;
this - > versionString = networkMessageIntro . getVersionString ( ) ;
2010-12-30 16:56:02 +01:00
this - > connectedRemoteIPAddress = networkMessageIntro . getExternalIp ( ) ;
2011-04-05 20:39:47 +02:00
this - > playerLanguage = networkMessageIntro . getPlayerLanguage ( ) ;
2010-03-17 07:25:19 +01:00
2011-04-05 22:19:25 +02:00
//printf("\n\n\n ##### GOT this->playerLanguage [%s]\n\n\n",this->playerLanguage.c_str());
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got name [%s] versionString [%s], msgSessionId = %d \n " , __FILE__ , __FUNCTION__ , name . c_str ( ) , versionString . c_str ( ) , msgSessionId ) ;
2010-08-23 17:40:43 +02:00
if ( msgSessionId ! = sessionKey ) {
string playerNameStr = name ;
2011-11-23 09:00:09 +01:00
string sErr = " Client gave invalid sessionid for player [ " + playerNameStr + " ] actual [ " + intToStr ( msgSessionId ) + " ] expected [ " + intToStr ( sessionKey ) + " ] " ;
2010-08-23 17:40:43 +02:00
printf ( " %s \n " , sErr . c_str ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
2010-08-23 17:40:43 +02:00
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-08-23 17:40:43 +02:00
}
2011-11-23 09:00:09 +01:00
else {
//check consistency
bool compatible = checkVersionComptability ( getNetworkVersionSVNString ( ) , networkMessageIntro . getVersionString ( ) ) ;
if ( compatible = = false ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
bool versionMatched = false ;
string platformFreeVersion = getNetworkPlatformFreeVersionString ( ) ;
string sErr = " " ;
if ( strncmp ( platformFreeVersion . c_str ( ) , networkMessageIntro . getVersionString ( ) . c_str ( ) , strlen ( platformFreeVersion . c_str ( ) ) ) ! = 0 ) {
string playerNameStr = name ;
sErr = " Server and client binary mismatch! \n You have to use the exactly same binaries! \n \n Server: " + getNetworkVersionSVNString ( ) +
" \n Client: " + networkMessageIntro . getVersionString ( ) + " player [ " + playerNameStr + " ] " ;
printf ( " %s \n " , sErr . c_str ( ) ) ;
serverInterface - > sendTextMessage ( " Server and client binary mismatch!! " , - 1 , true , " " , lockedSlotIndex ) ;
serverInterface - > sendTextMessage ( " Server: " + getNetworkVersionSVNString ( ) , - 1 , true , " " , lockedSlotIndex ) ;
serverInterface - > sendTextMessage ( " Client: " + networkMessageIntro . getVersionString ( ) , - 1 , true , " " , lockedSlotIndex ) ;
serverInterface - > sendTextMessage ( " Client player [ " + playerNameStr + " ] " , - 1 , true , " " , lockedSlotIndex ) ;
}
else {
versionMatched = true ;
2010-03-17 07:25:19 +01:00
2011-11-23 09:00:09 +01:00
string playerNameStr = name ;
sErr = " Warning, Server and client are using the same version but different platforms. \n \n Server: " + getNetworkVersionSVNString ( ) +
" \n Client: " + networkMessageIntro . getVersionString ( ) + " player [ " + playerNameStr + " ] " ;
//printf("%s\n",sErr.c_str());
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
}
2010-08-21 04:59:21 +02:00
2011-11-23 09:00:09 +01:00
if ( Config : : getInstance ( ) . getBool ( " PlatformConsistencyChecks " , " true " ) & &
versionMatched = = false ) { // error message and disconnect only if checked
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
close ( ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2010-08-21 04:59:21 +02:00
}
2011-11-23 09:00:09 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
gotIntro = true ;
2010-08-21 04:59:21 +02:00
2011-11-23 09:00:09 +01:00
this - > serverInterface - > addClientToServerIPAddress ( this - > getSocket ( ) - > getConnectedIPAddress ( this - > getSocket ( ) - > getIpAddress ( ) ) , this - > connectedRemoteIPAddress ) ;
2010-12-30 16:56:02 +01:00
2011-11-23 09:00:09 +01:00
if ( getAllowGameDataSynchCheck ( ) = = true & & serverInterface - > getGameSettings ( ) ! = NULL ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] sending NetworkMessageSynchNetworkGameData \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-25 20:06:42 +02:00
2011-11-23 09:00:09 +01:00
NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData ( serverInterface - > getGameSettings ( ) ) ;
sendMessage ( & networkMessageSynchNetworkGameData ) ;
}
2010-05-25 20:06:42 +02:00
}
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2010-06-29 08:50:35 +02:00
}
break ;
2010-05-25 20:06:42 +02:00
2011-09-24 09:46:56 +02:00
case nmtLaunch :
case nmtBroadCastSetup :
{
2011-11-23 09:00:09 +01:00
if ( gotIntro = = true ) {
if ( this - > serverInterface - > getGameSettings ( ) = = NULL | |
sessionKey ! = this - > serverInterface - > getGameSettings ( ) - > getMasterserver_admin ( ) ) {
string playerNameStr = name ;
string sErr = " Client has invalid admin sessionid for player [ " + playerNameStr + " ] " ;
printf ( " %s \n " , sErr . c_str ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
NetworkMessageLaunch networkMessageLaunch ;
if ( receiveMessage ( & networkMessageLaunch ) ) {
if ( networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got nmtLaunch \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
else if ( networkMessageLaunch . getMessageType ( ) = = nmtBroadCastSetup ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Lined: %d] got nmtBroadCastSetup \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
else {
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 ( ) ) ;
char szBuf [ 1024 ] = " " ;
snprintf ( szBuf , 1023 , " In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageLaunch . getMessageType ( ) ) ;
throw runtime_error ( szBuf ) ;
}
2012-01-20 05:15:13 +01:00
GameSettings gameSettingsBuffer ;
networkMessageLaunch . buildGameSettings ( & gameSettingsBuffer ) ;
2011-09-24 09:46:56 +02:00
2011-11-23 09:00:09 +01:00
//printf("Connection slot got networkMessageLaunch.getMessageType() = %d, got map [%s]\n",networkMessageLaunch.getMessageType(),gameSettings.getMap().c_str());
//printf("\n\n\n\n=====Connection slot got settings:\n%s\n",gameSettings.toString().c_str());
2012-01-20 05:15:13 +01:00
//this->serverInterface->setGameSettings(&gameSettingsBuffer,false);
this - > serverInterface - > broadcastGameSetup ( & gameSettingsBuffer , true ) ;
2011-11-23 09:00:09 +01:00
if ( networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
this - > serverInterface - > setMasterserverAdminRequestLaunch ( true ) ;
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
2011-09-24 09:46:56 +02:00
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2011-09-24 09:46:56 +02:00
}
break ;
2010-06-29 08:50:35 +02:00
//process datasynch messages
case nmtSynchNetworkGameDataStatus :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtSynchNetworkGameDataStatus, gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-05-25 20:06:42 +02:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = 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-08-22 10:00:05 +02:00
2010-08-20 22:03:06 +02:00
NetworkMessageSynchNetworkGameDataStatus networkMessageSynchNetworkGameDataStatus ;
2010-08-22 10:00:05 +02:00
if ( receiveMessage ( & networkMessageSynchNetworkGameDataStatus ) ) {
this - > setNetworkGameDataSynchCheckTechMismatchReport ( " " ) ;
this - > setReceivedDataSynchCheck ( false ) ;
2010-08-20 22:03:06 +02:00
Config & config = Config : : getInstance ( ) ;
string scenarioDir = " " ;
if ( serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) ! = " " ) {
scenarioDir = serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) ;
if ( EndsWith ( scenarioDir , " .xml " ) = = true ) {
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - 4 , 4 ) ;
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - serverInterface - > getGameSettings ( ) - > getScenario ( ) . size ( ) , serverInterface - > getGameSettings ( ) - > getScenario ( ) . size ( ) + 1 ) ;
}
2010-05-25 20:06:42 +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] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) . c_str ( ) , serverInterface - > getGameSettings ( ) - > getScenario ( ) . c_str ( ) , scenarioDir . c_str ( ) ) ;
2010-08-20 22:03:06 +02:00
}
2010-06-29 08:50:35 +02:00
2010-08-20 22:03:06 +02:00
//tileset
int32 tilesetCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + serverInterface - > getGameSettings ( ) - > getTileset ( ) + string ( " /* " ) , " .xml " , NULL ) ;
int32 techCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , NULL ) ;
Checksum checksum ;
2010-09-17 10:26:22 +02:00
string file = Map : : getMapPath ( serverInterface - > getGameSettings ( ) - > getMap ( ) , scenarioDir , false ) ;
2010-08-20 22:03:06 +02:00
checksum . addFile ( file ) ;
int32 mapCRC = checksum . getSum ( ) ;
networkGameDataSynchCheckOkMap = ( networkMessageSynchNetworkGameDataStatus . getMapCRC ( ) = = mapCRC ) ;
networkGameDataSynchCheckOkTile = ( networkMessageSynchNetworkGameDataStatus . getTilesetCRC ( ) = = tilesetCRC ) ;
networkGameDataSynchCheckOkTech = ( networkMessageSynchNetworkGameDataStatus . getTechCRC ( ) = = techCRC ) ;
// For testing
//techCRC++;
if ( networkGameDataSynchCheckOkMap = = true & &
networkGameDataSynchCheckOkTile = = true & &
networkGameDataSynchCheckOkTech = = true ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] client data synch ok \n " , __FILE__ , __FUNCTION__ ) ;
2010-08-20 22:03:06 +02:00
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] mapCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , mapCRC , networkMessageSynchNetworkGameDataStatus . getMapCRC ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] tilesetCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , tilesetCRC , networkMessageSynchNetworkGameDataStatus . getTilesetCRC ( ) ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] techCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , techCRC , networkMessageSynchNetworkGameDataStatus . getTechCRC ( ) ) ;
2010-08-20 22:03:06 +02:00
if ( allowDownloadDataSynch = = true ) {
// Now get all filenames with their CRC values and send to the client
vctFileList . clear ( ) ;
Config & config = Config : : getInstance ( ) ;
string scenarioDir = " " ;
if ( serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) ! = " " ) {
scenarioDir = serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) ;
if ( EndsWith ( scenarioDir , " .xml " ) = = true ) {
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - 4 , 4 ) ;
scenarioDir = scenarioDir . erase ( scenarioDir . size ( ) - serverInterface - > getGameSettings ( ) - > getScenario ( ) . size ( ) , serverInterface - > getGameSettings ( ) - > 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__ , serverInterface - > getGameSettings ( ) - > getScenarioDir ( ) . c_str ( ) , serverInterface - > getGameSettings ( ) - > getScenario ( ) . c_str ( ) , scenarioDir . c_str ( ) ) ;
2010-06-29 08:50:35 +02:00
}
2010-08-20 22:03:06 +02:00
if ( networkGameDataSynchCheckOkTile = = false ) {
if ( tilesetCRC = = 0 ) {
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + serverInterface - > getGameSettings ( ) - > getTileset ( ) + string ( " /* " ) , " " , & vctFileList ) ;
}
else {
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTileset ( ) + " /* " , " .xml " , & vctFileList ) ;
}
2010-06-29 08:50:35 +02:00
}
2010-08-20 22:03:06 +02:00
if ( networkGameDataSynchCheckOkTech = = false ) {
if ( techCRC = = 0 ) {
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " " , & vctFileList ) ;
}
else {
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , & vctFileList ) ;
}
2010-08-22 10:00:05 +02:00
2010-08-26 05:15:36 +02:00
string report = networkMessageSynchNetworkGameDataStatus . getTechCRCFileMismatchReport ( serverInterface - > getGameSettings ( ) - > getTech ( ) , vctFileList ) ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( report ) ;
2010-06-29 08:50:35 +02:00
}
2010-08-20 22:03:06 +02:00
if ( networkGameDataSynchCheckOkMap = = false ) {
2010-09-17 10:26:22 +02:00
vctFileList . push_back ( std : : pair < string , int32 > ( Map : : getMapPath ( serverInterface - > getGameSettings ( ) - > getMap ( ) , scenarioDir , false ) , mapCRC ) ) ;
2010-06-29 08:50:35 +02:00
}
2010-05-25 20:06:42 +02:00
2010-08-20 22:03:06 +02:00
//for(int i = 0; i < vctFileList.size(); i++)
//{
NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck ( vctFileList . size ( ) , 1 , vctFileList [ 0 ] . second , vctFileList [ 0 ] . first ) ;
sendMessage ( & networkMessageSynchNetworkGameDataFileCRCCheck ) ;
//}
}
2010-08-22 10:00:05 +02:00
else {
if ( networkGameDataSynchCheckOkTech = = false ) {
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , NULL ) ;
2010-08-26 05:15:36 +02:00
string report = networkMessageSynchNetworkGameDataStatus . getTechCRCFileMismatchReport ( serverInterface - > getGameSettings ( ) - > getTech ( ) , vctFileList ) ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( report ) ;
}
}
2010-06-29 08:50:35 +02:00
}
2010-08-22 10:00:05 +02:00
this - > setReceivedDataSynchCheck ( true ) ;
receivedNetworkGameStatus = 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-05-25 20:06:42 +02:00
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-05-25 20:06:42 +02:00
}
}
2010-06-29 08:50:35 +02:00
break ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
case nmtSynchNetworkGameDataFileCRCCheck :
{
2010-03-17 07:25:19 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck ;
if ( receiveMessage ( & networkMessageSynchNetworkGameDataFileCRCCheck ) )
{
int fileIndex = networkMessageSynchNetworkGameDataFileCRCCheck . getFileIndex ( ) ;
NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck ( vctFileList . size ( ) , fileIndex , vctFileList [ fileIndex - 1 ] . second , vctFileList [ fileIndex - 1 ] . first ) ;
sendMessage ( & networkMessageSynchNetworkGameDataFileCRCCheck ) ;
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-06-29 08:50:35 +02:00
}
2010-05-25 20:06:42 +02:00
}
2010-06-29 08:50:35 +02:00
break ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
case nmtSynchNetworkGameDataFileGet :
{
2010-03-17 07:25:19 +01:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtSynchNetworkGameDataFileGet \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageSynchNetworkGameDataFileGet networkMessageSynchNetworkGameDataFileGet ;
if ( receiveMessage ( & networkMessageSynchNetworkGameDataFileGet ) ) {
FileTransferInfo fileInfo ;
fileInfo . hostType = eServer ;
//fileInfo.serverIP = this->ip.getString();
fileInfo . serverPort = Config : : getInstance ( ) . getInt ( " ServerPort " , intToStr ( GameConstants : : serverPort ) . c_str ( ) ) ;
fileInfo . fileName = networkMessageSynchNetworkGameDataFileGet . getFileName ( ) ;
FileTransferSocketThread * fileXferThread = new FileTransferSocketThread ( fileInfo ) ;
fileXferThread - > start ( ) ;
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-06-29 08:50:35 +02:00
}
2010-05-25 20:06:42 +02:00
}
2010-06-29 08:50:35 +02:00
break ;
2010-05-25 20:06:42 +02:00
2010-06-29 08:50:35 +02:00
case nmtSwitchSetupRequest :
{
2011-09-01 23:54:31 +02:00
//printf("Got nmtSwitchSetupRequest A\n");
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-08-23 06:33:21 +02:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
2011-09-01 23:54:31 +02:00
//printf("Got nmtSwitchSetupRequest B\n");
2010-08-20 22:03:06 +02:00
SwitchSetupRequest switchSetupRequest ;
if ( receiveMessage ( & switchSetupRequest ) ) {
2011-11-23 09:00:09 +01:00
MutexSafeWrapper safeMutex ( getServerSynchAccessor ( ) , CODE_AT_LINE ) ;
2010-08-20 22:03:06 +02:00
2010-08-23 06:33:21 +02:00
int factionIdx = switchSetupRequest . getCurrentFactionIndex ( ) ;
if ( serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] = = NULL ) {
serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] = new SwitchSetupRequest ( ) ;
2010-08-20 22:03:06 +02:00
}
2010-08-23 06:33:21 +02:00
* ( serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] ) = switchSetupRequest ;
2011-03-11 12:11:46 +01:00
this - > playerStatus = switchSetupRequest . getNetworkPlayerStatus ( ) ;
2011-03-26 17:51:26 +01:00
this - > name = switchSetupRequest . getNetworkPlayerName ( ) ;
2011-04-05 20:39:47 +02:00
this - > playerLanguage = switchSetupRequest . getNetworkPlayerLanguage ( ) ;
2011-03-26 17:51:26 +01:00
2011-09-01 23:54:31 +02:00
//printf("Got nmtSwitchSetupRequest C\n");
//printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[factionIdx]->getNetworkPlayerName().c_str());
2011-03-26 17:51:26 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line %d] networkPlayerName [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] - > getNetworkPlayerName ( ) . c_str ( ) ) ;
2011-03-11 12:11:46 +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] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getNetworkPlayerStatus() = %d, switchSetupRequest.getSwitchFlags() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdx , switchSetupRequest . getNetworkPlayerName ( ) . c_str ( ) , switchSetupRequest . getNetworkPlayerStatus ( ) , switchSetupRequest . getSwitchFlags ( ) ) ;
2010-06-29 08:50:35 +02:00
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2010-06-29 08:50:35 +02:00
}
2011-11-23 09:00:09 +01:00
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2011-11-23 09:00:09 +01:00
}
2010-06-29 08:50:35 +02:00
break ;
2010-05-13 08:43:56 +02:00
}
2010-06-29 08:50:35 +02:00
case nmtReady :
2010-05-25 20:06:42 +02:00
{
2010-06-29 08:50:35 +02:00
// its simply ignored here. Probably we are starting a game
break ;
2010-05-13 08:43:56 +02:00
}
2011-03-31 20:13:02 +02:00
case nmtLoadingStatusMessage :
break ;
2010-06-29 08:50:35 +02:00
default :
{
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] networkMessageType = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageType ) ;
2010-06-29 08:50:35 +02:00
if ( gotIntro = = true ) {
//throw runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType));
string sErr = " Unexpected message in connection slot: " + intToStr ( networkMessageType ) ;
//sendTextMessage(sErr,-1);
//DisplayErrorMessage(sErr);
threadErrorList . push_back ( sErr ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-06-29 08:50:35 +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] got invalid message type before intro, disconnecting socket. \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-23 09:00:09 +01:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugError ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] \n Invalid message type before intro handshake [%d] \n Disconnecting socket for slot: %d [%s]. \n " , extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) , __FUNCTION__ , __LINE__ , networkMessageType , this - > playerIndex , this - > getIpAddress ( ) . c_str ( ) ) ;
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
2010-06-29 08:50:35 +02:00
close ( ) ;
2011-11-25 22:56:36 +01:00
return ;
2010-06-29 08:50:35 +02:00
}
}
}
2011-11-25 22:56:36 +01:00
}
2010-05-25 20:06:42 +02:00
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-08-20 22:03:06 +02:00
validateConnection ( ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-03-17 07:25:19 +01:00
}
2010-05-25 20:06:42 +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] calling close... \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
close ( ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-05-12 17:25:56 +02:00
}
2010-06-29 08:50:35 +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-03-17 07:25:19 +01:00
}
2010-05-25 20:06:42 +02:00
}
catch ( const exception & 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 ( ) ) ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] error detected [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-03-17 07:25:19 +01:00
2010-05-25 20:06:42 +02:00
threadErrorList . push_back ( ex . what ( ) ) ;
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-03-17 07:25:19 +01:00
}
2011-02-08 19:23:41 +01:00
//if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis());
2010-03-17 07:25:19 +01:00
}
2010-08-20 22:03:06 +02:00
void ConnectionSlot : : validateConnection ( ) {
2012-01-17 07:02:54 +01:00
if ( this - > isConnected ( ) = = true & &
gotIntro = = false & & connectedTime > 0 & &
2010-08-20 22:03:06 +02:00
difftime ( time ( NULL ) , connectedTime ) > GameConstants : : maxClientConnectHandshakeSecs ) {
close ( ) ;
}
}
2010-05-15 20:59:17 +02:00
void ConnectionSlot : : 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-28 01:46:38 +02:00
2011-01-11 21:02:07 +01:00
if ( this - > slotThreadWorker ! = 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__ ) ;
2011-01-11 21:02:07 +01:00
this - > slotThreadWorker - > setAllEventsCompleted ( ) ;
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-01-11 19:39:18 +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__ ) ;
2011-01-11 19:39:18 +01:00
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( mutexCloseConnection , CODE_AT_LINE ) ;
2010-03-17 07:25:19 +01:00
2011-11-23 09:00:09 +01:00
bool updateServerListener = ( this - > getSocket ( ) ! = NULL ) ;
this - > deleteSocket ( ) ;
2011-05-20 21:44:33 +02:00
safeMutex . ReleaseLock ( ) ;
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 01:46:38 +02:00
2010-06-04 01:00:31 +02:00
if ( updateServerListener = = true & & ready = = 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-05-28 16:59:09 +02:00
serverInterface - > updateListen ( ) ;
}
2010-05-12 17:25:56 +02:00
ready = false ;
gotIntro = false ;
2012-01-17 07:02:54 +01:00
connectedTime = 0 ;
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] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-21 18:36:08 +02:00
Mutex * ConnectionSlot : : getServerSynchAccessor ( ) {
return ( serverInterface ! = NULL ? serverInterface - > getServerSynchAccessor ( ) : NULL ) ;
}
2010-05-28 01:46:38 +02:00
void ConnectionSlot : : signalUpdate ( ConnectionSlotEvent * event ) {
2011-01-11 21:02:07 +01:00
//assert(slotThreadWorker != NULL);
if ( slotThreadWorker ! = NULL ) {
slotThreadWorker - > signalUpdate ( event ) ;
}
2010-05-28 01:46:38 +02:00
}
2010-12-25 09:14:35 +01:00
bool ConnectionSlot : : updateCompleted ( ConnectionSlotEvent * event ) {
2010-08-21 03:52:41 +02:00
bool waitingForThread = ( slotThreadWorker ! = NULL & &
2010-12-25 09:14:35 +01:00
slotThreadWorker - > isSignalCompleted ( event ) = = false & &
slotThreadWorker - > getQuitStatus ( ) = = false & &
slotThreadWorker - > getRunningStatus ( ) = = true ) ;
2010-06-28 05:17:50 +02:00
2010-05-28 01:46:38 +02:00
return ( waitingForThread = = false ) ;
}
2010-06-03 09:52:17 +02:00
void ConnectionSlot : : sendMessage ( const NetworkMessage * networkMessage ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutex ( socketSynchAccessor , CODE_AT_LINE ) ;
2011-04-05 22:19:25 +02:00
// Skip text messages not intended for the players preferred language
const NetworkMessageText * textMsg = dynamic_cast < const NetworkMessageText * > ( networkMessage ) ;
if ( textMsg ! = NULL ) {
//printf("\n\n\n~~~ SERVER HAS NetworkMessageText target [%s] player [%s] msg[%s]\n\n\n",textMsg->getTargetLanguage().c_str(),this->getNetworkPlayerLanguage().c_str(), textMsg->getText().c_str());
if ( textMsg - > getTargetLanguage ( ) ! = " " & &
textMsg - > getTargetLanguage ( ) ! = this - > getNetworkPlayerLanguage ( ) ) {
return ;
}
}
2010-06-03 09:52:17 +02:00
NetworkInterface : : sendMessage ( networkMessage ) ;
}
2010-08-21 15:04:52 +02:00
string ConnectionSlot : : getHumanPlayerName ( int index ) {
return serverInterface - > getHumanPlayerName ( index ) ;
}
2010-12-24 09:43:09 +01:00
vector < NetworkCommand > ConnectionSlot : : getPendingNetworkCommandList ( bool clearList ) {
2011-02-15 08:49:40 +01:00
vector < NetworkCommand > ret ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexPendingNetworkCommandList , CODE_AT_LINE ) ;
2011-09-01 03:11:23 +02:00
if ( vctPendingNetworkCommandList . empty ( ) = = false ) {
2011-02-15 08:49:40 +01:00
ret = vctPendingNetworkCommandList ;
if ( clearList = = true ) {
vctPendingNetworkCommandList . clear ( ) ;
}
2010-12-24 09:43:09 +01:00
}
safeMutexSlot . ReleaseLock ( ) ;
return ret ;
}
void ConnectionSlot : : clearPendingNetworkCommandList ( ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexPendingNetworkCommandList , CODE_AT_LINE ) ;
2011-09-01 03:11:23 +02:00
if ( vctPendingNetworkCommandList . empty ( ) = = false ) {
2011-02-15 08:49:40 +01:00
vctPendingNetworkCommandList . clear ( ) ;
}
safeMutexSlot . ReleaseLock ( ) ;
2010-12-24 09:43:09 +01:00
}
2011-11-27 06:27:50 +01:00
bool ConnectionSlot : : hasValidSocketId ( ) {
//bool result = (this->getSocket() != NULL && this->getSocket()->getSocketId() > 0);
//return result;
bool result = false ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-27 06:27:50 +01:00
if ( socket ! = NULL & & socket - > getSocketId ( ) > 0 ) {
result = true ;
}
return result ;
}
2011-11-25 06:37:55 +01:00
2011-11-23 09:00:09 +01:00
bool ConnectionSlot : : isConnected ( ) {
bool result = false ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-23 09:00:09 +01:00
if ( socket ! = NULL & & socket - > isConnected ( ) = = true ) {
result = true ;
}
return result ;
}
2011-11-25 00:15:21 +01:00
PLATFORM_SOCKET ConnectionSlot : : getSocketId ( ) {
PLATFORM_SOCKET result = 0 ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-25 00:15:21 +01:00
if ( socket ! = NULL ) {
result = socket - > getSocketId ( ) ;
}
return result ;
}
2011-11-25 06:37:55 +01:00
pair < bool , Socket * > ConnectionSlot : : getSocketInfo ( ) {
pair < bool , Socket * > result ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-25 06:37:55 +01:00
result . first = ( socket ! = NULL & & socket - > isConnected ( ) ) ;
result . second = socket ;
return result ;
}
2011-11-25 10:12:53 +01:00
Socket * ConnectionSlot : : getSocket ( bool mutexLock ) {
2011-11-27 06:27:50 +01:00
MutexSafeWrapper safeMutexSlot ( NULL , CODE_AT_LINE ) ;
2011-11-25 10:12:53 +01:00
if ( mutexLock = = true ) {
2011-12-02 17:07:59 +01:00
safeMutexSlot . setMutex ( mutexSocket , CODE_AT_LINE ) ;
2011-11-25 10:12:53 +01:00
}
2011-11-27 06:27:50 +01:00
return socket ;
2011-11-23 09:00:09 +01:00
}
void ConnectionSlot : : setSocket ( Socket * newSocket ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-23 09:00:09 +01:00
socket = newSocket ;
}
void ConnectionSlot : : deleteSocket ( ) {
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-23 09:00:09 +01:00
delete socket ;
socket = NULL ;
}
2011-11-25 00:15:21 +01:00
bool ConnectionSlot : : hasDataToRead ( ) {
bool result = false ;
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-25 00:15:21 +01:00
if ( socket ! = NULL & & socket - > hasDataToRead ( ) = = true ) {
result = true ;
}
return result ;
}
2010-03-17 07:25:19 +01:00
} } //end namespace