quakeball/Assets/Scripts/TeaNet/Peers/ConnectionManager.cs

235 lines
9.7 KiB
C#
Raw Normal View History

2020-08-05 03:21:04 +02:00
using UnityEngine;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System;
using NeonTea.Quakeball.TeaNet.Packets;
2020-08-05 03:21:04 +02:00
namespace NeonTea.Quakeball.TeaNet.Peers {
2020-08-05 03:21:04 +02:00
public class ConnectionManager {
private ulong ConnectionCounter;
private Dictionary<ulong, Connection> Connections = new Dictionary<ulong, Connection>();
private Dictionary<IPEndPoint, ulong> IPtoID = new Dictionary<IPEndPoint, ulong>();
private Dictionary<ulong, List<Packet>> PacketQueue = new Dictionary<ulong, List<Packet>>();
2020-08-05 03:21:04 +02:00
private Peer Peer;
private Thread UpdateThread;
public long Timeout = 8000;
2020-08-05 16:59:16 +02:00
public long Interval = 100;
2020-08-05 03:21:04 +02:00
public ConnectionManager(Peer peer) {
Peer = peer;
UpdateThread = new Thread(new ThreadStart(UpdateThreadMethod));
UpdateThread.Start();
}
public void StopThread() {
UpdateThread.Abort();
}
public Connection Find(IPEndPoint endpoint) {
if (IPtoID.ContainsKey(endpoint)) {
return Connections[IPtoID[endpoint]];
2020-08-05 03:21:04 +02:00
}
Connection conn = new Connection(endpoint, ConnectionStatus.Awaiting);
AddConnection(conn);
2020-08-05 03:21:04 +02:00
return conn;
}
public bool StartConnection(IPEndPoint endpoint, byte protocolIdent) {
if (IPtoID.ContainsKey(endpoint)) {
2020-08-05 03:21:04 +02:00
return false;
}
Connection conn = new Connection(endpoint);
conn.Internal.AssignedProtocol = protocolIdent;
AddConnection(conn);
2020-08-05 03:21:04 +02:00
return true;
}
public Connection GetConnection(ulong uid) {
Connection conn;
Connections.TryGetValue(uid, out conn);
return conn;
2020-08-05 03:21:04 +02:00
}
public void AddPacketToQueue(ulong uid, Packet p) {
if (!Connections.ContainsKey(uid)) {
return;
}
p.Id = Connections[uid].Internal.ReliablePacketIDCounter++;
PacketQueue[uid].Add(p);
}
public void SendPacketQueue(ulong uid) {
if (!Connections.ContainsKey(uid)) {
return;
}
Connection conn = Connections[uid];
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
if (protocol != null && conn.IsReady()) {
2020-08-05 03:21:04 +02:00
ByteBuffer buffer = protocol.BuildMessage(conn);
List<Packet> list = PacketQueue[uid];
2020-08-05 03:21:04 +02:00
buffer.WriteInt(list.Count);
foreach (Packet p in list) {
buffer.WritePacket(protocol, p);
2020-08-05 03:21:04 +02:00
}
Send(conn, buffer);
}
}
public void SendSingleUnreliable(ulong uid, Packet p) {
if (!Connections.ContainsKey(uid)) {
return;
}
Connection conn = Connections[uid];
p.Id = conn.Internal.UnreliablePacketIDCounter++;
p.Reliable = false;
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
if (protocol != null && conn.IsReady()) {
2020-08-05 03:21:04 +02:00
ByteBuffer buffer = protocol.BuildMessage(conn);
buffer.WriteInt(1);
buffer.WritePacket(protocol, p);
2020-08-05 03:21:04 +02:00
Send(conn, buffer);
}
}
private void AddConnection(Connection conn) {
conn.uid = ConnectionCounter++;
Connections.Add(conn.uid, conn);
IPtoID.Add(conn.Endpoint, conn.uid);
PacketQueue.Add(conn.uid, new List<Packet>());
}
private void RemoveConnection(Connection conn) {
Connections.Remove(conn.uid);
IPtoID.Remove(conn.Endpoint);
PacketQueue.Remove(conn.uid);
}
2020-08-05 03:21:04 +02:00
private void SendPlain(Connection conn) {
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
2020-08-05 03:21:04 +02:00
if (protocol != null) {
ByteBuffer buffer = protocol.BuildMessage(conn);
Send(conn, buffer);
}
}
private void Send(Connection conn, ByteBuffer buffer) {
if (conn.Status == ConnectionStatus.Lost) {
return;
}
byte[] bytes = buffer.Pack();
Peer.ListenerThread.LastSentConnection = conn;
Peer.UdpClient.Send(bytes, bytes.Length, conn.Endpoint);
}
public void Handle(IPEndPoint endpoint, ByteBuffer buffer) {
Connection conn = Find(endpoint);
conn.Internal.LastMessage = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
2020-08-05 03:21:04 +02:00
ConnectionStatus oldStatus = conn.Status;
byte protocolId = buffer.Read();
Protocol protocol = Peer.GetProtocol(protocolId);
PacketStage stage = buffer.ReadStage();
switch (stage) {
case PacketStage.Establishing:
if (conn.Status == ConnectionStatus.Awaiting) {
conn.Internal.AssignedProtocol = protocolId;
2020-08-05 03:21:04 +02:00
string version = buffer.ReadString();
if (protocol == null || !version.Equals(protocol.Version)) {
conn.Status = ConnectionStatus.Rejected;
conn.ClosingReason = ClosingReason.IncorrectVersion;
2020-08-05 03:21:04 +02:00
} else {
conn.Status = ConnectionStatus.Ready;
}
if (protocol != null) {
protocol.ConnectionStatusChanged(oldStatus, conn.Status, conn);
}
}
break;
case PacketStage.Rejected:
conn.Status = ConnectionStatus.Closed;
conn.ClosingReason = buffer.ReadClosingReason();
if (protocol != null) {
protocol.ConnectionStatusChanged(oldStatus, conn.Status, conn);
}
break;
case PacketStage.Closed:
if (conn.Status == ConnectionStatus.Stopped) {
break;
}
2020-08-05 03:21:04 +02:00
conn.Status = ConnectionStatus.Stopped;
if (protocol != null) {
protocol.ConnectionStatusChanged(oldStatus, conn.Status, conn);
}
break;
case PacketStage.Ready:
if (conn.Internal.AssignedProtocol != protocolId || protocol == null) {
2020-08-05 03:21:04 +02:00
break;
}
if (oldStatus == ConnectionStatus.Establishing) { // Update connection status
2020-08-05 03:21:04 +02:00
conn.Status = ConnectionStatus.Ready;
protocol.ConnectionStatusChanged(oldStatus, conn.Status, conn);
}
conn.Internal.LatestOutwardReliable = buffer.ReadInt();
List<Packet> list = PacketQueue[conn.uid];
list.RemoveAll(p => p.Id <= conn.Internal.LatestOutwardReliable);
PacketQueue[conn.uid] = list;
2020-08-05 03:21:04 +02:00
int PacketAmount = buffer.ReadInt();
for (int i = 0; i < PacketAmount; i++) {
Packet p = buffer.ReadPacket(protocol);
if (p.Reliable) {
if (p.Id > conn.Internal.LatestInwardReliable) {
conn.Internal.LatestInwardReliable = p.Id;
protocol.Receive(conn, p);
}
} else if (p.Id > conn.Internal.LatestInwardUnreliable) {
conn.Internal.LatestInwardUnreliable = p.Id;
protocol.Receive(conn, p);
}
2020-08-05 03:21:04 +02:00
}
break;
}
}
private void UpdateThreadMethod() {
try {
2020-08-05 16:59:16 +02:00
while (Thread.CurrentThread.IsAlive) {
2020-08-05 03:21:04 +02:00
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
List<ulong> timedOut = new List<ulong>();
foreach (ulong uid in Connections.Keys) {
Connection conn = Connections[uid];
if ((now - conn.Internal.LastMessage) > Timeout || conn.Status == ConnectionStatus.Lost) {
timedOut.Add(uid);
2020-08-05 16:59:16 +02:00
}
if (conn.Status != ConnectionStatus.Awaiting || conn.Status != ConnectionStatus.Stopped) {
if (conn.Status == ConnectionStatus.Ready) {
SendPacketQueue(uid);
2020-08-05 16:59:16 +02:00
} else {
SendPlain(conn);
2020-08-05 03:21:04 +02:00
}
}
2020-08-05 16:59:16 +02:00
}
foreach (ulong uid in timedOut) {
Connection conn = Connections[uid];
RemoveConnection(conn);
if (conn.Status == ConnectionStatus.Ready
|| conn.Status == ConnectionStatus.Establishing
|| conn.Status == ConnectionStatus.Awaiting
|| conn.Status == ConnectionStatus.Lost) {
Protocol protocol = Peer.GetProtocol(conn.Internal.AssignedProtocol);
2020-08-05 16:59:16 +02:00
if (protocol != null) {
protocol.Timeout(conn);
2020-08-05 03:21:04 +02:00
}
}
}
2020-08-05 16:59:16 +02:00
Thread.Sleep((int)Interval);
2020-08-05 03:21:04 +02:00
}
2020-08-05 16:59:16 +02:00
} catch (ThreadAbortException) {
Peer.MessageListener.Message("Connection Thread Stopped");
2020-08-05 03:21:04 +02:00
}
}
}
}