2010-03-17 07:25:19 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-04-03 04:22:41 +02:00
// Copyright (C) 2001-2008 Martio 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
// =====================================================
2010-06-28 05:17:50 +02:00
ConnectionSlotThread : : ConnectionSlotThread ( int slotIndex ) : BaseThread ( ) {
this - > slotIndex = slotIndex ;
2010-05-28 01:46:38 +02:00
this - > slotInterface = NULL ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2010-05-28 01:46:38 +02:00
}
2010-06-28 05:17:50 +02:00
ConnectionSlotThread : : ConnectionSlotThread ( ConnectionSlotCallbackInterface * slotInterface , int slotIndex ) : BaseThread ( ) {
this - > slotIndex = slotIndex ;
2010-05-28 01:46:38 +02:00
this - > slotInterface = slotInterface ;
2010-12-25 09:14:35 +01:00
//this->event = NULL;
eventList . clear ( ) ;
2010-05-28 01:46:38 +02:00
}
void ConnectionSlotThread : : setQuitStatus ( bool value ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d value = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , value ) ;
BaseThread : : setQuitStatus ( value ) ;
if ( value = = true ) {
signalUpdate ( NULL ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
void ConnectionSlotThread : : signalUpdate ( ConnectionSlotEvent * event ) {
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] event = %p\n",__FILE__,__FUNCTION__,__LINE__,event);
2010-05-28 01:46:38 +02:00
if ( event ! = NULL ) {
2010-06-02 03:37:45 +02:00
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
2011-01-11 09:45:58 +01:00
eventList . push_back ( * event ) ;
2010-06-02 03:37:45 +02:00
safeMutex . ReleaseLock ( ) ;
2010-05-28 01:46:38 +02:00
}
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
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 ) {
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
2011-01-11 09:45:58 +01:00
if ( eventId > 0 ) {
2010-06-02 03:37:45 +02:00
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
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
}
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
}
2011-01-11 21:02:07 +01:00
void ConnectionSlotThread : : purgeAllEvents ( ) {
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
eventList . clear ( ) ;
safeMutex . ReleaseLock ( ) ;
}
void ConnectionSlotThread : : setAllEventsCompleted ( ) {
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
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 ( ) {
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
//event->eventCompleted = true;
for ( int i = eventList . size ( ) - 1 ; i > = 0 ; i - - ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = true ) {
eventList . erase ( eventList . begin ( ) + i ) ;
}
}
safeMutex . ReleaseLock ( ) ;
}
2011-01-11 19:39:18 +01:00
bool ConnectionSlotThread : : canShutdown ( bool deleteSelfIfShutdownDelayed ) {
bool ret = ( getExecutingTask ( ) = = false ) ;
if ( ret = = false & & deleteSelfIfShutdownDelayed = = true ) {
setDeleteSelfOnExecutionDone ( deleteSelfIfShutdownDelayed ) ;
signalQuit ( ) ;
}
return ret ;
}
2010-12-25 09:14:35 +01:00
bool ConnectionSlotThread : : isSignalCompleted ( ConnectionSlotEvent * event ) {
2010-06-28 05:17:50 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] slotIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex);
2010-06-02 03:37:45 +02:00
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
2011-01-11 09:45:58 +01:00
//bool result = (event != NULL ? event->eventCompleted : true);
bool result = false ;
if ( event ! = NULL ) {
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventId = = event - > eventId ) {
//eventList.erase(eventList.begin() + i);
result = slotEvent . eventCompleted ;
break ;
}
}
}
2010-06-02 03:37:45 +02:00
safeMutex . ReleaseLock ( ) ;
2010-12-25 09:14:35 +01:00
//if(result == false) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] slotIndex = %d, result = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,result);
2010-05-28 01:46:38 +02:00
return result ;
}
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);
2010-05-28 01:46:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
unsigned int idx = 0 ;
for ( ; this - > slotInterface ! = NULL ; ) {
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
semTaskSignalled . waitTillSignalled ( ) ;
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
2010-12-25 09:14:35 +01:00
MutexSafeWrapper safeMutex ( & triggerIdMutex ) ;
int eventCount = eventList . size ( ) ;
if ( eventCount > 0 ) {
2011-01-11 21:02:07 +01:00
ConnectionSlotEvent eventCopy ;
eventCopy . eventId = - 1 ;
2011-01-11 09:45:58 +01:00
for ( int i = 0 ; i < eventList . size ( ) ; + + i ) {
ConnectionSlotEvent & slotEvent = eventList [ i ] ;
if ( slotEvent . eventCompleted = = false ) {
2011-01-11 21:02:07 +01:00
eventCopy = slotEvent ;
2011-01-11 09:45:58 +01:00
break ;
}
}
2010-12-25 09:14:35 +01:00
safeMutex . ReleaseLock ( ) ;
2011-01-11 21:02:07 +01:00
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
if ( eventCopy . eventId > 0 ) {
2011-01-11 19:39:18 +01:00
ExecutingTaskSafeWrapper safeExecutingTaskMutex ( this ) ;
2011-01-11 21:02:07 +01:00
this - > slotInterface - > slotUpdateTask ( & eventCopy ) ;
setTaskCompleted ( eventCopy . eventId ) ;
2011-01-11 09:45:58 +01:00
}
2010-12-25 23:38:00 +01:00
}
else {
safeMutex . ReleaseLock ( ) ;
2010-12-25 09:14:35 +01:00
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
2010-12-25 09:14:35 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
2010-05-28 01:46:38 +02:00
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
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 ( ) ) ;
2010-05-28 01:46:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
throw runtime_error ( ex . what ( ) ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-12-22 01:15:32 +01:00
//setRunningStatus(false);
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 ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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 ;
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 ;
2010-05-28 01:46:38 +02:00
this - > socket = NULL ;
this - > slotThreadWorker = NULL ;
2010-06-28 05:17:50 +02:00
this - > slotThreadWorker = new ConnectionSlotThread ( this - > serverInterface , playerIndex ) ;
2010-06-18 22:04:05 +02:00
this - > slotThreadWorker - > setUniqueID ( __FILE__ ) ;
2010-05-28 01:46:38 +02:00
this - > slotThreadWorker - > start ( ) ;
2010-05-28 16:59:09 +02:00
this - > ready = false ;
this - > gotIntro = false ;
2010-08-20 22:03:06 +02:00
this - > connectedTime = 0 ;
2010-03-17 07:25:19 +01:00
networkGameDataSynchCheckOkMap = false ;
networkGameDataSynchCheckOkTile = false ;
networkGameDataSynchCheckOkTech = false ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( " " ) ;
this - > setReceivedDataSynchCheck ( false ) ;
2010-07-01 02:08:59 +02:00
this - > clearChatInfo ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-12-25 09:14:35 +01:00
ConnectionSlot : : ~ ConnectionSlot ( ) {
2011-01-10 22:00:55 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] START \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-12-25 09:14:35 +01:00
if ( BaseThread : : shutdownAndWait ( slotThreadWorker ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
delete slotThreadWorker ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-05-28 01:46:38 +02:00
slotThreadWorker = NULL ;
2010-06-23 16:49:20 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
close ( ) ;
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-15 20:59:17 +02:00
void ConnectionSlot : : update ( ) {
2011-01-11 09:45:58 +01:00
if ( slotThreadWorker ! = NULL ) {
slotThreadWorker - > purgeCompletedEvents ( ) ;
}
2010-03-17 07:25:19 +01:00
update ( true ) ;
}
2010-05-15 20:59:17 +02:00
void ConnectionSlot : : update ( bool checkForNewClients ) {
2010-05-25 20:06:42 +02:00
clearThreadErrorList ( ) ;
try {
if ( socket = = NULL ) {
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?
//if(serverInterface->getServerSocket()->isReadable() == true)
if ( checkForNewClients = = true ) {
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] BEFORE accept new client connection, serverInterface->getOpenSlotCount() = %d \n " , __FILE__ , __FUNCTION__ , serverInterface - > getOpenSlotCount ( ) ) ;
2010-05-25 20:06:42 +02:00
bool hasOpenSlots = ( serverInterface - > getOpenSlotCount ( ) > 0 ) ;
2010-08-21 01:53:10 +02:00
if ( serverInterface - > getServerSocket ( ) ! = NULL & &
serverInterface - > getServerSocket ( ) - > hasDataToRead ( ) = = true ) {
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-05-25 20:06:42 +02:00
socket = serverInterface - > getServerSocket ( ) - > accept ( ) ;
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-06-28 05:17:50 +02:00
if ( socket ! = NULL ) {
2010-08-21 04:59:21 +02:00
this - > connectedTime = time ( NULL ) ;
this - > clearChatInfo ( ) ;
this - > name = " " ;
this - > ready = false ;
this - > vctFileList . clear ( ) ;
this - > receivedNetworkGameStatus = false ;
this - > gotIntro = false ;
2010-12-24 09:43:09 +01:00
MutexSafeWrapper safeMutexSlot ( & mutexPendingNetworkCommandList , intToStr ( __LINE__ ) ) ;
2010-08-21 04:59:21 +02:00
this - > vctPendingNetworkCommandList . clear ( ) ;
2010-12-24 09:43:09 +01:00
safeMutexSlot . ReleaseLock ( ) ;
2010-08-21 04:59:21 +02:00
this - > currentFrameCount = 0 ;
this - > currentLagCount = 0 ;
this - > lastReceiveCommandListTime = 0 ;
this - > gotLagCountWarning = false ;
this - > versionString = " " ;
2011-01-11 21:02:07 +01:00
//if(this->slotThreadWorker == NULL) {
// this->slotThreadWorker = new ConnectionSlotThread(this->serverInterface,playerIndex);
// this->slotThreadWorker->setUniqueID(__FILE__);
// this->slotThreadWorker->start();
//}
2010-06-28 05:17:50 +02:00
serverInterface - > updateListen ( ) ;
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-06-28 05:17:50 +02:00
}
2010-05-25 20:06:42 +02:00
}
2010-06-29 08:50:35 +02:00
2010-05-25 20:06:42 +02:00
//send intro message when connected
if ( socket ! = NULL ) {
2010-08-23 17:40:43 +02:00
RandomGen random ;
sessionKey = random . randRange ( - 100000 , 100000 ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , serverInterface - > getOpenSlotCount ( ) , sessionKey ) ;
2010-05-12 17:25:56 +02:00
2010-05-25 20:06:42 +02:00
if ( hasOpenSlots = = false ) {
2010-08-22 10:00:05 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] !!!!!!!!WARNING - no open slots, disconnecting client \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
2010-08-20 22:51:25 +02:00
if ( socket ! = NULL ) {
2010-12-30 16:56:02 +01:00
NetworkMessageIntro networkMessageIntro ( sessionKey , getNetworkVersionString ( ) , getHostName ( ) , playerIndex , nmgstNoSlots , 0 ) ;
2010-08-20 22:51:25 +02:00
sendMessage ( & networkMessageIntro ) ;
}
2010-05-12 17:25:56 +02:00
2010-05-25 20:06:42 +02:00
close ( ) ;
}
else {
2010-08-22 10:00:05 +02:00
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
2010-08-20 22:51:25 +02:00
if ( socket ! = NULL ) {
2010-12-30 16:56:02 +01:00
NetworkMessageIntro networkMessageIntro ( sessionKey , getNetworkVersionString ( ) , getHostName ( ) , playerIndex , nmgstOk , 0 ) ;
2010-08-20 22:51:25 +02:00
sendMessage ( & networkMessageIntro ) ;
}
2010-05-25 20:06:42 +02:00
}
2010-05-12 17:25:56 +02:00
}
}
2010-05-25 20:06:42 +02:00
}
else {
2010-05-28 01:46:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-08-20 22:51:25 +02:00
if ( socket ! = NULL & & socket - > isConnected ( ) ) {
2010-07-01 02:08:59 +02:00
this - > clearChatInfo ( ) ;
2010-03-17 07:25:19 +01:00
2010-08-07 05:26:38 +02:00
bool gotTextMsg = true ;
2010-08-20 22:29:30 +02:00
for ( ; socket ! = NULL & & socket - > hasDataToRead ( ) = = true & & gotTextMsg = = true ; ) {
2010-08-07 05:26:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] polling for networkMessageType... \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
NetworkMessageType networkMessageType = getNextMessageType ( true ) ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] networkMessageType = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageType ) ;
2010-05-28 01:46:38 +02:00
2010-08-07 05:26:38 +02:00
gotTextMsg = false ;
2010-06-29 08:50:35 +02:00
//process incoming commands
switch ( networkMessageType ) {
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
case nmtInvalid :
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtInvalid \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
2010-03-17 07:25:19 +01:00
2010-07-09 17:01:49 +02:00
case nmtPing :
{
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtPing \n " , __FILE__ , __FUNCTION__ ) ;
2010-08-20 22:03:06 +02:00
// client REQUIRES a ping before completing intro
// authentication
//if(gotIntro == true) {
NetworkMessagePing networkMessagePing ;
if ( receiveMessage ( & networkMessagePing ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
lastPingInfo = networkMessagePing ;
}
//}
2010-07-09 17:01:49 +02:00
}
break ;
2010-06-29 08:50:35 +02:00
case nmtText :
{
2010-08-23 06:33:21 +02:00
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 ) ) {
2010-10-23 06:00:39 +02:00
ChatMsgInfo msg ( networkMessageText . getText ( ) . c_str ( ) , networkMessageText . getTeamIndex ( ) , networkMessageText . getPlayerIndex ( ) ) ;
2010-08-20 22:03:06 +02:00
this - > addChatInfo ( msg ) ;
gotTextMsg = true ;
2010-07-01 02:08:59 +02:00
2010-08-20 22:03:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] chatText [%s] chatSender [%s] chatTeamIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,chatText.c_str(),chatSender.c_str(),chatTeamIndex);
}
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
2010-08-23 06:33:21 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtCommandList gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
//throw runtime_error("test");
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageCommandList networkMessageCommandList ;
if ( receiveMessage ( & networkMessageCommandList ) ) {
currentFrameCount = networkMessageCommandList . getFrameCount ( ) ;
lastReceiveCommandListTime = time ( NULL ) ;
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-01-02 01:39:13 +01:00
MutexSafeWrapper safeMutexSlot ( & mutexPendingNetworkCommandList , intToStr ( __LINE__ ) ) ;
for ( int i = 0 ; i < networkMessageCommandList . getCommandCount ( ) ; + + i ) {
2010-08-20 22:03:06 +02:00
vctPendingNetworkCommandList . push_back ( * networkMessageCommandList . getCommand ( i ) ) ;
}
2010-06-29 08:50:35 +02:00
}
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
{
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtIntro \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-06-29 08:50:35 +02:00
NetworkMessageIntro networkMessageIntro ;
2010-08-21 04:59:21 +02:00
if ( receiveMessage ( & networkMessageIntro ) ) {
2010-08-23 17:40:43 +02:00
int msgSessionId = networkMessageIntro . getSessionId ( ) ;
2010-06-29 08:50:35 +02:00
name = networkMessageIntro . getName ( ) ;
2010-08-05 07:58:14 +02:00
versionString = networkMessageIntro . getVersionString ( ) ;
2010-12-30 16:56:02 +01:00
this - > connectedRemoteIPAddress = networkMessageIntro . getExternalIp ( ) ;
2010-03-17 07:25:19 +01:00
2010-08-23 17:40:43 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got name [%s] versionString [%s], msgSessionId = %d \n " , __FILE__ , __FUNCTION__ , name . c_str ( ) , versionString . c_str ( ) , msgSessionId ) ;
if ( msgSessionId ! = sessionKey ) {
string playerNameStr = name ;
string sErr = " Client gave invalid sessionid for player [ " + playerNameStr + " ] " ;
printf ( " %s \n " , sErr . c_str ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] %s \n " , __FILE__ , __FUNCTION__ , __LINE__ , sErr . c_str ( ) ) ;
close ( ) ;
return ;
}
2010-03-17 07:25:19 +01:00
2010-08-21 04:59:21 +02:00
//check consistency
2010-10-15 19:27:00 +02:00
bool compatible = checkVersionComptability ( getNetworkVersionString ( ) , networkMessageIntro . getVersionString ( ) ) ;
if ( compatible = = false ) {
//if(networkMessageIntro.getVersionString() != getNetworkVersionString()) {
2010-08-21 04:59:21 +02:00
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 ) {
2010-08-21 15:04:52 +02:00
string playerNameStr = name ;
2010-08-21 04:59:21 +02:00
sErr = " Server and client binary mismatch! \n You have to use the exactly same binaries! \n \n Server: " + getNetworkVersionString ( ) +
" \n Client: " + networkMessageIntro . getVersionString ( ) + " player [ " + playerNameStr + " ] " ;
printf ( " %s \n " , sErr . c_str ( ) ) ;
serverInterface - > sendTextMessage ( " Server and client binary mismatch!! " , - 1 , true ) ;
serverInterface - > sendTextMessage ( " Server: " + getNetworkVersionString ( ) , - 1 , true ) ;
serverInterface - > sendTextMessage ( " Client: " + networkMessageIntro . getVersionString ( ) , - 1 , true ) ;
serverInterface - > sendTextMessage ( " Client player [ " + playerNameStr + " ] " , - 1 , true ) ;
}
else {
versionMatched = true ;
2010-08-21 15:04:52 +02:00
string playerNameStr = name ;
2010-08-21 04:59:21 +02:00
sErr = " Warning, Server and client are using the same version but different platforms. \n \n Server: " + getNetworkVersionString ( ) +
" \n Client: " + networkMessageIntro . getVersionString ( ) + " player [ " + playerNameStr + " ] " ;
printf ( " %s \n " , sErr . c_str ( ) ) ;
2010-08-23 17:40:43 +02:00
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
//sendTextMessage("Server and client have different platform mismatch.",-1, true);
//sendTextMessage(" Server:" + networkMessageIntro.getVersionString(),-1, true);
//sendTextMessage(" Client: "+ getNetworkVersionString(),-1, true);
//sendTextMessage(" Client player [" + playerNameStr + "]",-1, true);
}
if ( Config : : getInstance ( ) . getBool ( " PlatformConsistencyChecks " , " true " ) & &
versionMatched = = false ) { // error message and disconnect only if checked
2011-01-11 19:39:18 +01:00
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
//DisplayErrorMessage(sErr);
//quit= true;
close ( ) ;
2011-01-11 19:39:18 +01:00
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
return ;
}
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
gotIntro = true ;
2010-12-30 19:51:25 +01:00
this - > serverInterface - > addClientToServerIPAddress ( this - > getSocket ( ) - > getConnectedIPAddress ( this - > getSocket ( ) - > getIpAddress ( ) ) , this - > connectedRemoteIPAddress ) ;
2010-12-30 16:56:02 +01:00
2010-08-22 10:00:05 +02:00
if ( getAllowGameDataSynchCheck ( ) = = true & & serverInterface - > getGameSettings ( ) ! = NULL ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] sending NetworkMessageSynchNetworkGameData \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-25 20:06:42 +02:00
2010-06-29 08:50:35 +02:00
NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData ( serverInterface - > getGameSettings ( ) ) ;
sendMessage ( & networkMessageSynchNetworkGameData ) ;
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
//process datasynch messages
case nmtSynchNetworkGameDataStatus :
{
2010-08-22 10:00:05 +02:00
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 ) {
2010-08-22 10:00:05 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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
2010-08-20 22:03:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " 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
//tileset
int32 tilesetCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + serverInterface - > getGameSettings ( ) - > getTileset ( ) + string ( " /* " ) , " .xml " , NULL ) ;
int32 techCRC = getFolderTreeContentsCheckSumRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , NULL ) ;
Checksum checksum ;
2010-09-17 10:26:22 +02:00
string file = Map : : getMapPath ( serverInterface - > getGameSettings ( ) - > getMap ( ) , scenarioDir , false ) ;
2010-08-20 22:03:06 +02:00
checksum . addFile ( file ) ;
int32 mapCRC = checksum . getSum ( ) ;
networkGameDataSynchCheckOkMap = ( networkMessageSynchNetworkGameDataStatus . getMapCRC ( ) = = mapCRC ) ;
networkGameDataSynchCheckOkTile = ( networkMessageSynchNetworkGameDataStatus . getTilesetCRC ( ) = = tilesetCRC ) ;
networkGameDataSynchCheckOkTech = ( networkMessageSynchNetworkGameDataStatus . getTechCRC ( ) = = techCRC ) ;
// For testing
//techCRC++;
if ( networkGameDataSynchCheckOkMap = = true & &
networkGameDataSynchCheckOkTile = = true & &
networkGameDataSynchCheckOkTech = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] client data synch ok \n " , __FILE__ , __FUNCTION__ ) ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] mapCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , mapCRC , networkMessageSynchNetworkGameDataStatus . getMapCRC ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] tilesetCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , tilesetCRC , networkMessageSynchNetworkGameDataStatus . getTilesetCRC ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] techCRC = %d, remote = %d \n " , __FILE__ , __FUNCTION__ , techCRC , networkMessageSynchNetworkGameDataStatus . getTechCRC ( ) ) ;
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 ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " 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(string(GameConstants::folder_path_tilesets) + "/" + serverInterface->getGameSettings()->getTileset() + "/*", "", &vctFileList);
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTilesets , scenarioDir ) , string ( " / " ) + serverInterface - > getGameSettings ( ) - > getTileset ( ) + string ( " /* " ) , " " , & vctFileList ) ;
}
else {
//vctFileList = getFolderTreeContentsCheckSumListRecursively(string(GameConstants::folder_path_tilesets) + "/" + serverInterface->getGameSettings()->getTileset() + "/*", ".xml", &vctFileList);
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(string(GameConstants::folder_path_techs) + "/" + serverInterface->getGameSettings()->getTech() + "/*", "", &vctFileList);
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " " , & vctFileList ) ;
}
else {
//vctFileList = getFolderTreeContentsCheckSumListRecursively(string(GameConstants::folder_path_techs) + "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", &vctFileList);
vctFileList = getFolderTreeContentsCheckSumListRecursively ( config . getPathListForType ( ptTechs , scenarioDir ) , " / " + serverInterface - > getGameSettings ( ) - > getTech ( ) + " /* " , " .xml " , & vctFileList ) ;
}
2010-08-22 10:00:05 +02:00
2010-08-26 05:15:36 +02:00
string report = networkMessageSynchNetworkGameDataStatus . getTechCRCFileMismatchReport ( serverInterface - > getGameSettings ( ) - > getTech ( ) , vctFileList ) ;
2010-08-22 10:00:05 +02:00
this - > setNetworkGameDataSynchCheckTechMismatchReport ( report ) ;
2010-06-29 08:50:35 +02:00
}
2010-08-20 22:03:06 +02:00
if ( networkGameDataSynchCheckOkMap = = false ) {
2010-09-17 10:26:22 +02:00
vctFileList . push_back ( std : : pair < string , int32 > ( Map : : getMapPath ( serverInterface - > getGameSettings ( ) - > getMap ( ) , scenarioDir , false ) , mapCRC ) ) ;
2010-06-29 08:50:35 +02:00
}
2010-05-25 20:06:42 +02:00
2010-08-20 22:03:06 +02:00
//for(int i = 0; i < vctFileList.size(); i++)
//{
NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck ( vctFileList . size ( ) , 1 , vctFileList [ 0 ] . second , vctFileList [ 0 ] . first ) ;
sendMessage ( & networkMessageSynchNetworkGameDataFileCRCCheck ) ;
//}
}
2010-08-22 10:00:05 +02:00
else {
if ( networkGameDataSynchCheckOkTech = = false ) {
//if(techCRC == 0) {
//vctFileList = getFolderTreeContentsCheckSumListRecursively(string(GameConstants::folder_path_techs) + "/" + serverInterface->getGameSettings()->getTech() + "/*", "", &vctFileList);
//vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),"/" + serverInterface->getGameSettings()->getTech() + "/*", "", &vctFileList);
//}
//else {
//vctFileList = getFolderTreeContentsCheckSumListRecursively(string(GameConstants::folder_path_techs) + "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", &vctFileList);
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 ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
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
2010-06-29 08:50:35 +02:00
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 ) ;
}
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
2010-06-29 08:50:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] got nmtSynchNetworkGameDataFileGet \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
NetworkMessageSynchNetworkGameDataFileGet networkMessageSynchNetworkGameDataFileGet ;
if ( receiveMessage ( & networkMessageSynchNetworkGameDataFileGet ) ) {
FileTransferInfo fileInfo ;
fileInfo . hostType = eServer ;
//fileInfo.serverIP = this->ip.getString();
fileInfo . serverPort = Config : : getInstance ( ) . getInt ( " ServerPort " , intToStr ( GameConstants : : serverPort ) . c_str ( ) ) ;
fileInfo . fileName = networkMessageSynchNetworkGameDataFileGet . getFileName ( ) ;
FileTransferSocketThread * fileXferThread = new FileTransferSocketThread ( fileInfo ) ;
fileXferThread - > start ( ) ;
}
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 :
{
2010-08-23 06:33:21 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gotIntro ) ;
2010-08-20 22:03:06 +02:00
if ( gotIntro = = true ) {
SwitchSetupRequest switchSetupRequest ;
if ( receiveMessage ( & switchSetupRequest ) ) {
Mutex * mutex = getServerSynchAccessor ( ) ;
if ( mutex ! = NULL ) mutex - > p ( ) ;
2010-08-23 06:33:21 +02:00
int factionIdx = switchSetupRequest . getCurrentFactionIndex ( ) ;
if ( serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] = = NULL ) {
serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] = new SwitchSetupRequest ( ) ;
2010-08-20 22:03:06 +02:00
}
2010-08-23 06:33:21 +02:00
* ( serverInterface - > getSwitchSetupRequests ( ) [ factionIdx ] ) = switchSetupRequest ;
2010-08-26 07:30:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getSwitchFlags() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionIdx , switchSetupRequest . getNetworkPlayerName ( ) . c_str ( ) , switchSetupRequest . getSwitchFlags ( ) ) ;
2010-05-25 20:06:42 +02:00
2010-08-20 22:03:06 +02:00
if ( mutex ! = NULL ) mutex - > v ( ) ;
2010-06-29 08:50:35 +02:00
}
}
break ;
2010-05-13 08:43:56 +02:00
}
2010-06-29 08:50:35 +02:00
case nmtReady :
2010-05-25 20:06:42 +02:00
{
2010-06-29 08:50:35 +02:00
// its simply ignored here. Probably we are starting a game
break ;
2010-05-13 08:43:56 +02:00
}
2010-06-29 08:50:35 +02:00
default :
{
2010-08-23 06:33:21 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] networkMessageType = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , networkMessageType ) ;
2010-06-29 08:50:35 +02:00
if ( gotIntro = = true ) {
//throw runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType));
string sErr = " Unexpected message in connection slot: " + intToStr ( networkMessageType ) ;
//sendTextMessage(sErr,-1);
//DisplayErrorMessage(sErr);
threadErrorList . push_back ( sErr ) ;
return ;
}
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] got invalid message type before intro, disconnecting socket. \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
close ( ) ;
}
}
}
2010-05-25 20:06:42 +02:00
}
2010-08-20 22:03:06 +02:00
validateConnection ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-25 20:06:42 +02:00
else {
2010-05-28 16:59:09 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] calling close... \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-12 17:25:56 +02:00
close ( ) ;
}
2010-06-29 08:50:35 +02:00
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 ( ) ) ;
2010-05-25 20:06:42 +02:00
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 ( ) ) ;
2010-03-17 07:25:19 +01:00
}
}
2010-08-20 22:03:06 +02:00
void ConnectionSlot : : validateConnection ( ) {
if ( gotIntro = = false & & connectedTime > 0 & &
difftime ( time ( NULL ) , connectedTime ) > GameConstants : : maxClientConnectHandshakeSecs ) {
2010-12-24 09:43:09 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] difftime(time(NULL),connectedTime) = %f\n",__FILE__,__FUNCTION__,__LINE__,difftime(time(NULL),connectedTime));
2010-08-20 22:03:06 +02:00
close ( ) ;
}
}
2010-05-15 20:59:17 +02:00
void ConnectionSlot : : close ( ) {
2010-05-28 01:46:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s LINE: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2011-01-11 21:02:07 +01:00
if ( this - > slotThreadWorker ! = NULL ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
this - > slotThreadWorker - > setAllEventsCompleted ( ) ;
2011-01-11 19:39:18 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-04 01:00:31 +02:00
bool updateServerListener = ( socket ! = NULL ) ;
2010-03-17 07:25:19 +01:00
delete socket ;
socket = NULL ;
2010-05-28 01:46:38 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s LINE: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-04 01:00:31 +02:00
if ( updateServerListener = = true & & ready = = false ) {
2010-05-28 16:59:09 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s LINE: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
serverInterface - > updateListen ( ) ;
}
2010-05-12 17:25:56 +02:00
ready = false ;
gotIntro = false ;
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] END \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-15 20:59:17 +02:00
bool ConnectionSlot : : hasValidSocketId ( ) {
2010-03-17 07:25:19 +01:00
bool result = ( socket ! = NULL & & socket - > getSocketId ( ) > 0 ) ;
return result ;
}
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 ) {
2011-01-11 20:16:48 +01:00
//assert(slotThreadWorker != NULL);
2010-05-28 01:46:38 +02:00
2010-07-10 08:14:31 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex);
2010-06-28 05:17:50 +02:00
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-07-10 08:14:31 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, waitingForThread = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex,waitingForThread);
2010-06-28 05:17:50 +02:00
2010-05-28 01:46:38 +02:00
return ( waitingForThread = = false ) ;
}
2010-06-03 09:52:17 +02:00
void ConnectionSlot : : sendMessage ( const NetworkMessage * networkMessage ) {
MutexSafeWrapper safeMutex ( & socketSynchAccessor ) ;
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 ) {
MutexSafeWrapper safeMutexSlot ( & mutexPendingNetworkCommandList , intToStr ( __LINE__ ) ) ;
vector < NetworkCommand > ret = vctPendingNetworkCommandList ;
if ( clearList = = true ) {
vctPendingNetworkCommandList . clear ( ) ;
}
safeMutexSlot . ReleaseLock ( ) ;
return ret ;
}
void ConnectionSlot : : clearPendingNetworkCommandList ( ) {
MutexSafeWrapper safeMutexSlot ( & mutexPendingNetworkCommandList , intToStr ( __LINE__ ) ) ;
vctPendingNetworkCommandList . clear ( ) ;
}
2010-03-17 07:25:19 +01:00
} } //end namespace