2010-03-17 07:25:19 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-03-23 02:38:19 +01:00
// Copyright (C) 2001-2008 Marti<74> 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 "server_interface.h"
# include <cassert>
# include <stdexcept>
# include "platform_util.h"
# include "conversion.h"
# include "config.h"
# include "lang.h"
# include "logger.h"
# include <time.h>
2010-03-20 00:26:00 +01:00
# include "util.h"
# include "leak_dumper.h"
2010-03-17 07:25:19 +01:00
using namespace std ;
using namespace Shared : : Platform ;
using namespace Shared : : Util ;
namespace Glest { namespace Game {
2010-05-15 20:59:17 +02:00
ConnectionSlotThread : : ConnectionSlotThread ( ) : BaseThread ( ) {
this - > slotInterface = NULL ;
}
ConnectionSlotThread : : ConnectionSlotThread ( ConnectionSlotCallbackInterface * slotInterface ) : BaseThread ( ) {
this - > slotInterface = slotInterface ;
}
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 ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( event ! = NULL ) {
triggerIdMutex . p ( ) ;
this - > event = event ;
triggerIdMutex . v ( ) ;
}
semTaskSignalled . signal ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
void ConnectionSlotThread : : setTaskCompleted ( ConnectionSlotEvent * event ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( event ! = NULL ) {
triggerIdMutex . p ( ) ;
event - > eventCompleted = true ;
triggerIdMutex . v ( ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
bool ConnectionSlotThread : : isSignalCompleted ( ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
triggerIdMutex . p ( ) ;
bool result = this - > event - > eventCompleted ;
triggerIdMutex . v ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
return result ;
}
void ConnectionSlotThread : : execute ( ) {
try {
setRunningStatus ( true ) ;
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 ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
semTaskSignalled . waitTillSignalled ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
this - > slotInterface - > slotUpdateTask ( this - > event ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( getQuitStatus ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
break ;
}
setTaskCompleted ( this - > event ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
catch ( const exception & ex ) {
setRunningStatus ( false ) ;
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__ ) ;
setRunningStatus ( false ) ;
}
2010-03-17 07:25:19 +01:00
// =====================================================
// class ServerInterface
// =====================================================
ServerInterface : : ServerInterface ( ) {
gameHasBeenInitiated = false ;
gameSettingsUpdateCount = 0 ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
slots [ i ] = NULL ;
2010-04-29 01:59:37 +02:00
switchSetupRequests [ i ] = NULL ;
2010-05-15 20:59:17 +02:00
slotThreads [ i ] = NULL ;
2010-03-17 07:25:19 +01:00
}
serverSocket . setBlock ( false ) ;
2010-03-31 19:30:12 +02:00
serverSocket . bind ( Config : : getInstance ( ) . getInt ( " ServerPort " , intToStr ( GameConstants : : serverPort ) . c_str ( ) ) ) ;
2010-03-17 07:25:19 +01:00
}
ServerInterface : : ~ ServerInterface ( ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-05-15 20:59:17 +02:00
BaseThread : : shutdownAndWait ( slotThreads [ i ] ) ;
delete slotThreads [ i ] ;
slotThreads [ i ] = NULL ;
2010-03-17 07:25:19 +01:00
delete slots [ i ] ;
2010-04-29 01:59:37 +02:00
slots [ i ] = NULL ;
delete switchSetupRequests [ i ] ;
switchSetupRequests [ i ] = NULL ;
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
}
void ServerInterface : : addSlot ( int playerIndex ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
assert ( playerIndex > = 0 & & playerIndex < GameConstants : : maxPlayers ) ;
delete slots [ playerIndex ] ;
slots [ playerIndex ] = new ConnectionSlot ( this , playerIndex ) ;
updateListen ( ) ;
2010-05-15 20:59:17 +02:00
slotThreads [ playerIndex ] = new ConnectionSlotThread ( this ) ;
slotThreads [ playerIndex ] - > start ( ) ;
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-04-30 03:08:29 +02:00
bool ServerInterface : : switchSlot ( int fromPlayerIndex , int toPlayerIndex ) {
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
bool result = false ;
assert ( fromPlayerIndex > = 0 & & fromPlayerIndex < GameConstants : : maxPlayers ) ;
assert ( toPlayerIndex > = 0 & & toPlayerIndex < GameConstants : : maxPlayers ) ;
if ( fromPlayerIndex = = toPlayerIndex ) return false ; // doubleclicked or whatever
//printf(" checking if slot %d is free?\n",toPlayerIndex);
if ( ! slots [ toPlayerIndex ] - > isConnected ( ) ) {
//printf(" yes, its free :)\n");
slots [ fromPlayerIndex ] - > setPlayerIndex ( toPlayerIndex ) ;
2010-04-30 20:36:38 +02:00
slots [ toPlayerIndex ] - > setPlayerIndex ( fromPlayerIndex ) ;
2010-04-30 03:08:29 +02:00
ConnectionSlot * tmp = slots [ toPlayerIndex ] ;
slots [ toPlayerIndex ] = slots [ fromPlayerIndex ] ;
slots [ fromPlayerIndex ] = tmp ;
PlayerIndexMessage playerIndexMessage ( toPlayerIndex ) ;
slots [ toPlayerIndex ] - > sendMessage ( & playerIndexMessage ) ;
result = true ;
2010-04-30 20:36:38 +02:00
updateListen ( ) ;
2010-04-30 03:08:29 +02:00
}
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
return result ;
}
2010-05-15 20:59:17 +02:00
void ServerInterface : : removeSlot ( int playerIndex ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
//BaseThread::shutdownAndWait(slotThreads[playerIndex]);
//delete slotThreads[playerIndex];
//slotThreads[playerIndex] = NULL;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-03-17 07:25:19 +01:00
delete slots [ playerIndex ] ;
slots [ playerIndex ] = NULL ;
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-03-17 07:25:19 +01:00
updateListen ( ) ;
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d playerIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , playerIndex ) ;
2010-03-17 07:25:19 +01:00
}
ConnectionSlot * ServerInterface : : getSlot ( int playerIndex ) {
return slots [ playerIndex ] ;
}
int ServerInterface : : getConnectedSlotCount ( ) {
int connectedSlotCount = 0 ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
if ( slots [ i ] ! = NULL ) {
+ + connectedSlotCount ;
}
}
return connectedSlotCount ;
}
2010-05-15 20:59:17 +02:00
void ServerInterface : : slotUpdateTask ( ConnectionSlotEvent * event ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( event ! = NULL ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
updateSlot ( event ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
void ServerInterface : : updateSlot ( ConnectionSlotEvent * event ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( event ! = NULL ) {
ConnectionSlot * connectionSlot = event - > connectionSlot ;
bool & socketTriggered = event - > socketTriggered ;
bool checkForNewClients = true ;
if ( connectionSlot ! = NULL & &
( gameHasBeenInitiated = = false | | ( connectionSlot - > getSocket ( ) ! = NULL & & socketTriggered = = true ) ) ) {
if ( connectionSlot - > isConnected ( ) = = false | | socketTriggered = = true ) {
if ( gameHasBeenInitiated ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] socketTriggeredList[i] = %i \n " , __FILE__ , __FUNCTION__ , ( socketTriggered ? 1 : 0 ) ) ;
if ( connectionSlot - > isConnected ( ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] about to call connectionSlot->update() for socketId = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , connectionSlot - > getSocket ( ) - > getSocketId ( ) ) ;
}
else {
if ( gameHasBeenInitiated ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] getSocket() == NULL \n " , __FILE__ , __FUNCTION__ ) ;
}
connectionSlot - > update ( checkForNewClients ) ;
// This means no clients are trying to connect at the moment
if ( connectionSlot ! = NULL & & connectionSlot - > getSocket ( ) = = NULL ) {
checkForNewClients = false ;
}
if ( connectionSlot ! = NULL & &
connectionSlot - > getChatText ( ) . empty ( ) = = false ) {
chatText = connectionSlot - > getChatText ( ) ;
chatSender = connectionSlot - > getChatSender ( ) ;
chatTeamIndex = connectionSlot - > getChatTeamIndex ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatSender [%s] chatTeamIndex = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chatText . c_str ( ) , chatSender . c_str ( ) , chatTeamIndex ) ;
NetworkMessageText networkMessageText ( chatText , chatSender , chatTeamIndex ) ;
broadcastMessage ( & networkMessageText , connectionSlot - > getPlayerIndex ( ) ) ;
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__ ) ;
}
void ServerInterface : : update ( ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-13 09:29:48 +02:00
std : : map < PLATFORM_SOCKET , bool > socketTriggeredList ;
2010-03-17 07:25:19 +01:00
//update all slots
2010-05-15 20:59:17 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & & connectionSlot - > getSocket ( ) ! = NULL & &
2010-05-15 20:59:17 +02:00
slots [ i ] - > getSocket ( ) - > getSocketId ( ) > 0 ) {
2010-03-17 07:25:19 +01:00
socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] = false ;
}
}
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
chatText . clear ( ) ;
chatSender . clear ( ) ;
chatTeamIndex = - 1 ;
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( gameHasBeenInitiated = = false | | socketTriggeredList . size ( ) > 0 ) {
2010-03-20 00:26:00 +01:00
if ( gameHasBeenInitiated ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] socketTriggeredList.size() = %d \n " , __FILE__ , __FUNCTION__ , socketTriggeredList . size ( ) ) ;
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
std : : map < int , ConnectionSlotEvent > eventList ;
2010-03-17 07:25:19 +01:00
bool hasData = Socket : : hasDataToRead ( socketTriggeredList ) ;
2010-03-20 00:26:00 +01:00
if ( hasData ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] hasData == true \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
if ( gameHasBeenInitiated = = false | | hasData = = true ) {
2010-03-17 07:25:19 +01:00
//update all slots
bool checkForNewClients = true ;
2010-05-15 20:59:17 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
bool socketTriggered = ( connectionSlot ! = NULL & & connectionSlot - > getSocket ( ) ! = NULL ? socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] : false ) ;
ConnectionSlotEvent & event = eventList [ i ] ;
event . connectionSlot = connectionSlot ;
event . socketTriggered = socketTriggered ;
event . triggerId = i ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( slotThreads [ i ] ! = NULL ) {
slotThreads [ i ] - > signalUpdate ( & event ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
//updateSlot(event);
/*
2010-03-17 07:25:19 +01:00
if ( connectionSlot ! = NULL & &
2010-05-15 20:59:17 +02:00
( gameHasBeenInitiated = = false | | ( connectionSlot - > getSocket ( ) ! = NULL & & socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] = = true ) ) ) {
2010-03-17 07:25:19 +01:00
if ( connectionSlot - > isConnected ( ) = = false | |
2010-05-15 20:59:17 +02:00
( socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] = = true ) ) {
2010-03-20 00:26:00 +01:00
if ( gameHasBeenInitiated ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] socketTriggeredList[i] = %i \n " , __FILE__ , __FUNCTION__ , ( socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] ? 1 : 0 ) ) ;
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
if ( connectionSlot - > isConnected ( ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] about to call slots[i]->update() for slot = %d socketId = %d \n " ,
2010-03-17 07:25:19 +01:00
__FILE__ , __FUNCTION__ , i , connectionSlot - > getSocket ( ) - > getSocketId ( ) ) ;
}
2010-05-15 20:59:17 +02:00
else {
2010-03-20 00:26:00 +01:00
if ( gameHasBeenInitiated ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] slot = %d getSocket() == NULL \n " , __FILE__ , __FUNCTION__ , i ) ;
2010-03-17 07:25:19 +01:00
}
connectionSlot - > update ( checkForNewClients ) ;
// This means no clients are trying to connect at the moment
2010-05-15 20:59:17 +02:00
if ( connectionSlot ! = NULL & & connectionSlot - > getSocket ( ) = = NULL ) {
2010-03-17 07:25:19 +01:00
checkForNewClients = false ;
}
if ( connectionSlot ! = NULL & &
2010-05-15 20:59:17 +02:00
connectionSlot - > getChatText ( ) . empty ( ) = = false ) {
2010-03-17 07:25:19 +01:00
chatText = connectionSlot - > getChatText ( ) ;
chatSender = connectionSlot - > getChatSender ( ) ;
chatTeamIndex = connectionSlot - > getChatTeamIndex ( ) ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatSender [%s] chatTeamIndex = %d for SlotIndex# %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chatText . c_str ( ) , chatSender . c_str ( ) , chatTeamIndex , i ) ;
2010-03-17 07:25:19 +01:00
NetworkMessageText networkMessageText ( chatText , chatSender , chatTeamIndex ) ;
broadcastMessage ( & networkMessageText , i ) ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
break ;
}
}
}
2010-05-15 20:59:17 +02:00
*/
}
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
std : : map < int , bool > slotsCompleted ;
for ( bool threadsDone = false ; threadsDone = = false ; ) {
threadsDone = true ;
// Examine all threads for completion of delegation
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
if ( slotThreads [ i ] ! = NULL & & slotsCompleted . find ( i ) = = slotsCompleted . end ( ) ) {
if ( slotThreads [ i ] - > isSignalCompleted ( ) = = false & &
slotThreads [ i ] - > getQuitStatus ( ) = = false & &
slotThreads [ i ] - > getRunningStatus ( ) = = true ) {
threadsDone = false ;
break ;
}
else {
slotsCompleted [ i ] = true ;
}
}
}
sleep ( 0 ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
//process text messages
2010-05-15 20:59:17 +02:00
if ( chatText . empty ( ) = = true ) {
2010-03-17 07:25:19 +01:00
chatText . clear ( ) ;
chatSender . clear ( ) ;
chatTeamIndex = - 1 ;
2010-05-15 20:59:17 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & &
2010-05-15 20:59:17 +02:00
( gameHasBeenInitiated = = false | | ( connectionSlot - > getSocket ( ) ! = NULL & & socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] = = true ) ) ) {
if ( connectionSlot - > isConnected ( ) & & socketTriggeredList [ connectionSlot - > getSocket ( ) - > getSocketId ( ) ] = = true ) {
2010-03-20 00:26:00 +01:00
if ( connectionSlot - > getSocket ( ) ! = NULL ) SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] calling connectionSlot->getNextMessageType() for slots[i]->getSocket()->getSocketId() = %d \n " ,
2010-03-17 07:25:19 +01:00
__FILE__ , __FUNCTION__ , connectionSlot - > getSocket ( ) - > getSocketId ( ) ) ;
2010-05-15 20:59:17 +02:00
if ( connectionSlot - > getNextMessageType ( ) = = nmtText ) {
2010-03-17 07:25:19 +01:00
NetworkMessageText networkMessageText ;
2010-05-15 20:59:17 +02:00
if ( connectionSlot - > receiveMessage ( & networkMessageText ) ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] #2 about to broadcast nmtText msg for SlotIndex# %d \n " , __FILE__ , __FUNCTION__ , i ) ;
2010-03-17 07:25:19 +01:00
broadcastMessage ( & networkMessageText , i ) ;
chatText = networkMessageText . getText ( ) ;
chatSender = networkMessageText . getSender ( ) ;
chatTeamIndex = networkMessageText . getTeamIndex ( ) ;
break ;
}
2010-04-29 01:59:37 +02:00
}
2010-03-17 07:25:19 +01:00
}
}
}
}
2010-05-15 20:59:17 +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-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
}
void ServerInterface : : updateKeyframe ( int frameCount ) {
2010-04-15 03:19:00 +02:00
Chrono chrono ;
chrono . start ( ) ;
2010-03-17 07:25:19 +01:00
NetworkMessageCommandList networkMessageCommandList ( frameCount ) ;
//build command list, remove commands from requested and add to pending
2010-05-15 20:59:17 +02:00
while ( ! requestedCommands . empty ( ) ) {
if ( networkMessageCommandList . addCommand ( & requestedCommands . back ( ) ) ) {
2010-03-17 07:25:19 +01:00
pendingCommands . push_back ( requestedCommands . back ( ) ) ;
requestedCommands . pop_back ( ) ;
}
2010-05-15 20:59:17 +02:00
else {
2010-03-17 07:25:19 +01:00
break ;
}
}
2010-05-07 08:46:55 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] build command list took %d msecs, networkMessageCommandList.getCommandCount() = %d, frameCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , networkMessageCommandList . getCommandCount ( ) , frameCount ) ;
2010-04-15 03:19:00 +02:00
2010-03-17 07:25:19 +01:00
//broadcast commands
broadcastMessage ( & networkMessageCommandList ) ;
2010-04-15 03:19:00 +02:00
2010-05-07 08:46:55 +02:00
if ( chrono . getMillis ( ) > 0 ) SystemFlags : : OutputDebug ( SystemFlags : : debugPerformance , " In [%s::%s Line: %d] broadcastMessage took %d msecs, networkMessageCommandList.getCommandCount() = %d, frameCount = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , chrono . getMillis ( ) , networkMessageCommandList . getCommandCount ( ) , frameCount ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-04 04:32:43 +02:00
bool ServerInterface : : shouldDiscardNetworkMessage ( NetworkMessageType networkMessageType ,
ConnectionSlot * connectionSlot ) {
bool discard = false ;
if ( connectionSlot ! = NULL ) {
switch ( networkMessageType ) {
case nmtIntro :
{
discard = true ;
NetworkMessageIntro msg = NetworkMessageIntro ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtLaunch :
{
discard = true ;
NetworkMessageLaunch msg = NetworkMessageLaunch ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtText :
{
discard = true ;
NetworkMessageText msg = NetworkMessageText ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameData :
{
discard = true ;
NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataStatus :
{
discard = true ;
NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataFileCRCCheck :
{
discard = true ;
NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtSynchNetworkGameDataFileGet :
{
discard = true ;
NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtSwitchSetupRequest :
{
discard = true ;
SwitchSetupRequest msg = SwitchSetupRequest ( ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
case nmtPlayerIndexMessage :
{
discard = true ;
PlayerIndexMessage msg = PlayerIndexMessage ( 0 ) ;
connectionSlot - > receiveMessage ( & msg ) ;
}
break ;
}
}
return discard ;
}
2010-03-17 07:25:19 +01:00
void ServerInterface : : waitUntilReady ( Checksum * checksum ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s] START \n " , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
Logger & logger = Logger : : getInstance ( ) ;
gameHasBeenInitiated = true ;
Chrono chrono ;
bool allReady = false ;
chrono . start ( ) ;
//wait until we get a ready message from all clients
2010-05-04 04:32:43 +02:00
while ( allReady = = false ) {
2010-03-17 07:25:19 +01:00
vector < string > waitingForHosts ;
allReady = true ;
2010-05-04 04:32:43 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
2010-05-04 04:32:43 +02:00
if ( connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) = = true ) {
if ( connectionSlot - > isReady ( ) = = false ) {
2010-03-17 07:25:19 +01:00
NetworkMessageType networkMessageType = connectionSlot - > getNextMessageType ( true ) ;
2010-05-04 04:32:43 +02:00
// consume old messages from the lobby
bool discarded = shouldDiscardNetworkMessage ( networkMessageType , connectionSlot ) ;
if ( discarded = = false ) {
NetworkMessageReady networkMessageReady ;
if ( networkMessageType = = nmtReady & &
connectionSlot - > receiveMessage ( & networkMessageReady ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s] networkMessageType==nmtReady \n " , __FUNCTION__ ) ;
connectionSlot - > setReady ( ) ;
}
else if ( networkMessageType ! = nmtInvalid ) {
//throw runtime_error("Unexpected network message: " + intToStr(networkMessageType));
string sErr = " Unexpected network message: " + intToStr ( networkMessageType ) ;
sendTextMessage ( sErr , - 1 ) ;
DisplayErrorMessage ( sErr ) ;
return ;
}
2010-03-17 07:25:19 +01:00
}
2010-04-30 04:34:19 +02:00
//waitingForHosts.push_back(connectionSlot->getHostName());
waitingForHosts . push_back ( connectionSlot - > getName ( ) ) ;
2010-03-17 07:25:19 +01:00
allReady = false ;
}
}
}
//check for timeout
2010-05-04 04:32:43 +02:00
if ( allReady = = false ) {
if ( chrono . getMillis ( ) > readyWaitTimeout ) {
2010-03-17 07:25:19 +01:00
//throw runtime_error("Timeout waiting for clients");
string sErr = " Timeout waiting for clients. " ;
sendTextMessage ( sErr , - 1 ) ;
DisplayErrorMessage ( sErr ) ;
return ;
}
2010-05-04 04:32:43 +02:00
else {
if ( chrono . getMillis ( ) % 1000 = = 0 ) {
2010-03-17 07:25:19 +01:00
string waitForHosts = " " ;
2010-05-04 04:32:43 +02:00
for ( int i = 0 ; i < waitingForHosts . size ( ) ; i + + ) {
if ( waitForHosts ! = " " ) {
2010-03-17 07:25:19 +01:00
waitForHosts + = " , " ;
}
waitForHosts + = waitingForHosts [ i ] ;
}
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " Waiting for network: %d of %d max seconds (waiting for: %s) " , int ( chrono . getMillis ( ) / 1000 ) , int ( readyWaitTimeout / 1000 ) , waitForHosts . c_str ( ) ) ;
logger . add ( szBuf , true ) ;
}
}
}
}
// FOR TESTING ONLY - delay to see the client count up while waiting
//sleep(5000);
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s] PART B (telling client we are ready! \n " , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
//send ready message after, so clients start delayed
2010-05-04 04:32:43 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
2010-05-04 04:32:43 +02:00
if ( connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) = = true ) {
NetworkMessageReady networkMessageReady ( checksum - > getSum ( ) ) ;
2010-03-17 07:25:19 +01:00
connectionSlot - > sendMessage ( & networkMessageReady ) ;
}
}
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s] END \n " , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
}
void ServerInterface : : sendTextMessage ( const string & text , int teamIndex ) {
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-23 03:37:41 +01:00
NetworkMessageText networkMessageText ( text , Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) , teamIndex ) ;
2010-03-17 07:25:19 +01:00
broadcastMessage ( & networkMessageText ) ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
}
void ServerInterface : : quitGame ( bool userManuallyQuit )
{
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
if ( userManuallyQuit = = true )
{
2010-03-24 00:32:25 +01:00
string sQuitText = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " has chosen to leave the game! " ;
2010-03-17 07:25:19 +01:00
NetworkMessageText networkMessageText ( sQuitText , getHostName ( ) , - 1 ) ;
broadcastMessage ( & networkMessageText , - 1 ) ;
}
NetworkMessageQuit networkMessageQuit ;
broadcastMessage ( & networkMessageQuit ) ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
}
string ServerInterface : : getNetworkStatus ( ) const {
Lang & lang = Lang : : getInstance ( ) ;
string str ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
ConnectionSlot * connectionSlot = slots [ i ] ;
str + = intToStr ( i ) + " : " ;
if ( connectionSlot ! = NULL ) {
if ( connectionSlot - > isConnected ( ) ) {
str + = connectionSlot - > getName ( ) ;
}
}
else
{
str + = lang . get ( " NotConnected " ) ;
}
str + = ' \n ' ;
}
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
return str ;
}
bool ServerInterface : : launchGame ( const GameSettings * gameSettings ) {
bool bOkToStart = true ;
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-15 20:59:17 +02:00
serverSynchAccessor . p ( ) ;
2010-03-17 07:25:19 +01:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & &
connectionSlot - > getAllowDownloadDataSynch ( ) = = true & &
connectionSlot - > isConnected ( ) )
{
if ( connectionSlot - > getNetworkGameDataSynchCheckOk ( ) = = false )
{
bOkToStart = false ;
break ;
}
}
}
2010-05-15 20:59:17 +02:00
serverSynchAccessor . v ( ) ;
2010-03-17 07:25:19 +01:00
if ( bOkToStart = = true )
{
2010-04-01 08:31:10 +02:00
serverSocket . stopBroadCastThread ( ) ;
2010-04-22 01:13:39 +02:00
NetworkMessageLaunch networkMessageLaunch ( gameSettings , nmtLaunch ) ;
2010-03-17 07:25:19 +01:00
broadcastMessage ( & networkMessageLaunch ) ;
}
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
return bOkToStart ;
}
2010-05-15 20:59:17 +02:00
void ServerInterface : : broadcastGameSetup ( const GameSettings * gameSettings ) {
2010-04-22 01:13:39 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
NetworkMessageLaunch networkMessageLaunch ( gameSettings , nmtBroadCastSetup ) ;
broadcastMessage ( & networkMessageLaunch ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-03-17 07:25:19 +01:00
void ServerInterface : : broadcastMessage ( const NetworkMessage * networkMessage , int excludeSlot ) {
2010-05-15 20:59:17 +02:00
serverSynchAccessor . p ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
2010-05-15 20:59:17 +02:00
if ( i ! = excludeSlot & & connectionSlot ! = NULL ) {
if ( connectionSlot - > isConnected ( ) ) {
2010-03-17 07:25:19 +01:00
2010-05-15 20:59:17 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] before sendMessage \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
connectionSlot - > sendMessage ( networkMessage ) ;
}
2010-05-15 20:59:17 +02:00
else if ( gameHasBeenInitiated = = true ) {
2010-03-17 07:25:19 +01:00
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] #1 before removeSlot for slot# %d \n " , __FILE__ , __FUNCTION__ , i ) ;
2010-03-17 07:25:19 +01:00
removeSlot ( i ) ;
}
}
else if ( i = = excludeSlot & & gameHasBeenInitiated = = true & &
2010-05-15 20:59:17 +02:00
connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) = = false ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] #2 before removeSlot for slot# %d \n " , __FILE__ , __FUNCTION__ , i ) ;
2010-03-17 07:25:19 +01:00
removeSlot ( i ) ;
}
}
2010-05-15 20:59:17 +02:00
serverSynchAccessor . v ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-17 07:25:19 +01:00
}
void ServerInterface : : broadcastMessageToConnectedClients ( const NetworkMessage * networkMessage , int excludeSlot ) {
2010-05-15 20:59:17 +02:00
serverSynchAccessor . p ( ) ;
2010-03-17 07:25:19 +01:00
2010-04-11 03:25:06 +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-15 20:59:17 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-03-17 07:25:19 +01:00
ConnectionSlot * connectionSlot = slots [ i ] ;
2010-05-15 20:59:17 +02:00
if ( i ! = excludeSlot & & connectionSlot ! = NULL ) {
if ( connectionSlot - > isConnected ( ) ) {
2010-03-17 07:25:19 +01:00
2010-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] before sendMessage\n",__FILE__,__FUNCTION__);
2010-03-17 07:25:19 +01:00
connectionSlot - > sendMessage ( networkMessage ) ;
}
}
}
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-15 20:59:17 +02:00
serverSynchAccessor . v ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-15 20:59:17 +02:00
void ServerInterface : : updateListen ( ) {
if ( gameHasBeenInitiated = = true ) {
return ;
}
serverSynchAccessor . p ( ) ;
2010-03-17 07:25:19 +01:00
int openSlotCount = 0 ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
if ( slots [ i ] ! = NULL & & slots [ i ] - > isConnected ( ) = = false )
{
+ + openSlotCount ;
}
}
serverSocket . listen ( openSlotCount ) ;
2010-05-15 20:59:17 +02:00
serverSynchAccessor . v ( ) ;
2010-03-17 07:25:19 +01:00
}
2010-05-12 17:25:56 +02:00
int ServerInterface : : getOpenSlotCount ( ) {
int openSlotCount = 0 ;
2010-05-15 20:59:17 +02:00
serverSynchAccessor . p ( ) ;
2010-05-12 17:25:56 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
if ( slots [ i ] ! = NULL & & slots [ i ] - > isConnected ( ) = = false )
{
+ + openSlotCount ;
}
}
2010-05-15 20:59:17 +02:00
serverSynchAccessor . v ( ) ;
2010-05-12 17:25:56 +02:00
return openSlotCount ;
}
2010-03-17 07:25:19 +01:00
void ServerInterface : : setGameSettings ( GameSettings * serverGameSettings , bool waitForClientAck )
{
2010-04-29 08:10:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START gameSettingsUpdateCount = %d, waitForClientAck = %d \n " , __FILE__ , __FUNCTION__ , gameSettingsUpdateCount , waitForClientAck ) ;
2010-03-17 07:25:19 +01:00
if ( getAllowGameDataSynchCheck ( ) = = true )
{
if ( waitForClientAck = = true & & gameSettingsUpdateCount > 0 )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Waiting for client acks #1 \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
time_t tStart = time ( NULL ) ;
bool gotAckFromAllClients = false ;
while ( gotAckFromAllClients = = false & & difftime ( time ( NULL ) , tStart ) < = 5 )
{
gotAckFromAllClients = true ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) )
{
if ( connectionSlot - > getReceivedNetworkGameStatus ( ) = = false )
{
gotAckFromAllClients = false ;
}
connectionSlot - > update ( ) ;
}
}
}
}
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) )
{
connectionSlot - > setReceivedNetworkGameStatus ( false ) ;
}
}
gameSettings = * serverGameSettings ;
NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData ( getGameSettings ( ) ) ;
broadcastMessageToConnectedClients ( & networkMessageSynchNetworkGameData ) ;
if ( waitForClientAck = = true )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] Waiting for client acks #2 \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
time_t tStart = time ( NULL ) ;
bool gotAckFromAllClients = false ;
while ( gotAckFromAllClients = = false & & difftime ( time ( NULL ) , tStart ) < = 5 )
{
gotAckFromAllClients = true ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
ConnectionSlot * connectionSlot = slots [ i ] ;
if ( connectionSlot ! = NULL & & connectionSlot - > isConnected ( ) )
{
if ( connectionSlot - > getReceivedNetworkGameStatus ( ) = = false )
{
gotAckFromAllClients = false ;
}
connectionSlot - > update ( ) ;
}
}
}
}
gameSettingsUpdateCount + + ;
}
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
}
void ServerInterface : : close ( )
{
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugNetwork , " In [%s::%s] START \n " , __FILE__ , __FUNCTION__ ) ;
2010-03-17 07:25:19 +01:00
//serverSocket = ServerSocket();
}
} } //end namespace