예제 #1
0
        // Token: 0x06006B13 RID: 27411 RVA: 0x001E0934 File Offset: 0x001DEB34
        public static object DecodeMessage(MessageBlock recvBuffer, IProtoProvider protoProvider, out int msgId, Func <Stream, Type, int, object> deserializeMessageAction = null)
        {
            int num = 4;

            msgId = 0;
            if (recvBuffer.Length < num)
            {
                return(null);
            }
            ushort num2 = recvBuffer.ReadUInt16();

            if ((int)num2 < num)
            {
                throw new ProtoException(string.Format("Hack stream, TotalLength={0}", num2));
            }
            int num3 = (int)num2 - num;

            if (recvBuffer.Length < num3 + 2)
            {
                recvBuffer.ReadPtr(-2);
                return(null);
            }
            ushort num4 = recvBuffer.ReadUInt16();
            bool   flag = num4 >> 15 == 1;
            ushort num5 = num4 & 32767;

            msgId = (int)num5;
            Type   typeById = protoProvider.GetTypeById((int)num5);
            object result;

            try
            {
                if (flag)
                {
                    using (MemoryStream readStream = recvBuffer.GetReadStream(num3))
                    {
                        byte[] array = QuickLZ.decompress(readStream.ToArray());
                        int    count = array.Length;
                        using (MemoryStream memoryStream = new MemoryStream(array, 0, count))
                        {
                            if (deserializeMessageAction != null)
                            {
                                result = deserializeMessageAction(memoryStream, typeById, msgId);
                            }
                            else
                            {
                                result = RuntimeTypeModel.Default.Deserialize(memoryStream, null, typeById, null);
                            }
                        }
                    }
                }
                else
                {
                    using (MemoryStream readStream2 = recvBuffer.GetReadStream(num3))
                    {
                        if (deserializeMessageAction != null)
                        {
                            result = deserializeMessageAction(readStream2, typeById, msgId);
                        }
                        else
                        {
                            result = RuntimeTypeModel.Default.Deserialize(readStream2, null, typeById, null);
                        }
                    }
                }
            }
            catch (Exception innerException)
            {
                throw new Exception(string.Format("msgId={0}, isCompressed={1} pakBodyLength={2}", msgId, flag, num3), innerException);
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// Decodes the message.
        /// </summary>
        /// <returns>The message.</returns>
        /// <param name="dataBlock">Data block.</param>
        /// <param name="dataOffset">Data offset.</param>
        /// <param name="dataLen">Data length.</param>
        /// <param name="newMsg">New message.</param>
        /// <returns>
        ///     != null     : A protocol message
        ///     == null     : No full message exist
        ///     Exception   : ProtoException to indicate HACK stream
        /// </returns>
        public static Object DecodeMessage(MessageBlock rCache, IProtoProvider protoProvider, out int msgId)
        {
            msgId = 0;
            // get the message head's length
            int headLength = sizeof(ushort) * 2;

            // Not enough data for header
            if (rCache.Length < headLength)
            {
                //Debug.WriteLine(String.Format("DecodeMessage Not enough data for header. rCache.Length={0} < headLength={1}", rCache.Length, headLength));
                return(null);
            }

            // get message length
            ushort pakFullLength = rCache.ReadUInt16();

            // Bad stream
            if (pakFullLength < headLength)
            {
                throw new ProtoException(string.Format("Hack stream, TotalLength={0}", pakFullLength));
            }

            int pakBodyLength = pakFullLength - headLength;

            //Debug.WriteLine(String.Format("DecodeMessage Before rCache.Length={0} pakBodyLength={1} ", rCache.Length, pakBodyLength));

            // Not enough data for body
            // ע��������Ҫ���Ǻ���һ����ͷ�ij���
            if (rCache.Length < pakBodyLength + sizeof(ushort))
            {
                // Move read ptr to back
                // �˴�ֻ������UInt16���ȵ����ݣ����Իع���Ҫһ�£�����ʹ��headLength
                rCache.ReadPtr(-(sizeof(ushort)));
                //Debug.WriteLine(String.Format("DecodeMessage Not enough data for body rCache.Length={0} < pakBodyLength={1}", rCache.Length, pakBodyLength));
                return(null);
            }

            // get message id field
            ushort pakMessageIdField = rCache.ReadUInt16();
            // get compressed tag
            bool isCompressed = pakMessageIdField >> (sizeof(ushort) * 8 - 1) == 1;

            // get the protocol id
            msgId = (ushort)(pakMessageIdField & 0x7FFF);

            // deserialize message, we should decompress message body data if needed
            Type pakType = protoProvider.GetTypeById(msgId);
            // Use ProtoBuf to deserialize message
            object ccMsg = Activator.CreateInstance(pakType);

            //Debug.WriteLine(String.Format("DecodeMessage msgId={0} pakBodyLength={1} isCompressed={2} pakType={3}", msgId, pakBodyLength, isCompressed, pakType.ToString()));
            try
            {
                using (MemoryStream readStream = rCache.GetStream(pakBodyLength))
                {
                    //����Ϣ���л����������ṩ�����л����������л��ͷ����л�
                    //Debug.WriteLine(String.Format("DecodeMessage readStream={0}", printByteArray(readStream.ToArray())));
                    //RuntimeTypeModel.Default.Deserialize(readStream, ccMsg, pakType);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("msgId={0}, isCompressed={1} pakBodyLength={2}",
                                                  msgId, isCompressed, pakBodyLength), ex);
            }

            //Debug.WriteLine(String.Format("DecodeMessage rCacheInfo Length={0} WrPtr={1} RdPtr={2}", rCache.Length, rCache.WrPtr, rCache.RdPtr));
            return(ccMsg);
        }