陈嘉乐
2020-11-30 ed3bfb7462d44747230437717e8673a5192f833f
HDL_ON/DriverLayer/UdpSocket.cs
File was renamed from HDL_ON/DAL/DriverLayer/UdpSocket.cs
@@ -1,8 +1,9 @@
using System;
using System.Net.Sockets;
using System.Net;
using Newtonsoft.Json.Linq;
namespace HDL_ON.DriverLayer
namespace HDL_ON
{
   public class UdpSocket
   {
@@ -19,34 +20,39 @@
            }
        }
      /// <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 (int port)
      public void Start ()
        {
         if (IsRunning)
         {
                if (port == _port)
                {
                    return;
                }
                else
                {
                    busSocket.Close();
                }
            if (IsRunning || port == 0) {
                return;
            }
            _port = port;
            busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            busSocket.EnableBroadcast = true;
            try {
                //busSocket.Bind (new IPEndPoint (IPAddress.Any, 6000));
                busSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                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;
@@ -107,6 +113,10 @@
                Console.WriteLine($"asyncBeginReceive {e.Message}");
            } 
      }
      /// <summary>
        /// 搜索网络设备触发的回调事件
        /// </summary>
      public Action<string> SearchNetDeviceAction;
      /// <summary>
      /// 异步接收数据结束
@@ -129,11 +139,22 @@
            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);
               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());
                  }
               }
            }
            else if(((IPEndPoint)packet.RemoteEndPoint).Port == 6000)//处理bus 6000端口的数据
            {