aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/Packet.h
blob: c639c8f167a3302760990680ba88cdea91856231 (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
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
#pragma once
using namespace std;

class Packet;
class PacketListener;
class DataInputStream;
class DataOutputStream;

#define PACKET_ENABLE_STAT_TRACKING 0

class Packet;

typedef shared_ptr<Packet> (*packetCreateFn)();

class Packet
{
public:
	class PacketStatistics
	{
	private:
		int count;
		int totalSize;

		static const int TOTAL_TICKS = 100;

		// 4J Added
		int64_t countSamples[TOTAL_TICKS];
		int64_t sizeSamples[TOTAL_TICKS];
		int64_t timeSamples[TOTAL_TICKS];
		int samplesPos;

	public:
		const int id;

	public:
		PacketStatistics(int id);
		void addPacket(int bytes);
		int getCount();
		int getTotalSize();
		double getAverageSize();
		int64_t getRunningTotal();
		int64_t getRunningCount();
		void IncrementPos();
	};

	// 4J JEV, replaces the static blocks.
	static void staticCtor();

public:
	static unordered_map<int, packetCreateFn> idToCreateMap; // IntHashMap in 1.8.2 ... needed? // Made public in 1.0.1

	static unordered_set<int> clientReceivedPackets;
	static unordered_set<int> serverReceivedPackets;
	static unordered_set<int> sendToAnyClientPackets;

	// 4J Stu - Added the sendToAnyClient param so we can limit some packets to be only sent to one player on a system
	// 4J Stu - Added renderStats param for use in debugging
	static void map(int id, bool receiveOnClient, bool receiveOnServer, bool sendToAnyClient, bool renderStats, const type_info& clazz, packetCreateFn );

public:
	const int64_t createTime;

	Packet();

	static shared_ptr<Packet> getPacket(int id);

	// 4J Added
	static bool canSendToAnyClient(shared_ptr<Packet> packet);

	static void writeBytes(DataOutputStream *dataoutputstream, byteArray bytes);
	static byteArray readBytes(DataInputStream *datainputstream);

	virtual int getId() = 0;

	bool shouldDelay;

private:
	// 4J Added to track stats for packets that are going out via QNet
	static unordered_map<int, PacketStatistics *> outgoingStatistics; // IntHashMap in 1.8.2 ... needed?
	static vector<PacketStatistics *> renderableStats;
	static int renderPos;
public:
	static void recordOutgoingPacket(shared_ptr<Packet> packet, int playerIndex);
	static void updatePacketStatsPIX();
private :
	static unordered_map<int, PacketStatistics *> statistics;
	//static int nextPrint;

public:
	static shared_ptr<Packet> readPacket(DataInputStream *dis, bool isServer);
	static void writePacket(shared_ptr<Packet> packet, DataOutputStream *dos);
	static void writeUtf(const wstring& value, DataOutputStream *dos);
	static wstring readUtf(DataInputStream *dis, int maxLength);
	virtual void read(DataInputStream *dis) = 0; // throws IOException = 0; TODO 4J JEV, should this declare a throws?
	virtual void write(DataOutputStream *dos) = 0; // throws IOException = 0; TODO 4J JEV, should this declare a throws?
	virtual void handle(PacketListener *listener) = 0;
	virtual int getEstimatedSize() = 0;
	virtual bool canBeInvalidated();
	virtual bool isInvalidatedBy(shared_ptr<Packet> packet);
	virtual bool isAync();

	// 4J Stu - Brought these functions forward for enchanting/game rules
	static shared_ptr<ItemInstance> readItem(DataInputStream *dis);
	static void writeItem(shared_ptr<ItemInstance> item, DataOutputStream *dos);
	static CompoundTag *readNbt(DataInputStream *dis);

protected:
	static void writeNbt(CompoundTag *tag, DataOutputStream *dos);
};