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

202 lines
5.1 KiB
C#

using System.Collections.Generic;
using System;
using System.Text;
namespace NeonTea.Quakeball.TeaNet.Packets {
public class ByteBuffer {
private List<byte> Bytes;
private int pos = 0;
public ByteBuffer() {
Bytes = new List<byte>();
}
public ByteBuffer(byte[] bytes) {
Bytes = new List<byte>(bytes);
}
public byte[] Pack() {
return Bytes.ToArray();
}
public bool CanRead() {
return pos < Bytes.Count;
}
public Serializable ReadSerializable() {
Serializable s = (Serializable)Activator.CreateInstance(typeof(Serializable));
s.Read(this);
return s;
}
public void ReadSerializable(Serializable s) {
s.Read(this);
}
public char ReadChar() {
return BitConverter.ToChar(Read(2), 0);
}
public bool ReadBool() {
return Read() == 1;
}
public double ReadDouble() {
return BitConverter.ToDouble(Read(8), 0);
}
public float ReadFloat() {
return BitConverter.ToSingle(Read(4), 0);
}
public ulong ReadULong() {
return BitConverter.ToUInt64(Read(8), 0);
}
public uint ReadUInt() {
return BitConverter.ToUInt32(Read(4), 0);
}
public ushort ReadUShort() {
return BitConverter.ToUInt16(Read(2), 0);
}
public long ReadLong() {
return BitConverter.ToInt64(Read(8), 0);
}
public int ReadInt() {
return BitConverter.ToInt32(Read(4), 0);
}
public short ReadShort() {
return BitConverter.ToInt16(Read(2), 0);
}
public string ReadString() {
int length = ReadInt();
string s = Encoding.UTF8.GetString(Read(length));
return s;
}
public byte[] Read(int amount) {
byte[] bytes = Bytes.GetRange(pos, amount).ToArray();
pos += amount;
return bytes;
}
public byte Read() {
return Bytes[pos++];
}
public void Write(Serializable s) {
s.Write(this);
}
public void Write(char c) {
Bytes.AddRange(BitConverter.GetBytes(c));
}
public void Write(bool b) {
Write(b ? (byte)0b1 : (byte)0b0);
}
public void Write(double d) {
Bytes.AddRange(BitConverter.GetBytes(d));
}
public void Write(float f) {
Bytes.AddRange(BitConverter.GetBytes(f));
}
public void Write(ulong l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
public void Write(uint i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
public void Write(ushort s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
public void Write(long l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
public void Write(int i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
public void Write(short s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
public void Write(string s) {
byte[] bytes = Encoding.UTF8.GetBytes(s);
Write(bytes.Length);
Bytes.AddRange(bytes);
}
public void Write(byte b) {
Bytes.Add(b);
}
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;
}
public void WritePacket(Protocol protocol, Packet p) {
Write(protocol.GetPacketTypeID(p));
p.WriteMeta(this);
p.Write(this);
}
public Packet ReadPacket(Protocol protocol) {
int packetType = ReadInt();
Type t = protocol.GetPacketType(packetType);
Packet p = (Packet)Activator.CreateInstance(t);
p.ReadMeta(this);
p.Read(this);
return p;
}
}
}