From 23ca39219c5075d829d14137cb54896a24ae513d Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期五, 04 十二月 2020 13:57:08 +0800
Subject: [PATCH] 20201204
---
HDL_ON/DAL/DriverLayer/UdpSocket.cs | 59 +++++++++++++++++++----------------------------------------
1 files changed, 19 insertions(+), 40 deletions(-)
diff --git a/HDL_ON/DriverLayer/UdpSocket.cs b/HDL_ON/DAL/DriverLayer/UdpSocket.cs
similarity index 74%
rename from HDL_ON/DriverLayer/UdpSocket.cs
rename to HDL_ON/DAL/DriverLayer/UdpSocket.cs
index ac83f9b..f63851c 100644
--- a/HDL_ON/DriverLayer/UdpSocket.cs
+++ b/HDL_ON/DAL/DriverLayer/UdpSocket.cs
@@ -1,9 +1,8 @@
锘縰sing System;
using System.Net.Sockets;
using System.Net;
-using Newtonsoft.Json.Linq;
-namespace HDL_ON
+namespace HDL_ON.DriverLayer
{
public class UdpSocket
{
@@ -20,39 +19,34 @@
}
}
- /// <summary>
- /// 鐩戝惉绔彛
- /// </summary>
- public int port
- {
- get
- {
- if (MainPage.LoginUser != null && Entity.DB_ResidenceData.residenceData != null)
- {
- return Entity.DB_ResidenceData.residenceData.GatewayType == 0 ? 6000 : 8585;
- }
- return 6000;
- }
- }
-
//鏈湴Socket
private Socket busSocket;
-
+ int _port = 0;
/// <summary>
/// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
/// </summary>
- public void Start ()
+ public void Start (int port)
{
- if (IsRunning || port == 0) {
- return;
+ if (IsRunning)
+ {
+ if (port == _port)
+ {
+ return;
+ }
+ else
+ {
+ busSocket.Close();
+ }
}
+ _port = port;
busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
busSocket.EnableBroadcast = true;
try {
- busSocket.Bind (new IPEndPoint (IPAddress.Any, port));
+ //busSocket.Bind (new IPEndPoint (IPAddress.Any, 6000));
+ busSocket.Bind(new IPEndPoint(IPAddress.Any, port));
- busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
+ busSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.0.168.188")));
}
catch{
busSocket = null;
@@ -113,10 +107,6 @@
Console.WriteLine($"asyncBeginReceive {e.Message}");
}
}
- /// <summary>
- /// 鎼滅储缃戠粶璁惧瑙﹀彂鐨勫洖璋冧簨浠�
- /// </summary>
- public Action<string> SearchNetDeviceAction;
/// <summary>
/// 寮傛鎺ユ敹鏁版嵁缁撴潫
@@ -139,22 +129,11 @@
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)
{
- var reString = System.Text.Encoding.UTF8.GetString(bytes);
- var jt = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(reString);
- JToken jtc = null;
- jt.TryGetValue("command", out jtc);
- if (jtc != null && jtc.ToString() == "search_response")
- {
- JToken jto = null;
- jt.TryGetValue("objects", out jto);
- if (jto != null)
- {
- SearchNetDeviceAction?.Invoke(jto.ToString());
- }
- }
+ Control.Ins.ConvertReceiveData(bytes);
}
else if(((IPEndPoint)packet.RemoteEndPoint).Port == 6000)//澶勭悊bus 6000绔彛鐨勬暟鎹�
{
--
Gitblit v1.8.0