Пример #1
0
        private static byte[] GetRequestBytes(RequestEntity requestEntity)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BinaryWriter(stream);

                var peerBytes    = Encoding.UTF8.GetBytes(requestEntity.Peer);
                var requestBytes = SerializationUtil.Serialize(requestEntity);

                writer.Write(peerBytes.Length);
                writer.Write(peerBytes);

                switch (ConfigUtil.ConfigSection.Client.Compression.ToLower())
                {
                case "def":
                {
                    writer.Write((byte)1);
                    requestBytes = CompressionUtil.CompressDef(requestBytes);
                }
                break;

                case "lz":
                {
                    writer.Write((byte)2);
                    requestBytes = CompressionUtil.CompressLZ(requestBytes, false);
                }
                break;

                default:
                    writer.Write((byte)0);
                    break;
                }

                writer.Write(requestBytes.Length);
                writer.Write(requestBytes);

                writer.Flush();

                return(stream.ToArray());
            }
        }
Пример #2
0
        protected byte[] GetResponseBytes(ResponseEntity responseEntity)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new BinaryWriter(stream);

                var resultBytes = TrySerializeResponse(responseEntity);

                if (ConfigUtil.ConfigSection != null && ConfigUtil.ConfigSection.Client != null)
                {
                    switch (ConfigUtil.ConfigSection.Client.Compression.ToLower())
                    {
                    case "def":
                    {
                        writer.Write((byte)1);
                        resultBytes = CompressionUtil.CompressDef(resultBytes);
                    }
                    break;

                    case "lz":
                    {
                        writer.Write((byte)2);
                        resultBytes = CompressionUtil.CompressLZ(resultBytes, false);
                    }
                    break;

                    default:
                        writer.Write((byte)0);
                        break;
                    }
                }

                writer.Write(resultBytes.Length);
                writer.Write(resultBytes);

                writer.Flush();

                return(stream.ToArray());
            }
        }