2010-05-20 02:59:01 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_NETTURNMANAGER
|
|
|
|
#define INCLUDED_NETTURNMANAGER
|
|
|
|
|
|
|
|
#include "simulation2/helpers/SimulationCommand.h"
|
|
|
|
|
|
|
|
class CNetServer;
|
|
|
|
class CNetClient;
|
|
|
|
class CSimulationMessage;
|
|
|
|
class CSimulation2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file deals with the logic of the network turn system. The basic idea is as in
|
|
|
|
* http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php?print=1
|
|
|
|
*
|
|
|
|
* Each player performs the simulation for turn N.
|
|
|
|
* User input is translated into commands scheduled for execution in turn N+2 which are
|
|
|
|
* distributed to all other clients.
|
2010-06-30 23:41:04 +02:00
|
|
|
* After a while, a player wants to perform the simulation for turn N+1,
|
2010-05-20 02:59:01 +02:00
|
|
|
* which first requires that it has all the other clients' commands for turn N+1.
|
2010-06-30 23:41:04 +02:00
|
|
|
* In that case, it does the simulation and tells all the other clients (via the server)
|
|
|
|
* it has finished sending commands for turn N+2, and it starts sending commands for turn N+3.
|
2010-05-20 02:59:01 +02:00
|
|
|
*
|
|
|
|
* Commands are redistributed immediately by the server.
|
|
|
|
* To ensure a consistent execution of commands, they are each associated with a
|
|
|
|
* client session ID (which is globally unique and consistent), which is used to sort them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-06-30 23:41:04 +02:00
|
|
|
* Common network turn system (used by clients and offline games).
|
2010-05-20 02:59:01 +02:00
|
|
|
*/
|
|
|
|
class CNetTurnManager
|
|
|
|
{
|
|
|
|
NONCOPYABLE(CNetTurnManager);
|
|
|
|
public:
|
|
|
|
/**
|
2010-06-30 23:41:04 +02:00
|
|
|
* Construct for a given network session ID.
|
2010-05-20 02:59:01 +02:00
|
|
|
*/
|
2010-06-30 23:41:04 +02:00
|
|
|
CNetTurnManager(CSimulation2& simulation, int clientId);
|
2010-05-20 02:59:01 +02:00
|
|
|
|
|
|
|
virtual ~CNetTurnManager() { }
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
/**
|
|
|
|
* Set the current user's player ID, which will be added into command messages.
|
|
|
|
*/
|
|
|
|
void SetPlayerID(int playerId);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
/**
|
|
|
|
* Advance the simulation by a certain time. If this brings us past the current
|
|
|
|
* turn length, the next turn is processed and the function returns true.
|
|
|
|
* Otherwise, nothing happens and it returns false.
|
|
|
|
* @param frameLength length of the previous frame in seconds
|
|
|
|
*/
|
|
|
|
bool Update(float frameLength);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Advance the graphics by a certain time.
|
|
|
|
* @param frameLength length of the previous frame in seconds
|
|
|
|
*/
|
|
|
|
void Interpolate(float frameLength);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by networking code when a simulation message is received.
|
|
|
|
*/
|
|
|
|
virtual void OnSimulationMessage(CSimulationMessage* msg) = 0;
|
|
|
|
|
2010-05-22 16:05:56 +02:00
|
|
|
/**
|
|
|
|
* Called when there has been an out-of-sync error.
|
|
|
|
*/
|
|
|
|
virtual void OnSyncError(u32 turn, const std::string& expectedHash);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
/**
|
|
|
|
* Called by simulation code, to add a new command to be distributed to all clients and executed soon.
|
|
|
|
*/
|
|
|
|
virtual void PostCommand(CScriptValRooted data) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when all commands for a given turn have been received.
|
|
|
|
* This allows Update to progress to that turn.
|
|
|
|
*/
|
|
|
|
void FinishedAllCommands(u32 turn);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Store a command to be executed at a given turn.
|
|
|
|
*/
|
|
|
|
void AddCommand(int client, int player, CScriptValRooted data, u32 turn);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when this client has finished sending all its commands scheduled for the given turn.
|
|
|
|
*/
|
|
|
|
virtual void NotifyFinishedOwnCommands(u32 turn) = 0;
|
|
|
|
|
2010-05-22 16:05:56 +02:00
|
|
|
/**
|
|
|
|
* Called when this client has finished a simulation update, with the current state hash.
|
|
|
|
*/
|
|
|
|
virtual void NotifyFinishedUpdate(u32 turn, const std::string& hash) = 0;
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CSimulation2& m_Simulation2;
|
|
|
|
|
|
|
|
/// The turn that we have most recently executed
|
|
|
|
u32 m_CurrentTurn;
|
|
|
|
|
|
|
|
/// The latest turn for which we have received all commands from all clients
|
|
|
|
u32 m_ReadyTurn;
|
|
|
|
|
|
|
|
/// Commands queued at each turn (index 0 is for m_CurrentTurn+1)
|
|
|
|
std::deque<std::map<u32, std::vector<SimulationCommand> > > m_QueuedCommands;
|
|
|
|
|
|
|
|
int m_PlayerId;
|
|
|
|
uint m_ClientId;
|
|
|
|
|
|
|
|
/// Time remaining until we ought to execute the next turn
|
|
|
|
float m_DeltaTime;
|
2010-05-22 16:05:56 +02:00
|
|
|
|
|
|
|
bool m_HasSyncError;
|
2010-05-20 02:59:01 +02:00
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
/**
|
|
|
|
* Implementation of CNetTurnManager for network clients.
|
|
|
|
*/
|
2010-05-20 02:59:01 +02:00
|
|
|
class CNetClientTurnManager : public CNetTurnManager
|
|
|
|
{
|
|
|
|
public:
|
2010-06-30 23:41:04 +02:00
|
|
|
CNetClientTurnManager(CSimulation2& simulation, CNetClient& client, int clientId) :
|
|
|
|
CNetTurnManager(simulation, clientId), m_NetClient(client)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnSimulationMessage(CSimulationMessage* msg);
|
|
|
|
|
|
|
|
virtual void PostCommand(CScriptValRooted data);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void NotifyFinishedOwnCommands(u32 turn);
|
|
|
|
|
2010-05-22 16:05:56 +02:00
|
|
|
virtual void NotifyFinishedUpdate(u32 turn, const std::string& hash);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CNetClient& m_NetClient;
|
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
/**
|
|
|
|
* Implementation of CNetTurnManager for offline games.
|
|
|
|
*/
|
|
|
|
class CNetLocalTurnManager : public CNetTurnManager
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
public:
|
2010-06-30 23:41:04 +02:00
|
|
|
CNetLocalTurnManager(CSimulation2& simulation) :
|
|
|
|
CNetTurnManager(simulation, 0)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnSimulationMessage(CSimulationMessage* msg);
|
|
|
|
|
|
|
|
virtual void PostCommand(CScriptValRooted data);
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
protected:
|
|
|
|
virtual void NotifyFinishedOwnCommands(u32 turn);
|
|
|
|
|
|
|
|
virtual void NotifyFinishedUpdate(u32 turn, const std::string& hash);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server-side counterpart to CNetClientTurnManager.
|
|
|
|
* Records the turn state of each client, and sends turn advancement messages
|
|
|
|
* when all clients are ready.
|
|
|
|
*/
|
|
|
|
class CNetServerTurnManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CNetServerTurnManager(CNetServer& server);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void NotifyFinishedClientCommands(int client, u32 turn);
|
|
|
|
|
2010-05-22 16:05:56 +02:00
|
|
|
void NotifyFinishedClientUpdate(int client, u32 turn, const std::string& hash);
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
void InitialiseClient(int client);
|
|
|
|
|
|
|
|
protected:
|
2010-06-30 23:41:04 +02:00
|
|
|
/// The latest turn for which we have received all commands from all clients
|
|
|
|
u32 m_ReadyTurn;
|
2010-05-22 16:05:56 +02:00
|
|
|
|
|
|
|
// Client ID -> ready turn number (the latest turn for which all commands have been received from that client)
|
2010-05-20 02:59:01 +02:00
|
|
|
std::map<int, u32> m_ClientsReady;
|
|
|
|
|
2010-05-22 16:05:56 +02:00
|
|
|
// Client ID -> last known simulated turn number (for which we have the state hash)
|
|
|
|
// (the client has reached the start of this turn, not done the update for it yet)
|
|
|
|
std::map<int, u32> m_ClientsSimulated;
|
|
|
|
|
|
|
|
// Map of turn -> {Client ID -> state hash}; old indexes <= min(m_ClientsSimulated) are deleted
|
|
|
|
std::map<u32, std::map<int, std::string> > m_ClientStateHashes;
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
CNetServer& m_NetServer;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // INCLUDED_NETTURNMANAGER
|