Add disconnect by timeout

This commit is contained in:
Sofia 2017-11-25 16:23:55 +02:00
parent 02406eab24
commit b9aacb530b
5 changed files with 57 additions and 1 deletions

View File

@ -28,6 +28,14 @@ namespace Network {
return false; return false;
} }
public bool RemoveConnection(Connection conn) {
if (Contains(conn)) {
Connections.Remove(conn);
return true;
}
return false;
}
public bool Contains(Connection conn) { public bool Contains(Connection conn) {
for (int i = 0; i < Connections.Count; i++) { for (int i = 0; i < Connections.Count; i++) {
Connection inList = Connections[i]; Connection inList = Connections[i];

View File

@ -8,6 +8,8 @@ using Thread = System.Threading.Thread;
namespace Network { namespace Network {
public abstract class Peer : Object { public abstract class Peer : Object {
public const float TIMEOUT = 4f; // 4 seconds
private static PacketPeerUDP PacketPeer; private static PacketPeerUDP PacketPeer;
private static Peer Singleton; private static Peer Singleton;
public readonly bool IsServer; public readonly bool IsServer;
@ -26,7 +28,7 @@ namespace Network {
IsServer = isServer; IsServer = isServer;
Protocol = new Protocol(this); Protocol = new Protocol(this);
ConnectionList = new ConnectionList(this); ConnectionList = new ConnectionList(this);
PacketDistributor = new PacketDistributor(this, 2); PacketDistributor = new PacketDistributor(this, 0.5f);
} }
public void Update(float delta) { public void Update(float delta) {
@ -37,6 +39,7 @@ namespace Network {
public abstract void Initialize(string address, int port); public abstract void Initialize(string address, int port);
public abstract void Process(float delta); public abstract void Process(float delta);
public abstract void Connected(Connection conn); public abstract void Connected(Connection conn);
public abstract void Disconnected(Connection conn);
public void SendBuffer(PacketBuffer packetBuffer, Connection to) { public void SendBuffer(PacketBuffer packetBuffer, Connection to) {
if (LastConnectionSended != to.ID) { if (LastConnectionSended != to.ID) {
@ -44,6 +47,7 @@ namespace Network {
LastConnectionSended = to.ID; LastConnectionSended = to.ID;
} }
PacketPeer.PutPacket(packetBuffer.ByteBuffer); PacketPeer.PutPacket(packetBuffer.ByteBuffer);
GD.print("Sent PacketBuffer with size " + packetBuffer.ByteBuffer.Length + " bytes.");
} }
public bool StartListening(int port, string address = "*") { public bool StartListening(int port, string address = "*") {
@ -93,5 +97,22 @@ namespace Network {
} }
} }
} }
public void Disconnect(Connection conn, DisconnectReason reason) {
GD.print(conn + " disconnected. Reason:");
switch (reason) {
case DisconnectReason.TIMEOUT: {
GD.print("TIMEOUT");
break;
}
default: {
GD.print("Unknown.");
break;
}
}
ConnectionList.RemoveConnection(conn);
PacketDistributor.RemoveHandler(conn);
}
} }
} }

View File

@ -3,6 +3,7 @@ using Godot;
using Network.PacketHandling; using Network.PacketHandling;
using Network.PacketHandling.Packets; using Network.PacketHandling.Packets;
using System.Collections.Generic; using System.Collections.Generic;
using Util;
namespace Network.Syncing { namespace Network.Syncing {
public class ConnectionHandler { public class ConnectionHandler {
@ -26,6 +27,13 @@ namespace Network.Syncing {
Connection = connection; Connection = connection;
} }
public void Update(float delta) {
TimeSinceLastPacket += delta;
if (TimeSinceLastPacket > Peer.TIMEOUT) {
Peer.Disconnect(Connection, DisconnectReason.TIMEOUT);
}
}
public void SerializeAndSendQueues(int UnreliableSyncID) { public void SerializeAndSendQueues(int UnreliableSyncID) {
PacketBuffer Buffer = PacketBuffer.WithSignature(); PacketBuffer Buffer = PacketBuffer.WithSignature();
@ -59,6 +67,8 @@ namespace Network.Syncing {
} }
public void ReceiveBuffer(PacketBuffer packetBuffer) { public void ReceiveBuffer(PacketBuffer packetBuffer) {
TimeSinceLastPacket = 0;
SyncPkt Sync = new SyncPkt(); SyncPkt Sync = new SyncPkt();
Sync.Deserialize(packetBuffer); Sync.Deserialize(packetBuffer);

View File

@ -31,6 +31,10 @@ namespace Network.Syncing {
Handler.SerializeAndSendQueues(UnreliableIDCounter++); Handler.SerializeAndSendQueues(UnreliableIDCounter++);
} }
} }
foreach (ConnectionHandler Handler in HandlerList.ToArray()) {
Handler.Update(delta);
}
} }
public void HandleRawPacket(PacketBuffer packetBuffer, Connection connection) { public void HandleRawPacket(PacketBuffer packetBuffer, Connection connection) {
@ -47,6 +51,13 @@ namespace Network.Syncing {
HandlerList.Add(Handler); HandlerList.Add(Handler);
} }
public void RemoveHandler(Connection connection) {
Optional<ConnectionHandler> Handler = GetHandlerFor(connection);
if (Handler.HasValue) {
HandlerList.Remove(Handler);
}
}
public void AddReliableFor(Connection connection, Serializable serializable) { public void AddReliableFor(Connection connection, Serializable serializable) {
var ConnHandlerOpt = GetHandlerFor(connection); var ConnHandlerOpt = GetHandlerFor(connection);
if (ConnHandlerOpt.HasValue) { if (ConnHandlerOpt.HasValue) {

View File

@ -0,0 +1,6 @@

namespace Util {
public enum DisconnectReason {
TIMEOUT
}
}