File: BaseNetProtocol.cpp

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (618 lines) | stat: -rw-r--r-- 23,925 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "BaseNetProtocol.h"

#include "Game/Players/PlayerStatistics.h"
#include "Sim/Misc/TeamStatistics.h"
#include "System/Net/RawPacket.h"
#include "System/Net/PackPacket.h"
#include "System/Net/ProtocolDef.h"
#include <cinttypes>

using netcode::PackPacket;
typedef std::shared_ptr<const netcode::RawPacket> PacketType;

CBaseNetProtocol& CBaseNetProtocol::Get()
{
	static CBaseNetProtocol instance;
	return instance;
}

PacketType CBaseNetProtocol::SendKeyFrame(int32_t frameNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(frameNum), NETMSG_KEYFRAME);
	*packet << frameNum;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendNewFrame()
{
	return PacketType(new PackPacket(sizeof(uint8_t), NETMSG_NEWFRAME));
}


PacketType CBaseNetProtocol::SendQuit(const std::string& reason)
{
	// NOTE:
	//   transmit size as uint16, but calculate it using uint32's so overflow is safe
	//   the extra null-terminator +1 is no longer necessary, PackPacket handles this
	// const uint32_t headerSize = sizeof(static_cast<uint8_t>(NETMSG_QUIT)) + sizeof(static_cast<uint16_t>(packetSize));
	const uint32_t payloadSize = reason.size() + 1;
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_QUIT);
	*packet << static_cast<uint16_t>(packetSize) << reason;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendStartPlaying(uint32_t countdown)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(countdown), NETMSG_STARTPLAYING);
	*packet << countdown;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSetPlayerNum(uint8_t myPlayerNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum), NETMSG_SETPLAYERNUM);
	*packet << myPlayerNum;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendPlayerName(uint8_t myPlayerNum, const std::string& playerName)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + (playerName.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_PLAYERNAME);
	*packet << static_cast<uint8_t>(packetSize) << myPlayerNum << playerName;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendRandSeed(uint32_t randSeed)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(randSeed), NETMSG_RANDSEED);
	*packet << randSeed;
	return PacketType(packet);
}

// NETMSG_GAMEID = 9, char gameID[16];
PacketType CBaseNetProtocol::SendGameID(const uint8_t* buf)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + 16, NETMSG_GAMEID);
	memcpy(packet->GetWritingPos(), buf, 16);
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendPathCheckSum(uint8_t myPlayerNum, uint32_t checksum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(uint32_t), NETMSG_PATH_CHECKSUM);
	*packet << myPlayerNum;
	*packet << checksum;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendCommand(uint8_t myPlayerNum, int32_t id, uint8_t options, const std::vector<float>& params)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(id) + sizeof(options) + (params.size() * sizeof(float));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_COMMAND);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << id << options << params;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSelect(uint8_t myPlayerNum, const std::vector<int16_t>& selectedUnitIDs)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + (selectedUnitIDs.size() * sizeof(int16_t));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_SELECT);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << selectedUnitIDs;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendPause(uint8_t myPlayerNum, uint8_t bPaused)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(bPaused), NETMSG_PAUSE);
	*packet << myPlayerNum << bPaused;
	return PacketType(packet);
}



