]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Better enable very early connection validation
authorForest <forest@innersloth.com>
Tue, 20 Aug 2019 22:33:13 +0000 (15:33 -0700)
committerForest <forest@innersloth.com>
Tue, 20 Aug 2019 22:34:17 +0000 (15:34 -0700)
Tidy up some broadcaster stuff
Fix a misreported statistic
Some logging for suspect stuff

Hazel/Udp/UdpBroadcastListener.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnection.cs
Hazel/Udp/UdpConnectionListener.cs

index 5ac1a17a7e7351e37a65848b12eb49374736160a..03ab9f277a959569bf7671a89d1b8019dc6944e7 100644 (file)
@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
+using System.Threading;
 
 namespace Hazel.Udp
 {
@@ -48,6 +49,7 @@ namespace Hazel.Udp
         public UdpBroadcastListener(int port)
         {
             this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+            this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
             this.endpoint = new IPEndPoint(IPAddress.Any, port);
             this.socket.Bind(this.endpoint);
         }
@@ -64,7 +66,7 @@ namespace Hazel.Udp
                 var result = this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endpt, this.HandleData, null);
                 if (result.CompletedSynchronously)
                 {
-                    this.HandleData(result);
+                    ThreadPool.QueueUserWorkItem(_ => this.HandleData(result));
                 }
             }
             catch
index 6f7e54efee01cc9e004d88ff3fca6872cb049dfc..a2a245087677a2978dc076b3bfe212048b76dd03 100644 (file)
@@ -41,7 +41,7 @@ namespace Hazel.Udp
         /// <summary>
         ///     Holds the last ID allocated.
         /// </summary>
-        private int lastIDAllocated = ushort.MaxValue + 1;
+        private int lastIDAllocated = 0;
 
         /// <summary>
         ///     The packets of data that have been transmitted reliably and not acknowledged.
@@ -400,7 +400,7 @@ namespace Hazel.Udp
         ///     Handles acknowledgement packets to us.
         /// </summary>
         /// <param name="bytes">The buffer containing the data.</param>
-        private void AcknowledgementMessageReceive(byte[] bytes)
+        private void AcknowledgementMessageReceive(byte[] bytes, int bytesReceived)
         {
             this.pingsSinceAck = 0;
 
@@ -432,7 +432,7 @@ namespace Hazel.Udp
                 }
             }
 
-            Statistics.LogReliableReceive(0, bytes.Length);
+            Statistics.LogReliableReceive(bytesReceived - 3, bytesReceived);
         }
 
         /// <summary>
index 014a61e402e22e972c1823efcea4fde3fb7c6fa0..07b66e768f148ad31a2a3ec0863be6122d4bc749 100644 (file)
@@ -8,7 +8,7 @@ namespace Hazel.Udp
     /// <inheritdoc />
     public abstract partial class UdpConnection : NetworkConnection
     {
-        protected static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
+        public static readonly byte[] EmptyDisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
 
         /// <summary>
         ///     Writes the given bytes to the connection.
@@ -97,19 +97,19 @@ namespace Hazel.Udp
 
                 //Handle acknowledgments
                 case (byte)UdpSendOption.Acknowledgement:
-                    AcknowledgementMessageReceive(message.Buffer);
+                    AcknowledgementMessageReceive(message.Buffer, bytesReceived);
                     message.Recycle();
                     break;
 
                 //We need to acknowledge hello and ping messages but dont want to invoke any events!
                 case (byte)UdpSendOption.Ping:
                     ProcessReliableReceive(message.Buffer, 1, out id);
-                    Statistics.LogHelloReceive(message.Length);
+                    Statistics.LogHelloReceive(bytesReceived);
                     message.Recycle();
                     break;
                 case (byte)UdpSendOption.Hello:
                     ProcessReliableReceive(message.Buffer, 1, out id);
-                    Statistics.LogHelloReceive(message.Length);
+                    Statistics.LogHelloReceive(bytesReceived);
                     break;
 
                 case (byte)UdpSendOption.Disconnect:
@@ -122,7 +122,7 @@ namespace Hazel.Udp
                 //Treat everything else as unreliable
                 default:
                     InvokeDataReceived(SendOption.None, message, 1, bytesReceived);
-                    Statistics.LogUnreliableReceive(message.Length - 1, message.Length);
+                    Statistics.LogUnreliableReceive(bytesReceived - 1, bytesReceived);
                     break;
             }
         }
index 32a3e06a55ced6e358928e8d1c4a40f5a07d1132..5b618854fd5a59847aaa456a1847f50b14f81190 100644 (file)
@@ -16,7 +16,7 @@ namespace Hazel.Udp
 
         public int MinConnectionLength = 0;
 
-        public delegate bool AcceptConnectionCheck(out byte[] response);
+        public delegate bool AcceptConnectionCheck(byte[] input, out byte[] response);
         public AcceptConnectionCheck AcceptConnection;
 
         /// <summary>
@@ -109,7 +109,11 @@ namespace Hazel.Udp
             {
                 message = MessageReader.GetSized(BufferSize);
 
-                socket.BeginReceiveFrom(message.Buffer, 0, message.Buffer.Length, SocketFlags.None, ref remoteEP, ReadCallback, message);
+                var result = socket.BeginReceiveFrom(message.Buffer, 0, message.Buffer.Length, SocketFlags.None, ref remoteEP, ReadCallback, message);
+                if (result.CompletedSynchronously)
+                {
+                    this.Logger("Operation completed synchronously");
+                }
             }
             catch (SocketException sx)
             {
@@ -208,7 +212,7 @@ namespace Hazel.Udp
 
                         if (AcceptConnection != null)
                         {
-                            if (!AcceptConnection(out var response))
+                            if (!AcceptConnection(message.Buffer, out var response))
                             {
                                 message.Recycle();
                                 SendData(response, response.Length, remoteEndPoint);