Пример #1
0
        //------------------------------------------------------------
        #region 设置通用参数

        public void SetCryptKey(byte[] key)
        {
            Debuger.Log(LOG_TAG_MAIN, "SetCryptKey() {0}", key != null && key.Length > 0);
            if (key != null && key.Length > 0)
            {
                m_Cryptor = new FSPCryptor(key);
            }
            else
            {
                m_Cryptor = null;
            }
        }
Пример #2
0
        private int SendBuffer(NetBuffer buffer, bool isReSend)
        {
            //进行加密
            if (m_Cryptor != null)
            {
                if (!isReSend)
                {
                    m_Cryptor.Encrypt(buffer.GetBytes(), buffer.Length);
                }
            }

            //写入CheckSum
            if (m_UseCheckSum_Send)
            {
                if (isReSend)
                {
                    FSPCryptor.ModifyCheckSum(buffer);
                }
                else
                {
                    FSPCryptor.AddCheckSum(buffer);
                }
            }

            if (FSPDebuger.EnableLog)
            {
                Debuger.Log(LOG_TAG_RECV, "SendBuffer() Size={0}, IP={1}, Buffer={2}",
                            buffer.Length, m_HostEndPoint, buffer.ToHexString());
            }

            int sendCnt = 0;

            try
            {
                sendCnt = m_Socket.SendTo(buffer.GetBytes(), buffer.Length, m_HostEndPoint);
            }
            catch (Exception e)
            {
                Debuger.LogWarning(LOG_TAG_SEND, "SendBuffer() " + e);
            }

            if (sendCnt <= 0)
            {
                if (FSPDebuger.EnableLog)
                {
                    Debuger.LogWarning(LOG_TAG_SEND, "SendBuffer() 数据发送失败, IP={0}, Size={1}", m_HostEndPoint,
                                       buffer.Length);
                }
            }

            return(sendCnt);
        }
Пример #3
0
        private NetBuffer ReceiveBuffer()
        {
            int        len        = 0;
            IPEndPoint ipepRemote = m_HostEndPoint;

            len = m_Socket.ReceiveFrom(m_ReceiveBufferTemp.GetBytes(), m_ReceiveBufferTemp.Capacity, ref ipepRemote);

            if (len <= 0)
            {
                //Debuger.LogWarning(LOG_TAG_RECV, "DoReceive() 收到的数据Len <= 0! ");
                return(null);
            }

            m_ReceiveBufferTemp.AddLength(len, 0);
            m_ReceiveBufferTemp.Position = 0;

            NetBuffer buffer = m_ReceiveBufferTemp;

            //CheckSum
            if (m_UseCheckSum_Recv)
            {
                if (!FSPCryptor.ValidCheckSum(buffer))
                {
                    return(null);
                }
            }

            //解密
            if (m_Cryptor != null)
            {
                m_Cryptor.Decrypt(buffer.GetBytes(), buffer.Length);
            }

            if (FSPDebuger.EnableLog)
            {
                Debuger.Log(LOG_TAG_RECV, "ReceiveBuffer() Size={0}, IP={1}, Buffer={2}",
                            buffer.Length, ipepRemote, buffer.ToHexString());
            }

            return(buffer);
        }