Ejemplo n.º 1
0
 public void Send(byte[] data)
 {
     if (!IsPtP || RemoteAddress == null)
     {
         return;
     }
     if (RemoteAddress.Protol == 0)
     {
         TcpClientSocket tcpClient = new TcpClientSocket()
         {
             RemoteHost = RemoteAddress.Address, RemotePort = RemoteAddress.Port
         };
         if (tcpClient.Connect())
         {
             tcpClient.Send(data);
         }
         tcpClient.Close();
     }
     else
     {
         UDPSocketPack uDP = new UDPSocketPack();
         uDP.Send(RemoteAddress.Address, RemoteAddress.Port, data);
         uDP.Stop();
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 直接发送订阅信息
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="addr"></param>
        /// <returns></returns>
        public bool SendSubscribeTopic(string topic, AddressInfo addr)
        {
            bool isSucess = false;
            var  local    = new AddressInfo();

            local.Reset(LocalNode.TopicAddress);
            byte[] tmp = DataPack.PackSubscribeMsg(topic, new AddressInfo[] { local });
            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
            {
                UDPSocketPack uDP = new UDPSocketPack();
                int           num = udpTimes;
                while (true)
                {
                    uDP.Send(addr.Address, addr.Port, tmp);
                    byte[] buf = new byte[1024];
                    Debug.WriteLine("发送订阅信息:" + addr.ToString());
                    var tsk = Task.Factory.StartNew(() =>
                    {
                        return(uDP.Recvice(buf));//只要有接收就确定收到
                    });
                    if (tsk.Wait(udpWait))
                    {
                        isSucess = true;
                        break;
                    }
                    num--;
                    if (num < 0)
                    {
                        break;
                    }
                }
            }
            return(isSucess);
        }
Ejemplo n.º 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
                        {
                            RequestCopy(topic, p, bytes);
                        }
                    }
                    if (lst != null)
                    {
                        PubData(lst.ToArray(), topic, data);
                    }
                }
            }
            else
            {
                PubData(array, topic, data);
            }
        }
Ejemplo n.º 5
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);
            bool               findTcp = false;
            List <AddressInfo> lstTcp  = new List <AddressInfo>();

            foreach (var p in lst)
            {
                if (p.Protol == 0)
                {
                    if (findTcp)
                    {
                        if (lstTcp.Contains(p))
                        {
                            continue;//已经处理
                        }
                        else
                        {
                            TcpClientSocket tcp = new TcpClientSocket
                            {
                                RemoteHost = p.Address,
                                RemotePort = p.Port
                            };
                            if (tcp.Connect())
                            {
                                tcp.Send(BitConverter.GetBytes(bytes.Length));
                                tcp.Send(bytes);
                                ClientProcess.Instance.Update(topic, tcp);
                                //tcp.Close();
                            }
                        }
                        continue;
                    }

                    var lstSocket = ClientProcess.Instance.GetTcpClients(topic);
                    if (lstSocket != null)
                    {
                        lock (lstSocket)
                        {
                            //必须锁定防止处理线程正在关闭
                            foreach (var tcpClient in lstSocket)
                            {
                                try
                                {
                                    tcpClient.Send(bytes);
                                    lstTcp.Add(new AddressInfo()
                                    {
                                        Address = tcpClient.RemoteHost, Port = tcpClient.RemotePort, Protol = 0
                                    });
                                }
                                catch
                                {
                                    //有可能正在关闭;
                                }
                            }
                        }
                    }
                    //当前第一个地址
                    if (!lstTcp.Contains(p))
                    {
                        TcpClientSocket tcp = new TcpClientSocket
                        {
                            RemoteHost = p.Address,
                            RemotePort = p.Port
                        };
                        if (tcp.Connect())
                        {
                            tcp.Send(BitConverter.GetBytes(bytes.Length));
                            tcp.Send(bytes);
                            ClientProcess.Instance.Update(topic, tcp);
                            //tcp.Close();
                        }
                    }
                    findTcp = true;//已经完全处理过;
                }
                else
                {
                    //  UDPSocket uDP = new UDPSocket();
                    UDPSocketPack uDP = null;

                    if (udpSocket.TryTake(out uDP))
                    {
                        uDP.Send(p.Address, p.Port, bytes);
                    }
                    else
                    {
                        if (Interlocked.Decrement(ref UdpSocketNum) > 0)
                        {
                            uDP = new UDPSocketPack();
                            uDP.Send(p.Address, p.Port, bytes);
                        }
                        else
                        {
                            uDP = udpSocket.Take();
                            uDP.Send(p.Address, p.Port, bytes);
                        }
                    }
                    if (!udpSocket.TryAdd(uDP))
                    {
                        uDP.Stop();
                    }
                }
            }
        }