GodotTicTacToe/scripts/net/packethandling/PacketBuffer.cs

33 lines
948 B
C#

using Godot;
using System.Collections.Generic;
namespace Network.PacketHandling {
public class PacketBuffer {
private List<byte> ByteList;
private int ReadCounter = 0;
public byte[] ByteBuffer { get { return ByteList.ToArray(); } }
public int Length { get { return ByteList.Count; } }
public bool HasNext { get { return ReadCounter > ByteList.Count; } }
public byte Read() {
if (!HasNext) {
GD.printerr("Attempted to read byte; Impossible, byte buffer ended.");
return 0;
}
return ByteList[ReadCounter++];
}
public void Write(byte toWrite) {
ByteList.Add(toWrite);
}
public static PacketBuffer FromByteBuffer(byte[] byteBuffer) {
PacketBuffer PB = new PacketBuffer();
PB.ByteList = new List<byte>(byteBuffer);
return PB;
}
}
}