PacketType CBaseNetProtocol::SendAICommand(
	uint8_t myPlayerNum,
	uint8_t aiID,
	int16_t unitID,
	int32_t commandID,
	int32_t aiCommandID,
	uint8_t options,
	const std::vector<float>& params
) {
	const int32_t commandTypeID = (aiCommandID != -1)? NETMSG_AICOMMAND_TRACKED: NETMSG_AICOMMAND;

	const uint32_t payloadSize =
		sizeof(myPlayerNum) + sizeof(aiID) + sizeof(unitID) + sizeof(commandID) + sizeof(options) +
		(sizeof(commandTypeID) * (commandTypeID == NETMSG_AICOMMAND_TRACKED)) + (params.size() * sizeof(float));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	// packetSize must be an uint32_t for this to work
	if (packetSize >= (1 << (sizeof(uint16_t) * 8)))
		throw netcode::PackPacketException("[BaseNetProto::SendAICommand] maximum packet-size exceeded");

	PackPacket* packet = new PackPacket(packetSize, commandTypeID);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << aiID << unitID << commandID << options;

	if (commandTypeID == NETMSG_AICOMMAND_TRACKED)
		*packet << aiCommandID;

	*packet << params;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendAIShare(uint8_t myPlayerNum, uint8_t aiID, uint8_t sourceTeam, uint8_t destTeam, float metal, float energy, const std::vector<int16_t>& unitIDs)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(aiID) + sizeof(sourceTeam) + sizeof(destTeam) + (2 * sizeof(float)) + (unitIDs.size() * sizeof(int16_t));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	if (packetSize >= (1 << (sizeof(uint16_t) * 8)))
		throw netcode::PackPacketException("[BaseNetProto::SendAIShare] maximum packet-size exceeded");

	PackPacket* packet = new PackPacket(packetSize, NETMSG_AISHARE);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << aiID << sourceTeam << destTeam << metal << energy << unitIDs;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendUserSpeed(uint8_t myPlayerNum, float userSpeed)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(userSpeed), NETMSG_USER_SPEED);
	*packet << myPlayerNum << userSpeed;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendInternalSpeed(float internalSpeed)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(internalSpeed), NETMSG_INTERNAL_SPEED);
	*packet << internalSpeed;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendCPUUsage(float cpuUsage)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(cpuUsage), NETMSG_CPU_USAGE);
	*packet << cpuUsage;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendDirectControl(uint8_t myPlayerNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum), NETMSG_DIRECT_CONTROL);
	*packet << myPlayerNum;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendDirectControlUpdate(uint8_t myPlayerNum, uint8_t status, int16_t heading, int16_t pitch)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(status) + sizeof(heading) + sizeof(pitch), NETMSG_DC_UPDATE);
	*packet << myPlayerNum << status << heading << pitch;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendAttemptConnect(const std::string& name, const std::string& passwd, const std::string& version, int32_t netloss, bool reconnect)
{
	const uint32_t payloadSize = sizeof(NETWORK_VERSION) + sizeof(netloss) + sizeof(static_cast<uint8_t>(reconnect)) + name.size() + passwd.size() + version.size();
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize , NETMSG_ATTEMPTCONNECT);
	*packet << static_cast<uint16_t>(packetSize) << NETWORK_VERSION << name << passwd << version << uint8_t(reconnect) << uint8_t(netloss);
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendRejectConnect(const std::string& reason)
{
	const uint32_t payloadSize = reason.size() + 1;
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_REJECT_CONNECT);
	*packet << static_cast<uint16_t>(packetSize) << reason;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendShare(uint8_t myPlayerNum, uint8_t shareTeam, uint8_t bShareUnits, float shareMetal, float shareEnergy)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(shareTeam) + sizeof(bShareUnits) + (sizeof(shareMetal) * 2), NETMSG_SHARE);
	*packet << myPlayerNum << shareTeam << bShareUnits << shareMetal << shareEnergy;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSetShare(uint8_t myPlayerNum, uint8_t myTeam, float metalShareFraction, float energyShareFraction)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(myTeam) + (sizeof(metalShareFraction) * 2), NETMSG_SETSHARE);
	*packet << myPlayerNum << myTeam << metalShareFraction << energyShareFraction;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendPlayerStat(uint8_t myPlayerNum, const PlayerStatistics& currentStats)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(PlayerStatistics), NETMSG_PLAYERSTAT);
	*packet << myPlayerNum << currentStats;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendTeamStat(uint8_t teamNum, const TeamStatistics& currentStats)
{
	PackPacket* packet = new netcode::PackPacket(sizeof(uint8_t) + sizeof(teamNum) + sizeof(TeamStatistics), NETMSG_TEAMSTAT);
	*packet << teamNum << currentStats;
	return PacketType(packet);
}



PacketType CBaseNetProtocol::SendGameOver(uint8_t myPlayerNum, const std::vector<uint8_t>& winningAllyTeams)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + (winningAllyTeams.size() * sizeof(uint8_t));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_GAMEOVER);
	*packet << static_cast<uint8_t>(packetSize) << myPlayerNum << winningAllyTeams;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendMapErase(uint8_t myPlayerNum, int16_t x, int16_t z)
{
	constexpr uint8_t drawType = MAPDRAW_ERASE;

	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(drawType) + sizeof(x) + sizeof(z);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_MAPDRAW);
	*packet << static_cast<uint8_t>(packetSize) << myPlayerNum << drawType << x << z;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendMapDrawPoint(uint8_t myPlayerNum, int16_t x, int16_t z, const std::string& label, bool fromLua)
{
	constexpr uint8_t drawType = MAPDRAW_POINT;

	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(drawType) + sizeof(x) + sizeof(z) + sizeof(fromLua) + (label.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_MAPDRAW);
	*packet <<
		static_cast<uint8_t>(packetSize) <<
		myPlayerNum <<
		drawType <<
		x <<
		z <<
		static_cast<uint8_t>(fromLua) <<
		label;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendMapDrawLine(uint8_t myPlayerNum, int16_t x1, int16_t z1, int16_t x2, int16_t z2, bool fromLua)
{
	constexpr uint8_t drawType = MAPDRAW_LINE;

	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(drawType) + sizeof(x1) + sizeof(z1) + sizeof(x2) + sizeof(z2) + sizeof(fromLua);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_MAPDRAW);
	*packet <<
		static_cast<uint8_t>(packetSize) <<
		myPlayerNum <<
		drawType <<
		x1 << z1 <<
		x2 << z2 <<
		static_cast<uint8_t>(fromLua);
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendSyncResponse(uint8_t myPlayerNum, int32_t frameNum, uint32_t checksum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(frameNum) + sizeof(checksum), NETMSG_SYNCRESPONSE);
	*packet << myPlayerNum << frameNum << checksum;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSystemMessage(uint8_t myPlayerNum, std::string message)
{
	if (message.size() > 65000) {
		message.resize(65000);
		message.append("...");
	}

	const uint32_t payloadSize = sizeof(myPlayerNum) + (message.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_SYSTEMMSG);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << message;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendStartPos(uint8_t myPlayerNum, uint8_t teamNum, uint8_t readyState, float x, float y, float z)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(teamNum) + sizeof(readyState) + (3 * sizeof(x)), NETMSG_STARTPOS);
	*packet << myPlayerNum << teamNum << readyState << x << y << z;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendPlayerInfo(uint8_t myPlayerNum, float cpuUsage, int32_t ping)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(cpuUsage) + sizeof(ping), NETMSG_PLAYERINFO);
	*packet << myPlayerNum << cpuUsage << static_cast<uint32_t>(ping);
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendPlayerLeft(uint8_t myPlayerNum, uint8_t bIntended)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(bIntended), NETMSG_PLAYERLEFT);
	*packet << myPlayerNum << bIntended;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendLogMsg(uint8_t myPlayerNum, uint8_t logMsgLvl, const std::string& strData)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(logMsgLvl) + (strData.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	if (packetSize >= (1 << (sizeof(uint16_t) * 8)))
		throw netcode::PackPacketException("[BaseNetProto::SendLogMsg] maximum packet-size exceeded");

	PackPacket* packet = new PackPacket(packetSize, NETMSG_LOGMSG);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << logMsgLvl << strData;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendLuaMsg(uint8_t myPlayerNum, uint16_t script, uint8_t mode, const std::vector<uint8_t>& rawData)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(script) + sizeof(mode) + rawData.size();
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	if (packetSize >= (1 << (sizeof(uint16_t) * 8)))
		throw netcode::PackPacketException("[BaseNetProto::SendLuaMsg] maximum packet-size exceeded");

	PackPacket* packet = new PackPacket(packetSize, NETMSG_LUAMSG);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << script << mode << rawData;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendGiveAwayEverything(uint8_t myPlayerNum, uint8_t giveToTeam, uint8_t takeFromTeam)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + 1 + sizeof(giveToTeam) + sizeof(takeFromTeam), NETMSG_TEAM);
	*packet << myPlayerNum << static_cast<uint8_t>(TEAMMSG_GIVEAWAY) << giveToTeam << takeFromTeam;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendResign(uint8_t myPlayerNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + 1 + 1 + 1, NETMSG_TEAM);
	*packet << myPlayerNum << static_cast<uint8_t>(TEAMMSG_RESIGN) << static_cast<uint8_t>(0) << static_cast<uint8_t>(0);
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendJoinTeam(uint8_t myPlayerNum, uint8_t wantedTeamNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + 1 + sizeof(wantedTeamNum) + 1, NETMSG_TEAM);
	*packet << myPlayerNum << static_cast<uint8_t>(TEAMMSG_JOIN_TEAM) << wantedTeamNum << static_cast<uint8_t>(0);
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendTeamDied(uint8_t myPlayerNum, uint8_t whichTeam)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + 1 + sizeof(whichTeam) + 1, NETMSG_TEAM);
	*packet << myPlayerNum << static_cast<uint8_t>(TEAMMSG_TEAM_DIED) << whichTeam << static_cast<uint8_t>(0);
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendAICreated(uint8_t myPlayerNum, uint8_t whichSkirmishAI, uint8_t team, const std::string& name)
{
	// do not hand optimize this math; the compiler will do that
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(whichSkirmishAI) + sizeof(team) + (name.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint8_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_AI_CREATED);
	*packet
		<< static_cast<uint8_t>(packetSize)
		<< myPlayerNum
		<< whichSkirmishAI
		<< team
		<< name;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendAIStateChanged(uint8_t myPlayerNum, uint8_t whichSkirmishAI, uint8_t newState)
{
	// do not hand optimize this math; the compiler will do that
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(whichSkirmishAI) + sizeof(newState), NETMSG_AI_STATE_CHANGED);
	*packet << myPlayerNum << whichSkirmishAI << newState;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSetAllied(uint8_t myPlayerNum, uint8_t whichAllyTeam, uint8_t state)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(myPlayerNum) + sizeof(whichAllyTeam) + sizeof(state), NETMSG_ALLIANCE);
	*packet << myPlayerNum << whichAllyTeam << state;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendCreateNewPlayer(uint8_t playerNum, bool spectator, uint8_t teamNum, std::string playerName )
{
	const uint32_t payloadSize = sizeof(playerNum) + sizeof(spectator) + sizeof(teamNum) + (playerName.size() + 1);
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_CREATE_NEWPLAYER);
	*packet << static_cast<uint16_t>(packetSize) << playerNum << (uint8_t)spectator << teamNum << playerName;
	return PacketType(packet);

}

