Beispiel #1
0
        private void UDP_UDPCall(object sender, byte[] data, SocketRsp rsp)
        {
            string        topic;
            UDPSocketPack uDP = sender as UDPSocketPack;

            if (dicReqTopic.TryRemove(uDP, out topic))
            {
                var lst = DataPack.UnPackCopyRspTopic(data);

                SubscribeList.Subscribe.AddAddress(topic, lst.ToArray());

                dicReq.TryRemove(topic, out uDP);
                uDP.Close();
            }
        }
Beispiel #2
0
 /// <summary>
 /// 心跳包
 /// </summary>
 private void Heart()
 {
     Task.Factory.StartNew(() =>
     {
         var msg = DataPack.PackNodeState(LocalNode.NodeId, LocalNode.TopicAddress);
         Multicast.SendTo(msg);
         //
         if (NodeList.LstNodeInfo.Count == 0)
         {
             int num       = NodeList.dicRefresh.Count;
             string[] keys = new string[num];
             NodeList.dicRefresh.Keys.CopyTo(keys, 0);
             NodeList.LstNodeInfo.AddRange(keys);
             //取出ID
             foreach (var ky in keys)
             {
                 int index = ky.IndexOf(",");
                 string id = ky.Substring(0, index);
                 NodeList.UpdateListId.Add(long.Parse(id));
             }
         }
         else
         {
             var array = NodeList.LstNodeInfo.ToArray();
             foreach (var node in array)
             {
                 int Sp = 0;
                 NodeList.dicRefresh.TryGetValue(node, out Sp);
                 if (Math.Abs(DateTime.Now.Second - Sp) / WaitTime > 2)
                 {
                     //启动侦测
                     int index        = node.IndexOf(",");
                     string addr      = node.Substring(index);
                     string[] address = addr.Split('_');
                     if (address[0] == "0")
                     {
                         TcpClientSocket tcpClient = new TcpClientSocket();
                         tcpClient.RemoteHost      = address[0];
                         tcpClient.RemotePort      = int.Parse(address[1]);
                         if (tcpClient.Connect())
                         {
                             //刷新
                             NodeList.dicRefresh[node] = DateTime.Now.Second;
                         }
                         else
                         {
                             //移除节点信息
                             string id = node.Substring(0, index);
                             NodeList.LstNodeInfo.Remove(node);
                             NodeList.UpdateListId.Remove(long.Parse(id));
                             AddressInfo info = new AddressInfo();
                             info.Reset(addr);
                             SubscribeList.Subscribe.Remove(info);
                         }
                     }
                     else
                     {
                         //UDPSocket uDP = new UDPSocket();
                         UDPSocketPack uDP = new UDPSocketPack();
                         byte[] buf        = new byte[1024];
                         int num           = WaitTectNum;
                         while (num > 0)
                         {
                             uDP.Send(address[0], int.Parse(address[1]), tectBytes);
                             var tsk = Task.Factory.StartNew(() =>
                             {
                                 return(uDP.Recvice(buf));//有数据返回就说明收到了;
                             });
                             if (tsk.Wait(100))
                             {
                                 NodeList.dicRefresh[node] = DateTime.Now.Second;
                                 break;
                             }
                             num--;
                         }
                         if (num <= 0)
                         {
                             //移除节点信息
                             NodeList.LstNodeInfo.Remove(node);
                             AddressInfo info = new AddressInfo();
                             info.Reset(addr);
                             SubscribeList.Subscribe.Remove(info);
                             string id = node.Substring(0, index);
                             NodeList.UpdateListId.Remove(long.Parse(id));
                         }
                         uDP.Close();
                     }
                 }
             }
         }
     });
 }