From 5cfc61b2145c9b5a892136a376efa969a8d6bf4c Mon Sep 17 00:00:00 2001 From: Forest Date: Tue, 20 Aug 2019 15:33:13 -0700 Subject: [PATCH] Better enable very early connection validation Tidy up some broadcaster stuff Fix a misreported statistic Some logging for suspect stuff --- Hazel/Udp/UdpBroadcastListener.cs | 4 +++- Hazel/Udp/UdpConnection.Reliable.cs | 6 +++--- Hazel/Udp/UdpConnection.cs | 10 +++++----- Hazel/Udp/UdpConnectionListener.cs | 10 +++++++--- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Hazel/Udp/UdpBroadcastListener.cs b/Hazel/Udp/UdpBroadcastListener.cs index 5ac1a17..03ab9f2 100644 --- a/Hazel/Udp/UdpBroadcastListener.cs +++ b/Hazel/Udp/UdpBroadcastListener.cs @@ -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 diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 6f7e54e..a2a2450 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -41,7 +41,7 @@ namespace Hazel.Udp /// /// Holds the last ID allocated. /// - private int lastIDAllocated = ushort.MaxValue + 1; + private int lastIDAllocated = 0; /// /// The packets of data that have been transmitted reliably and not acknowledged. @@ -400,7 +400,7 @@ namespace Hazel.Udp /// Handles acknowledgement packets to us. /// /// The buffer containing the data. - 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); } /// diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index 014a61e..07b66e7 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -8,7 +8,7 @@ namespace Hazel.Udp /// 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 }; /// /// 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; } } diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index 32a3e06..5b61885 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -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; /// @@ -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); -- 2.39.5