Exemplo n.º 1
0
        public bool Request(string proto, SpObject args, int session, SpStream stream)
        {
            SpStream encode_stream = EncodeRequest(proto, args, session);

            encode_stream.Position = 0;
            return(SpPacker.Pack(encode_stream, stream));
        }
Exemplo n.º 2
0
        public SpStream Encode(SpType type, SpObject obj)
        {
            SpStream stream = new SpStream();

            if (Encode(type, obj, stream) == false)
            {
                if (stream.IsOverflow())
                {
                    if (stream.Position > MAX_SIZE)
                    {
                        return(null);
                    }

                    int size = stream.Position;
                    size   = ((size + 7) / 8) * 8;
                    stream = new SpStream(size);
                    if (Encode(type, obj, stream) == false)
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }

            return(stream);
        }
Exemplo n.º 3
0
        public SpObject Decode(SpType type, SpStream stream)
        {
            if (type == null || stream == null)
            {
                return(null);
            }

            mStream = stream;
            return(DecodeInternal(type));
        }
Exemplo n.º 4
0
        public static bool Unpack(SpStream input, SpStream output)
        {
            int src_size   = input.Tail;
            int src_offset = input.Offset;

            byte[] src = input.Buffer;

            while (src_offset < src_size)
            {
                byte header = src[src_offset];
                src_offset++;

                if (header == 0xff)
                {
                    if (src_offset > src_size)
                    {
                        return(false);
                    }

                    int n = (src[src_offset] + 1) * 8;
                    if (src_size - src_offset < n + 1)
                    {
                        return(false);
                    }

                    src_offset++;
                    output.Write(src, src_offset, n);
                    src_offset += n;
                }
                else
                {
                    for (int i = 0; i < 8; i++)
                    {
                        int nz = (header >> i) & 1;
                        if (nz != 0)
                        {
                            if (src_offset > src_size)
                            {
                                return(false);
                            }

                            output.Write(src[src_offset]);
                            src_offset++;
                        }
                        else
                        {
                            output.Write((byte)0);
                        }
                    }
                }
            }

            return(output.IsOverflow() == false);
        }
Exemplo n.º 5
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);

            return(success && stream.IsOverflow() == false);
        }
Exemplo n.º 6
0
        public static void WriteFF(byte[] src, int src_offset, SpStream output, int n, int correctPos)
        {
            int align8_n = (n + 7) & (~7);

            output.Write((byte)0xff);
            output.Write((byte)(align8_n / 8 - 1));
            output.Write(src, src_offset, n);
            for (int i = 0; i < align8_n - n; i++)
            {
                output.Write((byte)0);
            }
            output.CorrectLength(correctPos);
        }
Exemplo n.º 7
0
        public SpRpcResult Dispatch(SpStream stream)
        {
            SpStream unpack_stream = SpPacker.Unpack(stream);

            unpack_stream.Position = 0;
            SpObject header = mHostTypeManager.Codec.Decode(mHeaderType, unpack_stream);

            int session = 0;

            if (header["session"] != null)
            {
                session = header["session"].AsInt();
            }

            if (header["type"] != null)
            {
                // handle request
                SpProtocol protocol = mHostTypeManager.GetProtocolByTag(header["type"].AsInt());
                SpObject   obj      = mHostTypeManager.Codec.Decode(protocol.Request, unpack_stream);
                if (session != 0)
                {
                    mSessions[session] = protocol;
                }
                return(new SpRpcResult(session, protocol, SpRpcOp.Request, obj));
            }
            else
            {
                // handle response
                if (mSessions.ContainsKey(session) == false)
                {
                    return(new SpRpcResult());
                }

                SpProtocol protocol = mSessions[session];
                mSessions.Remove(session);

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

                if (protocol.Response == null)
                {
                    return(new SpRpcResult(session, protocol, SpRpcOp.Response, null));
                }

                SpObject obj = mAttachTypeManager.Codec.Decode(protocol.Response, unpack_stream);
                return(new SpRpcResult(session, protocol, SpRpcOp.Response, obj));
            }
        }
Exemplo n.º 8
0
        private static int PackSeg(byte[] src, int src_offset, SpStream output, int n)
        {
            byte header  = 0;
            int  notzero = 0;

            int dest_begin = output.Position;

            output.Position++;

            for (int i = 0; i < 8; i++)
            {
                if (src[src_offset + i] != 0)
                {
                    notzero++;
                    header |= (byte)(1 << i);

                    output.Write(src[src_offset + i]);
                }
            }

            if ((notzero == 7 || notzero == 6) && n > 0)
            {
                notzero = 8;
            }

            if (notzero == 8)
            {
                if (n > 0)
                {
                    return(8);
                }
                else
                {
                    return(10);
                }
            }

            int dest_end = output.Position;

            output.Position = dest_begin;
            output.Write(header);
            output.Position = dest_end;

            return(notzero + 1);
        }
Exemplo n.º 9
0
        public static SpStream Unpack(SpStream input)
        {
            SpStream stream = new SpStream();

            int pos = input.Position;

            if (Unpack(input, stream) == false)
            {
                stream = new SpStream(stream.Position);

                input.Position = pos;
                if (Unpack(input, stream) == false)
                {
                    return(null);
                }
            }

            return(stream);
        }
Exemplo n.º 10
0
        public static SpStream Pack(SpStream input)
        {
            SpStream stream = new SpStream();

            int pos = input.Position;

            if (Pack(input, stream) == false)
            {
                int size = stream.Position;
                size   = ((size + 7) / 8) * 8;
                stream = new SpStream(size);

                input.Position = pos;
                // do we need this check? it's not possible it will return false this time
                if (Pack(input, stream) == false)
                {
                    return(null);
                }
            }

            return(stream);
        }
Exemplo n.º 11
0
        public SpStream Response(int session, SpObject args)
        {
            SpObject header = new SpObject();

            header.Insert("session", session);

            SpStream encode_stream = new SpStream();

            mHostTypeManager.Codec.Encode(mHeaderType, header, encode_stream);

            if (session != 0 && mSessions.ContainsKey(session))
            {
                mHostTypeManager.Codec.Encode(mSessions[session].Response, args, encode_stream);
            }

            SpStream pack_stream = new SpStream();

            encode_stream.Position = 0;
            SpPacker.Pack(encode_stream, pack_stream);

            pack_stream.Position = 0;
            return(pack_stream);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
0
 public bool Encode(string proto, SpObject obj, SpStream stream)
 {
     return(Encode(mTypeManager.GetType(proto), obj, stream));
 }
Exemplo n.º 15
0
 public SpObject Decode(string proto, SpStream stream)
 {
     return(Decode(mTypeManager.GetType(proto), stream));
 }