From 7b426f4f8c0151fd67584c2210d65dc51a8196aa Mon Sep 17 00:00:00 2001 From: teascade Date: Wed, 5 Aug 2020 17:59:16 +0300 Subject: [PATCH] Make some neatness updates --- Assets/Scripts/Net/Peers/ConnectionManager.cs | 57 +++++++++---------- Assets/Scripts/Net/Peers/ListenerThread.cs | 34 +++++------ ProjectSettings/ProjectSettings.asset | 1 + 3 files changed, 46 insertions(+), 46 deletions(-) diff --git a/Assets/Scripts/Net/Peers/ConnectionManager.cs b/Assets/Scripts/Net/Peers/ConnectionManager.cs index 9f7dcd4..feb40be 100644 --- a/Assets/Scripts/Net/Peers/ConnectionManager.cs +++ b/Assets/Scripts/Net/Peers/ConnectionManager.cs @@ -12,10 +12,9 @@ namespace NeonTea.Quakeball.Net.Peers { private Peer Peer; private Thread UpdateThread; - private long LastUpdate; public long Timeout = 8000; - public long Frequency = 100; + public long Interval = 100; public ConnectionManager(Peer peer) { Peer = peer; @@ -159,40 +158,38 @@ namespace NeonTea.Quakeball.Net.Peers { private void UpdateThreadMethod() { try { - while (true) { + while (Thread.CurrentThread.IsAlive) { long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - if ((now - LastUpdate) > Frequency) { - LastUpdate = now; - List> timedOut = new List>(); - foreach (KeyValuePair pair in Connections) { - Connection conn = pair.Value; - if ((now - conn.LastMessage) > Timeout || conn.Status == ConnectionStatus.Lost) { - timedOut.Add(pair); - } - if (conn.Status != ConnectionStatus.Awaiting || conn.Status != ConnectionStatus.Stopped) { - if (conn.Status == ConnectionStatus.Ready) { - SendPacketQueue(conn); - } else { - SendPlain(conn); - } - } + List> timedOut = new List>(); + foreach (KeyValuePair pair in Connections) { + Connection conn = pair.Value; + if ((now - conn.LastMessage) > Timeout || conn.Status == ConnectionStatus.Lost) { + timedOut.Add(pair); } - foreach (KeyValuePair pair in timedOut) { - Connections.Remove(pair.Key); - PacketQueue.Remove(pair.Value); - if (pair.Value.Status == ConnectionStatus.Ready - || pair.Value.Status == ConnectionStatus.Establishing - || pair.Value.Status == ConnectionStatus.Awaiting - || pair.Value.Status == ConnectionStatus.Lost) { - Protocol protocol = Peer.GetProtocol(pair.Value.AssignedProtocol); - if (protocol != null) { - protocol.Timeout(pair.Value); - } + if (conn.Status != ConnectionStatus.Awaiting || conn.Status != ConnectionStatus.Stopped) { + if (conn.Status == ConnectionStatus.Ready) { + SendPacketQueue(conn); + } else { + SendPlain(conn); } } } + foreach (KeyValuePair pair in timedOut) { + Connections.Remove(pair.Key); + PacketQueue.Remove(pair.Value); + if (pair.Value.Status == ConnectionStatus.Ready + || pair.Value.Status == ConnectionStatus.Establishing + || pair.Value.Status == ConnectionStatus.Awaiting + || pair.Value.Status == ConnectionStatus.Lost) { + Protocol protocol = Peer.GetProtocol(pair.Value.AssignedProtocol); + if (protocol != null) { + protocol.Timeout(pair.Value); + } + } + } + Thread.Sleep((int)Interval); } - } catch (ThreadAbortException e) { + } catch (ThreadAbortException) { Debug.Log("Connection Thread Stopped"); } } diff --git a/Assets/Scripts/Net/Peers/ListenerThread.cs b/Assets/Scripts/Net/Peers/ListenerThread.cs index cb6dd8b..7c405d7 100644 --- a/Assets/Scripts/Net/Peers/ListenerThread.cs +++ b/Assets/Scripts/Net/Peers/ListenerThread.cs @@ -44,26 +44,28 @@ namespace NeonTea.Quakeball.Net.Peers { private void ListenThreadMethod() { try { - while (true) { - IPEndPoint Listened = new IPEndPoint(EndPoint.Address, EndPoint.Port); - ByteBuffer Buffer = new ByteBuffer(); - try { - Buffer = new ByteBuffer(Peer.UdpClient.Receive(ref Listened)); - } catch (SocketException e) { - if (Array.Exists(CONN_LOST_CODES, x => x == e.ErrorCode)) { - if (LastSentConnection != null) { - LastSentConnection.Status = ConnectionStatus.Lost; - Peer.MessageListener.Message($"Connection lost to {LastSentConnection.Endpoint}: {e.ToString()}"); + while (Thread.CurrentThread.IsAlive) { + if (Peer.UdpClient.Available > 0) { + IPEndPoint Listened = new IPEndPoint(EndPoint.Address, EndPoint.Port); + ByteBuffer Buffer = new ByteBuffer(); + try { + Buffer = new ByteBuffer(Peer.UdpClient.Receive(ref Listened)); + } catch (SocketException e) { + if (Array.Exists(CONN_LOST_CODES, x => x == e.ErrorCode)) { + if (LastSentConnection != null) { + LastSentConnection.Status = ConnectionStatus.Lost; + Peer.MessageListener.Message($"Connection lost to {LastSentConnection.Endpoint}: {e.ToString()}"); + } + } else { + Peer.MessageListener.Message($"Listener error: {e.ToString()}"); } - } else { - Peer.MessageListener.Message($"Listener error: {e.ToString()}"); + } + if (Buffer.ReadFingerprint(Peer.Fingerprint)) { + Peer.ConnectionManager.Handle(Listened, Buffer); } } - if (Buffer.ReadFingerprint(Peer.Fingerprint)) { - Peer.ConnectionManager.Handle(Listened, Buffer); - } } - } catch (ThreadAbortException e) { + } catch (ThreadAbortException) { Debug.Log("Listener Thread stopped"); } } diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 0c49b4f..d01afab 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -129,6 +129,7 @@ PlayerSettings: preloadedAssets: - {fileID: 0} - {fileID: 0} + - {fileID: 0} metroInputSource: 0 wsaTransparentSwapchain: 0 m_HolographicPauseOnTrackingLoss: 1