GodotTicTacToe/scripts/net/syncing/ConnectionHandler.cs

139 lines
4.5 KiB
C#
Raw Normal View History


using Godot;
using Network.PacketHandling;
2017-11-25 11:29:48 +01:00
using Network.PacketHandling.Packets;
using System.Collections.Generic;
2017-11-25 15:23:55 +01:00
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<Serializable> ReliableQueue = new List<Serializable>();
private List<Serializable> UnreliableQueue = new List<Serializable>();
public ConnectionHandler(Peer peer, Connection connection) {
Peer = peer;
Connection = connection;
}
2017-11-25 15:23:55 +01:00
public void Update(float delta) {
TimeSinceLastPacket += delta;
if (TimeSinceLastPacket > Peer.TIMEOUT) {
Peer.Disconnect(Connection, DisconnectReason.TIMEOUT);
}
}
2017-11-25 12:27:55 +01:00
public void SerializeAndSendQueues(int UnreliableSyncID) {
PacketBuffer Buffer = PacketBuffer.WithSignature();
2017-11-25 12:27:55 +01:00
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);
}
GD.print("Sending sync-" + Sync.Metadata.ID + " to " + Connection);
Peer.SendBuffer(Buffer, Connection);
// Clear unreliables-queue
UnreliableQueue.Clear();
}
public void ReceiveBuffer(PacketBuffer packetBuffer) {
2017-11-25 15:23:55 +01:00
TimeSinceLastPacket = 0;
SyncPkt Sync = new SyncPkt();
Sync.Deserialize(packetBuffer);
GD.print("Received sync-" + Sync.Metadata.ID);
LastReliableIDTheyReceived = Sync.LastReliableIDReceived;
RemoveRedundantReliables();
// Read Queues
int AmountOfPackets = Sync.AmountOfPackets;
List<Serializable> Packets = new List<Serializable>();
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() {
2017-11-25 14:59:42 +01:00
ReliableQueue = ReliableQueue.FindAll(Packet => Packet.Metadata.ID > LastReliableIDTheyReceived);
}
}
}