PacketType CBaseNetProtocol::SendCurrentFrameProgress(int32_t frameNum)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(frameNum), NETMSG_GAME_FRAME_PROGRESS);
	*packet << frameNum;
	return PacketType(packet);
}


PacketType CBaseNetProtocol::SendClientData(uint8_t playerNum, const std::vector<uint8_t>& data)
{
	const uint32_t payloadSize = sizeof(playerNum) + data.size();
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_CLIENTDATA);
	*packet << static_cast<uint16_t>(packetSize);
	*packet << playerNum;
	*packet << data;

	return PacketType(packet);
}



#ifdef SYNCDEBUG
PacketType CBaseNetProtocol::SendSdCheckrequest(int32_t frameNum)
{
	PackPacket* packet = new PackPacket(5, NETMSG_SD_CHKREQUEST);
	*packet << frameNum;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSdCheckresponse(uint8_t myPlayerNum, uint64_t flop, std::vector<uint32_t> checksums)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + sizeof(flop) + (checksums.size() * sizeof(uint32_t));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_SD_CHKRESPONSE);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << flop << checksums;
	return PacketType(packet);
}

PacketType CBaseNetProtocol::SendSdReset()
{
	return PacketType(new PackPacket(sizeof(uint8_t), NETMSG_SD_RESET));
}


PacketType CBaseNetProtocol::SendSdBlockrequest(uint16_t begin, uint16_t length, uint16_t requestSize)
{
	PackPacket* packet = new PackPacket(sizeof(uint8_t) + sizeof(begin) + sizeof(length) + sizeof(requestSize), NETMSG_SD_BLKREQUEST);
	*packet << begin << length << requestSize;
	return PacketType(packet);

}

PacketType CBaseNetProtocol::SendSdBlockresponse(uint8_t myPlayerNum, std::vector<uint32_t> checksums)
{
	const uint32_t payloadSize = sizeof(myPlayerNum) + (checksums.size() * sizeof(uint32_t));
	const uint32_t headerSize = sizeof(uint8_t) + sizeof(uint16_t);
	const uint32_t packetSize = headerSize + payloadSize;

	PackPacket* packet = new PackPacket(packetSize, NETMSG_SD_BLKRESPONSE);
	*packet << static_cast<uint16_t>(packetSize) << myPlayerNum << checksums;
	return PacketType(packet);
}
#endif // SYNCDEBUG


