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

52 lines
1.9 KiB
C#

namespace NeonTea.Quakeball.TeaNet.Packets {
/// <summary>A packet for sending stuff over to connections.</summary>
public abstract class Packet {
/// <summary>Packet meta-information: Is this packet reliable. Set just before sending.</summary>
public bool PacketIsReliable = true;
/// <summary>Packet meta-information: Id of this packet. Set just before sending.</summary>
public int PacketId;
/// <summary>Write any relevant information about this packet into the buffer.</summary>
public abstract void Write(ByteBuffer buffer);
/// <summary>Read and assign any relevant information about this packet from the buffer.</summary>
public abstract void Read(ByteBuffer buffer);
/// <summary>Reads packet meta-information from the buffer.</summary>
public void ReadMeta(ByteBuffer buffer) {
PacketId = buffer.ReadInt();
PacketIsReliable = buffer.ReadBool();
}
/// <summary>Writes packet meta-information to the buffer.</summary>
public void WriteMeta(ByteBuffer buffer) {
buffer.Write(PacketId);
buffer.Write(PacketIsReliable);
}
/// <summary>Make a shallow copy for this packet, copying any primitives but retaining any references to instances.</summary>
public Packet ShallowCopy() {
return (Packet)this.MemberwiseClone();
}
}
/// <summary>Defines something as writeable/readable by the buffer. Useful for creating abstractions within packets.</summary>
public interface Serializable {
void Write(ByteBuffer buffer);
void Read(ByteBuffer buffer);
}
public enum PacketStage {
Establishing = 0,
Rejected = 1,
Closed = 2,
Ready = 3,
}
public enum ClosingReason {
Unknown = 0,
IncorrectVersion = 1,
Timeout = 2,
Manual = 3,
}
}