File: BaseNetProtocol.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 (228 lines) | stat: -rwxr-xr-x 12,078 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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef _BASE_NET_PROTOCOL_H
#define _BASE_NET_PROTOCOL_H

#include <boost/shared_ptr.hpp>
#include <boost/cstdint.hpp>
#include <vector>
#include <string>

namespace netcode
{
	class RawPacket;
}
struct PlayerStatistics;

const unsigned short NETWORK_VERSION = 5;

/*
 * Comment behind NETMSG enumeration constant gives the extra data belonging to
 * the net message.
 * An empty comment means no extra data (message is only 1 byte).
 * Messages either consist of:
 *  1. uchar command; (NETMSG_* constant) and the specified extra data; or
 *  2. uchar command; uchar messageSize; and the specified extra data,
 *     for messages that contain a trailing std::string in the extra data; or
 *  3. uchar command; short messageSize; and the specified extra data,
 *     for messages that contain a trailing std::vector in the extra data.
 * Note that NETMSG_MAPDRAW can behave like 1. or 2. depending on the
 * MapDrawAction::NET_* command. messageSize is always the size of the entire
 * message in bytes, including 'command' and 'messageSize'.
 */

enum NETMSG {
	NETMSG_KEYFRAME         = 1,  // int framenum
	NETMSG_NEWFRAME         = 2,  //
	NETMSG_QUIT             = 3,  // string reason
	NETMSG_STARTPLAYING     = 4,  // uint countdown
	NETMSG_SETPLAYERNUM     = 5,  // uchar myPlayerNum;
	NETMSG_PLAYERNAME       = 6,  // uchar myPlayerNum; std::string playerName;
	NETMSG_CHAT             = 7,  // uchar from, dest; std::string message;
	NETMSG_RANDSEED         = 8,  // uint randSeed;
	NETMSG_GAMEID           = 9,  // uchar gameID[16];
	NETMSG_PATH_CHECKSUM    = 10, // uchar myPlayerNum, boost::uint32_t checksum
	NETMSG_COMMAND          = 11, // uchar myPlayerNum; int id; uchar options; std::vector<float> params;
	NETMSG_SELECT           = 12, // uchar myPlayerNum; std::vector<short> selectedUnitIDs;
	NETMSG_PAUSE            = 13, // uchar playerNum, bPaused;

	NETMSG_AICOMMAND        = 14, // uchar myPlayerNum; uchar aiID; short unitID; int id; uchar options; std::vector<float> params;
	NETMSG_AICOMMANDS       = 15, // uchar myPlayerNum; uchar aiID;
	                              // short unitIDCount;  unitIDCount * short(unitID)
	                              // short commandCount; commandCount * { int id; uchar options; std::vector<float> params }
	NETMSG_AISHARE          = 16, // uchar myPlayerNum, uchar aiID, uchar sourceTeam, uchar destTeam, float metal, float energy, std::vector<short> unitIDs

	NETMSG_USER_SPEED       = 19, // uchar myPlayerNum, float userSpeed;
	NETMSG_INTERNAL_SPEED   = 20, // float internalSpeed;
	NETMSG_CPU_USAGE        = 21, // float cpuUsage;
	NETMSG_DIRECT_CONTROL   = 22, // uchar myPlayerNum;
	NETMSG_DC_UPDATE        = 23, // uchar myPlayerNum, status; short heading, pitch;
	NETMSG_SHARE            = 26, // uchar myPlayerNum, shareTeam, bShareUnits; float shareMetal, shareEnergy;
	NETMSG_SETSHARE         = 27, // uchar myPlayerNum, uchar myTeam; float metalShareFraction, energyShareFraction;
	NETMSG_SENDPLAYERSTAT   = 28, //
	NETMSG_PLAYERSTAT       = 29, // uchar myPlayerNum; CPlayer::Statistics currentStats;
	NETMSG_GAMEOVER         = 30, // uchar myPlayerNum; std::vector<uchar> winningAllyTeams
	NETMSG_MAPDRAW          = 31, // uchar messageSize =  8, myPlayerNum, command = MapDrawAction::NET_ERASE; short x, z;
	                              // uchar messageSize = 12, myPlayerNum, command = MapDrawAction::NET_LINE; short x1, z1, x2, z2;
	                              // /*messageSize*/   uchar myPlayerNum, command = MapDrawAction::NET_POINT; short x, z; std::string label;
	NETMSG_SYNCRESPONSE     = 33, // uchar myPlayerNum; int frameNum; uint checksum;
	NETMSG_SYSTEMMSG        = 35, // uchar myPlayerNum, std::string message;
	NETMSG_STARTPOS         = 36, // uchar myPlayerNum, uchar myTeam, ready /*0: not ready, 1: ready, 2: don't update readiness*/; float x, y, z;
	NETMSG_PLAYERINFO       = 38, // uchar myPlayerNum; float cpuUsage; int ping /*in frames*/;
	NETMSG_PLAYERLEFT       = 39, // uchar myPlayerNum, bIntended /*0: lost connection, 1: left, 2: forced (kicked) */;

#ifdef SYNCDEBUG
	NETMSG_SD_CHKREQUEST    = 41,
	NETMSG_SD_CHKRESPONSE   = 42,
	NETMSG_SD_BLKREQUEST    = 43,
	NETMSG_SD_BLKRESPONSE   = 44,
	NETMSG_SD_RESET         = 45,
#endif // SYNCDEBUG

