summaryrefslogtreecommitdiffstats
path: root/src/Stream.hpp
blob: a7c161b6edd46860edc1043726e8e2f2d0bcb181 (plain) (blame)
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
#pragma once

#include <vector>

#include "Utility.hpp"
#include "Vector.hpp"
#include "Chat.hpp"

class Socket;

struct SlotDataType {
    short BlockId = -1;
    signed char ItemCount = 1;
    short ItemDamage = 0;
    //Nbt NBT;
};

class Stream {
public:
	virtual ~Stream() {};
};

class StreamInput : Stream {
	virtual void ReadData(unsigned char *buffPtr, size_t buffLen) = 0;
public:
	virtual ~StreamInput() = default;
	bool ReadBool();
	signed char ReadByte();
	unsigned char ReadUByte();
	short ReadShort();
	unsigned short ReadUShort();
	int ReadInt();
	long long ReadLong();
	float ReadFloat();
	double ReadDouble();
	std::string ReadString();
	Chat ReadChat();
	int ReadVarInt();
	long long ReadVarLong();
	std::vector<unsigned char> ReadEntityMetadata();
	SlotDataType ReadSlot();
	std::vector<unsigned char> ReadNbtTag();
	Vector ReadPosition();
	unsigned char ReadAngle();
	Uuid ReadUuid();
	std::vector<unsigned char> ReadByteArray(size_t arrLength);
};

class StreamOutput : Stream {
	virtual void WriteData(unsigned char *buffPtr, size_t buffLen) = 0;
public:
	virtual ~StreamOutput() = default;
	void WriteBool(bool value);
	void WriteByte(signed char value);
	void WriteUByte(unsigned char value);
	void WriteShort(short value);
	void WriteUShort(unsigned short value);
	void WriteInt(int value);
	void WriteLong(long long value);
	void WriteFloat(float value);
	void WriteDouble(double value);
	void WriteString(const std::string &value);
	void WriteChat(const Chat &value);
	void WriteVarInt(int value);
	void WriteVarLong(long long value);
	void WriteEntityMetadata(const std::vector<unsigned char> &value);
	void WriteSlot(const SlotDataType &value);
	void WriteNbtTag(const std::vector<unsigned char> &value);
	void WritePosition(const Vector &value);
	void WriteAngle(unsigned char value);
	void WriteUuid(const Uuid &value);
	void WriteByteArray(const std::vector<unsigned char> &value);
};

class StreamBuffer : public StreamInput, public StreamOutput {
	std::vector<unsigned char> buffer;
	unsigned char *bufferPtr;

	void ReadData(unsigned char *buffPtr, size_t buffLen) override;
	void WriteData(unsigned char *buffPtr, size_t buffLen) override;

public:
	StreamBuffer(unsigned char *data, size_t dataLen);
	StreamBuffer(size_t bufferLen);

	std::vector<unsigned char> GetBuffer();
    size_t GetReadedLength();
};

class StreamCounter : public StreamOutput {
	void WriteData(unsigned char *buffPtr, size_t buffLen) override;

	size_t size;
public:
	StreamCounter(size_t initialSize = 0);
	~StreamCounter();

	size_t GetCountedSize();
};

class StreamSocket : public StreamInput, public StreamOutput {
	Socket *socket;
	void ReadData(unsigned char *buffPtr, size_t buffLen) override;
	void WriteData(unsigned char *buffPtr, size_t buffLen) override;
public:
	StreamSocket(Socket *socketPtr);
	~StreamSocket() = default;
};