示例#1
0
 public void Subscribe(string topic)
 {
     if (ObserverInit.isInit)
     {
         ObserverInit.Init();
     }
     SubscribeMgr.Instance.Add(this, topic);
 }
示例#2
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);
            }
        }