	NETMSG_LUAMSG           = 50, // /* uint16_t messageSize */, uchar myPlayerNum, unsigned short script, uchar mode, std::vector<uint8_t> msg
	NETMSG_TEAM             = 51, // uchar myPlayerNum, uchar action, uchar parameter1
	NETMSG_GAMEDATA         = 52, // /* uchar messageSize */, std::string setupText, std::string script, std::string map, int mapChecksum,
	                              // std::string mod, int modChecksum, int randomSeed (each string ends with \0)
	NETMSG_ALLIANCE         = 53, // uchar myPlayerNum, uchar otherAllyTeam, uchar allianceState (0 = not allied / 1 = allied)
	NETMSG_CCOMMAND         = 54, // /* short! messageSize */, int! myPlayerNum, std::string command, std::string extra (each string ends with \0)
	NETMSG_CUSTOM_DATA      = 55, // uchar myPlayerNum, uchar dataType, uchar dataValue
	NETMSG_TEAMSTAT         = 60, // uchar teamNum, struct TeamStatistics statistics      # used by LadderBot #

	NETMSG_ATTEMPTCONNECT   = 65, // ushort msgsize, ushort netversion, string playername, string passwd, string VERSION_STRING_DETAILED

	NETMSG_AI_CREATED       = 70, // /* uchar messageSize */, uchar myPlayerNum, uchar whichSkirmishAI, uchar team, std::string name (ends with \0)
	NETMSG_AI_STATE_CHANGED = 71, // uchar myPlayerNum, uchar whichSkirmishAI, uchar newState

	NETMSG_REQUEST_TEAMSTAT = 72, // uchar teamNum, ushort statFrameNum                   # used by LadderBot #

	NETMSG_CREATE_NEWPLAYER = 75, // uchar myPlayerNum, uchar spectator, uchar teamNum, std::string playerName #used for players not preset in script.txt#

	NETMSG_AICOMMAND_TRACKED= 76,  // uchar myPlayerNum; uchar aiID; short unitID; int id; uchar options; int aiCommandId, std::vector<float> params;

	NETMSG_GAME_FRAME_PROGRESS= 77, // int frameNum # this special packet skips queue & cache entirely, indicates current game progress for clients fast-forwarding to current point the game #


	NETMSG_LAST //max types of netmessages, internal only
};

/// Data types for NETMSG_CUSTOM_DATA
enum CustomData {
	CUSTOM_DATA_SPEEDCONTROL = 0,
	CUSTOM_DATA_LUADRAWTIME  = 1
};

/// sub-action-types of NETMSG_TEAM
enum TEAMMSG {
//	TEAMMSG_NAME            = number    parameter1, ...
	TEAMMSG_GIVEAWAY        = 1,     // team to give stuff to, team to take stuff from (player has to be leader of the team)
	TEAMMSG_RESIGN          = 2,     // not used
	TEAMMSG_JOIN_TEAM       = 3,     // team to join
	TEAMMSG_TEAM_DIED       = 4,     // team which had died special note: this is sent by all players to prevent cheating
//TODO: changing teams (to spectator, from spectator to specific team)
//TODO: in-game allyteams
};

/// sub-action-types of NETMSG_MAPDRAW
enum MapDrawAction {
	MAPDRAW_POINT,
	MAPDRAW_ERASE,
	MAPDRAW_LINE
};

/**
 * @brief A factory used to make often-used network messages.
 *
 * Use this if you want to create a network message. Implemented as a singleton.
 */
class CBaseNetProtocol
{
public:
	typedef unsigned char uchar;
	typedef unsigned int uint;
	typedef boost::shared_ptr<const netcode::RawPacket> PacketType;

	static CBaseNetProtocol& Get();

	PacketType SendKeyFrame(int frameNum);
	PacketType SendNewFrame();
	PacketType SendQuit(const std::string& reason);
	/// client can send these to force-start the game
	PacketType SendStartPlaying(unsigned countdown);
	PacketType SendSetPlayerNum(uchar myPlayerNum);
	PacketType SendPlayerName(uchar myPlayerNum, const std::string& playerName);
	PacketType SendRandSeed(uint randSeed);
	PacketType SendGameID(const uchar* buf);
	PacketType SendPathCheckSum(uchar myPlayerNum, boost::uint32_t checksum);
	PacketType SendCommand(uchar myPlayerNum, int id, uchar options, const std::vector<float>& params);
	PacketType SendSelect(uchar myPlayerNum, const std::vector<short>& selectedUnitIDs);
	PacketType SendPause(uchar myPlayerNum, uchar bPaused);

