From af1cb3ecd0f4b0589e00b28f7f9edccf39e6e12b Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 10 九月 2020 16:15:11 +0800
Subject: [PATCH] 202009101

---
 HDL_ON/DriverLayer/BusSocket.cs |   55 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/HDL_ON/DriverLayer/BusSocket.cs b/HDL_ON/DriverLayer/BusSocket.cs
index 84f1942..c41e235 100644
--- a/HDL_ON/DriverLayer/BusSocket.cs
+++ b/HDL_ON/DriverLayer/BusSocket.cs
@@ -1,7 +1,8 @@
 锘縰sing System;
 using System.Net.Sockets;
 using System.Net;
-using HDL_ON.UI;
+using HDL_ON.DAL.Net;
+//using HDL_ON.UI;
 
 namespace HDL_ON
 {
@@ -33,7 +34,10 @@
             busSocket.EnableBroadcast = true;
             try {
                 busSocket.Bind (new IPEndPoint (IPAddress.Any, port));
-            }catch{
+
+				busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("224.0.168.188")));
+			}
+			catch{
                 busSocket = null;
                 return;
             }
@@ -63,8 +67,6 @@
 			}
 			catch { }
 			busSocket = null;
-            //HDL_ON.SimpleControl.Phone.Music.A31PlayMusicPage.isExit = true;
-            //SmartHome.MqttCommon.DisConnect ();
             MainPage.Log("BusSocket鍏抽棴鎴愬姛!");
 		}
 
@@ -90,17 +92,14 @@
 			}
 
             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>
@@ -109,7 +108,6 @@
 		/// <param name="iar"></param>
 		private static void asyncEndReceive(IAsyncResult iar)
 		{
-
 			if (!IsRunning)
 			{
 				return;
@@ -117,17 +115,31 @@
 
 			try
 			{
-                asyncBeginReceive ();
-
+				asyncBeginReceive();
 				Packet packet = (Packet)iar.AsyncState;
-
 				int len = busSocket.EndReceiveFrom(iar, ref packet.RemoteEndPoint);
+
 				byte[] bytes = packet.Bytes;
 				packet.Bytes = new byte[len];
-				System.Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
+				Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
+
+				//mqtt杩炴帴鏁版嵁璇诲彇
+				if (packet.RemoteEndPoint.ToString() == "172.16.2.237:6688")
+				{
+					//var reString = DAL.CommonPage.MyEncodingUTF8.GetString(bytes);
+					//var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(reString);
+					//var gateway_objects = jt.GetValue("objects").ToString();
+					//var jt2 = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(gateway_objects);
+					//MqttCommon.mqttClientIP = jt2.GetValue("gateway_ip").ToString();
+					//MqttCommon.mqttGatewayMAC = jt2.GetValue("board_id").ToString();
+				}
+
 				packet.Manager();
 			}
-			catch { }
+			catch (Exception ex)
+			{
+				MainPage.Log($"寮傛鎺ユ敹鏁版嵁缁撴潫 {ex.Message},{((Packet)iar.AsyncState).Bytes}");
+			}
 		}
 
 		/// <summary>
@@ -138,16 +150,19 @@
 		{
 			try
 			{
-                if (!IsRunning) {
-                    return;
-                }
+				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 { 
-            
-            }
+			catch (Exception ex)
+			{
+				MainPage.Log($"AsyncBeginSend error {ex.Message}");
+			}
 		}
 
 		/// <summary>

--
Gitblit v1.8.0