Separate ByteBuffer.cs and add rest read/writes

This commit is contained in:
Sofia 2020-08-05 21:33:22 +03:00
parent bc5eaed2ee
commit abc3a672db
2 changed files with 207 additions and 123 deletions

View File

@ -0,0 +1,202 @@
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 WriteSerializable(Serializable s) {
s.Write(this);
}
public void WriteChar(char c) {
Bytes.AddRange(BitConverter.GetBytes(c));
}
public void WriteBool(bool b) {
Write(b ? (byte)0b1 : (byte)0b0);
}
public void WriteDouble(double d) {
Bytes.AddRange(BitConverter.GetBytes(d));
}
public void WriteFloat(float f) {
Bytes.AddRange(BitConverter.GetBytes(f));
}
public void WriteULong(ulong l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
public void WriteUInt(uint i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
public void WriteUShort(ushort s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
public void WriteLong(long l) {
Bytes.AddRange(BitConverter.GetBytes(l));
}
public void WriteInt(int i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
public void WriteShort(short s) {
Bytes.AddRange(BitConverter.GetBytes(s));
}
public void WriteString(string s) {
byte[] bytes = Encoding.UTF8.GetBytes(s);
WriteInt(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) {
WriteInt(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;
}
}
}

View File

@ -1,7 +1,3 @@
using System.Collections.Generic;
using System;
using System.Text;
namespace NeonTea.Quakeball.TeaNet.Packets {
public abstract class Packet {
public bool Reliable = true;
@ -21,6 +17,11 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
}
}
public interface Serializable {
void Write(ByteBuffer buffer);
void Read(ByteBuffer buffer);
}
public enum PacketStage {
Establishing = 0,
Rejected = 1,
@ -33,123 +34,4 @@ namespace NeonTea.Quakeball.TeaNet.Packets {
IncorrectVersion = 1,
}
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 bool ReadBool() {
return Read() == 1;
}
public int ReadInt() {
return BitConverter.ToInt32(Read(4), 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 WriteBool(bool b) {
Write(b ? (byte)0b1 : (byte)0b0);
}
public void WriteInt(int i) {
Bytes.AddRange(BitConverter.GetBytes(i));
}
public void WriteString(string s) {
byte[] bytes = Encoding.UTF8.GetBytes(s);
WriteInt(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) {
WriteInt(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;
}
}
}