Example #1
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);
        }
Example #2
0
 public int Serialize(out byte[] tempBuffer)
 {
     lock (DefaultWriter)
     {
         DefaultWriter.Clear();
         this.Serialize(DefaultWriter);
         tempBuffer = DefaultWriter.GetBytes();
         return(DefaultWriter.Length);
     }
 }
Example #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);
        }
Example #4
0
        internal static void ReturnErrorMessage(Socket socket, IPEndPoint remoteEndPoint, NetErrorCode errcode, string errinfo = "")
        {
            NetErrorMessage msg = new NetErrorMessage();

            msg.code = (int)errcode;
            msg.info = errinfo;

            NetMessage msgobj = new NetMessage();

            msgobj.head.index    = 0;
            msgobj.head.cmd      = 0;
            msgobj.head.token    = 0;
            msgobj.head.sid      = 0;
            msgobj.content       = PBSerializer.NSerialize(msg);
            msgobj.head.dataSize = (uint)msgobj.content.Length;

            NetBuffer bufferSend = new NetBuffer(new byte[msgobj.Length]);

            msgobj.Serialize(bufferSend);
            socket.SendTo(bufferSend.GetBytes(), remoteEndPoint);
        }
Example #5
0
        private void DoReceive()
        {
            if (m_SystemSocket.Available <= 0)
            {
                return;
            }

            EndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
            int      cnt         = m_SystemSocket.ReceiveFrom(m_RecvBuffer.GetBytes(), m_RecvBuffer.Capacity,
                                                              SocketFlags.None, ref remotePoint);

            if (cnt > 0)
            {
                m_RecvBuffer.AddLength(cnt, 0);
                m_RecvBuffer.Position = 0;

                KCPProxy proxy = GetKcp((IPEndPoint)remotePoint);
                if (proxy != null)
                {
                    proxy.DoReceive(m_RecvBuffer);
                }
            }
        }