CBaseNetProtocol::CBaseNetProtocol()
{
	netcode::ProtocolDef* proto = netcode::ProtocolDef::GetInstance();
	// proto->AddType() length parameter:
	//   > 0:  if its fixed length
	//   < 0:  means the next x bytes represent the length

	proto->AddType(NETMSG_KEYFRAME, 5);
	proto->AddType(NETMSG_NEWFRAME, 1);
	proto->AddType(NETMSG_QUIT, -2);
	proto->AddType(NETMSG_STARTPLAYING, 5);
	proto->AddType(NETMSG_SETPLAYERNUM, 2);
	proto->AddType(NETMSG_PLAYERNAME, -1);
	proto->AddType(NETMSG_CHAT, -1);
	proto->AddType(NETMSG_RANDSEED, 5);
	proto->AddType(NETMSG_GAMEID, 17);
	proto->AddType(NETMSG_PATH_CHECKSUM, 1 + 1 + sizeof(uint32_t));
	proto->AddType(NETMSG_COMMAND, -2);
	proto->AddType(NETMSG_SELECT, -2);
	proto->AddType(NETMSG_PAUSE, 3);

	proto->AddType(NETMSG_AICOMMAND, -2);
	proto->AddType(NETMSG_AICOMMAND_TRACKED, -2);
	proto->AddType(NETMSG_AICOMMANDS, -2);
	proto->AddType(NETMSG_AISHARE, -2);

	proto->AddType(NETMSG_USER_SPEED, 6);
	proto->AddType(NETMSG_INTERNAL_SPEED, 5);
	proto->AddType(NETMSG_CPU_USAGE, 5);
	proto->AddType(NETMSG_DIRECT_CONTROL, 2);
	proto->AddType(NETMSG_DC_UPDATE, 7);
	proto->AddType(NETMSG_ATTEMPTCONNECT, -2);
	proto->AddType(NETMSG_REJECT_CONNECT, -2);
	proto->AddType(NETMSG_SHARE, 12);
	proto->AddType(NETMSG_SETSHARE, 11);

	proto->AddType(NETMSG_PLAYERSTAT, 2 + sizeof(PlayerStatistics));
	proto->AddType(NETMSG_GAMEOVER, -1);
	proto->AddType(NETMSG_MAPDRAW, -1);
	proto->AddType(NETMSG_SYNCRESPONSE, 10);
	proto->AddType(NETMSG_SYSTEMMSG, -2);
	proto->AddType(NETMSG_STARTPOS, 16);
	proto->AddType(NETMSG_PLAYERINFO, 10);
	proto->AddType(NETMSG_PLAYERLEFT, 3);
	proto->AddType(NETMSG_LOGMSG, -2);
	proto->AddType(NETMSG_LUAMSG, -2);
	proto->AddType(NETMSG_TEAM, 5);
	proto->AddType(NETMSG_GAMEDATA, -2);
	proto->AddType(NETMSG_ALLIANCE, 4);
	proto->AddType(NETMSG_CCOMMAND, -2);
	proto->AddType(NETMSG_TEAMSTAT, 2 + sizeof(TeamStatistics));
	proto->AddType(NETMSG_CLIENTDATA, -2);
	proto->AddType(NETMSG_REQUEST_TEAMSTAT, 4 );

	proto->AddType(NETMSG_CREATE_NEWPLAYER, -2);

	proto->AddType(NETMSG_AI_CREATED, -1);
	proto->AddType(NETMSG_AI_STATE_CHANGED, 4);
	proto->AddType(NETMSG_GAME_FRAME_PROGRESS,5);

#ifdef SYNCDEBUG
	proto->AddType(NETMSG_SD_CHKREQUEST, 5);
	proto->AddType(NETMSG_SD_CHKRESPONSE, -2);
	proto->AddType(NETMSG_SD_RESET, 1);
	proto->AddType(NETMSG_SD_BLKREQUEST, 7);
	proto->AddType(NETMSG_SD_BLKRESPONSE, -2);
#endif // SYNCDEBUG
}