GodotTicTacToe/scripts/net/syncing/ConnectionHandler.cs

65 lines
1.8 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;
namespace Network.Syncing {
public class ConnectionHandler {
public readonly Connection Connection;
private readonly Peer Peer;
private int LastUnreliableIDReceived = -1;
private int LastReliableIDReceived = -1;
private int LastUnreliableIDTheyReceived = -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;
}
public void SerializeAndSendQueues() {
PacketBuffer Buffer = PacketBuffer.WithSignature();
SyncPkt Sync = new SyncPkt();
Sync.Serialize(Buffer);
// Add Queues
Peer.SendBuffer(Buffer, Connection);
}
public void ReceiveBuffer(PacketBuffer packetBuffer) {
SyncPkt Sync = new SyncPkt();
Sync.Deserialize(packetBuffer);
GD.print("Received sync-" + Sync.ID);
// Read Queues
}
public void AddReliable(Serializable serializable, int ReliableID) {
serializable.Reliable = true;
serializable.ID = ReliableID;
ReliableQueue.Add(serializable);
}
public void AddUnreliable(Serializable serializable, int UnreliableID) {
serializable.Reliable = false;
serializable.ID = UnreliableID;
UnreliableQueue.Add(serializable);
}
}
}