2017-11-25 11:26:55 +01:00
|
|
|
|
|
2017-11-25 12:20:59 +01:00
|
|
|
|
using Godot;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
using Network.PacketHandling;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Util;
|
|
|
|
|
|
|
|
|
|
namespace Network.Syncing {
|
|
|
|
|
public class PacketDistributor {
|
|
|
|
|
|
|
|
|
|
private Peer Peer;
|
|
|
|
|
|
|
|
|
|
private List<ConnectionHandler> HandlerList = new List<ConnectionHandler>();
|
|
|
|
|
|
|
|
|
|
private int UnreliableIDCounter = 0;
|
|
|
|
|
private int ReliableIDCounter = 0;
|
|
|
|
|
|
|
|
|
|
private float TimeSinceLastSync = 0;
|
|
|
|
|
private readonly float Interval;
|
|
|
|
|
|
|
|
|
|
public PacketDistributor(Peer peer, float interval) {
|
|
|
|
|
Peer = peer;
|
|
|
|
|
Interval = interval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(float delta) {
|
|
|
|
|
TimeSinceLastSync += delta;
|
|
|
|
|
if (TimeSinceLastSync > Interval) {
|
2017-11-25 12:20:59 +01:00
|
|
|
|
TimeSinceLastSync = 0;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
foreach (ConnectionHandler Handler in HandlerList) {
|
2017-11-25 12:27:55 +01:00
|
|
|
|
GD.print("Sending to " + Handler.Connection.Name);
|
|
|
|
|
Handler.SerializeAndSendQueues(UnreliableIDCounter++);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HandleRawPacket(PacketBuffer packetBuffer, Connection connection) {
|
|
|
|
|
var Handler = GetHandlerFor(connection);
|
2017-11-25 12:03:26 +01:00
|
|
|
|
if (Handler.isEmpty) {
|
2017-11-25 12:20:59 +01:00
|
|
|
|
Handler = new ConnectionHandler(Peer, connection);
|
|
|
|
|
HandlerList.Add(Handler);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
2017-11-25 12:20:59 +01:00
|
|
|
|
Handler.Value.ReceiveBuffer(packetBuffer);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddHandler(Connection connection) {
|
2017-11-25 11:29:48 +01:00
|
|
|
|
var Handler = new ConnectionHandler(Peer, connection);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
HandlerList.Add(Handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddReliableFor(Connection connection, Serializable serializable) {
|
|
|
|
|
var ConnHandlerOpt = GetHandlerFor(connection);
|
|
|
|
|
if (ConnHandlerOpt.HasValue) {
|
|
|
|
|
ConnHandlerOpt.Value.AddReliable(serializable, ReliableIDCounter++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddUnreliableFor(Connection connection, Serializable serializable) {
|
|
|
|
|
var ConnHandlerOpt = GetHandlerFor(connection);
|
|
|
|
|
if (ConnHandlerOpt.HasValue) {
|
|
|
|
|
ConnHandlerOpt.Value.AddUnreliable(serializable, UnreliableIDCounter++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddReliableForAll(Serializable serializable) {
|
|
|
|
|
foreach (ConnectionHandler Handler in HandlerList) {
|
|
|
|
|
Handler.AddReliable(serializable, ReliableIDCounter++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddUnreliableForAll(Serializable serializable) {
|
|
|
|
|
foreach (ConnectionHandler Handler in HandlerList) {
|
|
|
|
|
Handler.AddUnreliable(serializable, UnreliableIDCounter++);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Optional<ConnectionHandler> GetHandlerFor(Connection connection) {
|
|
|
|
|
foreach (ConnectionHandler Curr in HandlerList) {
|
|
|
|
|
if (Curr.Connection == connection) {
|
|
|
|
|
return Curr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-25 12:20:59 +01:00
|
|
|
|
return Optional<ConnectionHandler>.None;
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|