コード例 #1
0
        /// <summary>
        /// 根据具体内容初始化
        /// </summary>
        /// <param name="packageId">packageId</param>
        /// <param name="ChannelName">ChannelName</param>
        /// <param name="ChannelData">ChannelData</param>
        public PackagePlugin(byte packageId, string ChannelName, byte[] ChannelData, bool is112)
        {
            //成员
            this.ChannelName = ChannelName;
            this.ChannelData = ChannelData;

            byte[] channelDataLengthBytes = BitConverter.GetBytes((Int16)ChannelData.Length).Reverse();

            //Data
            List <byte> data = new List <byte>();

            data.AddRange(ProtoBufUtil.GetVarStringBytes(ChannelName));
            if (!is112)
            {
                data.AddRange(channelDataLengthBytes);
            }
            data.AddRange(ChannelData);

            //OriginData
            List <byte> originData       = new List <byte>();
            List <byte> packageIdAndData = new List <byte>();

            packageIdAndData.Add(packageId);
            packageIdAndData.AddRange(data);
            if (is112)
            {
                // 如果大于阈值 , 就压缩
                if (packageIdAndData.Count > Global.CompressionThreshold)
                {
                    byte[] compressData            = ZLibUtil.Compress(packageIdAndData.ToArray());
                    byte[] compressDataLengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count);
                    byte[] lengthBytes             = ProtoBufUtil.GetVarIntBytes(compressDataLengthBytes.Length + compressData.Length);

                    originData.AddRange(lengthBytes);
                    originData.AddRange(compressDataLengthBytes);
                    originData.AddRange(compressData);
                }
                else
                {
                    byte[] lengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count + 1);
                    originData.AddRange(lengthBytes);
                    originData.Add(0);
                    originData.AddRange(packageIdAndData);
                }
            }
            else
            {
                byte[] lengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count);
                originData.AddRange(lengthBytes);
                originData.AddRange(packageIdAndData);
            }

            //父类成员
            this.PackageId  = packageId;
            this.Data       = data.ToArray();
            this.OriginData = originData.ToArray();
        }
コード例 #2
0
        public static Package Create(byte packageId, byte[] data, bool is112)
        {
            Package package = new Package();

            package.PackageId = packageId;
            package.Data      = data;

            List <byte> packageIdAndData = new List <byte>();
            List <byte> originData       = new List <byte>();

            packageIdAndData.Add(packageId);
            packageIdAndData.AddRange(data);
            if (is112)
            {
                // 如果大于阈值 , 就压缩
                if (packageIdAndData.Count > Global.CompressionThreshold)
                {
                    byte[] compressData            = ZLibUtil.Compress(packageIdAndData.ToArray());
                    byte[] compressDataLengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count);
                    byte[] lengthBytes             = ProtoBufUtil.GetVarIntBytes(compressDataLengthBytes.Length + compressData.Length);

                    originData.AddRange(lengthBytes);
                    originData.AddRange(compressDataLengthBytes);
                    originData.AddRange(compressData);
                }
                else
                {
                    byte[] lengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count + 1);
                    originData.AddRange(lengthBytes);
                    originData.Add(0);
                    originData.AddRange(packageIdAndData);
                }
            }
            else
            {
                byte[] lengthBytes = ProtoBufUtil.GetVarIntBytes(packageIdAndData.Count);
                originData.AddRange(lengthBytes);
                originData.AddRange(packageIdAndData);
            }
            package.OriginData = originData.ToArray();
            return(package);
        }