2020-08-05 03:21:04 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System;
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
using NeonTea.Quakeball.TeaNet.Peers;
|
2020-08-05 03:21:04 +02:00
|
|
|
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
namespace NeonTea.Quakeball.TeaNet.Packets {
|
2020-08-05 03:21:04 +02:00
|
|
|
public abstract class Protocol {
|
|
|
|
private Dictionary<Type, int> PacketToId = new Dictionary<Type, int>();
|
|
|
|
private Dictionary<int, Type> IdToPacket = new Dictionary<int, Type>();
|
|
|
|
private int PacketIdCounter;
|
|
|
|
|
|
|
|
public Peer Peer;
|
|
|
|
|
|
|
|
public abstract byte Identifier { get; }
|
|
|
|
public abstract string Version { get; }
|
|
|
|
|
|
|
|
public abstract void Receive(Connection conn, Packet packet);
|
|
|
|
public abstract void ConnectionStatusChanged(ConnectionStatus oldStatus, ConnectionStatus newStatus, Connection conn);
|
|
|
|
public abstract void Timeout(Connection conn);
|
|
|
|
|
|
|
|
public void SendPacket(Packet p, Connection conn) {
|
2020-08-05 19:39:52 +02:00
|
|
|
Peer.ConnectionManager.AddPacketToQueue(conn.uid, p);
|
|
|
|
Peer.ConnectionManager.SendPacketQueue(conn.uid);
|
2020-08-05 03:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int RegisterPacket(Type t) {
|
|
|
|
if (t.BaseType != typeof(Packet) || PacketToId.ContainsKey(t)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int id = PacketIdCounter++;
|
|
|
|
PacketToId.Add(t, id);
|
|
|
|
IdToPacket.Add(id, t);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ByteBuffer BuildMessage(Connection connection) {
|
|
|
|
ByteBuffer buffer = new ByteBuffer();
|
|
|
|
foreach (byte b in Peer.Fingerprint) {
|
|
|
|
buffer.Write(b);
|
|
|
|
}
|
|
|
|
buffer.Write(Identifier);
|
|
|
|
if (connection.Status == ConnectionStatus.Establishing) {
|
|
|
|
buffer.Write((byte)PacketStage.Establishing);
|
2020-08-05 20:38:37 +02:00
|
|
|
buffer.Write(Version);
|
2020-08-05 03:21:04 +02:00
|
|
|
} else if (connection.Status == ConnectionStatus.Closed) {
|
|
|
|
buffer.Write((byte)PacketStage.Closed);
|
|
|
|
} else if (connection.Status == ConnectionStatus.Rejected) {
|
|
|
|
buffer.Write((byte)PacketStage.Rejected);
|
|
|
|
buffer.Write((byte)connection.ClosingReason);
|
|
|
|
} else if (connection.Status == ConnectionStatus.Ready) {
|
|
|
|
buffer.Write((byte)PacketStage.Ready);
|
2020-08-05 20:38:37 +02:00
|
|
|
buffer.Write(connection.Internal.LatestInwardReliable);
|
2020-08-05 03:21:04 +02:00
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2020-08-05 18:50:28 +02:00
|
|
|
public int GetPacketTypeID(Packet packet) {
|
2020-08-05 03:21:04 +02:00
|
|
|
return PacketToId[packet.GetType()];
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type GetPacketType(int id) {
|
|
|
|
return IdToPacket[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|