Example #1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="dat"></param>
        private void PubData(AddressInfo[] lst, string topic, byte[] data)
        {
            byte[] bytes = DataPack.PackTopicData(topic, data);
            foreach (var p in lst)
            {
                if (p.Protol == 0)
                {
                    TcpClientSocket tcp = new TcpClientSocket();
                    tcp.RemoteHost = p.Address;
                    tcp.RemotePort = p.Port;
                    if (tcp.Connect())
                    {
                        tcp.Send(BitConverter.GetBytes(bytes.Length));
                        tcp.Send(bytes);
                        tcp.Close();
                    }
                }
                else
                {
                    UDPSocket uDP = new UDPSocket();

                    uDP.Send(p.Address, p.Port, bytes);
                }
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="nodeAddr"></param>
        /// <returns></returns>
        public bool CopyAddress(string topic, AddressInfo nodeAddr)
        {
            TcpClientSocket tcpClient = new TcpClientSocket();

            tcpClient.RemoteHost = nodeAddr.Address;
            tcpClient.RemotePort = nodeAddr.Port;
            if (tcpClient.Connect())
            {
                tcpClient.Send(Encoding.Default.GetBytes(topic));
            }
            return(true);
        }
Example #3
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();
                         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(50))
                             {
                                 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();
                     }
                 }
             }
         }
     });
 }
Example #4
0
        /// <summary>
        /// 发布数据
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="data"></param>
        public void Publish(string topic, byte[] data)
        {
            if (ObserverInit.isInit)
            {
                ObserverInit.Init();
            }
            var array = SubscribeList.Subscribe.GetAddresses(topic);

            if (array == null || array.Length == 0)
            {
                //没有订阅看发布
                array = PublishList.Publish.GetAddresses(topic);
                if (array == null || (array.Length == 1 && array[0].ToString() == LocalNode.TopicAddress))
                {
                    //没有地址,或者只是本节点发布了数据,当做新增,组播扩展新增
                    NewTopicPub.Pub.SendNewTopic(topic);
                    PublishList.Publish.AddLocal(topic);
                    array = SubscribeList.Subscribe.GetAddresses(topic);
                    if (array != null)
                    {
                        //再次订阅的地址
                        PubData(array, topic, data);
                    }
                    else
                    {
                        //丢弃
                    }
                }
                else
                {
                    //复制订阅地址
                    List <AddressInfo> lst = null;//地址
                    foreach (var p in array)
                    {
                        var    bytes = DataPack.PackCopyTopic(topic);
                        byte[] buf   = new byte[10240];

                        if (p.Protol == 0)
                        {
                            TcpClientSocket tcpClient = new TcpClientSocket();
                            tcpClient.RemoteHost = p.Address;
                            tcpClient.RemotePort = p.Port;
                            if (tcpClient.Connect())
                            {
                                tcpClient.Send(bytes);
                                int r = tcpClient.Recvice(buf);
                                lst = DataPack.UnPackCopyRspTopic(buf);
                                SubscribeList.Subscribe.AddAddress(topic, lst.ToArray());
                                tcpClient.Close();
                                break;//成功一个就退出
                            }
                            tcpClient.Close();
                        }
                        else
                        {
                            UDPSocket uDP      = new UDPSocket();
                            bool      isSucess = false;
                            for (int i = 0; i < 20; i++)
                            {
                                var tsk = Task.Factory.StartNew(() =>
                                {
                                    uDP.Send(p.Address, p.Port, bytes);
                                    int r = uDP.Recvice(buf);
                                    if (r > 0)
                                    {
                                        lst = DataPack.UnPackCopyRspTopic(buf);
                                        SubscribeList.Subscribe.AddAddress(topic, lst.ToArray());
                                    }
                                });
                                if (tsk.Wait(50))
                                {
                                    //成功就退出
                                    isSucess = true;
                                    break;
                                }
                            }
                            if (isSucess)
                            {
                                //成功退出外层
                                break;
                            }
                        }
                    }
                    if (lst != null)
                    {
                        PubData(lst.ToArray(), topic, data);
                    }
                }
            }
            else
            {
                PubData(array, topic, data);
            }
        }
Example #5
0
        /// <summary>
        /// 发送订阅信息
        /// </summary>
        /// <param name="topic"></param>
        private void SendSubscriber(string topic)
        {
            if (dicFilter.ContainsKey(topic))
            {
                return;
            }
            dicFilter[topic] = null;
            var  array    = PublishList.Publish.GetAddresses(topic);
            bool isSucess = false;

            //发送订阅信息
            if (array != null)
            {
                var local = new AddressInfo();
                local.Reset(LocalNode.TopicAddress);
                byte[] tmp = DataPack.PackSubscribeMsg(topic, new AddressInfo[] { local });
                foreach (var addr in array)
                {
                    if (addr.Protol == 0)
                    {
                        TcpClientSocket tcp = new TcpClientSocket();
                        tcp.RemoteHost = addr.Address;
                        tcp.RemotePort = addr.Port;
                        if (tcp.Connect())
                        {
                            tcp.Send(tmp);
                            isSucess = true;
                        }
                        tcp.Close();
                    }
                    else
                    {
                        UDPSocket uDP = new UDPSocket();
                        int       num = udpTimes;
                        while (true)
                        {
                            uDP.Send(addr.Address, addr.Port, tmp);
                            byte[] buf = new byte[3];

                            var tsk = Task.Factory.StartNew(() =>
                            {
                                return(uDP.Recvice(buf)); //只要有接收就确定收到
                            });
                            if (tsk.Wait(udpWait))
                            {
                                isSucess = true;
                                break;
                            }
                            num--;
                            if (num < 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                //没有发布地址,进入等待订阅列表
                NodeList.dicWaitSubscribe[topic] = null;
                //
            }
            if (!isSucess)
            {
                //订阅全部没有成功
                NodeList.dicWaitSubscribe[topic] = null;
                string t;
                dicFilter.TryRemove(topic, out t);
            }
            else
            {
                string t;
                //订阅有一个成功
                NodeList.dicWaitSubscribe.TryRemove(topic, out t);
            }
        }