	PacketType SendAICommand(uchar myPlayerNum, unsigned char aiID, short unitID, int id, int aiCommandId, uchar options, const std::vector<float>& params);
	PacketType SendAIShare(uchar myPlayerNum, unsigned char aiID, uchar sourceTeam, uchar destTeam, float metal, float energy, const std::vector<short>& unitIDs);

	PacketType SendUserSpeed(uchar myPlayerNum, float userSpeed);
	PacketType SendInternalSpeed(float internalSpeed);
	PacketType SendCPUUsage(float cpuUsage);
	PacketType SendCustomData(uchar myPlayerNum, uchar dataType, int dataValue);
	PacketType SendSpeedControl(uchar myPlayerNum, int speedCtrl);
	PacketType SendLuaDrawTime(uchar myPlayerNum, int mSec);
	PacketType SendDirectControl(uchar myPlayerNum);
	PacketType SendDirectControlUpdate(uchar myPlayerNum, uchar status, short heading, short pitch);
	PacketType SendAttemptConnect(const std::string& name, const std::string& passwd, const std::string& version, int netloss, bool reconnect = false);
	PacketType SendShare(uchar myPlayerNum, uchar shareTeam, uchar bShareUnits, float shareMetal, float shareEnergy);
	PacketType SendSetShare(uchar myPlayerNum, uchar myTeam, float metalShareFraction, float energyShareFraction);
	PacketType SendSendPlayerStat();
	PacketType SendPlayerStat(uchar myPlayerNum, const PlayerStatistics& currentStats);
	PacketType SendGameOver(uchar myPlayerNum, const std::vector<uchar>& winningAllyTeams);
	PacketType SendMapErase(uchar myPlayerNum, short x, short z);
	PacketType SendMapDrawLine(uchar myPlayerNum, short x1, short z1, short x2, short z2, bool);
	PacketType SendMapDrawPoint(uchar myPlayerNum, short x, short z, const std::string& label, bool);
	PacketType SendSyncResponse(uchar myPlayerNum, int frameNum, uint checksum);
	PacketType SendSystemMessage(uchar myPlayerNum, std::string message);
	PacketType SendStartPos(uchar myPlayerNum, uchar teamNum, uchar ready, float x, float y, float z);
	PacketType SendPlayerInfo(uchar myPlayerNum, float cpuUsage, int ping);
	PacketType SendPlayerLeft(uchar myPlayerNum, uchar bIntended);
	PacketType SendLuaMsg(uchar myPlayerNum, unsigned short script, uchar mode, const std::vector<boost::uint8_t>& msg);
	PacketType SendCurrentFrameProgress(int frameNum);

	PacketType SendGiveAwayEverything(uchar myPlayerNum, uchar giveToTeam);
	/**
	 * Gives everything from one team to another.
	 * The player issuing this command has to be leader of the takeFromTeam.
	 */
	PacketType SendGiveAwayEverything(uchar myPlayerNum, uchar giveToTeam, uchar takeFromTeam);
	PacketType SendResign(uchar myPlayerNum);
	PacketType SendJoinTeam(uchar myPlayerNum, uchar wantedTeamNum);
	/**
	 * This is currently only used to inform the server about its death.
	 * It may have some problems when desync, because the team may not die
	 * on every client.
	 */
	PacketType SendTeamDied(uchar myPlayerNum, uchar whichTeam);

	PacketType SendAICreated(const uchar myPlayerNum,
	                         const uchar whichSkirmishAI,
	                         const uchar team,
	                         const std::string& name);
	PacketType SendAIStateChanged(const uchar myPlayerNum,
	                              const uchar whichSkirmishAI,
	                              const uchar newState);

	PacketType SendSetAllied(uchar myPlayerNum, uchar whichAllyTeam, uchar state);

	PacketType SendCreateNewPlayer( uchar playerNum, bool spectator, uchar teamNum, std::string playerName);

#ifdef SYNCDEBUG
	PacketType SendSdCheckrequest(int frameNum);
	PacketType SendSdCheckresponse(uchar myPlayerNum, boost::uint64_t flop, std::vector<unsigned> checksums);
	PacketType SendSdReset();
	PacketType SendSdBlockrequest(unsigned short begin, unsigned short length, unsigned short requestSize);
	PacketType SendSdBlockresponse(uchar myPlayerNum, std::vector<unsigned> checksums);
#endif
private:
	CBaseNetProtocol();
	~CBaseNetProtocol();
};

#endif // _BASE_NET_PROTOCOL_H