コード例 #1
0
ファイル: ProtoHelper.cs プロジェクト: swordlegend/Langrisser
        // Token: 0x06006B14 RID: 27412 RVA: 0x001E0AF0 File Offset: 0x001DECF0
        public static ArraySegment <byte> EncodeMessage(object vMsg, IProtoProvider protoProvider)
        {
            byte[]       array        = new byte[65536];
            MemoryStream memoryStream = new MemoryStream(array);
            BinaryWriter binaryWriter = new BinaryWriter(memoryStream);

            binaryWriter.Write(0);
            binaryWriter.Write(0);
            RuntimeTypeModel.Default.Serialize(memoryStream, vMsg, null);
            bool   flag = memoryStream.Position - 4L >= 400L;
            ushort num;

            if (flag)
            {
                using (MemoryStream memoryStream2 = new MemoryStream(memoryStream.ToArray(), 4, (int)memoryStream.Position - 4))
                {
                    byte[] array2 = QuickLZ.compress(memoryStream2.ToArray(), 3);
                    memoryStream.Seek(4L, SeekOrigin.Begin);
                    memoryStream.Write(array2, 0, array2.Length);
                }
                num = (ushort)memoryStream.Position;
                binaryWriter.Seek(0, SeekOrigin.Begin);
                binaryWriter.Write(num);
                ushort num2 = (ushort)protoProvider.GetIdByType(vMsg.GetType());
                num2 |= 32768;
                binaryWriter.Write(num2);
            }
            else
            {
                num = (ushort)memoryStream.Position;
                binaryWriter.Seek(0, SeekOrigin.Begin);
                binaryWriter.Write(num);
                ushort num3 = (ushort)protoProvider.GetIdByType(vMsg.GetType());
                num3 &= 32767;
                binaryWriter.Write(num3);
            }
            return(new ArraySegment <byte>(array, 0, (int)num));
        }
コード例 #2
0
        ///// <summary>
        ///// ��ӡbyte����
        ///// </summary>
        ///// <param name="array"></param>
        ///// <returns></returns>
        //private static string printByteArray(byte[] array)
        //{
        //    if (array == null || array.Length == 0) return string.Empty;

        //    System.Text.StringBuilder sb = new System.Text.StringBuilder();
        //    foreach (var c in array)
        //    {
        //        sb.AppendFormat("{0} ", c);
        //    }
        //    return sb.ToString();
        //}

        /// <summary>
        /// Encodes the message through protobuff, the message's format is : messageSize(UInt16)-ProtocolId(UInt16, including the compressTag and the protocolId value)-ProtocolData
        /// </summary>
        /// <returns>The ecoded message stream.</returns>
        /// <param name="vMsg">The message.</param>
        public static ArraySegment <byte> EncodeMessage(Object vMsg, IProtoProvider protoProvider)
        {
            byte[]       fullBuf  = new byte[(Int32)ProtoConst.MAX_PACKAGE_LENGTH];
            MemoryStream wrStream = new MemoryStream(fullBuf);
            BinaryWriter bnWriter = new BinaryWriter(wrStream);

            // get the message head's length
            int headLength = sizeof(ushort) * 2;

            // Total length, placehold only now
            bnWriter.Write((ushort)0);
            // Message ID, placehold only now
            bnWriter.Write((ushort)0);
            // write message body data to stream
            //RuntimeTypeModel.Default.Serialize(wrStream, vMsg);

            // if the length of message body data is longer than a specific value, then we need compress the message body data
            bool   needCompress = wrStream.Position - headLength >= ProtoConst.ZIP_BUFFER_MIN_LENGTH;
            ushort fullLength   = 0;

            if (needCompress)
            {
                // allocate the buffer for keeping compressed data
                //using (MemoryStream zipMs = new MemoryStream())
                //{
                //    using (GZipStream compressionStream = new GZipStream(zipMs, CompressionMode.Compress))
                //    {
                //        compressionStream.Write(wrStream.ToArray(), headLength, (int)wrStream.Position - headLength);
                //    }
                //    wrStream.Seek(headLength, SeekOrigin.Begin);
                //    wrStream.Write(zipMs.ToArray(), 0, zipMs.ToArray().Length);
                //}

                using (MemoryStream zipMs = new MemoryStream(wrStream.ToArray(), headLength, (int)wrStream.Position - headLength))
                {
                    //var compressedByte = QuickLZSharp.QuickLZ.compress(zipMs.ToArray(), 3);
                    //var compressedByte;

                    //wrStream.Seek(headLength, SeekOrigin.Begin);
                    //wrStream.Write(compressedByte, 0, compressedByte.Length);
                }

                // Rewrite the message length
                fullLength = (ushort)wrStream.Position;
                bnWriter.Seek(0, SeekOrigin.Begin);
                bnWriter.Write(fullLength);
                // Rewrite the message ID info
                ushort protocolId = (ushort)(protoProvider.GetIdByType(vMsg.GetType()));
                protocolId = (ushort)(protocolId | 0x8000);
                bnWriter.Write(protocolId);
            }
            else
            {
                // Rewrite the message length
                fullLength = (ushort)wrStream.Position;
                bnWriter.Seek(0, SeekOrigin.Begin);
                bnWriter.Write(fullLength);
                // Rewrite the message ID info
                ushort protocolId = (ushort)(protoProvider.GetIdByType(vMsg.GetType()));
                protocolId = (ushort)(protocolId & 0x7FFF);
                bnWriter.Write(protocolId);
            }

            return
                (new ArraySegment <byte>(fullBuf, 0, fullLength));
        }