Make some neatness updates

This commit is contained in:
Sofia 2020-08-05 17:59:16 +03:00
parent 63eddfa43b
commit 7b426f4f8c
3 changed files with 46 additions and 46 deletions

View File

@ -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<KeyValuePair<IPEndPoint, Connection>> timedOut = new List<KeyValuePair<IPEndPoint, Connection>>();
foreach (KeyValuePair<IPEndPoint, Connection> 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<KeyValuePair<IPEndPoint, Connection>> timedOut = new List<KeyValuePair<IPEndPoint, Connection>>();
foreach (KeyValuePair<IPEndPoint, Connection> pair in Connections) {
Connection conn = pair.Value;
if ((now - conn.LastMessage) > Timeout || conn.Status == ConnectionStatus.Lost) {
timedOut.Add(pair);
}
foreach (KeyValuePair<IPEndPoint, Connection> 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<IPEndPoint, Connection> 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");
}
}

View File

@ -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");
}
}

View File

@ -129,6 +129,7 @@ PlayerSettings:
preloadedAssets:
- {fileID: 0}
- {fileID: 0}
- {fileID: 0}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1