Beispiel #1
0
        public void SetLocalAddress(string LocalAddressString, bool p_ifSendHeartBeat, bool p_ifReplyHeartBeat)
        {
            this.m_ifSendHeartBeat  = p_ifSendHeartBeat;
            this.m_ifReplyHeartBeat = p_ifReplyHeartBeat;

            NETADDR NetAddr = NetRouterClientHelper.ConvertAddress(LocalAddressString);

            this.m_LocalAddr = NetAddr;
        }
Beispiel #2
0
        /// <summary>
        /// 将需要保持连接的远程地址添加到地址表
        /// </summary>
        /// <param name="p_Address"></param>
        public void AddRemoteAddress(string p_Address)
        {
            NETADDR        t_NetAddr  = NetRouterClientHelper.ConvertAddress(p_Address);
            EndPointRecord t_EndPoint = new EndPointRecord();

            t_EndPoint.m_NetAddr    = t_NetAddr;
            t_EndPoint.m_AddressStr = p_Address;
            this.m_RemoteAddressMap.TryAdd(p_Address, t_EndPoint);
        }
Beispiel #3
0
 public void Send(string p_DstAddressStr, byte[] p_Data)
 {
     try
     {
         NETADDR NetAddr = NetRouterClientHelper.ConvertAddress(p_DstAddressStr);
         Send(NetAddr, p_Data);
     }
     catch (Exception e)
     {
         if (this.m_StatusStrAction != null)
         {
             this.m_StatusStrAction(e.Message);
             return;
         }
     }
 }
Beispiel #4
0
 private void ReceiveThreadMethod()
 {
     while (!m_ThreadClose)
     {
         try
         {
             if (this.m_NetRouterClient != null)
             {
                 if (this.m_ConnectStatus1 || this.m_ConnectStatus2)
                 {
                     REVMSG recvMsg = new REVMSG();
                     while (m_NetRouterClient.receiveMessage(ref recvMsg))
                     {
                         byte[] t_ReceiveData = recvMsg.msg;
                         if (t_ReceiveData != null)
                         {
                             int  t_DataLength  = ByteToInt32(t_ReceiveData);
                             bool t_NeedOperate = true;
                             if (t_DataLength == 4)
                             {
                                 if (t_ReceiveData[4] == m_HeartBeatData[0] &&
                                     t_ReceiveData[5] == m_HeartBeatData[1] &&
                                     t_ReceiveData[6] == m_HeartBeatData[2] &&
                                     t_ReceiveData[7] == m_HeartBeatData[3])
                                 {
                                     t_NeedOperate = false;
                                     DateTime dt = DateTime.Now;
                                     if (this.m_StatusStrAction != null)
                                     {
                                         this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息包");
                                     }
                                     if (m_ifReplyHeartBeat)
                                     {
                                         SendHeartBeatReply(recvMsg.srcAddr);
                                     }
                                 }
                                 else if (t_ReceiveData[4] == m_HeartBeatReply[0] &&
                                          t_ReceiveData[5] == m_HeartBeatReply[1] &&
                                          t_ReceiveData[6] == m_HeartBeatReply[2] &&
                                          t_ReceiveData[7] == m_HeartBeatReply[3])
                                 {
                                     t_NeedOperate = false;
                                     DateTime dt = DateTime.Now;
                                     if (this.m_StatusStrAction != null)
                                     {
                                         this.m_StatusStrAction(dt.ToLongTimeString() + " 收到心跳信息应答包");
                                     }
                                     NETADDR        t_SrcAddr = recvMsg.srcAddr;
                                     string         t_AddrStr = NetRouterClientHelper.ConvertAddress(t_SrcAddr);
                                     EndPointRecord t_EndPoint;
                                     if (this.m_RemoteAddressMap.TryGetValue(t_AddrStr, out t_EndPoint))
                                     {
                                         t_EndPoint.m_HeartBeatInterval = 0;
                                     }
                                 }
                             }
                             if (t_NeedOperate && this.m_ReceiveAction != null)
                             {
                                 this.m_ReceiveAction(t_ReceiveData, 4, t_DataLength);
                             }
                         }
                     }
                 }
             }
             Thread.Sleep(10);
         }
         catch (ThreadAbortException e)
         {
             System.Console.WriteLine(Thread.CurrentThread.Name + "已被要求退出");
             Thread.ResetAbort();
         }
         catch (Exception e)
         {
             if (this.m_StatusStrAction != null)
             {
                 this.m_StatusStrAction(e.Message);
             }
         }
     }
 }