示例#1
0
        /// <summary>
        /// Gets the frame bytes.
        /// </summary>
        /// <returns></returns>
        public Stream GetFrameBytes(bool compress, int compressTreshold)
        {
            var buffer = new PoolMemoryStream();

            buffer.WriteByte((byte)Version);
            buffer.WriteByte((byte)Flags);
            buffer.WriteByte(unchecked ((byte)Stream));
            buffer.WriteByte((byte)OpCode);

            //write length placeholder
            buffer.WriteInt(0);

            //write uncompressed data
            WriteData(buffer);

            //compress if allowed, and buffer is large enough to compress
            if (compress && buffer.Length > compressTreshold + 8)
            {
                buffer.Position = 8;

                //compress data to temporary stream
                using (var compressed = new PoolMemoryStream())
                {
                    //compress the data to the buffer
                    int length = Compressor.Compress(buffer, compressed);

                    //add compression to flags
                    Flags          |= FrameFlags.Compression;
                    buffer.Position = 1;
                    buffer.WriteByte((byte)Flags);

                    //overwrite data with compressed data
                    buffer.Position     = 8;
                    compressed.Position = 0;
                    compressed.CopyTo(buffer);
                    buffer.SetLength(length + 8);
                }
            }

            //overwrite length with real value
            buffer.Position = 4;
            buffer.WriteInt((int)buffer.Length - 8);

            //reset buffer position
            buffer.Position = 0;

            //return the buffer
            return(buffer);
        }
示例#2
0
        public static void EncodeRequest(Request request, Stream outputStream)
        {
            var header = new byte[HeaderLength];

            header.WriteUShort(Magic);
            header[2] = RequestFlag | HessianSerialize;
            if (request.IsEvent)
            {
                header[2] |= EventFlag;
            }

            if (request.IsTwoWay)
            {
                header[2] |= TwowayFlag;
            }

            header.WriteLong(request.RequestId, 4);
            using (var dataStream = new PoolMemoryStream())
            {
                var output = new Hessian2Writer(dataStream);
                if (request.IsEvent)
                {
                    output.WriteNull();
                }
                else
                {
                    output.WriteString("2.0.0");
                    output.WriteObject(request.Service);
                    output.WriteObject(request.Version);
                    output.WriteObject(request.MethodName);
                    output.WriteString(request.ParameterTypeInfo);
                    if (request.Arguments != null && request.Arguments.Length > 0)
                    {
                        foreach (var arg in request.Arguments)
                        {
                            output.WriteObject(arg);
                        }
                    }
                    output.WriteObject(request.Attachments);
                }
                header.WriteInt((int)dataStream.Length, 12);
                outputStream.Write(header, 0, header.Length);
                dataStream.CopyTo(outputStream);
            }
        }