Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1
12#ifndef MINESERVER_STREAM_H
13#define MINESERVER_STREAM_H
14
15#include <cstdint>
16#include <cstddef>
17#include <string>
18#include <vector>
19#include <utils/network.h>
20#include <types/chatmessage.h>
21#include <types/uuid.h>
22#include <utils/crypto.h>
23
32{
33public:
34 virtual ~IMCStream() = default;
35
46 virtual void read(std::byte *buffer, std::size_t offset, std::size_t len) = 0;
57 virtual void write(const std::byte *buffer, std::size_t offset, std::size_t len) = 0;
63 virtual void flush() = 0;
69 virtual size_t available() = 0;
70
77 virtual void finishPacketWrite(const std::byte *packetData, size_t len) = 0;
78
84 bool readBoolean();
91 void writeBoolean(bool b);
92
98 std::int8_t readByte();
105 void writeByte(std::int8_t b);
106
112 std::uint8_t readUnsignedByte();
119 void writeUnsignedByte(std::uint8_t b);
120
126 std::int16_t readShort();
133 void writeShort(std::int16_t s);
134
140 std::uint16_t readUnsignedShort();
147 void writeUnsignedShort(std::uint16_t s);
148
154 std::int32_t readInt();
161 void writeInt(std::int32_t i);
162
168 std::int64_t readLong();
175 void writeLong(std::int64_t l);
176
182 float readFloat();
189 void writeFloat(float f);
190
196 double readDouble();
203 void writeDouble(double d);
204
210 std::string readString();
217 void writeString(const std::string &s);
218
231 void writeChat(const ChatMessage &c);
232
239 std::int32_t readVarInt();
247 void writeVarInt(std::int32_t i);
248
255 std::int64_t readVarLong();
263 void writeVarLong(std::int64_t l);
264
279 void writeUUID(const MinecraftUUID &uuid);
280};
281
292{
293private:
294 std::vector<std::byte> data;
295 std::uint32_t readIndex{};
296
297public:
301 MemoryStream() = default;
306 ~MemoryStream() override = default;
307
316 void read(std::byte *buffer, std::size_t offset, std::size_t len) override;
325 void write(const std::byte *buffer, std::size_t offset, std::size_t len) override;
331 void flush() override;
337 size_t available() override;
338
345 void finishPacketWrite(const std::byte *packetData, size_t len) override;
346
352 void clear();
359 const std::vector<std::byte> &getData() const;
360};
361
368{
369private:
370 ClientSocket socket;
371
372public:
378 NetSocketStream(const ClientSocket& socket);
384
394 void read(std::byte *buffer, std::size_t offset, std::size_t len) override;
403 void write(const std::byte *buffer, std::size_t offset, std::size_t len) override;
409 size_t available() override;
410
417 void finishPacketWrite(const std::byte *packetData, size_t len) override;
418
423 void flush() override;
424};
425
433{
434private:
435 IMCStream *baseStream;
438
439public:
447 CipherStream(IMCStream *baseStream, std::byte *key, std::byte *iv);
453
461 void read(std::byte *buffer, std::size_t offset, std::size_t len) override;
469 void write(const std::byte *buffer, std::size_t offset, std::size_t len) override;
479 size_t available() override;
480
487 void finishPacketWrite(const std::byte *packetData, size_t len) override;
488
493 void flush() override;
494};
495
503class ZLibStream : public IMCStream
504{
505private:
506 IMCStream *baseStream;
508
509 std::vector<std::byte> inBuffer{};
510 std::uint32_t inIndex{};
511
512 std::vector<std::byte> outBuffer{};
513 int threshold;
514
515public:
523 ZLibStream(IMCStream *baseStream, int level, int threshold);
528 ~ZLibStream();
529
537 void read(std::byte *buffer, std::size_t offset, std::size_t len) override;
545 void write(const std::byte *buffer, std::size_t offset, std::size_t len) override;
551 size_t available() override;
552
559 void finishPacketWrite(const std::byte *packetData, size_t len) override;
560
573 void flush() override;
574};
575
576#endif // MINESERVER_STREAM_H
The file containing minecraft chat message implementation.
Minecraft Chat Message implementation.
Definition chatmessage.h:28
AES 128bit CFB8 Cipher stream.
Definition stream.h:433
CipherStream(IMCStream *baseStream, std::byte *key, std::byte *iv)
Construct a new Cipher Stream object.
Definition stream.cpp:439
size_t available() override
Gets the number of available bytes Returns the number of available bytes in the sub-stream....
Definition stream.cpp:473
void write(const std::byte *buffer, std::size_t offset, std::size_t len) override
Writes to an encrypted stream.
Definition stream.cpp:464
void finishPacketWrite(const std::byte *packetData, size_t len) override
Finishes to write the packet in a Minecrafty way.
Definition stream.cpp:478
~CipherStream()
Destroy the Cipher Stream object.
Definition stream.cpp:446
void read(std::byte *buffer, std::size_t offset, std::size_t len) override
Reads from an encrypted stream.
Definition stream.cpp:451
void flush() override
Flushes the base stream.
Definition stream.cpp:484
Network POSIX Client.
Definition network.h:42
Stream interface.
Definition stream.h:32
void writeUUID(const MinecraftUUID &uuid)
Writes an MinecraftUUID.
Definition stream.cpp:348
void writeByte(std::int8_t b)
Writes a Byte.
Definition stream.cpp:39
std::int32_t readVarInt()
Reads a Variable Integer.
Definition stream.cpp:269
void writeUnsignedByte(std::uint8_t b)
Writes an Unsigned Byte.
Definition stream.cpp:50
void writeVarLong(std::int64_t l)
Writes a Variable Long.
Definition stream.cpp:332
std::int64_t readVarLong()
Reads a Variable Long.
Definition stream.cpp:309
std::int64_t readLong()
Reads a Long.
Definition stream.cpp:145
float readFloat()
Reads a Float.
Definition stream.cpp:185
virtual size_t available()=0
Gets the number of available bytes.
void writeFloat(float f)
Writes a Float.
Definition stream.cpp:191
void writeChat(const ChatMessage &c)
Writes a Chat Message.
Definition stream.cpp:240
ChatMessage readChat()
Reads a Chat Message.
Definition stream.cpp:224
void writeUnsignedShort(std::uint16_t s)
Writes an Unsigned short.
Definition stream.cpp:98
void writeDouble(double d)
Writes a Double.
Definition stream.cpp:203
void writeLong(std::int64_t l)
Writes a Long.
Definition stream.cpp:167
virtual void finishPacketWrite(const std::byte *packetData, size_t len)=0
Finishes packet write to stream.
std::uint16_t readUnsignedShort()
Reads an Unsigned Short.
Definition stream.cpp:92
std::string readString()
Reads a String.
Definition stream.cpp:208
double readDouble()
Reads a Double.
Definition stream.cpp:196
void writeShort(std::int16_t s)
Writes a Short.
Definition stream.cpp:72
void writeBoolean(bool b)
Writes a Boolean.
Definition stream.cpp:27
void writeInt(std::int32_t i)
Writes a Integer.
Definition stream.cpp:126
void writeVarInt(std::int32_t i)
Writes a Variable Integer.
Definition stream.cpp:293
virtual void write(const std::byte *buffer, std::size_t offset, std::size_t len)=0
Writes data to the sub-class.
MinecraftUUID readUUID()
Reads an MinecraftUUID.
Definition stream.cpp:355
void writeString(const std::string &s)
Writes a String.
Definition stream.cpp:218
virtual void flush()=0
Flushes the stream.
std::int32_t readInt()
Reads an Integer.
Definition stream.cpp:103
std::int8_t readByte()
Reads a Byte.
Definition stream.cpp:32
std::uint8_t readUnsignedByte()
Reads an Unsigned Byte.
Definition stream.cpp:44
std::int16_t readShort()
Reads a Short.
Definition stream.cpp:55
bool readBoolean()
Reads a Boolean.
Definition stream.cpp:20
virtual void read(std::byte *buffer, std::size_t offset, std::size_t len)=0
Reads data from the sub-class.
A stream from Memory.
Definition stream.h:292
void write(const std::byte *buffer, std::size_t offset, std::size_t len) override
Writes to the stream.
Definition stream.cpp:372
const std::vector< std::byte > & getData() const
Get the written data.
Definition stream.cpp:399
void clear()
Cleares the stream.
Definition stream.cpp:393
size_t available() override
Gets the number of available bytes.
Definition stream.cpp:382
void flush() override
Flushes the stream.
Definition stream.cpp:377
MemoryStream()=default
Construct a new Memory Stream object.
void read(std::byte *buffer, std::size_t offset, std::size_t len) override
Reads from the stream.
Definition stream.cpp:366
~MemoryStream() override=default
Destroy the Memory Stream object.
void finishPacketWrite(const std::byte *packetData, size_t len) override
Finishes to write the packet in a Minecrafty way.
Definition stream.cpp:387
MinecraftUUID compliant data holder.
Definition uuid.h:27
Stream from a net client socket.
Definition stream.h:368
void write(const std::byte *buffer, std::size_t offset, std::size_t len) override
Writes to the network socket.
Definition stream.cpp:418
size_t available() override
Gets the number of available bytes.
Definition stream.cpp:423
void read(std::byte *buffer, std::size_t offset, std::size_t len) override
Reads from the network socket.
Definition stream.cpp:413
void finishPacketWrite(const std::byte *packetData, size_t len) override
Finishes to write the packet in a Minecrafty way.
Definition stream.cpp:428
void flush() override
Flushes the stream.
Definition stream.cpp:434
NetSocketStream(const ClientSocket &socket)
Construct a new Net Socket Stream object.
Definition stream.cpp:404
~NetSocketStream()
Destroy the Net Socket Stream object.
Definition stream.cpp:408
ZLib Stream.
Definition stream.h:504
void write(const std::byte *buffer, std::size_t offset, std::size_t len) override
Writes to a compressed stream.
Definition stream.cpp:504
void finishPacketWrite(const std::byte *packetData, size_t len) override
Finishes to write the packet in a Minecrafty way.
Definition stream.cpp:529
ZLibStream(IMCStream *baseStream, int level, int threshold)
Construct a new ZLibStream object.
Definition stream.cpp:489
size_t available() override
Gets the number of available bytes.
Definition stream.cpp:509
void flush() override
Flushes the stream.
Definition stream.cpp:548
void read(std::byte *buffer, std::size_t offset, std::size_t len) override
Reads from a compressed stream.
Definition stream.cpp:498
~ZLibStream()
Destroy the ZLibStream object.
Definition stream.cpp:493
AES/CFB8 128bit cipher class.
Definition crypto.h:205
ZLib compressor.
Definition crypto.h:271
The file containing all the logic for encryption.
The network (TCP) cross-platform implementation.
The file containing MinecraftUUID logic.