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 ( ) {
2013-01-10 22:16:28 +01:00
this - > masterController = NULL ;
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 ;
2013-05-21 22:28:42 +02:00
uniqueID = " ConnectionSlotThread " ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2011-02-15 08:49:40 +01:00
eventList . reserve ( 100 ) ;
2013-06-04 02:31:41 +02:00
triggerGameStarted = new Mutex ( ) ;
gameStarted = false ;
2010-05-28 01:46:38 +02:00
}
2011-11-27 06:27:50 +01:00
ConnectionSlotThread : : ConnectionSlotThread ( ConnectionSlotCallbackInterface * slotInterface , int slotIndex ) : BaseThread ( ) {
2013-01-10 22:16:28 +01:00
this - > masterController = NULL ;
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 ;
2013-05-21 22:28:42 +02:00
uniqueID = " ConnectionSlotThread " ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2013-06-04 02:31:41 +02:00
triggerGameStarted = new Mutex ( ) ;
gameStarted = false ;
2010-05-28 01:46:38 +02:00
}
2011-12-02 17:07:59 +01:00
ConnectionSlotThread : : ~ ConnectionSlotThread ( ) {
delete triggerIdMutex ;
triggerIdMutex = NULL ;
2013-06-04 02:31:41 +02:00
delete triggerGameStarted ;
triggerGameStarted = NULL ;
2011-12-02 17:07:59 +01:00
}
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
}
2013-06-04 02:31:41 +02:00
if ( getGameStarted ( ) = = true & & getQuitStatus ( ) = = true ) {
return ;
}
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 ) ;
2013-06-04 02:31:41 +02:00
deleteSelfIfRequired ( ) ;
2011-01-11 19:39:18 +01:00
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__ ) ;
}
}
}
2013-01-10 22:16:28 +01:00
void ConnectionSlotThread : : signalSlave ( void * userdata ) {
//ConnectionSlotEvent &event = eventList[i];
std : : map < int , ConnectionSlotEvent > * eventList = ( std : : map < int , ConnectionSlotEvent > * ) userdata ;
ConnectionSlotEvent & event = ( * eventList ) [ slotIndex ] ;
signalUpdate ( & event ) ;
}
2013-06-04 02:31:41 +02:00
bool ConnectionSlotThread : : getGameStarted ( ) {
MutexSafeWrapper safeMutexGameStarted ( triggerGameStarted , CODE_AT_LINE ) ;
return gameStarted ;
}
void ConnectionSlotThread : : setGameStarted ( bool value ) {
MutexSafeWrapper safeMutexGameStarted ( triggerGameStarted , CODE_AT_LINE ) ;
if ( gameStarted ! = value ) {
gameStarted = value ;
if ( gameStarted = = true ) {
//printf("Signal game has started for slot: %d\n",slotIndex);
semTaskSignalled . signal ( ) ;
}
else {
//printf("Signal game has NOT started for slot: %d\n",slotIndex);
}
}
}
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
2013-06-04 02:31:41 +02:00
//printf("Starting client SLOT thread: %d\n",slotIndex);
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 ;
}
2013-03-01 07:52:33 +01:00
if ( this - > slotInterface - > getAllowInGameConnections ( ) = = true & &
this - > slotInterface - > isClientConnected ( slotIndex ) = = false ) {
//printf("#1 Non connected slot: %d waiting for client connection..\n",slotIndex);
sleep ( 100 ) ;
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
2013-03-01 07:52:33 +01:00
//printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount);
//if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,eventCount);
2013-01-10 22:16:28 +01:00
2013-03-01 07:52:33 +01:00
//if(eventCount > 0) {
ConnectionSlotEvent eventCopy ;
eventCopy . eventType = eReceiveSocketData ;
eventCopy . connectionSlot = this - > slotInterface - > getSlot ( slotIndex ) ;
eventCopy . eventId = - 1 ;
if ( getQuitStatus ( ) = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
//if(eventCopy.eventId > 0) {
ExecutingTaskSafeWrapper safeExecutingTaskMutex ( this ) ;
//if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,eventCount,(int)eventCopy.eventId);
//printf("#1 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId);
//this->slotInterface->slotUpdateTask(&eventCopy);
//printf("#2 Non connected slot: %d waiting for client connection..\n",slotIndex);
this - > slotUpdateTask ( & eventCopy ) ;
//setTaskCompleted(eventCopy.eventId);
//printf("#2 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId);
//}
//}
//else {
// safeMutex.ReleaseLock();
//}
2011-11-25 22:56:36 +01:00
}
2013-03-01 07:52:33 +01:00
else {
2013-06-04 02:31:41 +02:00
if ( getGameStarted ( ) = = true ) {
//printf("#A Checking action for slot: %d\n",slotIndex);
2010-05-28 01:46:38 +02:00
2013-06-04 02:31:41 +02:00
if ( getQuitStatus ( ) = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
ExecutingTaskSafeWrapper safeExecutingTaskMutex ( this ) ;
// If the slot or socket are NULL the connection was lost
// so exit the thread
ConnectionSlot * slot = this - > slotInterface - > getSlot ( slotIndex ) ;
if ( slot = = NULL ) {
break ;
}
Socket * socket = slot - > getSocket ( true ) ;
if ( socket = = NULL ) {
break ;
}
ConnectionSlotEvent eventCopy ;
eventCopy . eventType = eReceiveSocketData ;
eventCopy . connectionSlot = slot ;
eventCopy . eventId = slotIndex ;
eventCopy . socketTriggered = socket - > hasDataToReadWithWait ( 5000 ) ;
//eventCopy.socketTriggered = true;
if ( getQuitStatus ( ) = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
//printf("#C Checking action for slot: %d\n",slotIndex);
this - > slotUpdateTask ( & eventCopy ) ;
//printf("#D Checking action for slot: %d\n",slotIndex);
2013-03-01 07:52:33 +01:00
}
2013-06-04 02:31:41 +02:00
else {
//printf("#1 Checking action for slot: %d\n",slotIndex);
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
if ( getGameStarted ( ) = = true ) {
continue ;
}
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
//printf("#2 Checking action for slot: %d\n",slotIndex);
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
semTaskSignalled . waitTillSignalled ( ) ;
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
//printf("#3 Checking action for slot: %d\n",slotIndex);
if ( getGameStarted ( ) = = true ) {
continue ;
2013-03-01 07:52:33 +01:00
}
2013-06-04 02:31:41 +02:00
//printf("#4 Checking action for slot: %d\n",slotIndex);
static string masterSlaveOwnerId = string ( __FILE__ ) + string ( " _ " ) + intToStr ( __LINE__ ) ;
MasterSlaveThreadControllerSafeWrapper safeMasterController ( masterController , 20000 , masterSlaveOwnerId ) ;
2013-03-01 07:52:33 +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 ;
}
2013-06-04 02:31:41 +02:00
MutexSafeWrapper safeMutex ( triggerIdMutex , CODE_AT_LINE ) ;
int eventCount = eventList . size ( ) ;
//printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount);
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , slotIndex , eventCount ) ;
if ( eventCount > 0 ) {
ConnectionSlotEvent eventCopy ;
eventCopy . eventId = - 1 ;
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = false ) {
eventCopy = slotEvent ;
break ;
}
}
safeMutex . ReleaseLock ( ) ;
2013-03-01 07:52:33 +01:00
2013-06-04 02:31:41 +02:00
if ( getQuitStatus ( ) = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
if ( eventCopy . eventId > 0 ) {
ExecutingTaskSafeWrapper safeExecutingTaskMutex ( this ) ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , slotIndex , eventCount , ( int ) eventCopy . eventId ) ;
//printf("#1 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId);
//this->slotInterface->slotUpdateTask(&eventCopy);
this - > slotUpdateTask ( & eventCopy ) ;
setTaskCompleted ( eventCopy . eventId ) ;
//printf("#2 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId);
}
}
else {
safeMutex . ReleaseLock ( ) ;
2013-03-01 07:52:33 +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 ;
}
}
2013-06-04 02:31:41 +02:00
//printf("Ending client SLOT thread: %d\n",slotIndex);
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
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( ex . what ( ) ) ;
2010-05-28 01:46:38 +02:00
}
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-28 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 ( ) ;
2012-09-22 22:13:57 +02:00
this - > socket = NULL ;
2011-12-02 17:07:59 +01:00
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 ;
2012-09-21 17:03:13 +02:00
this - > playerStatus = npst_None ;
2011-04-05 20:39:47 +02:00
this - > playerLanguage = " " ;
2013-06-01 04:31:12 +02:00
this - > playerUUID = " " ;
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 ;
2013-03-14 22:41:15 +01:00
this - > skipLagCheck = false ;
this - > joinGameInProgress = false ;
2012-03-20 21:31:41 +01:00
this - > canAcceptConnections = true ;
2013-02-19 23:00:15 +01:00
this - > startInGameConnectionLaunch = false ;
2013-03-14 22:41:15 +01:00
this - > pauseForInGameConnection = false ;
2013-03-09 21:57:06 +01:00
this - > unPauseForInGameConnection = false ;
2013-03-14 22:41:15 +01:00
this - > sentSavedGameInfo = false ;
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 ;
2013-01-23 15:51:28 +01:00
static string mutexOwnerId = string ( extractFileFromDirectoryPath ( __FILE__ ) . c_str ( ) ) + string ( " _ " ) + intToStr ( __LINE__ ) ;
2011-11-27 06:27:50 +01:00
this - > slotThreadWorker = new ConnectionSlotThread ( this - > serverInterface , playerIndex ) ;
2013-01-23 15:51:28 +01:00
this - > slotThreadWorker - > setUniqueID ( mutexOwnerId ) ;
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
2013-02-15 19:25:10 +01:00
//printf("Deleting connection slot\n");
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
2013-06-04 02:31:41 +02:00
//printf("#1 Ending client SLOT: %d slotThreadWorker: %p\n",playerIndex,slotThreadWorker);
2013-06-04 05:47:04 +02:00
if ( slotThreadWorker ! = NULL ) {
slotThreadWorker - > signalQuit ( ) ;
}
if ( slotThreadWorker ! = NULL & & slotThreadWorker - > canShutdown ( false ) = = true & &
slotThreadWorker - > getRunningStatus ( ) = = false ) {
2013-06-04 02:31:41 +02:00
//printf("#2 Ending client SLOT: %d\n",playerIndex);
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
delete slotThreadWorker ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
//else if(BaseThread::shutdownAndWait(slotThreadWorker) == true) {
2013-06-04 05:47:04 +02:00
else if ( slotThreadWorker ! = NULL & & slotThreadWorker - > canShutdown ( true ) = = true ) {
if ( slotThreadWorker - > getRunningStatus ( ) = = false ) {
//printf("#3 Ending client SLOT: %d\n",playerIndex);
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
delete slotThreadWorker ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
else {
slotThreadWorker - > setDeleteSelfOnExecutionDone ( true ) ;
slotThreadWorker - > setDeleteAfterExecute ( true ) ;
}
2010-12-25 09:14:35 +01:00
}
2013-06-04 02:31:41 +02:00
//printf("#4 Ending client SLOT: %d\n",playerIndex);
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
}
2013-06-04 02:31:41 +02:00
bool ConnectionSlot : : getGameStarted ( ) {
bool result = false ;
if ( this - > slotThreadWorker ! = NULL ) {
result = this - > slotThreadWorker - > getGameStarted ( ) ;
}
return result ;
}
void ConnectionSlot : : setGameStarted ( bool value ) {
if ( this - > slotThreadWorker ! = NULL ) {
this - > slotThreadWorker - > setGameStarted ( value ) ;
}
}
2013-03-06 15:29:49 +01:00
void ConnectionSlot : : setPlayerIndex ( int value ) {
playerIndex = value ;
if ( this - > slotThreadWorker ! = NULL ) {
this - > slotThreadWorker - > setSlotIndex ( playerIndex ) ;
}
}
2013-02-15 19:25:10 +01:00
void ConnectionSlot : : setReady ( ) {
this - > ready = true ;
this - > skipLagCheck = false ;
this - > joinGameInProgress = false ;
2013-02-27 00:31:59 +01:00
this - > sentSavedGameInfo = false ;
2013-02-15 19:25:10 +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 ( ) ;
2013-03-01 07:52:33 +01:00
//if(serverInterface->getGameHasBeenInitiated() == true &&
//serverInterface->getAllowInGameConnections() == true) {
2013-02-15 19:25:10 +01:00
//printf("Checking updateSlot event = %p\n",event);
2013-03-01 07:52:33 +01:00
//}
2013-02-15 19:25:10 +01:00
2011-11-25 22:56:36 +01:00
if ( event ! = NULL ) {
bool & socketTriggered = event - > socketTriggered ;
2013-02-15 19:25:10 +01:00
bool checkForNewClients =
( serverInterface - > getGameHasBeenInitiated ( ) = = false | |
serverInterface - > getAllowInGameConnections ( ) = = true ) ;
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());
2013-03-01 07:52:33 +01:00
//if(serverInterface->getGameHasBeenInitiated() == true &&
//serverInterface->getAllowInGameConnections() == true) {
2013-02-15 19:25:10 +01:00
//printf("Checking for new client connection on slot, checkForNewClients: %d this->canAcceptConnections: %d\n",checkForNewClients,this->canAcceptConnections);
2013-03-01 07:52:33 +01:00
//}
2013-02-15 19:25:10 +01:00
2011-11-25 22:56:36 +01:00
if ( ( serverInterface - > getGameHasBeenInitiated ( ) = = false | |
2013-03-01 07:52:33 +01:00
( serverInterface - > getAllowInGameConnections ( ) = = true & & this - > isConnected ( ) = = 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 | |
2013-02-15 19:25:10 +01:00
( ( serverInterface - > getGameHasBeenInitiated ( ) = = false | |
serverInterface - > getAllowInGameConnections ( ) = = true ) & &
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-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
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 ( ) ) ;
2011-02-08 19:23:41 +01:00
bool hasData = ( serverInterface - > getServerSocket ( ) ! = NULL & & serverInterface - > getServerSocket ( ) - > hasDataToRead ( ) = = true ) ;
if ( hasData = = true ) {
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
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
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 = " " ;
2013-06-01 04:31:12 +02:00
this - > playerUUID = " " ;
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-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
2010-08-21 04:59:21 +02:00
this - > currentFrameCount = 0 ;
this - > currentLagCount = 0 ;
this - > lastReceiveCommandListTime = 0 ;
this - > gotLagCountWarning = false ;
this - > versionString = " " ;
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 ) ;
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
2013-06-01 04:31:12 +02:00
//send intro message when connected
2012-01-20 05:15:13 +01:00
if ( this - > isConnected ( ) = = true ) {
2013-06-01 04:31:12 +02:00
Chrono seed ( true ) ;
srand ( ( unsigned int ) seed . getCurTicks ( ) / ( this - > playerIndex + 1 ) ) ;
2011-09-25 07:38:35 +02:00
2013-06-01 04:31:12 +02:00
sessionKey = rand ( ) % 1000000 ;
2010-05-12 17:25:56 +02:00
2013-06-01 04:31:12 +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 ) ;
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
2012-10-02 08:20:39 +02:00
NetworkMessageIntro networkMessageIntro (
sessionKey ,
getNetworkVersionSVNString ( ) ,
getHostName ( ) ,
playerIndex ,
nmgstOk ,
0 ,
ServerSocket : : getFTPServerPort ( ) ,
2013-02-15 19:25:10 +01:00
" " ,
2013-06-01 04:31:12 +02:00
serverInterface - > getGameHasBeenInitiated ( ) ,
Config : : getInstance ( ) . getString ( " PlayerId " , " " ) ) ;
2012-10-02 08:20:39 +02:00
sendMessage ( & networkMessageIntro ) ;
2012-01-20 05:15:13 +01:00
}
2010-05-12 17:25:56 +02: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] \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-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 ;
2012-06-22 07:46:19 +02:00
bool gotCellMarkerMsg = true ;
2012-07-21 02:23:27 +02:00
bool waitForLaggingClient = false ;
2012-07-21 02:55:57 +02:00
bool waitedForLaggingClient = false ;
2013-06-04 02:31:41 +02:00
//printf("Update slot: %d this->hasDataToRead(): %d\n",this->playerIndex,this->hasDataToRead());
2012-07-21 02:23:27 +02:00
for ( ; waitForLaggingClient = = true | |
( this - > hasDataToRead ( ) = = true & &
( gotTextMsg = = true | | gotCellMarkerMsg = = true ) ) ; ) {
2013-03-02 08:33:01 +01:00
//printf("Server slot checking for waitForLaggingClient = %d this->hasDataToRead() = %d gotTextMsg = %d gotCellMarkerMsg = %d\n",waitForLaggingClient,this->hasDataToRead(),gotTextMsg,gotCellMarkerMsg);
2012-07-21 02:23:27 +02:00
waitForLaggingClient = false ;
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 ;
2012-06-22 07:46:19 +02:00
gotCellMarkerMsg = 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 ( ) ) ;
2012-06-12 22:37:00 +02:00
close ( ) ;
return ;
}
}
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 ( ) ;
return ;
}
}
break ;
case nmtMarkCell :
{
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
if ( gotIntro = = true ) {
NetworkMessageMarkCell networkMessageMarkCell ;
if ( receiveMessage ( & networkMessageMarkCell ) ) {
MarkedCell msg ( networkMessageMarkCell . getTarget ( ) ,
networkMessageMarkCell . getFactionIndex ( ) ,
2012-09-21 00:18:10 +02:00
networkMessageMarkCell . getText ( ) . c_str ( ) ,
networkMessageMarkCell . getPlayerIndex ( ) ) ;
2012-06-12 22:37:00 +02:00
this - > addMarkedCell ( msg ) ;
2012-06-22 07:46:19 +02:00
gotCellMarkerMsg = true ;
2012-06-12 22:37:00 +02: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 ( ) ) ;
2012-06-13 18:19:44 +02:00
close ( ) ;
return ;
}
}
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 ( ) ;
return ;
}
}
break ;
case nmtUnMarkCell :
{
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtUnMarkCell gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
if ( gotIntro = = true ) {
NetworkMessageUnMarkCell networkMessageMarkCell ;
if ( receiveMessage ( & networkMessageMarkCell ) ) {
UnMarkedCell msg ( networkMessageMarkCell . getTarget ( ) ,
networkMessageMarkCell . getFactionIndex ( ) ) ;
this - > addUnMarkedCell ( msg ) ;
2012-06-22 07:46:19 +02:00
gotCellMarkerMsg = true ;
2012-06-13 18:19:44 +02: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 ( ) ) ;
2012-07-13 23:50:34 +02:00
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
close ( ) ;
return ;
}
}
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 ( ) ;
return ;
}
}
break ;
case nmtHighlightCell :
{
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
if ( gotIntro = = true ) {
NetworkMessageHighlightCell networkMessageHighlightCell ;
if ( receiveMessage ( & networkMessageHighlightCell ) ) {
MarkedCell msg ( networkMessageHighlightCell . getTarget ( ) ,
2012-09-21 00:18:10 +02:00
networkMessageHighlightCell . getFactionIndex ( ) , " none " , - 1 ) ;
2012-07-13 23:50:34 +02:00
this - > setHighlightedCell ( msg ) ;
gotCellMarkerMsg = 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 ( ) ) ;
2012-06-13 18:19:44 +02:00
this - > serverInterface - > notifyBadClientConnectAttempt ( this - > getIpAddress ( ) ) ;
2011-11-23 09:00:09 +01:00
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
2012-04-14 23:21:09 +02:00
//throw megaglest_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 ) ;
2013-03-02 08:33:01 +01:00
//printf("#1 Server slot got currentFrameCount = %d\n",currentFrameCount);
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 ( ) ;
2013-06-04 02:31:41 +02:00
//printf("Got commands from client frame: %d count: %d\n",currentFrameCount,vctPendingNetworkCommandList.size());
2013-03-02 08:33:01 +01:00
//printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount);
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 ) ) {
2012-11-03 01:19:13 +01:00
int32 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 ( ) ;
2013-06-01 04:31:12 +02:00
this - > playerUUID = networkMessageIntro . getPlayerUUID ( ) ;
//printf("Got uuid from client [%s]\n",this->playerUUID.c_str());
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
}
2013-06-01 04:31:12 +02:00
else if ( this - > playerUUID = = " " ) {
string playerNameStr = name ;
string sErr = " Client gave an invalid UUID 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 ( ) ;
return ;
}
2011-11-23 09:00:09 +01:00
else {
//check consistency
2012-07-07 01:18:30 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-23 09:00:09 +01:00
bool compatible = checkVersionComptability ( getNetworkVersionSVNString ( ) , networkMessageIntro . getVersionString ( ) ) ;
2012-07-07 01:18:30 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-11-23 09:00:09 +01:00
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
2013-06-01 04:31:12 +02:00
int factionIndex = this - > serverInterface - > gameSettings . getFactionIndexForStartLocation ( playerIndex ) ;
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
2013-06-01 04:31:12 +02:00
this - > serverInterface - > gameSettings . setNetworkPlayerUUID ( factionIndex , this - > playerUUID ) ;
2013-05-26 08:45:56 +02:00
if ( serverInterface - > getGameHasBeenInitiated ( ) = = true & &
serverInterface - > getAllowInGameConnections ( ) = = true ) {
2013-06-01 04:31:12 +02:00
//int factionIndex = this->serverInterface->gameSettings.getFactionIndexForStartLocation(playerIndex);
2013-05-26 08:45:56 +02:00
this - > serverInterface - > gameSettings . setNetworkPlayerStatuses ( factionIndex , npst_None ) ;
}
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 ) ;
}
2013-02-15 19:25:10 +01:00
if ( serverInterface - > getGameHasBeenInitiated ( ) = = true & &
serverInterface - > getAllowInGameConnections ( ) = = true ) {
2013-02-16 11:07:36 +01:00
setJoinGameInProgressFlags ( ) ;
2013-02-19 23:00:15 +01:00
this - > setPauseForInGameConnection ( true ) ;
2013-02-15 19:25:10 +01:00
}
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 | |
2013-02-15 19:25:10 +01:00
( joinGameInProgress = = false & & sessionKey ! = this - > serverInterface - > getGameSettings ( ) - > getMasterserver_admin ( ) ) ) {
2011-11-23 09:00:09 +01:00
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__ ) ;
2013-02-15 19:25:10 +01:00
2013-03-06 15:29:49 +01:00
//printf("Got launch request from client joinGameInProgress = %d joinGameInProgress = %d!\n",joinGameInProgress,joinGameInProgress);
2011-11-23 09:00:09 +01:00
}
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 ( ) ) ;
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( szBuf ) ;
2011-11-23 09:00:09 +01:00
}
2012-07-07 07:35:25 +02:00
int minHeadLessPlayersRequired = Config : : getInstance ( ) . getInt ( " MinHeadlessPlayersRequired " , " 2 " ) ;
2013-03-06 15:29:49 +01:00
if ( this - > joinGameInProgress = = false & & networkMessageLaunch . getMessageType ( ) = = nmtLaunch & &
this - > ready = = false & &
2013-01-12 00:48:57 +01:00
this - > serverInterface - > getConnectedSlotCount ( true ) < minHeadLessPlayersRequired ) {
2012-07-07 07:35:25 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2012-11-15 15:45:22 +01:00
const vector < string > languageList = this - > serverInterface - > getGameSettings ( ) - > getUniqueNetworkPlayerLanguages ( ) ;
2012-07-07 07:35:25 +02:00
for ( unsigned int i = 0 ; i < languageList . size ( ) ; + + i ) {
2012-11-15 15:45:22 +01:00
char szBuf [ 4096 ] = " " ;
2012-07-07 07:35:25 +02:00
string msgTemplate = " You must have have at least %d player(s) connected to start this game! " ;
2012-11-15 15:45:22 +01:00
if ( lang . hasString ( " HeadlessAdminRequiresMorePlayers " , languageList [ i ] ) = = true ) {
2012-07-07 07:35:25 +02:00
msgTemplate = lang . get ( " HeadlessAdminRequiresMorePlayers " , languageList [ i ] ) ;
}
# ifdef WIN32
_snprintf ( szBuf , 4095 , msgTemplate . c_str ( ) , minHeadLessPlayersRequired ) ;
# else
snprintf ( szBuf , 4095 , msgTemplate . c_str ( ) , minHeadLessPlayersRequired ) ;
# endif
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , szBuf ) ;
string sMsg = szBuf ;
bool echoLocal = lang . isLanguageLocal ( languageList [ i ] ) ;
this - > serverInterface - > sendTextMessage ( sMsg , - 1 , echoLocal , languageList [ i ] , this - > getPlayerIndex ( ) ) ;
2012-11-15 15:45:22 +01:00
//printf("Lang [%s] msgTemplate [%s] echoLocal = %d\n",languageList[i].c_str(),msgTemplate.c_str(),echoLocal);
2012-07-07 07:35:25 +02:00
}
}
else {
2013-03-06 15:29:49 +01:00
if ( this - > joinGameInProgress = = false ) {
2013-02-15 19:25:10 +01:00
GameSettings gameSettingsBuffer ;
networkMessageLaunch . buildGameSettings ( & gameSettingsBuffer ) ;
2011-09-24 09:46:56 +02:00
2013-02-15 19:25:10 +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());
2011-11-23 09:00:09 +01:00
2013-02-15 19:25:10 +01:00
//this->serverInterface->setGameSettings(&gameSettingsBuffer,false);
this - > serverInterface - > broadcastGameSetup ( & gameSettingsBuffer , true ) ;
}
2011-11-23 09:00:09 +01:00
2013-03-06 15:29:49 +01:00
if ( this - > joinGameInProgress = = false & & networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
2012-07-07 07:35:25 +02:00
this - > serverInterface - > setMasterserverAdminRequestLaunch ( true ) ;
}
2013-03-06 15:29:49 +01:00
else if ( this - > joinGameInProgress = = true & & networkMessageLaunch . getMessageType ( ) = = nmtLaunch ) {
2013-02-15 19:25:10 +01:00
//printf("!!! setStartInGameConnectionLaunch for client joinGameInProgress = %d!\n",joinGameInProgress);
//GameSettings gameSettingsBuffer;
//networkMessageLaunch.buildGameSettings(&gameSettingsBuffer);
int factionIndex = this - > serverInterface - > gameSettings . getFactionIndexForStartLocation ( playerIndex ) ;
this - > serverInterface - > gameSettings . setFactionControl ( factionIndex , ctNetwork ) ;
this - > serverInterface - > gameSettings . setNetworkPlayerName ( factionIndex , this - > name ) ;
2013-06-01 04:31:12 +02:00
this - > serverInterface - > gameSettings . setNetworkPlayerUUID ( factionIndex , this - > playerUUID ) ;
2013-05-28 09:10:13 +02:00
if ( this - > serverInterface - > gameSettings . getNetworkPlayerStatuses ( factionIndex ) = = npst_Disconnected ) {
this - > serverInterface - > gameSettings . setNetworkPlayerStatuses ( factionIndex , npst_None ) ;
}
2013-05-26 06:01:01 +02:00
2013-02-15 19:25:10 +01:00
this - > serverInterface - > broadcastGameSetup ( & this - > serverInterface - > gameSettings , true ) ;
2013-02-19 23:00:15 +01:00
this - > setStartInGameConnectionLaunch ( true ) ;
2013-02-15 19:25:10 +01: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 ( ) ) ;
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
2012-07-07 04:46:57 +02:00
uint32 tilesetCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + serverInterface - > getGameSettings ( ) - > getTileset ( ) + string ( " /* " ) , " .xml " , NULL ) ;
uint32 techCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , NULL ) ;
2010-08-20 22:03:06 +02:00
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 ) ;
2012-07-07 04:46:57 +02:00
uint32 mapCRC = checksum . getSum ( ) ;
2010-08-20 22:03:06 +02:00
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 ) {
2012-07-07 04:46:57 +02:00
vctFileList . push_back ( std : : pair < string , uint32 > ( 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();
2012-10-06 14:56:53 +02:00
fileInfo . serverPort = Config : : getInstance ( ) . getInt ( " PortServer " , intToStr ( GameConstants : : serverPort ) . c_str ( ) ) ;
2010-08-20 22:03:06 +02:00
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 :
{
2013-02-16 11:07:36 +01:00
//printf("Got nmtSwitchSetupRequest A gotIntro = %d\n",gotIntro);
2011-09-01 23:54:31 +02:00
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugNetwork ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got 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
2013-02-16 11:07:36 +01:00
int slotIdx = switchSetupRequest . getCurrentSlotIndex ( ) ;
2013-05-17 05:59:34 +02:00
//int newSlotIdx = switchSetupRequest.getToSlotIndex();
2013-02-16 11:07:36 +01:00
//printf("slotIdx = %d newSlotIdx = %d\n",slotIdx,newSlotIdx);
if ( serverInterface - > getSwitchSetupRequests ( slotIdx ) = = NULL ) {
serverInterface - > setSwitchSetupRequests ( slotIdx , new SwitchSetupRequest ( ) ) ;
2010-08-20 22:03:06 +02:00
}
2013-02-16 11:07:36 +01:00
* ( serverInterface - > getSwitchSetupRequests ( slotIdx ) ) = switchSetupRequest ;
//printf("slotIdx = %d newSlotIdx = %d\n",serverInterface->getSwitchSetupRequests(slotIdx)->getCurrentSlotIndex(),serverInterface->getSwitchSetupRequests(slotIdx)->getToSlotIndex());
2010-08-23 06:33:21 +02:00
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());
2013-02-16 11:07:36 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line %d] networkPlayerName [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getSwitchSetupRequests ( ) [ slotIdx ] - > getNetworkPlayerName ( ) . c_str ( ) ) ;
2011-03-11 12:11:46 +01:00
2013-02-16 11:07:36 +01: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__ , slotIdx , 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
}
2013-02-16 11:07:36 +01:00
2010-06-29 08:50:35 +02:00
case nmtReady :
2010-05-25 20:06:42 +02:00
{
2013-02-15 19:25:10 +01:00
NetworkMessageReady networkMessageReady ;
this - > receiveMessage ( & networkMessageReady ) ;
2010-06-29 08:50:35 +02:00
// its simply ignored here. Probably we are starting a game
2013-02-15 19:25:10 +01:00
//printf("Got ready message from client slot joinGameInProgress = %d\n",joinGameInProgress);
if ( joinGameInProgress = = true ) {
NetworkMessageReady networkMessageReady ( 0 ) ;
this - > sendMessage ( & networkMessageReady ) ;
2013-06-04 02:31:41 +02:00
this - > setGameStarted ( true ) ;
2013-02-15 19:25:10 +01:00
this - > currentFrameCount = serverInterface - > getCurrentFrameCount ( ) ;
2013-03-02 08:33:01 +01:00
//printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount);
2013-02-15 19:25:10 +01:00
this - > currentLagCount = 0 ;
this - > lastReceiveCommandListTime = time ( NULL ) ;
this - > setReady ( ) ;
}
// unpause the game
else {
2013-02-19 23:00:15 +01:00
this - > setUnPauseForInGameConnection ( true ) ;
2013-02-15 19:25:10 +01:00
}
2010-06-29 08:50:35 +02:00
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 ) {
2012-04-14 23:21:09 +02:00
//throw megaglest_runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType));
2010-06-29 08:50:35 +02:00
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
}
}
}
2012-07-21 02:23:27 +02:00
2013-03-02 08:33:01 +01:00
//printf("#3 Server slot got currentFrameCount = %d\n",currentFrameCount);
2012-09-22 07:37:43 +02:00
// This may end up continuously lagging and not disconnecting players who have
// just the 'wrong' amount of lag (but not enough to be horrible for a disconnect)
if ( Config : : getInstance ( ) . getBool ( " AutoClientLagCorrection " , " true " ) = = true ) {
double LAG_CHECK_GRACE_PERIOD = 15 ;
2013-03-02 08:33:01 +01:00
//printf("#4 Server slot got currentFrameCount = %d\n",currentFrameCount);
2012-09-22 07:37:43 +02:00
if ( this - > serverInterface - > getGameStartTime ( ) > 0 & &
2013-03-01 07:52:33 +01:00
difftime ( ( long int ) time ( NULL ) , this - > serverInterface - > getGameStartTime ( ) ) > = LAG_CHECK_GRACE_PERIOD & &
difftime ( ( long int ) time ( NULL ) , this - > getConnectedTime ( ) ) > = LAG_CHECK_GRACE_PERIOD ) {
2013-02-15 19:25:10 +01:00
if ( this - > isConnected ( ) = = true & & this - > gotIntro = = true & & this - > skipLagCheck = = false ) {
2012-09-22 07:37:43 +02:00
double clientLag = this - > serverInterface - > getCurrentFrameCount ( ) - this - > getCurrentFrameCount ( ) ;
double clientLagCount = ( gameSettings . getNetworkFramePeriod ( ) > 0 ? ( clientLag / gameSettings . getNetworkFramePeriod ( ) ) : 0 ) ;
2012-09-22 22:13:57 +02:00
double clientLagTime = difftime ( ( long int ) time ( NULL ) , this - > getLastReceiveCommandListTime ( ) ) ;
2012-09-22 07:37:43 +02:00
2012-10-06 09:06:40 +02:00
double maxFrameCountLagAllowed = 10 ;
double maxClientLagTimeAllowed = 8 ;
2012-09-22 07:37:43 +02:00
// New lag check
if ( ( maxFrameCountLagAllowed > 0 & & clientLagCount > maxFrameCountLagAllowed ) | |
( maxClientLagTimeAllowed > 0 & & clientLagTime > maxClientLagTimeAllowed ) ) {
waitForLaggingClient = true ;
if ( waitedForLaggingClient = = false ) {
waitedForLaggingClient = true ;
printf ( " *TESTING*: START Waiting for lagging client playerIndex = %d [%s] clientLagCount = %f [%f] \n " , playerIndex , name . c_str ( ) , clientLagCount , clientLagTime ) ;
}
2012-07-21 02:55:57 +02:00
}
2012-07-21 02:23:27 +02:00
}
}
2013-03-02 08:33:01 +01:00
//printf("#5 Server slot got currentFrameCount = %d\n",currentFrameCount);
2012-07-21 02:23:27 +02:00
}
2013-03-02 08:33:01 +01:00
//printf("#5a Server slot got currentFrameCount = %d\n",currentFrameCount);
2011-11-25 22:56:36 +01:00
}
2010-05-25 20:06:42 +02:00
2013-03-02 08:33:01 +01:00
//printf("#6 Server slot got currentFrameCount = %d\n",currentFrameCount);
2012-07-21 02:55:57 +02:00
if ( waitedForLaggingClient = = true ) {
2012-07-21 06:34:55 +02:00
printf ( " *TESTING*: FINISHED Waiting for lagging client playerIndex = %d [%s] \n " , playerIndex , name . c_str ( ) ) ;
2012-07-21 02:55:57 +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
2013-03-02 08:33:01 +01:00
//printf("#7 Server slot got currentFrameCount = %d\n",currentFrameCount);
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
2013-02-15 19:25:10 +01:00
//printf("Closing connection slot socketInfo.first = %d\n",socketInfo.first);
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
2013-03-02 08:33:01 +01:00
//printf("#8 Server slot got currentFrameCount = %d\n",currentFrameCount);
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 & &
2012-09-22 22:13:57 +02:00
difftime ( ( long int ) time ( NULL ) , connectedTime ) > GameConstants : : maxClientConnectHandshakeSecs ) {
2013-02-15 19:25:10 +01:00
//printf("Closing connection slot timed out!\n");
2010-08-20 22:03:06 +02:00
close ( ) ;
}
}
2013-02-16 11:07:36 +01:00
void ConnectionSlot : : resetJoinGameInProgressFlags ( ) {
this - > gotIntro = false ;
this - > skipLagCheck = false ;
this - > joinGameInProgress = false ;
this - > ready = false ;
}
void ConnectionSlot : : setJoinGameInProgressFlags ( ) {
this - > gotIntro = true ;
this - > skipLagCheck = true ;
this - > joinGameInProgress = true ;
this - > ready = false ;
2013-02-27 00:31:59 +01:00
this - > sentSavedGameInfo = false ;
2013-02-16 11:07:36 +01:00
}
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
2013-02-24 01:28:45 +01:00
//printf("Closing slot for playerIndex = %d\n",playerIndex);
2013-02-15 19:25:10 +01:00
//if(serverInterface->getAllowInGameConnections() == true) {
//printf("Closing connection slot!\n");
//}
2013-03-15 01:31:51 +01:00
//printf("ConnectionSlot::close() #1 this->getSocket() = %p\n",this->getSocket());
2013-02-15 19:25:10 +01:00
this - > gotIntro = false ;
this - > skipLagCheck = false ;
this - > joinGameInProgress = false ;
2013-02-27 00:31:59 +01:00
this - > sentSavedGameInfo = false ;
2013-03-14 22:41:15 +01:00
this - > pauseForInGameConnection = false ;
2013-03-09 21:57:06 +01:00
this - > unPauseForInGameConnection = false ;
2013-02-24 01:28:45 +01:00
this - > ready = false ;
this - > connectedTime = 0 ;
2013-02-15 19:25:10 +01: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
2013-03-15 01:31:51 +01:00
//printf("ConnectionSlot::close() #2 this->getSocket() = %p\n",this->getSocket());
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 ) ;
2013-03-15 01:31:51 +01:00
//printf("ConnectionSlot::close() #3 this->getSocket() = %p updateServerListener = %d\n",this->getSocket(),updateServerListener);
2011-11-23 09:00:09 +01:00
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
2013-02-24 01:28:45 +01:00
//printf("Closing slot for playerIndex = %d updateServerListener = %d ready = %d\n",playerIndex,updateServerListener,ready);
2013-02-26 08:05:46 +01:00
if ( updateServerListener = = 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 16:59:09 +02:00
serverInterface - > updateListen ( ) ;
}
2013-02-24 01:28:45 +01:00
//ready = false;
//gotIntro = false;
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 ) ;
}
2012-11-01 01:06:23 +01:00
void ConnectionSlot : : sendMessage ( 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
2012-11-01 01:06:23 +01:00
NetworkMessageText * textMsg = dynamic_cast < NetworkMessageText * > ( networkMessage ) ;
2011-04-05 22:19:25 +02:00
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 ;
2013-03-02 08:33:01 +01:00
//printf("==> #1 Slot hasDataToRead()\n");
2011-12-02 17:07:59 +01:00
MutexSafeWrapper safeMutexSlot ( mutexSocket , CODE_AT_LINE ) ;
2011-11-25 00:15:21 +01:00
2013-03-02 08:33:01 +01:00
//printf("==> #2 Slot hasDataToRead()\n");
2011-11-25 00:15:21 +01:00
if ( socket ! = NULL & & socket - > hasDataToRead ( ) = = true ) {
result = true ;
}
2013-03-02 08:33:01 +01:00
//printf("==> #3 Slot hasDataToRead()\n");
2011-11-25 00:15:21 +01:00
return result ;
}
2010-03-17 07:25:19 +01:00
} } //end namespace