quakeball/Assets/Scripts/TeaNet/Packets/ByteBuffer.cs

235 lines
7.7 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
using System;
using System.Text;
namespace NeonTea.Quakeball.TeaNet.Packets {
2020-08-06 20:49:51 +02:00
/// <summary>Contains a stream of bytes for sending or receiving over the internet.</summary>
public class ByteBuffer {
public int Size => Bytes.Count;
private List<byte> Bytes;
private int pos = 0;
2020-08-06 20:49:51 +02:00
/// <summary>Creates a new empty ByteBuffer</summary>
public ByteBuffer() {
Bytes = new List<byte>();
}
2020-08-06 20:49:51 +02:00
/// <summary>Creates a new ByteBuffer from the given byte[]</summary>
public ByteBuffer(byte[] bytes) {
Bytes = new List<byte>(bytes);
}
2020-08-06 20:49:51 +02:00
/// <summary>Packs the byte buffer in to a byte[] array.</summary>
public byte[] Pack() {
return Bytes.ToArray();
}
2020-08-06 20:49:51 +02:00
/// <summary>is there a byte to read next?</summary>
public bool CanRead() {
return pos < Bytes.Count;
}
2020-08-06 20:49:51 +02:00
/// <summary>Reads a given object with Serializable implemented. Assumes there is one next.</summary>
public void ReadSerializable(Serializable s) {
s.Read(this);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a char on the buffer. Assumes there is one next.</summary>
public char ReadChar() {
return BitConverter.ToChar(Read(2), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a boolean on the buffer. Assumes there is one next.</summary>
public bool ReadBool() {
return Read() == 1;
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a double on the buffer. Assumes there is one next.</summary>
public double ReadDouble() {
return BitConverter.ToDouble(Read(8), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a float on the buffer. Assumes there is one next.</summary>
public float ReadFloat() {
return BitConverter.ToSingle(Read(4), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an unsigned long on the buffer. Assumes there is one next.</summary>
public ulong ReadULong() {
return BitConverter.ToUInt64(Read(8), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an unsigned integer on the buffer. Assumes there is one next.</summary>
public uint ReadUInt() {
return BitConverter.ToUInt32(Read(4), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an unsigned short on the buffer. Assumes there is one next.</summary>
public ushort ReadUShort() {
return BitConverter.ToUInt16(Read(2), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a long on the buffer. Assumes there is one next.</summary>
public long ReadLong() {
return BitConverter.ToInt64(Read(8), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an integer on the buffer. Assumes there is one next.</summary>
public int ReadInt() {
return BitConverter.ToInt32(Read(4), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a short on the buffer. Assumes there is one next.</summary>
public short ReadShort() {
return BitConverter.ToInt16(Read(2), 0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read a string on the buffer. Assumes there is one next.</summary>
public string ReadString() {
int length = ReadInt();
string s = Encoding.UTF8.GetString(Read(length));
return s;
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an integer on the buffer. Assumes there is one next.</summary>
public byte[] Read(int amount) {
byte[] bytes = Bytes.GetRange(pos, amount).ToArray();
pos += amount;
return bytes;
}
2020-08-06 20:49:51 +02:00
/// <summary>Read the next byte on the buffer. Assumes there is one next.</summary>
public byte Read() {
return Bytes[pos++];
}
2020-08-06 20:49:51 +02:00
/// <summary>Write something that implements Serializable.</summary>
2020-08-05 20:38:37 +02:00
public void Write(Serializable s) {
s.Write(this);
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a char to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(char c) {
Bytes.AddRange(BitConverter.GetBytes(c));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a boolean to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(bool b) {
Write(b ? (byte)0b1 : (byte)0b0);
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a double to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(double d) {
Bytes.AddRange(BitConverter.GetBytes(d));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a float to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(float f) {
Bytes.AddRange(BitConverter.GetBytes(f));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write an unsigned long to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(ulong l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write an unsigned integer to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(uint i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write an unsigned short to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(ushort s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a long to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(long l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write an integer to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(int i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a short to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(short s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a string to the buffer.</summary>
2020-08-05 20:38:37 +02:00
public void Write(string s) {
byte[] bytes = Encoding.UTF8.GetBytes(s);
2020-08-05 20:38:37 +02:00
Write(bytes.Length);
Bytes.AddRange(bytes);
}
2020-08-06 20:49:51 +02:00
/// <summary>Write a byte to the buffer.</summary>
public void Write(byte b) {
Bytes.Add(b);
}
2020-08-06 20:49:51 +02:00
/// <summary>Read weather the given fingerprint is next on the buffer.</summary>
public bool ReadFingerprint(byte[] fingerprint) {
foreach (byte b in fingerprint) {
if (!(CanRead() && Read() == b)) {
return false;
}
}
return true;
}
public PacketStage ReadStage() {
PacketStage stage = PacketStage.Closed;
switch (Read()) {
case 0:
stage = PacketStage.Establishing;
break;
case 1:
stage = PacketStage.Rejected;
break;
case 2:
stage = PacketStage.Closed;
break;
case 3:
stage = PacketStage.Ready;
break;
}
return stage;
}
public ClosingReason ReadClosingReason() {
ClosingReason reason = ClosingReason.Unknown;
switch (Read()) {
case 0:
reason = ClosingReason.Unknown;
break;
case 1:
reason = ClosingReason.IncorrectVersion;
break;
}
return reason;
}
2020-08-06 20:49:51 +02:00
/// <summary>Write an entire packet using the protocol to the buffer.</summary>
public void WritePacket(Protocol protocol, Packet p) {
int old = Bytes.Count;
2020-08-05 20:38:37 +02:00
Write(protocol.GetPacketTypeID(p));
p.Write(this);
p.Size = Bytes.Count - old;
}
2020-08-06 20:49:51 +02:00
/// <summary>Read an entire packet using the given protocol from the buffer.</summary>
public Packet ReadPacket(Protocol protocol) {
int old = pos;
int packetType = ReadInt();
Type t = protocol.GetPacketType(packetType);
Packet p = (Packet)Activator.CreateInstance(t);
p.Read(this);
p.Size = pos - old;
return p;
}
}
}