2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
using Godot;
|
|
|
|
|
using Network.PacketHandling;
|
2017-11-25 11:29:48 +01:00
|
|
|
|
using Network.PacketHandling.Packets;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-11-25 15:23:55 +01:00
|
|
|
|
using Util;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
namespace Network.Syncing {
|
|
|
|
|
public class ConnectionHandler {
|
|
|
|
|
|
|
|
|
|
public readonly Connection Connection;
|
|
|
|
|
|
|
|
|
|
private readonly Peer Peer;
|
|
|
|
|
|
|
|
|
|
private int LastUnreliableIDReceived = -1;
|
|
|
|
|
private int LastReliableIDReceived = -1;
|
2017-11-25 14:21:06 +01:00
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
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) {
|
2017-11-25 11:26:55 +01:00
|
|
|
|
PacketBuffer Buffer = PacketBuffer.WithSignature();
|
2017-11-25 12:27:55 +01:00
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
SyncPkt Sync = new SyncPkt();
|
2017-11-25 14:21:06 +01:00
|
|
|
|
Sync.Metadata.Reliable = false;
|
|
|
|
|
Sync.Metadata.ID = UnreliableSyncID;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
Sync.LastReliableIDReceived = LastReliableIDReceived;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
// Add Queues
|
2017-11-25 14:21:06 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
GD.print("Sending sync-" + Sync.Metadata.ID + " to " + Connection);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
Peer.SendBuffer(Buffer, Connection);
|
2017-11-25 14:21:06 +01:00
|
|
|
|
|
|
|
|
|
// Clear unreliables-queue
|
|
|
|
|
|
|
|
|
|
UnreliableQueue.Clear();
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReceiveBuffer(PacketBuffer packetBuffer) {
|
2017-11-25 15:23:55 +01:00
|
|
|
|
TimeSinceLastPacket = 0;
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
SyncPkt Sync = new SyncPkt();
|
|
|
|
|
Sync.Deserialize(packetBuffer);
|
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
GD.print("Received sync-" + Sync.Metadata.ID);
|
|
|
|
|
|
|
|
|
|
LastReliableIDTheyReceived = Sync.LastReliableIDReceived;
|
|
|
|
|
|
|
|
|
|
RemoveRedundantReliables();
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
// Read Queues
|
2017-11-25 14:21:06 +01:00
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 14:38:04 +01:00
|
|
|
|
int BiggestReliableIDReceived = -1;
|
|
|
|
|
int BiggestUnreliableIDReceived = -1;
|
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
for (int i = 0; i < Packets.Count; i++) {
|
|
|
|
|
Serializable Curr = Packets[i];
|
|
|
|
|
if (Curr.Metadata.Reliable) {
|
|
|
|
|
if (Curr.Metadata.ID <= LastReliableIDReceived) {
|
|
|
|
|
continue;
|
2017-11-25 14:38:04 +01:00
|
|
|
|
} else if (Curr.Metadata.ID > BiggestReliableIDReceived) {
|
|
|
|
|
BiggestReliableIDReceived = Curr.Metadata.ID;
|
2017-11-25 14:21:06 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (Curr.Metadata.ID <= LastUnreliableIDReceived) {
|
|
|
|
|
continue;
|
2017-11-25 14:38:04 +01:00
|
|
|
|
} else if (Curr.Metadata.ID > BiggestUnreliableIDReceived) {
|
|
|
|
|
BiggestUnreliableIDReceived = Curr.Metadata.ID;
|
2017-11-25 14:21:06 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 14:38:04 +01:00
|
|
|
|
LastReliableIDReceived = BiggestReliableIDReceived;
|
|
|
|
|
LastUnreliableIDReceived = BiggestUnreliableIDReceived;
|
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
Peer.Protocol.HandleSerializable(Curr);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddReliable(Serializable serializable, int ReliableID) {
|
2017-11-25 14:21:06 +01:00
|
|
|
|
serializable.Metadata.Reliable = true;
|
|
|
|
|
serializable.Metadata.ID = ReliableID;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
ReliableQueue.Add(serializable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddUnreliable(Serializable serializable, int UnreliableID) {
|
2017-11-25 14:21:06 +01:00
|
|
|
|
serializable.Metadata.Reliable = false;
|
|
|
|
|
serializable.Metadata.ID = UnreliableID;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
UnreliableQueue.Add(serializable);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
private void RemoveRedundantReliables() {
|
2017-11-25 14:59:42 +01:00
|
|
|
|
ReliableQueue = ReliableQueue.FindAll(Packet => Packet.Metadata.ID > LastReliableIDTheyReceived);
|
2017-11-25 14:21:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|