| | |
| | | public static class Application |
| | | { |
| | | public static bool isInited; |
| | | static bool isEncry; |
| | | /// <summary> |
| | | /// 是否正在搜索网关 |
| | | /// </summary> |
| | |
| | | { |
| | | return; |
| | | } |
| | | isInited = true; |
| | | isInited = true;
|
| | |
|
| | | //初始化Socket
|
| | | FindGateWaySocket.Start(); |
| | | |
| | | new System.Threading.Thread(async () => |
| | | { |
| | |
| | | if (paths.Length < 3) |
| | | continue; |
| | | |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.getGatewayBaseInfo != null) && (obj.getGatewayBaseInfo.gwID == paths[2])); |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2])); |
| | | if (gateWay == null) |
| | | { |
| | | gateWay = new ZbGateway { IsVirtual = true }; |
| | | gateWay.getGatewayBaseInfo.gwID = paths[2]; |
| | | gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId; |
| | | gateWay.GwId = paths[2]; |
| | | gateWay.HomeId = Shared.Common.Config.Instance.HomeId; |
| | | ZbGateway.GateWayList.Add(gateWay); |
| | | } |
| | | } |
| | | } |
| | | |
| | | var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
|
| | |
|
| | |
|
| | | if (Shared.Application.IsWifi)
|
| | | {
|
| | |
| | | //点对点发送(先发一条已有的点播,回复几率高一点) |
| | | for (int i = 0; i < gateWayList.Count; i++)
|
| | | { |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes); |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes); |
| | | } |
| | | //广播发送 |
| | | if (broadcastIpAddress.ToString() != "0.0.0.0") |
| | |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes); |
| | | } |
| | | } |
| | | catch
|
| | | { |
| | | //关闭Socket,下次发送会自动连接
|
| | | FindGateWaySocket.Stop();
|
| | | } |
| | | catch{} |
| | | System.Threading.Thread.Sleep(500); |
| | | } |
| | | }) |
| | |
| | | var bytes = new byte[1024]; |
| | | |
| | | var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None); |
| | | |
| | | if (bytes[43] == 0xA2) |
| | | { |
| | | //广播回复网关的基本信息处理 |
| | |
| | | var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength); |
| | | var zbGateWay = new ZbGateway |
| | | { |
| | | getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo |
| | | { |
| | | IpAddress = ipAddress, |
| | | GwName = gwName, |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | | gwID = id, |
| | | }, |
| | | GwIP = ipAddress, |
| | | GwName = gwName, |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | | GwId = id, |
| | | GatewayOnlineFlage = true, |
| | | PubKey = pubKey |
| | | }; |
| | | //通讯到网关列表 |
| | | if (gateWayList.Find(obj => obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID) == null) |
| | | if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null) |
| | | { |
| | | //网关匹配当前住宅中到网关 |
| | | if (Shared.Common.Config.Instance.HomeId == homeID) |
| | |
| | | //} |
| | | } |
| | | //网关列表存储处理 |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID); |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId); |
| | | if (gateWay == null) |
| | | {
|
| | | ZbGateway.GateWayList.Add(zbGateWay); |
| | |
| | | gateWay.IsVirtual = false; |
| | | //将该网关标识为【可搜索到,即:在线】 |
| | | gateWay.GatewayOnlineFlage = true; |
| | | if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress) |
| | | if (gateWay.GwIP != ipAddress) |
| | | { |
| | | await gateWay.DisConnectLocalMqttClient("2"); |
| | | ZbGateway.GateWayList.Remove(gateWay); |
| | |
| | | else |
| | | { |
| | | gateWay.PubKey = pubKey; |
| | | gateWay.getGatewayBaseInfo.GwName = gwName; |
| | | gateWay.getGatewayBaseInfo.HomeId = homeID;
|
| | | gateWay.GwName = gwName; |
| | | gateWay.HomeId = homeID;
|
| | | await gateWay.StartLocalMqtt(ipAddress); |
| | | } |
| | | |
| | | //主网关设置 |
| | | if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId) |
| | | if (isMainGateWay && oldHomeID == gateWay.HomeId) |
| | | { |
| | | for (int i = 0; i < ZbGateway.GateWayList.Count; i++) |
| | | { |
| | | var gw = ZbGateway.GateWayList[i]; |
| | | //网关ID不是当前的网关,则把网关列表中其他网关标记为不是主网关 |
| | | if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId) |
| | | if (gw.GwId != id && oldHomeID == gw.HomeId) |
| | | { |
| | | gw.getGatewayBaseInfo.IsMainGateWay = false; |
| | | gw.IsMainGateWay = false; |
| | | } |
| | | } |
| | | //标记当前网关是主网关 |
| | | gateWay.getGatewayBaseInfo.IsMainGateWay = true; |
| | | gateWay.IsMainGateWay = true; |
| | | } |
| | | }
|
| | |
|
| | |
| | | }
|
| | | } |
| | | } |
| | | else |
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | var data33 = Encoding.UTF8.GetString(bytes).Replace("�)", string.Empty).TrimStart('\0').TrimEnd('\0');
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
|
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | //关闭Socket,下次发送会自动连接 |
| | | FindGateWaySocket.Stop(); |
| | | } |
| | | catch{} |
| | | } |
| | | #endregion |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | //检测连接状态 |
| | | CheckConnectiton(); |
| | | |
| | | Start(7624); |
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null); |
| | | if (IsRunning)
|
| | | {
|
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
|
| | | } |
| | | |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 检测连接状态
|
| | | /// </summary> |
| | | private static void CheckConnectiton()
|
| | | {
|
| | | if (busSocket == null) { return; }
|
| | |
|
| | | bool blockingState = busSocket.Blocking;
|
| | | try
|
| | | catch (Exception ex) |
| | | {
|
| | | byte[] tmp = new byte[1];
|
| | |
|
| | | busSocket.Blocking = false;
|
| | | busSocket.Send(tmp, 0, 0);
|
| | | //tcp还在连接着
|
| | | busSocket.Blocking = blockingState;
|
| | | }
|
| | | catch (SocketException e)
|
| | | {
|
| | | // 10035 == WSAEWOULDBLOCK
|
| | | if (e.NativeErrorCode.Equals(10035))
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | //Still Connected, but the Send would block
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "发送异常(BeginSendTo)\r\n" + ex.Message);
|
| | | }
|
| | | else
|
| | | {
|
| | | //tcp已经断开了连接
|
| | | Stop();
|
| | | }
|
| | | }
|
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | int bytesSent = busSocket.EndSendTo(iar); |
| | | } |
| | | catch { } |
| | | catch(Exception ex) |
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "异步发送结束异常(asyncEndSend)\r\n" + ex.Message);
|
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |