2020-08-05 18:50:28 +02:00
|
|
|
namespace NeonTea.Quakeball.TeaNet.Packets {
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>A packet for sending stuff over to connections.</summary>
|
2020-08-05 03:21:04 +02:00
|
|
|
public abstract class Packet {
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Packet meta-information: Is this packet reliable. Set just before sending.</summary>
|
2020-08-07 03:46:09 +02:00
|
|
|
public bool PacketIsReliable = true;
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Packet meta-information: Id of this packet. Set just before sending.</summary>
|
2020-08-07 03:46:09 +02:00
|
|
|
public int PacketId;
|
2020-08-10 19:57:35 +02:00
|
|
|
/// <summary>Size of this packet in bytes. Only available after the packet has been Read (when received) or Written (when sent).</summary>
|
|
|
|
public int Size;
|
2020-08-05 03:21:04 +02:00
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Write any relevant information about this packet into the buffer.</summary>
|
2020-08-05 03:21:04 +02:00
|
|
|
public abstract void Write(ByteBuffer buffer);
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Read and assign any relevant information about this packet from the buffer.</summary>
|
2020-08-05 03:21:04 +02:00
|
|
|
public abstract void Read(ByteBuffer buffer);
|
2020-08-05 18:50:28 +02:00
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Reads packet meta-information from the buffer.</summary>
|
2020-08-05 18:50:28 +02:00
|
|
|
public void ReadMeta(ByteBuffer buffer) {
|
2020-08-07 03:46:09 +02:00
|
|
|
PacketId = buffer.ReadInt();
|
|
|
|
PacketIsReliable = buffer.ReadBool();
|
2020-08-05 18:50:28 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Writes packet meta-information to the buffer.</summary>
|
2020-08-05 18:50:28 +02:00
|
|
|
public void WriteMeta(ByteBuffer buffer) {
|
2020-08-07 03:46:09 +02:00
|
|
|
buffer.Write(PacketId);
|
|
|
|
buffer.Write(PacketIsReliable);
|
2020-08-05 18:50:28 +02:00
|
|
|
}
|
2020-08-05 21:02:56 +02:00
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Make a shallow copy for this packet, copying any primitives but retaining any references to instances.</summary>
|
2020-08-05 21:02:56 +02:00
|
|
|
public Packet ShallowCopy() {
|
|
|
|
return (Packet)this.MemberwiseClone();
|
|
|
|
}
|
2020-08-05 03:21:04 +02:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:49:51 +02:00
|
|
|
/// <summary>Defines something as writeable/readable by the buffer. Useful for creating abstractions within packets.</summary>
|
2020-08-05 20:33:22 +02:00
|
|
|
public interface Serializable {
|
|
|
|
void Write(ByteBuffer buffer);
|
|
|
|
void Read(ByteBuffer buffer);
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:21:04 +02:00
|
|
|
public enum PacketStage {
|
|
|
|
Establishing = 0,
|
|
|
|
Rejected = 1,
|
|
|
|
Closed = 2,
|
|
|
|
Ready = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ClosingReason {
|
|
|
|
Unknown = 0,
|
2020-08-05 18:50:28 +02:00
|
|
|
IncorrectVersion = 1,
|
2020-08-07 03:46:09 +02:00
|
|
|
Timeout = 2,
|
|
|
|
Manual = 3,
|
2020-08-05 03:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|