From: Forest Date: Thu, 2 Apr 2020 23:07:08 +0000 (-0700) Subject: Add 8x redundancy to acks X-Git-Tag: 1.0.0~27^2~8 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=2ceee1f8fbdfc17f19de361654e7e1c0885f2dc1;p=rhonda%2Fimpostor.hazel.git Add 8x redundancy to acks --- diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 5a7c440..b15f883 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -56,12 +56,7 @@ namespace Hazel.Udp /// /// The packet id that was received last. /// - private volatile ushort reliableReceiveLast = 0; - - /// - /// Has the connection received anything yet - /// - private volatile bool hasReceivedSomething = false; + private volatile ushort reliableReceiveLast = ushort.MaxValue; private object PingLock = new object(); @@ -330,7 +325,7 @@ namespace Hazel.Udp id = (ushort)((b1 << 8) + b2); //Send an acknowledgement - SendAck(b1, b2); + SendAck(id); /* * It gets a little complicated here (note the fact I'm actually using a multiline comment for once...) @@ -370,7 +365,7 @@ namespace Hazel.Udp isNew = id > reliableReceiveLast && id <= overwritePointer; //Figure (3) //If it's new or we've not received anything yet - if (isNew || !hasReceivedSomething) + if (isNew) { //Mark items between the most recent receive and the id received as missing for (ushort i = (ushort)(reliableReceiveLast + 1); i < id; i++) @@ -380,7 +375,6 @@ namespace Hazel.Udp //Update the most recently received reliableReceiveLast = id; - hasReceivedSomething = true; } //Else it could be a missing packet @@ -405,9 +399,33 @@ namespace Hazel.Udp { this.pingsSinceAck = 0; - //Get ID + // Get ID ushort id = (ushort)((bytes[1] << 8) + bytes[2]); + AcknowledgeMessageId(id); + + if (bytesReceived == 4) + { + byte recentPackets = bytes[3]; + for (int i = 1; i <= 8; ++i) + { + if ((recentPackets & 1) != 0) + { + AcknowledgeMessageId((ushort)(id - i)); + } + + recentPackets >>= 1; + } + + Statistics.LogReliableReceive(bytesReceived - 4, bytesReceived); + } + else + { + Statistics.LogReliableReceive(bytesReceived - 3, bytesReceived); + } + } + private void AcknowledgeMessageId(ushort id) + { // Dispose of timer and remove from dictionary if (reliableDataPacketsSent.TryRemove(id, out Packet packet)) { @@ -432,8 +450,6 @@ namespace Hazel.Udp this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f); } } - - Statistics.LogReliableReceive(bytesReceived - 3, bytesReceived); } /// @@ -441,13 +457,27 @@ namespace Hazel.Udp /// /// The first identification byte. /// The second identification byte. - private void SendAck(byte byte1, byte byte2) + private void SendAck(ushort id) { + const byte Found = 1; + const byte Missing = 1; + + byte recentPackets = 0; + lock (this.reliableDataPacketsMissing) + { + for (int i = 1; i <= 8; ++i) + { + recentPackets |= this.reliableDataPacketsMissing.Contains((ushort)(id - i)) ? Found : Missing; + recentPackets <<= 1; + } + } + byte[] bytes = new byte[] { (byte)UdpSendOption.Acknowledgement, - byte1, - byte2 + (byte)(id >> 8), + (byte)(id >> 0), + recentPackets }; // Always reply with acknowledgement in order to stop the sender repeatedly sending it diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index 083cb72..d22542d 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -73,7 +73,7 @@ namespace Hazel.Udp default: WriteBytesToConnection(buffer, buffer.Length); - Statistics.LogUnreliableSend(buffer.Length - 1, buffer.Length);; + Statistics.LogUnreliableSend(buffer.Length - 1, buffer.Length); break; } }