From: Forest Date: Wed, 3 Oct 2018 19:54:16 +0000 (-0700) Subject: Apparently I have made a lot of changes that were not committed. But I fixed a bug... X-Git-Tag: 1.0.0~79 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=7e34d90fa9ee331fc1f8fab60fedf338725b8d1c;p=rhonda%2Fimpostor.hazel.git Apparently I have made a lot of changes that were not committed. But I fixed a bug. NICE --- diff --git a/Hazel.UnitTests/BroadcastTests.cs b/Hazel.UnitTests/BroadcastTests.cs index d916446..53b3139 100644 --- a/Hazel.UnitTests/BroadcastTests.cs +++ b/Hazel.UnitTests/BroadcastTests.cs @@ -15,8 +15,8 @@ namespace Hazel.UnitTests { const string TestData = "pwerowerower"; - using (UdpBroadcaster caster = new UdpBroadcaster(4777)) - using (UdpBroadcastListener listener = new UdpBroadcastListener(4777)) + using (UdpBroadcaster caster = new UdpBroadcaster(47777)) + using (UdpBroadcastListener listener = new UdpBroadcastListener(47777)) { listener.StartListen(); diff --git a/Hazel.UnitTests/MessageReaderTests.cs b/Hazel.UnitTests/MessageReaderTests.cs index f0454e3..56894e2 100644 --- a/Hazel.UnitTests/MessageReaderTests.cs +++ b/Hazel.UnitTests/MessageReaderTests.cs @@ -131,10 +131,73 @@ namespace Hazel.UnitTests [TestMethod] public void Test() { - sbyte s = -1; - Assert.AreEqual(255, (byte)s); - byte b = 255; - Assert.AreEqual(-1, (sbyte)b); + 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.UnitTests/MessageWriterTests.cs b/Hazel.UnitTests/MessageWriterTests.cs index b72e0da..c4096be 100644 --- a/Hazel.UnitTests/MessageWriterTests.cs +++ b/Hazel.UnitTests/MessageWriterTests.cs @@ -7,6 +7,44 @@ namespace Hazel.UnitTests [TestClass] public class MessageWriterTests { + + [TestMethod] + public void CancelMessages() + { + var msg = new MessageWriter(128); + + msg.StartMessage(1); + msg.Write(32); + + msg.StartMessage(2); + msg.Write(2); + msg.CancelMessage(); + + Assert.AreEqual(7, msg.Length); + Assert.IsFalse(msg.HasBytes(7)); + + msg.CancelMessage(); + + Assert.AreEqual(0, msg.Length); + Assert.IsFalse(msg.HasBytes(1)); + } + + [TestMethod] + public void HasBytes() + { + var msg = new MessageWriter(128); + + msg.StartMessage(1); + msg.Write(32); + + msg.StartMessage(2); + msg.Write(2); + msg.EndMessage(); + + // Assert.AreEqual(7, msg.Length); + Assert.IsTrue(msg.HasBytes(7)); + } + [TestMethod] public void WriteProperInt() { diff --git a/Hazel/MessageWriter.cs b/Hazel/MessageWriter.cs index 29a4941..d9ad298 100644 --- a/Hazel/MessageWriter.cs +++ b/Hazel/MessageWriter.cs @@ -97,6 +97,7 @@ namespace Hazel public void CancelMessage() { this.Position = this.messageStarts.Pop(); + this.Length = this.Position; } public void Clear(SendOption sendOption) diff --git a/Hazel/Udp/UdpBroadcastListener.cs b/Hazel/Udp/UdpBroadcastListener.cs index a9b1828..5304332 100644 --- a/Hazel/Udp/UdpBroadcastListener.cs +++ b/Hazel/Udp/UdpBroadcastListener.cs @@ -42,6 +42,8 @@ namespace Hazel.Udp private List packets = new List(); + public bool Running { get; private set; } + /// public UdpBroadcastListener(int port) { @@ -53,7 +55,8 @@ namespace Hazel.Udp /// public void StartListen() { - if (this.socket == null) return; + if (this.Running) return; + this.Running = true; try { @@ -70,9 +73,10 @@ namespace Hazel.Udp } } - /// - public void HandleData(IAsyncResult result) + private void HandleData(IAsyncResult result) { + this.Running = false; + int numBytes; EndPoint endpt = new IPEndPoint(IPAddress.Any, 0); try @@ -85,8 +89,12 @@ namespace Hazel.Udp return; } - if (numBytes < 2) return; - if (buffer[0] != 4 || buffer[1] != 2) return; + if (numBytes < 2 + || buffer[0] != 4 || buffer[1] != 2) + { + this.StartListen(); + return; + } IPEndPoint ipEnd = (IPEndPoint)endpt; string data = ASCIIEncoding.ASCII.GetString(buffer, 2, numBytes - 2); diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index f1d6f3c..1c05049 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -65,8 +65,16 @@ namespace Hazel.Udp keepAliveTimer = new Timer( (o) => { - Trace.WriteLine("Keepalive packet sent."); - SendHello(null, null); + try + { + SendHello(null, null); + Trace.WriteLine("Keepalive packet sent."); + } + catch + { + Trace.WriteLine("Keepalive packet failed to send."); + DisposeKeepAliveTimer(); + } }, null, keepAliveInterval, diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 5b8758d..f262f7d 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -249,6 +249,9 @@ namespace Hazel.Udp /// The callback to make once the packet has been acknowledged. void ReliableSend(byte sendOption, byte[] data, int offset, int length, Action ackCallback = null) { + //Inform keepalive not to send for a while + ResetKeepAliveTimer(); + byte[] bytes = new byte[length + 3]; //Add message type diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index d18c03e..dfa00ec 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -45,12 +45,11 @@ namespace Hazel.Udp byte[] buffer = new byte[msg.Length]; Buffer.BlockCopy(msg.Buffer, 0, buffer, 0, msg.Length); - //Inform keepalive not to send for a while - ResetKeepAliveTimer(); - switch (msg.SendOption) { case SendOption.Reliable: + // Inform keepalive not to send for a while + ResetKeepAliveTimer(); AttachReliableID(buffer, 1, buffer.Length); WriteBytesToConnection(buffer, buffer.Length); Statistics.LogReliableSend(buffer.Length - 3, buffer.Length); @@ -106,10 +105,6 @@ namespace Hazel.Udp if (State != ConnectionState.Connected) throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?"); - - //Inform keepalive not to send for a while - ResetKeepAliveTimer(); - switch (sendOption) { //Handle reliable header and hellos @@ -138,9 +133,6 @@ namespace Hazel.Udp /// The bytes that should actually be sent. protected void HandleSend(byte[] data, byte sendOption, Action ackCallback = null) { - //Inform keepalive not to send for a while - ResetKeepAliveTimer(); - switch (sendOption) { //Handle reliable header and hellos @@ -167,10 +159,7 @@ namespace Hazel.Udp protected internal void HandleReceive(byte[] buffer) { InvokeDataReceivedRaw(buffer); - - //Inform keepalive not to send for a while - ResetKeepAliveTimer(); - + switch (buffer[0]) { //Handle reliable receives diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 26d95ee..2fcd09d 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -257,6 +257,23 @@ namespace Hazel.Udp /// protected override void Dispose(bool disposing) { + lock (connections) + { + foreach (var kvp in this.connections) + { + if (kvp.Value.State == ConnectionState.Connected) + { + try + { + kvp.Value.SendDisconnect(); + } + catch { } + } + } + + connections.Clear(); + } + if (listener != null) { listener.Close();