| | |
| | | using System; |
| | | using System.Net.Sockets; |
| | | using System.Net; |
| | | using HDL_ON.DAL.Net; |
| | | //using HDL_ON.UI; |
| | | |
| | | namespace HDL_ON |
| | |
| | | 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; |
| | | } |
| | |
| | | } |
| | | |
| | | 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> |
| | |
| | | /// <param name="iar"></param> |
| | | private static void asyncEndReceive(IAsyncResult iar) |
| | | { |
| | | |
| | | if (!IsRunning) |
| | | { |
| | | return; |
| | |
| | | try |
| | | { |
| | | 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); |
| | | |
| | | 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}"); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |