summaryrefslogtreecommitdiffstats
path: root/src/network/Stream.hpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-06-17 16:23:53 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-06-17 16:23:53 +0200
commit789f70b6f1a9067843dfc1ff73d86b645efe1da9 (patch)
treef3db085f2ac1ec5fd6ad0869a63e77a7f0fb1a17 /src/network/Stream.hpp
parent2017-06-14 (diff)
downloadAltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar.gz
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar.bz2
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar.lz
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar.xz
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.tar.zst
AltCraft-789f70b6f1a9067843dfc1ff73d86b645efe1da9.zip
Diffstat (limited to 'src/network/Stream.hpp')
-rw-r--r--src/network/Stream.hpp115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/network/Stream.hpp b/src/network/Stream.hpp
new file mode 100644
index 0000000..5babb08
--- /dev/null
+++ b/src/network/Stream.hpp
@@ -0,0 +1,115 @@
+#pragma once
+
+#include <algorithm>
+#include <string>
+#include <stdexcept>
+#include <vector>
+#include <cstring>
+#include <nlohmann/json.hpp>
+#include <easylogging++.h>
+#include "Socket.hpp"
+#include "../utility/Vector.hpp"
+
+class Stream {
+protected:
+ template<class T>
+ void endswap(T &obj) {
+ unsigned char *raw = reinterpret_cast<unsigned char *>(&obj);
+ std::reverse(raw, raw + sizeof(T));
+ }
+
+ void endswap(unsigned char *arr, size_t arrLen) {
+ std::reverse(arr, arr + arrLen);
+ }
+
+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();
+ std::string ReadChat();
+ int ReadVarInt();
+ long long ReadVarLong();
+ std::vector<unsigned char> ReadEntityMetadata();
+ std::vector<unsigned char> ReadSlot();
+ std::vector<unsigned char> ReadNbtTag();
+ Vector ReadPosition();
+ unsigned char ReadAngle();
+ std::vector<unsigned char> 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(std::string value);
+ void WriteChat(std::string value);
+ void WriteVarInt(int value);
+ void WriteVarLong(long long value);
+ void WriteEntityMetadata(std::vector<unsigned char> value);
+ void WriteSlot(std::vector<unsigned char> value);
+ void WriteNbtTag(std::vector<unsigned char> value);
+ void WritePosition(Vector value);
+ void WriteAngle(unsigned char value);
+ void WriteUuid(std::vector<unsigned char> value);
+ void WriteByteArray(std::vector<unsigned char> value);
+};
+
+class StreamBuffer : public StreamInput, public StreamOutput {
+ unsigned char *buffer;
+ unsigned char *bufferPtr;
+ size_t bufferLength;
+
+ 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);
+ ~StreamBuffer();
+
+ std::vector<unsigned char> GetBuffer();
+};
+
+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;
+}; \ No newline at end of file