From: Forest Date: Thu, 5 Sep 2019 21:14:26 +0000 (-0700) Subject: Improve broadcast listener debugging and ignore some infrequent exceptions X-Git-Tag: 1.0.0~36 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=91a7c236972dfd70e135243fb2cac5d3643dd6cc;p=rhonda%2Fimpostor.hazel.git Improve broadcast listener debugging and ignore some infrequent exceptions --- diff --git a/Hazel/Udp/UdpBroadcastListener.cs b/Hazel/Udp/UdpBroadcastListener.cs index 03ab9f2..973f63e 100644 --- a/Hazel/Udp/UdpBroadcastListener.cs +++ b/Hazel/Udp/UdpBroadcastListener.cs @@ -7,19 +7,12 @@ using System.Threading; namespace Hazel.Udp { - /// public class BroadcastPacket { - /// public string Data; - - /// public DateTime ReceiveTime; - - /// public IPEndPoint Sender; - /// public BroadcastPacket(string data, IPEndPoint sender) { this.Data = data; @@ -33,11 +26,11 @@ namespace Hazel.Udp } } - /// public class UdpBroadcastListener : IDisposable { private Socket socket; private EndPoint endpoint; + private Action logger; private byte[] buffer = new byte[1024]; @@ -46,10 +39,12 @@ namespace Hazel.Udp public bool Running { get; private set; } /// - public UdpBroadcastListener(int port) + public UdpBroadcastListener(int port, Action logger = null) { + this.logger = logger; this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); + this.socket.EnableBroadcast = true; + this.socket.MulticastLoopback = false; this.endpoint = new IPEndPoint(IPAddress.Any, port); this.socket.Bind(this.endpoint); } @@ -59,7 +54,7 @@ namespace Hazel.Udp { if (this.Running) return; this.Running = true; - + try { EndPoint endpt = new IPEndPoint(IPAddress.Any, 0); @@ -69,8 +64,10 @@ namespace Hazel.Udp ThreadPool.QueueUserWorkItem(_ => this.HandleData(result)); } } - catch + catch (NullReferenceException) { } + catch (Exception e) { + this.logger?.Invoke("BroadcastListener: " + e); this.Dispose(); } } @@ -85,13 +82,19 @@ namespace Hazel.Udp { numBytes = this.socket.EndReceiveFrom(result, ref endpt); } - catch + catch (NullReferenceException) { + // Already disposed + return; + } + catch (Exception e) + { + this.logger?.Invoke("BroadcastListener: " + e); this.Dispose(); return; } - if (numBytes < 2 + if (numBytes < 3 || buffer[0] != 4 || buffer[1] != 2) { this.StartListen(); @@ -108,6 +111,13 @@ namespace Hazel.Udp for (int i = 0; i < this.packets.Count; ++i) { var pkt = this.packets[i]; + if (pkt == null || pkt.Data == null) + { + this.packets.RemoveAt(i); + i--; + continue; + } + if (pkt.Data.GetHashCode() == dataHash && pkt.Sender.Equals(ipEnd)) { diff --git a/Hazel/Udp/UdpBroadcaster.cs b/Hazel/Udp/UdpBroadcaster.cs index 50c3c68..0c81bf9 100644 --- a/Hazel/Udp/UdpBroadcaster.cs +++ b/Hazel/Udp/UdpBroadcaster.cs @@ -8,20 +8,18 @@ namespace Hazel.Udp /// public class UdpBroadcaster : IDisposable { - /// private Socket socket; - - /// private byte[] data; - - /// private EndPoint endpoint; + private Action logger; /// - public UdpBroadcaster(int port) + public UdpBroadcaster(int port, Action logger = null) { + this.logger = logger; this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); - this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); + this.socket.EnableBroadcast = true; + this.socket.MulticastLoopback = false; this.endpoint = new IPEndPoint(IPAddress.Broadcast, port); } @@ -44,7 +42,26 @@ namespace Hazel.Udp return; } - this.socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, this.endpoint, (evt) => this.socket.EndSendTo(evt), null); + try + { + this.socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, this.endpoint, this.FinishSendTo, null); + } + catch (Exception e) + { + this.logger?.Invoke("BroadcastListener: " + e); + } + } + + private void FinishSendTo(IAsyncResult evt) + { + try + { + this.socket.EndSendTo(evt); + } + catch (Exception e) + { + this.logger?.Invoke("BroadcastListener: " + e); + } } ///