Example #1
0
        public void SendCommand <T>(uint id1, uint id2, T cmd)
        {
            byte[] pbdata = ProtoBufSerialize.Serialize(cmd);
            sendCache.Clear();

            CUtility.UInt8ToBytes(id1, sendCache);
            CUtility.UInt8ToBytes(id2, sendCache);
            CUtility.UInt32ToBytes(AddSendPackCount(), sendCache);
            CUtility.BytesToBytes(pbdata, pbdata.Length, sendCache);

            Send(sendCache.ToArray());
        }
Example #2
0
    public void HandleCommond(byte[] msg, bool bFromServer = true)
    {
        if (msg.Length < 6)
        {
            //throw new IndexOutOfRangeException("msg error, recieve msg length lower 3");
            return;
        }

        uint gsCmd       = (uint)msg[0];                                //大消息号
        uint gsParamType = (uint)msg[1];                                //小消息号

        ////Debug.LogError("gsCmd : " + gsCmd + "   gsParamType : " + gsParamType + "     msg.Length : " + msg.Length);
        byte[] data = CUtility.BytesToBytes(msg, 6, msg.Length - 6);    //取出剩余数据包

        switch (gsCmd)
        {
        default:
            break;
        }
    }
Example #3
0
        public static byte[] Wrap(byte[] message, int packHeadLen = 3)
        {
            if (message == null)
            {
                return(null);
            }

            uint msgLength = (uint)message.Length;

            if ((PacketConstDefine.SendSerializeType & SerializeType.Compress) != 0)
            {
            }
            if ((PacketConstDefine.SendSerializeType & SerializeType.Encript) != 0)
            {
            }

            dataCache.Clear();
            uint flag = 0;

            CUtility.UInt8ToBytes(flag, dataCache);
            CUtility.UInt16ToBytes(msgLength, dataCache);
            CUtility.BytesToBytes(message, (int)msgLength, dataCache);
            return(dataCache.ToArray());
        }
Example #4
0
        public static void UnWrap(ref byte[] recvBuffer, ref int nRecvLen)
        {
            int  offset         = 0;
            bool bCompress      = false;
            bool bEncrypt       = false;
            int  nMessageLen    = 0;
            int  datasize       = nRecvLen;
            int  nPacketHeadLen = PacketConstDefine.PacketHeadLen;

            //尝试读取包头
            while (offset + nPacketHeadLen < datasize)
            {
                if (nMessageLen == 0)
                {
                    //包头还没有读取过
                    //第一个字节 是否压缩
                    //第二个 + 第三个 代表包体长度
                    bCompress   = (recvBuffer[offset] & 0x01) > 0;
                    bEncrypt    = (recvBuffer[offset] & 0x02) > 0;
                    nMessageLen = CUtility.BytesToUshort(recvBuffer, offset + 1);
                }

                if (nMessageLen <= 0)
                {
                    //GDebug.LogError("<color=red> 消息长度小于等于0 </color>");
                    offset     += nPacketHeadLen;
                    nMessageLen = 0;
                    continue;
                }
                else if (offset + nMessageLen + nPacketHeadLen > datasize)
                {
                    //byte offset0 = recvBuffer[offset];
                    //UInt16 offset12 = CUtility.BytesToUshort(recvBuffer, offset+1);
                    //byte offset3 = recvBuffer[offset + 3];    //大消息号
                    //byte offset4 = recvBuffer[offset + 4];    //小消息号
                    //GDebug.LogError("<color=red> 被拆分的包体 offset0 = " + offset0 + " offset12 = " + offset12 + " offset3 = " + offset3 + " offset4 = " + offset4 + " iscompress = " + m_bCompress + " m_nMessageLen = " + m_nMessageLen + " offset  " + offset + "  datasize " + datasize + "</color>");
                    break;
                }

                //尝试读取包体
                if ((offset + nMessageLen + nPacketHeadLen) <= datasize)
                {
                    //消息包头已经读取,并且剩下的数据足够读包体
                    //先解密再解压
                    byte[] bytes = CUtility.BytesToBytes(recvBuffer, offset + nPacketHeadLen, nMessageLen);
                    byte[] desdata;
                    if (bEncrypt)
                    {
                        Init();
                        desdata = dec.TransformFinalBlock(bytes, 0, bytes.Length);
                    }
                    else
                    {
                        desdata = bytes;
                    }

                    byte[] uncompressData;
                    // 判断是否需要解压
                    if (bCompress)
                    {
                        uncompressData = CUtility.deCompressBytes(desdata);
                    }
                    else
                    {
                        uncompressData = desdata;
                    }

                    NetWorkConnection.Instance.Push(uncompressData, (int)uncompressData.Length);

                    offset     += nMessageLen + nPacketHeadLen;
                    nMessageLen = 0;
                }
            }

            nRecvLen = datasize - offset;
            if (nRecvLen > 0)
            {
                Array.Copy(recvBuffer, offset, recvBuffer, 0, nRecvLen);
            }
        }