From efcffde735fa65ae34bae0bcc86313b74ed0e36c Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期一, 20 五月 2024 13:47:38 +0800
Subject: [PATCH] 优化离线数据

---
 HDL_ON/DAL/DriverLayer/UdpSocket.cs |  352 ++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 192 insertions(+), 160 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/UdpSocket.cs b/HDL_ON/DAL/DriverLayer/UdpSocket.cs
index 36637f9..f6f9c40 100644
--- a/HDL_ON/DAL/DriverLayer/UdpSocket.cs
+++ b/HDL_ON/DAL/DriverLayer/UdpSocket.cs
@@ -4,31 +4,31 @@
 
 namespace HDL_ON.DriverLayer
 {
-	public class UdpSocket
-	{
-		static UdpSocket _busSocket;
-		public static UdpSocket _BusSocket
+    public class UdpSocket
+    {
+        static UdpSocket _busSocket;
+        public static UdpSocket _BusSocket
         {
             get
             {
-				if(_busSocket == null)
+                if (_busSocket == null)
                 {
-					_busSocket = new UdpSocket();
+                    _busSocket = new UdpSocket();
                 }
-				return _busSocket;
+                return _busSocket;
             }
         }
 
-		//鏈湴Socket
-		private Socket busSocket;
-		public int Port = 0;
-		/// <summary>
-		/// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
-		/// </summary>
-		public void Start (int port = 0)
+        //鏈湴Socket
+        private Socket busSocket;
+        public int Port = 0;
+        /// <summary>
+        /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
+        /// </summary>
+        public void Start(int port = 0)
         {
-			if (IsRunning)
-			{
+            if (IsRunning)
+            {
                 if (port == Port)
                 {
                     return;
@@ -38,159 +38,191 @@
                     busSocket.Close();
                 }
             }
-			if (port != 0)
-				Port = port;
-			if (Port == 0)
-				return;
+            if (port != 0)
+                Port = port;
+            if (Port == 0)
+                return;
 
-            busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
-            busSocket.EnableBroadcast = true;
-            try {
-				busSocket.Bind(new IPEndPoint(IPAddress.Any, Port));
+            try
+            {
+                busSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
+                busSocket.EnableBroadcast = true;
+            }catch(Exception ex)
+            {
+                MainPage.Log("Socket 寮傚父 : " + ex.Message);
+            }
+            try
+            {
+                busSocket.Bind(new IPEndPoint(IPAddress.Any, Port));
 
-				busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
-			}
-			catch (Exception ex){
-				MainPage.Log ($"udp port bind error : {ex.Message}");
+                busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
+                relinkCount = 0;
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log($"udp port bind error : {ex.Message}");
 
-				busSocket = null;
+                busSocket = null;
                 return;
             }
 
             asyncBeginReceive();
 
-            MainPage.Log ($"udp port : {port}");
+            MainPage.Log($"udp port : {port}");
         }
 
-		/// <summary>
-		/// 鍋滄Socket
-		/// </summary>
-		public void Stop()
-		{
-			if (!IsRunning)
-			{
-				return;
-			}
-			try
-			{
-				busSocket.Close();
-			}
-			catch { }
-			busSocket = null;
-			MainPage.Log("Socket鍏抽棴");
-		}
-
-		/// <summary>
-		/// 褰撳墠鐨凷ocket鏄惁杩愯
-		/// </summary>
-		public bool IsRunning
-		{
-			get
-			{
-				return null == busSocket ? false : true;
-			}
-		}
-
-		/// <summary>
-		/// 寮�濮嬪紓姝ユ帴鏀舵暟鎹�
-		/// </summary>
-		private void asyncBeginReceive()
-		{
-			if (!IsRunning)
-			{
-				return;
-			}
-
-            try {
-                Packet packet = new Packet ();
-                busSocket.BeginReceiveFrom (packet.Bytes, 0, packet.Bytes.Length, SocketFlags.None, ref packet.RemoteEndPoint, new AsyncCallback (asyncEndReceive), packet);
-            } 
-            catch (Exception e) {
-                System.Threading.Thread.Sleep (1);
-                asyncBeginReceive ();
-                Console.WriteLine($"asyncBeginReceive {e.Message}");
-            } 
-		}
-
-		/// <summary>
-		/// 寮傛鎺ユ敹鏁版嵁缁撴潫
-		/// </summary>
-		/// <param name="iar"></param>
-		private void asyncEndReceive(IAsyncResult iar)
-		{
-			if (!IsRunning)
-			{
-				return;
-			}
-			try
-			{
-				if (busSocket == null)
-				{
-					return;
-				}
-				asyncBeginReceive();
-				Packet packet = (Packet)iar.AsyncState;
-				int len = busSocket.EndReceiveFrom(iar, ref packet.RemoteEndPoint);
-
-				byte[] bytes = packet.Bytes;
-				packet.Bytes = new byte[len];
-				Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
-
-				//MainPage.Log($"鎺ユ敹{packet.RemoteEndPoint}鏁版嵁");
-				//mqtt杩炴帴鏁版嵁璇诲彇  A鍗忚缃戠粶璁惧淇℃伅璇诲彇鍥炲 澶勭悊
-				if (((IPEndPoint)packet.RemoteEndPoint).Port == 8585)
-				{
-					Control.Ins.ConvertReceiveData(bytes, ((IPEndPoint)packet.RemoteEndPoint).Address.ToString());
-				}
-				else if (((IPEndPoint)packet.RemoteEndPoint).Port == 6000)//澶勭悊bus 6000绔彛鐨勬暟鎹�
-				{
-					packet.Manager();
-				}
-
-			}
-			catch (Exception ex)
-			{
-				MainPage.Log($"寮傛鎺ユ敹鏁版嵁缁撴潫 {ex.Message},{((Packet)iar.AsyncState).Bytes}");
-			}
-		}
-
-		/// <summary>
-		/// 寮傛鍙戦�佹暟鎹�
-		/// </summary>
-		/// <param name="tempPacket"></param>
-		public void AsyncBeginSend(Packet tempPacket)
-		{
-			try
-			{
-				if (!IsRunning)
-				{
-					tempPacket.HaveSendCount++;
-					return;
-				}
-				tempPacket.FlagDateTime = System.DateTime.Now;
-				tempPacket.HaveSendCount++;
-				busSocket.BeginSendTo(tempPacket.Bytes, 0, tempPacket.Bytes.Length, SocketFlags.None, tempPacket.RemoteEndPoint, new AsyncCallback(asyncEndSend), tempPacket);
-			}
-			catch (Exception ex)
-			{
-				MainPage.Log($"AsyncBeginSend error {ex.Message}");
-			}
-		}
-
-		/// <summary>
-		/// 寮傛鍙戦�佹暟鎹粨鏉�
-		/// </summary>
-		/// <param name="iar"></param>
-		private void asyncEndSend(IAsyncResult iar)
-		{
-			Packet tempUDPPacketBuffer = (Packet)iar.AsyncState;
-			try
-			{
-				int bytesSent = busSocket.EndSendTo(iar);
-			}
-			catch {
-            
+        /// <summary>
+        /// 鍋滄Socket
+        /// </summary>
+        public void Stop()
+        {
+            if (busSocket == null)
+            {
+                return;
             }
-		}
-	}
+            if (!IsRunning)
+            {
+                return;
+            }
+            try
+            {
+                busSocket.Close();
+                relinkCount = 0;
+            }
+            catch { }
+            busSocket = null;
+            MainPage.Log("Socket鍏抽棴");
+        }
+
+        /// <summary>
+        /// 褰撳墠鐨凷ocket鏄惁杩愯
+        /// </summary>
+        public bool IsRunning
+        {
+            get
+            {
+                return null == busSocket ? false : true;
+            }
+        }
+
+        /// <summary>
+        /// 寮�濮嬪紓姝ユ帴鏀舵暟鎹�
+        /// </summary>
+        private void asyncBeginReceive()
+        {
+            if (!IsRunning)
+            {
+                return;
+            }
+            if (busSocket == null)
+            {
+                return;
+            }
+
+            try
+            {
+                Packet packet = new Packet();
+                busSocket.BeginReceiveFrom(packet.Bytes, 0, packet.Bytes.Length, SocketFlags.None, ref packet.RemoteEndPoint, new AsyncCallback(asyncEndReceive), packet);
+            }
+            catch (Exception e)
+            {
+                System.Threading.Thread.Sleep(1);
+                Console.WriteLine("asyncBeginReceive " + relinkCount);
+                if (relinkCount == 0)
+                {
+                    relinkCount = 1;
+                    asyncBeginReceive();
+                }
+                Console.WriteLine($"asyncBeginReceive {e.Message}");
+            }
+        }
+        /// <summary>
+        /// 閲嶈繛娆℃暟
+        /// </summary>
+        private int relinkCount = 0;
+
+
+
+        /// <summary>
+        /// 寮傛鎺ユ敹鏁版嵁缁撴潫
+        /// </summary>
+        /// <param name="iar"></param>
+        private void asyncEndReceive(IAsyncResult iar)
+        {
+            if (!IsRunning)
+            {
+                return;
+            }
+            try
+            {
+                if (busSocket == null)
+                {
+                    return;
+                }
+                asyncBeginReceive();
+                Packet packet = (Packet)iar.AsyncState;
+                int len = busSocket.EndReceiveFrom(iar, ref packet.RemoteEndPoint);
+
+                byte[] bytes = packet.Bytes;
+                packet.Bytes = new byte[len];
+                Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
+
+                //MainPage.Log($"鎺ユ敹{packet.RemoteEndPoint}鏁版嵁");
+                //mqtt杩炴帴鏁版嵁璇诲彇  A鍗忚缃戠粶璁惧淇℃伅璇诲彇鍥炲 澶勭悊
+                if (((IPEndPoint)packet.RemoteEndPoint).Port == 8585)
+                {
+                    Control.Ins.ConvertReceiveData(bytes, ((IPEndPoint)packet.RemoteEndPoint).Address.ToString());
+                }
+                else if (((IPEndPoint)packet.RemoteEndPoint).Port == 6000)//澶勭悊bus 6000绔彛鐨勬暟鎹�
+                {
+                    packet.Manager();
+                }
+
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log($"寮傛鎺ユ敹鏁版嵁缁撴潫 {ex.Message},{((Packet)iar.AsyncState).Bytes}");
+            }
+        }
+
+        /// <summary>
+        /// 寮傛鍙戦�佹暟鎹�
+        /// </summary>
+        /// <param name="tempPacket"></param>
+        public void AsyncBeginSend(Packet tempPacket)
+        {
+            try
+            {
+                if (!IsRunning)
+                {
+                    tempPacket.HaveSendCount++;
+                    return;
+                }
+                tempPacket.FlagDateTime = System.DateTime.Now;
+                tempPacket.HaveSendCount++;
+                busSocket.BeginSendTo(tempPacket.Bytes, 0, tempPacket.Bytes.Length, SocketFlags.None, tempPacket.RemoteEndPoint, new AsyncCallback(asyncEndSend), tempPacket);
+            }
+            catch (Exception ex)
+            {
+                MainPage.Log($"AsyncBeginSend error {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 寮傛鍙戦�佹暟鎹粨鏉�
+        /// </summary>
+        /// <param name="iar"></param>
+        private void asyncEndSend(IAsyncResult iar)
+        {
+            Packet tempUDPPacketBuffer = (Packet)iar.AsyncState;
+            try
+            {
+                int bytesSent = busSocket.EndSendTo(iar);
+            }
+            catch
+            {
+
+            }
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0