2017-11-22 19:39:24 +01:00
|
|
|
|
using Godot;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Network.PacketHandling {
|
|
|
|
|
public class PacketBuffer {
|
|
|
|
|
|
2017-11-22 19:58:47 +01:00
|
|
|
|
private List<byte> ByteList = new List<byte>();
|
2017-11-22 19:39:24 +01:00
|
|
|
|
private int ReadCounter = 0;
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public static byte[] SignatureBytes = { 154, 211 };
|
2017-11-23 01:41:37 +01:00
|
|
|
|
|
2017-11-22 19:39:24 +01:00
|
|
|
|
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
|
|
|
|
|
public int Length { get { return ByteList.Count; } }
|
2017-11-22 21:40:40 +01:00
|
|
|
|
public bool HasNext { get { return ReadCounter < ByteList.Count; } }
|
2017-11-22 19:39:24 +01:00
|
|
|
|
|
|
|
|
|
public byte Read() {
|
|
|
|
|
if (!HasNext) {
|
|
|
|
|
GD.printerr("Attempted to read byte; Impossible, byte buffer ended.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return ByteList[ReadCounter++];
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 01:41:37 +01:00
|
|
|
|
public short ReadShort() { return PrimitiveSerializer.DeserializeShort(this); }
|
|
|
|
|
|
|
|
|
|
public int ReadInt() { return PrimitiveSerializer.DeserializeInt(this); }
|
|
|
|
|
|
|
|
|
|
public long ReadLong() { return PrimitiveSerializer.DeserializeLong(this); }
|
|
|
|
|
|
|
|
|
|
public float ReadFloat() { return PrimitiveSerializer.DeserializeFloat(this); }
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public bool ReadBool() { return PrimitiveSerializer.DeserializeBool(this); }
|
|
|
|
|
|
2017-11-23 01:41:37 +01:00
|
|
|
|
public string ReadString() { return PrimitiveSerializer.DeserializeString(this); }
|
|
|
|
|
|
2017-11-22 19:39:24 +01:00
|
|
|
|
public void Write(byte toWrite) {
|
|
|
|
|
ByteList.Add(toWrite);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-23 01:41:37 +01:00
|
|
|
|
public void Write(short toWrite) { PrimitiveSerializer.SerializeShort(toWrite, this); }
|
|
|
|
|
|
|
|
|
|
public void Write(int toWrite) { PrimitiveSerializer.SerializeInt(toWrite, this); }
|
|
|
|
|
|
|
|
|
|
public void Write(long toWrite) { PrimitiveSerializer.SerializeLong(toWrite, this); }
|
|
|
|
|
|
|
|
|
|
public void Write(float toWrite) { PrimitiveSerializer.SerializeFloat(toWrite, this); }
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public void Write(bool toWrite) { PrimitiveSerializer.SerializeBool(toWrite, this); }
|
|
|
|
|
|
2017-11-23 01:41:37 +01:00
|
|
|
|
public void Write(string toWrite) { PrimitiveSerializer.SerializeString(toWrite, this); }
|
|
|
|
|
|
2017-11-22 19:39:24 +01:00
|
|
|
|
public static PacketBuffer FromByteBuffer(byte[] byteBuffer) {
|
|
|
|
|
PacketBuffer PB = new PacketBuffer();
|
|
|
|
|
PB.ByteList = new List<byte>(byteBuffer);
|
|
|
|
|
return PB;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public static PacketBuffer WithSignature() {
|
2017-11-23 01:41:37 +01:00
|
|
|
|
PacketBuffer PB = new PacketBuffer();
|
2017-11-25 11:26:55 +01:00
|
|
|
|
foreach (byte B in SignatureBytes) {
|
2017-11-23 01:41:37 +01:00
|
|
|
|
PB.Write(B);
|
|
|
|
|
}
|
|
|
|
|
return PB;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public static PacketBuffer operator +(PacketBuffer first, PacketBuffer second) {
|
|
|
|
|
PacketBuffer PB = new PacketBuffer();
|
|
|
|
|
PB.ByteList.AddRange(first.ByteList);
|
|
|
|
|
PB.ByteList.AddRange(second.ByteList);
|
|
|
|
|
return PB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-11-22 19:39:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|