File: GameServer.h

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (251 lines) | stat: -rwxr-xr-x 6,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _GAME_SERVER_H
#define _GAME_SERVER_H

#include <boost/thread/thread.hpp>
#include <boost/scoped_ptr.hpp>
#include <string>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <list>

#include "GameData.h"
#include "Sim/Misc/TeamBase.h"
#include "System/UnsyncedRNG.h"
#include "System/float3.h"
#include "System/Misc/SpringTime.h"
#include "System/Platform/Synchro.h"


namespace netcode
{
	class RawPacket;
	class CConnection;
	class UDPListener;
}
class CDemoReader;
class Action;
class CDemoRecorder;
class AutohostInterface;
class CGameSetup;
class ClientSetup;
class ChatMessage;
class GameParticipant;
class GameSkirmishAI;

/**
 * When the Server generates a message,
 * this value is used as the sending player-number.
 */
const unsigned SERVER_PLAYER = 255;
const unsigned numCommands = 20;
extern const std::string commands[numCommands];

class GameTeam : public TeamBase
{
public:
	GameTeam() : active(false) {}
	bool active;
	GameTeam& operator=(const TeamBase& base) { TeamBase::operator=(base); return *this; }
};

/**
 * @brief Server class for game handling
 * This class represents a gameserver. It is responsible for recieving,
 * checking and forwarding gamedata to the clients. It keeps track of the sync,
 * cpu and other stats and informs all clients about events.
 */
class CGameServer
{
	friend class CCregLoadSaveHandler; // For initializing server state after load
public:
	CGameServer(const std::string& hostIP, int hostPort, const GameData* const gameData, const CGameSetup* const setup);
	~CGameServer();

	void AddLocalClient(const std::string& myName, const std::string& myVersion);

	void AddAutohostInterface(const std::string& autohostIP, const int autohostPort);

	/**
	 * @brief Set frame after loading
	 * WARNING! No checks are done, so be carefull
	 */
	void PostLoad(unsigned lastTick, int serverFrameNum);

	void CreateNewFrame(bool fromServerThread, bool fixedFrameTime);

	bool WaitsOnCon() const;

	void SetGamePausable(const bool arg);

	bool HasStarted() const { return gameHasStarted; }
	bool HasGameID() const { return generatedGameID; }
	/// Is the server still running?
	bool HasFinished() const;

	void UpdateSpeedControl(int speedCtrl);
	static std::string SpeedControlToString(int speedCtrl);

	#ifdef DEDICATED
	const boost::scoped_ptr<CDemoRecorder>& GetDemoRecorder() const { return demoRecorder; }
	#endif

private:
	/**
	 * @brief relay chat messages to players / autohost
	 */
	void GotChatMessage(const ChatMessage& msg);

	/// Execute textual messages received from clients
	void PushAction(const Action& action);

	/**
	 * @brief kick the specified player from the battle
	 */
	void KickPlayer(const int playerNum);

	unsigned BindConnection(std::string name, const std::string& passwd, const std::string& version, bool isLocal, boost::shared_ptr<netcode::CConnection> link, bool reconnect = false, int netloss = 0);

	void CheckForGameStart(bool forced=false);
	void StartGame();
	void UpdateLoop();
	void Update();
	void ProcessPacket(const unsigned playerNum, boost::shared_ptr<const netcode::RawPacket> packet);
	void CheckSync();
	void ServerReadNet();

	/** @brief Generate a unique game identifier and send it to all clients. */
	void GenerateAndSendGameID();
	std::string GetPlayerNames(const std::vector<int>& indices) const;

	/// read data from demo and send it to clients
	bool SendDemoData(int targetFrameNum);

	void Broadcast(boost::shared_ptr<const netcode::RawPacket> packet);

	/**
	 * @brief skip frames
	 *
	 * If you are watching a demo, this will push out all data until
	 * targetFrame to all clients
	 */
	void SkipTo(int targetFrameNum);

	void Message(const std::string& message, bool broadcast = true);
	void PrivateMessage(int playerNum, const std::string& message);

	void AddToPacketCache(boost::shared_ptr<const netcode::RawPacket>& pckt);

	bool AdjustPlayerNumber(netcode::RawPacket* buf, int pos, int val = -1);
	void UpdatePlayerNumberMap();

	float GetDemoTime() const;

	/////////////////// game status variables ///////////////////

	unsigned char playerNumberMap[256];
	volatile bool quitServer;
	int serverFrameNum;

	spring_time serverStartTime;
	spring_time readyTime;
	spring_time gameStartTime;
	spring_time gameEndTime;	///< Tick when game end was detected
	spring_time lastTick;
	float timeLeft;
	spring_time lastPlayerInfo;
	spring_time lastUpdate;
	float modGameTime;
	float gameTime;
	float startTime;

	bool isPaused;
	float userSpeedFactor;
	float internalSpeed;
	bool cheating;

	unsigned char ReserveNextAvailableSkirmishAIId();

	std::map<unsigned char, GameSkirmishAI> ais;
	std::list<unsigned char> usedSkirmishAIIds;
	void FreeSkirmishAIId(const unsigned char skirmishAIId);

	std::vector<GameParticipant> players;
	std::vector<GameTeam> teams;
	std::vector<unsigned char> winningAllyTeams;

	float medianCpu;
	int medianPing;
	int curSpeedCtrl;

	/**
	 * throttles speed based on:
	 * 0 : players (max cpu)
	 * 1 : players (median cpu)
	 * 2 : (same as 0)
	 * -x: same as x, but ignores votes from players that may change
	 *     the speed-control mode
	 */
	int speedControl;
	/////////////////// game settings ///////////////////
	boost::scoped_ptr<const CGameSetup> setup;
	boost::scoped_ptr<const GameData> gameData;
	/// Wheter the game is pausable for others than the host
	bool gamePausable;

	/// The maximum speed users are allowed to set
	float maxUserSpeed;

	/// The minimum speed users are allowed to set (actual speed can be lower due to high cpu usage)
	float minUserSpeed;

	bool noHelperAIs;
	bool canReconnect;
	bool allowSpecDraw;
	bool allowAdditionalPlayers;
	bool whiteListAdditionalPlayers;
	std::list< std::vector<boost::shared_ptr<const netcode::RawPacket> > > packetCache;

	/////////////////// sync stuff ///////////////////
#ifdef SYNCCHECK
	std::set<int> outstandingSyncFrames;
#endif
	int syncErrorFrame;
	int syncWarningFrame;

	///////////////// internal stuff //////////////////
	void InternalSpeedChange(float newSpeed);
	void UserSpeedChange(float newSpeed, int player);

	void AddAdditionalUser( const std::string& name, const std::string& passwd, bool fromDemo = false, bool spectator = true, int team = 0);

	bool hasLocalClient;
	unsigned localClientNumber;

	/// If the server receives a command, it will forward it to clients if it is not in this set
	std::set<std::string> commandBlacklist;

	boost::scoped_ptr<netcode::UDPListener> UDPNet;
	boost::scoped_ptr<CDemoReader> demoReader;
#ifdef DEDICATED
	boost::scoped_ptr<CDemoRecorder> demoRecorder;
#endif
	boost::scoped_ptr<AutohostInterface> hostif;
	UnsyncedRNG rng;
	boost::thread* thread;

	mutable Threading::RecursiveMutex gameServerMutex;

	volatile bool gameHasStarted;
	volatile bool generatedGameID;

	spring_time lastBandwidthUpdate;
	int linkMinPacketSize;
};

extern CGameServer* gameServer;

#endif // _GAME_SERVER_H