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="p"></param>
        /// <param name="bytes"></param>
        private void RequestCopy(string topic, AddressInfo p, byte[] bytes)
        {
            UDPSocketPack uDP = new UDPSocketPack();

            uDP = dicReq.GetOrAdd(topic, uDP);
            dicReqTopic[uDP] = topic;
            uDP.UDPCall     -= UDP_UDPCall;
            uDP.UDPCall     += UDP_UDPCall;
            uDP.Send(p.Address, p.Port, bytes);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 返回订阅地址
        /// </summary>
        /// <param name="data"></param>
        private void CopyAddress(byte[] data, SocketRsp rsp)
        {
            string topic = Encoding.Default.GetString(data, 1, data.Length - 1);
            var    addrs = SubscribeList.Subscribe.GetAddresses(topic);
            var    bytes = DataPack.PackCopyRspTopic(addrs);

            if (bytes == null)
            {
                bytes = Encoding.Default.GetBytes(" ");
            }
            if (rsp.Rsp != null)
            {
                rsp.Rsp.Send(bytes);
            }
            else
            {
                uDP.Send(rsp.Address, rsp.Port, bytes);
            }
        }
Ejemplo n.º 4
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.º 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();
                    }
                }
            }
        }
Ejemplo n.º 6
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();
                     }
                 }
             }
         }
     });
 }