aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/GameCommandPacket.cpp
blob: 17c5316aff3c3a291d89bdb72bdfd3939e2d5e68 (plain)
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
#include "stdafx.h"
#include "InputOutputStream.h"
#include "PacketListener.h"
#include "BasicTypeContainers.h"
#include "GameCommandPacket.h"

GameCommandPacket::GameCommandPacket()
{
	length = 0;
}

GameCommandPacket::GameCommandPacket(EGameCommand command, byteArray data)
{
	this->command = command;
	this->data = data;
	length = 0;

	if (data.data != nullptr)
	{
		length = data.length;

		if (length > Short::MAX_VALUE)
		{
			app.DebugPrintf("Payload may not be larger than 32K\n");
#ifndef _CONTENT_PACKAGE
			__debugbreak();
#endif
			//throw new IllegalArgumentException("Payload may not be larger than 32k");
		}
	}
}

GameCommandPacket::~GameCommandPacket()
{
	delete [] data.data;
}

void GameCommandPacket::read(DataInputStream *dis)
{
	command = static_cast<EGameCommand>(dis->readInt());
	length = dis->readShort();

	if (length > 0 && length < Short::MAX_VALUE)
	{
		if(data.data != nullptr)
		{
			delete [] data.data;
		}
		data = byteArray(length);
		dis->readFully(data);
	}
}

void GameCommandPacket::write(DataOutputStream *dos)
{
	dos->writeInt(command);
	dos->writeShort(static_cast<short>(length));
	if (data.data != nullptr)
	{
		dos->write(data);
	}
}

void GameCommandPacket::handle(PacketListener *listener)
{
	listener->handleGameCommand( shared_from_this() );
}

int GameCommandPacket::getEstimatedSize()
{
	return 2 + 2 + length;
}