Esempio n. 1
0
        public bool Encode(SpType type, SpObject obj, SpStream stream)
        {
            if (type == null || obj == null || stream == null)
            {
                return(false);
            }

            //mStream = stream;
            bool success = EncodeInternal(type, obj, stream);

            return(success && stream.IsOverflow() == false);
        }
Esempio n. 2
0
        private SpStream EncodeRequest(string proto, SpObject args, int session)
        {
            if (mAttachTypeManager == null || mHostTypeManager == null || mHeaderType == null)
            {
                return(null);
            }

            SpProtocol protocol = mAttachTypeManager.GetProtocolByName(proto);

            if (protocol == null)
            {
                return(null);
            }

            SpObject header = new SpObject();

            header.Insert("type", protocol.Tag);
            if (session != 0)
            {
                header.Insert("session", session);
            }

            SpStream stream = mHostTypeManager.Codec.Encode(mHeaderType, header);

            if (stream == null)
            {
                return(null);
            }

            if (args != null)
            {
                if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false)
                {
                    if (stream.IsOverflow())
                    {
                        if (stream.Position > SpCodec.MAX_SIZE)
                        {
                            return(null);
                        }

                        int size = stream.Position;
                        size   = ((size + 7) / 8) * 8;
                        stream = new SpStream(size);
                        if (mAttachTypeManager.Codec.Encode(protocol.Request, args, stream) == false)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            if (session != 0)
            {
                mSessions[session] = protocol;
            }

            return(stream);
        }
Esempio n. 3
0
        public static bool Pack(SpStream input, SpStream output)
        {
            int src_size = input.Length;

            if (src_size % 8 != 0)
            {
                int new_size = ((src_size + 7) / 8) * 8;
                if (input.Capacity < new_size)
                {
                    SpStream new_input = new SpStream(new_size);
                    Array.Copy(input.Buffer, input.Offset, new_input.Buffer, new_input.Offset, input.Length);
                    new_input.Position = new_input.Offset;
                    input    = new_input;
                    src_size = new_size;
                }
                else
                {
                    int pos = input.Position;
                    input.Position = input.Tail;
                    for (int i = src_size; i < new_size; i++)
                    {
                        input.Write((byte)0);
                    }

                    input.Position = pos;
                }
            }

            int src_start  = input.Offset;
            int src_offset = input.Offset;

            byte[] src = input.Buffer;

            int ff_n          = 0;
            int ff_src_start  = 0;
            int ff_dest_start = 0;

            for (; src_offset < src_size; src_offset += 8)
            {
                int pos = output.Position;
                int n   = PackSeg(src, src_offset, output, ff_n);

                if (n == 10)
                {
                    ff_src_start  = src_offset;
                    ff_dest_start = pos;
                    ff_n          = 1;
                }
                else if (n == 8 && ff_n > 0)
                {
                    ff_n++;
                    if (ff_n == 256)
                    {
                        output.Position = ff_dest_start;
                        WriteFF(src, ff_src_start, output, 256 * 8, n);
                        ff_n = 0;
                    }
                }
                else
                {
                    if (ff_n > 0)
                    {
                        output.Position = ff_dest_start;
                        WriteFF(src, ff_src_start, output, ff_n * 8, n);
                        ff_n = 0;
                    }
                }

                output.Position = pos + n;
            }

            if (ff_n == 1)
            {
                output.Position = ff_dest_start;
                WriteFF(src, ff_src_start, output, 8, 0);
            }
            else if (ff_n > 1)
            {
                output.Position = ff_dest_start;
                WriteFF(src, ff_src_start, output, src_size - (ff_src_start - src_start), 0);
            }

            return(output.IsOverflow() == false);
        }