From: Forest Date: Wed, 28 Nov 2018 22:39:20 +0000 (-0800) Subject: Remove a test that didn't belong in this project, work towards separating hello from... X-Git-Tag: 1.0.0~76 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=ef160cc3697f2a72eeb422a5666d5d4767bf6f99;p=rhonda%2Fimpostor.hazel.git Remove a test that didn't belong in this project, work towards separating hello from ping packets --- diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index fbcd9ec..9655ba5 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -127,77 +127,5 @@ namespace Hazel.UnitTests { Assert.IsTrue(MessageWriter.IsLittleEndian()); } - - // [TestMethod] - public void Test() - { - string dataStr = "4 0 5 32 0 0 0 6 0 1 5"; - byte[] data = dataStr.Split(' ').Select(b => byte.Parse(b)).ToArray(); - MessageReader readerParent1 = MessageReader.Get(data); - while (readerParent1.Position < readerParent1.Length) - { - var readerParent = readerParent1.ReadMessage(); // Loop of InnerNetClient - while (readerParent.Position < readerParent.Length) - { - - Console.WriteLine($"{readerParent.Tag} = {readerParent.Length}"); - switch (readerParent.Tag) - { - case 1: - { - int gameIdConfirm = readerParent.ReadInt32(); - break; - } - case 5: - { - int gameIdConfirm = readerParent.ReadInt32(); - HandleGameData(readerParent); - break; - } - case 6: - { - int gameIdConfirm = readerParent.ReadInt32(); - int targetId = readerParent.ReadPackedInt32(); // Skip target id - HandleGameData(readerParent); - } - break; - } - } - } - } - - private static void HandleGameData(MessageReader readerParent) - { - while (readerParent.Position < readerParent.Length) - { - var reader = readerParent.ReadMessage(); - - switch (reader.Tag) - { - case 4: - { - Console.WriteLine($"\t{reader.Tag} = SpawnId: {reader.ReadPackedUInt32()}"); - Console.WriteLine($"\t{reader.Tag} = OwnerId: {reader.ReadPackedInt32()}"); - Console.WriteLine($"\t{reader.Tag} = Flags: {reader.ReadByte()}"); - var numChildren = reader.ReadPackedInt32(); - Console.WriteLine($"\t{reader.Tag} = NumChildren: {numChildren}"); - for (int i = 0; i < numChildren; ++i) - { - Console.WriteLine($"\t{reader.Tag} = NetId: {reader.ReadPackedUInt32()}"); - var datam = reader.ReadMessage(); - Console.WriteLine($"\t{reader.Tag} = Data: {string.Join(" ", datam.ReadBytes(datam.Length))}"); - } - break; - } - default: - { - Console.WriteLine($"\t{reader.Tag} = {string.Join(" ", reader.ReadBytes(reader.Length))}"); - } - break; - } - - Console.WriteLine(); - } - } } } \ No newline at end of file diff --git a/Hazel/Udp/SendOptionInternal.cs b/Hazel/Udp/SendOptionInternal.cs index 164ab83..397334f 100644 --- a/Hazel/Udp/SendOptionInternal.cs +++ b/Hazel/Udp/SendOptionInternal.cs @@ -16,6 +16,11 @@ namespace Hazel.Udp /// Hello = 8, + /// + /// A single byte of continued existence + /// + Ping = 12, + /// /// Message for discontinuing communication. /// @@ -29,6 +34,6 @@ namespace Hazel.Udp /// /// Message that is part of a larger, fragmented message. /// - Fragment = 11 + Fragment = 11, } } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 0fab271..3976af6 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -61,6 +61,16 @@ namespace Hazel.Udp /// protected override void WriteBytesToConnection(byte[] bytes, int length) { + if (TestLagMs > 0) + { + ThreadPool.QueueUserWorkItem(a => { Thread.Sleep(this.TestLagMs); WriteBytesToConnectionReal(bytes, length); }); + } + + WriteBytesToConnectionReal(bytes, length); + } + + private void WriteBytesToConnectionReal(byte[] bytes, int length) + { InvokeDataSentRaw(bytes, length); lock (stateLock) diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index 1c05049..57beecb 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -49,12 +49,7 @@ namespace Hazel.Udp /// Lock for keep alive timer. /// Object keepAliveTimerLock = new Object(); - - /// - /// Has the keep alive timer been disposed already? - /// - bool keepAliveTimerDisposed; - + /// /// Starts the keepalive timer. /// @@ -67,7 +62,7 @@ namespace Hazel.Udp { try { - SendHello(null, null); + ReliableSend((byte)UdpSendOption.Hello); // TODO: Change to ping after server can handle it, before clients update Trace.WriteLine("Keepalive packet sent."); } catch @@ -89,7 +84,9 @@ namespace Hazel.Udp void ResetKeepAliveTimer() { lock (keepAliveTimerLock) + { keepAliveTimer.Change(keepAliveInterval, keepAliveInterval); + } } /// @@ -97,11 +94,13 @@ namespace Hazel.Udp /// void DisposeKeepAliveTimer() { - lock(keepAliveTimerLock) + lock (keepAliveTimerLock) { - if (!keepAliveTimerDisposed) + if (keepAliveTimer != null) + { keepAliveTimer.Dispose(); - keepAliveTimerDisposed = true; + keepAliveTimer = null; + } } } } diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 81663b6..7f802ec 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -269,6 +269,20 @@ namespace Hazel.Udp Statistics.LogReliableSend(length, bytes.Length); } + void ReliableSend(byte sendOption) + { + byte[] bytes = new byte[3]; + bytes[0] = sendOption; + + //Add reliable ID + AttachReliableID(bytes, 1, bytes.Length, null); + + //Write to connection + WriteBytesToConnection(bytes, bytes.Length); + + Statistics.LogReliableSend(0, bytes.Length); + } + /// /// Handles a reliable message being received and invokes the data event. /// diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index ccfde7c..16a85d9 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -135,7 +135,7 @@ namespace Hazel.Udp { switch (sendOption) { - //Handle reliable header and hellos + case (byte)UdpSendOption.Ping: case (byte)SendOption.Reliable: case (byte)UdpSendOption.Hello: ReliableSend(sendOption, data, ackCallback); @@ -172,7 +172,8 @@ namespace Hazel.Udp AcknowledgementMessageReceive(buffer); break; - //We need to acknowledge hello messages but dont want to invoke any events! + //We need to acknowledge hello and ping messages but dont want to invoke any events! + case (byte)UdpSendOption.Ping: case (byte)UdpSendOption.Hello: ushort id; ProcessReliableReceive(buffer, 1, out id);