using Godot; using Network.PacketHandling; using Network.PacketHandling.Packets; using System.Collections.Generic; using Util; namespace Network.Syncing { public class ConnectionHandler { public readonly Connection Connection; private readonly Peer Peer; private int LastUnreliableIDReceived = -1; private int LastReliableIDReceived = -1; private int LastReliableIDTheyReceived = -1; private float TimeSinceLastPacket = 0; private List ReliableQueue = new List(); private List UnreliableQueue = new List(); public ConnectionHandler(Peer peer, Connection connection) { Peer = peer; Connection = connection; } public void Update(float delta) { TimeSinceLastPacket += delta; if (TimeSinceLastPacket > Peer.TIMEOUT) { Peer.Disconnect(Connection, DisconnectReason.TIMEOUT); } } public void SerializeAndSendQueues(int UnreliableSyncID) { PacketBuffer Buffer = PacketBuffer.WithSignature(); SyncPkt Sync = new SyncPkt(); Sync.Metadata.Reliable = false; Sync.Metadata.ID = UnreliableSyncID; Sync.LastReliableIDReceived = LastReliableIDReceived; // Add Queues var Unreliables = UnreliableQueue.ToArray(); var Reliables = ReliableQueue.ToArray(); Sync.AmountOfPackets = Unreliables.Length + Reliables.Length; Sync.Serialize(Buffer); foreach (Serializable Unreliable in Unreliables) { Unreliable.Serialize(Buffer); } foreach (Serializable Reliable in Reliables) { Reliable.Serialize(Buffer); } Peer.SendBuffer(Buffer, Connection); // Clear unreliables-queue UnreliableQueue.Clear(); } public void ReceiveBuffer(PacketBuffer packetBuffer) { TimeSinceLastPacket = 0; SyncPkt Sync = new SyncPkt(); Sync.Deserialize(packetBuffer); LastReliableIDTheyReceived = Sync.LastReliableIDReceived; RemoveRedundantReliables(); // Read Queues int AmountOfPackets = Sync.AmountOfPackets; List Packets = new List(); for (int i = 0; i < AmountOfPackets; i++) { var Metadata = new SerializableMetadata(0); Metadata.Deserialize(packetBuffer); Packets.Add(Peer.Protocol.DeserializePacket(packetBuffer, Metadata)); } int BiggestReliableIDReceived = -1; int BiggestUnreliableIDReceived = -1; for (int i = 0; i < Packets.Count; i++) { Serializable Curr = Packets[i]; if (Curr.Metadata.Reliable) { if (Curr.Metadata.ID <= LastReliableIDReceived) { continue; } else if (Curr.Metadata.ID > BiggestReliableIDReceived) { BiggestReliableIDReceived = Curr.Metadata.ID; } } else { if (Curr.Metadata.ID <= LastUnreliableIDReceived) { continue; } else if (Curr.Metadata.ID > BiggestUnreliableIDReceived) { BiggestUnreliableIDReceived = Curr.Metadata.ID; } } LastReliableIDReceived = BiggestReliableIDReceived; LastUnreliableIDReceived = BiggestUnreliableIDReceived; Peer.Protocol.HandleSerializable(Curr); } } public void AddReliable(Serializable serializable, int ReliableID) { serializable.Metadata.Reliable = true; serializable.Metadata.ID = ReliableID; ReliableQueue.Add(serializable); } public void AddUnreliable(Serializable serializable, int UnreliableID) { serializable.Metadata.Reliable = false; serializable.Metadata.ID = UnreliableID; UnreliableQueue.Add(serializable); } private void RemoveRedundantReliables() { ReliableQueue = ReliableQueue.FindAll(Packet => Packet.Metadata.ID > LastReliableIDTheyReceived); } } }