Example #1
0
 public SpRpcResult()
 {
     Session  = 0;
     Protocol = null;
     Op       = SpRpcOp.Unknown;
     Arg      = null;
 }
Example #2
0
 public SpRpcResult(int s, SpProtocol p, SpRpcOp o, SpObject a)
 {
     Session  = s;
     Protocol = p;
     Op       = o;
     Arg      = a;
 }
Example #3
0
        public void Parse(string str)
        {
            mCurrentProtocol = null;
            mCurrentType     = null;
            mLastType        = null;

            str = PreProcess(str);
            Scan(str, 0);
        }
Example #4
0
        public void OnNewProtocol(SpProtocol protocol)
        {
            if (GetProtocolByName(protocol.Name) != null || GetProtocolByTag(protocol.Tag) != null)
            {
                return;
            }

            mProtocols.Add(protocol.Name, protocol);
            mProtocolsByTag.Add(protocol.Tag, protocol);
        }
Example #5
0
        private void Scan(string str, int start)
        {
            int pos = str.IndexOfAny(sDelimiters, start);

            if (pos < 0)
            {
                return;
            }

            switch (str[pos])
            {
            case '{':
                string title = str.Substring(start, pos - start).Trim();
                if (IsProtocol(title))
                {
                    mCurrentProtocol = NewProtocol(title);
                }
                else
                {
                    mLastType    = mCurrentType;
                    mCurrentType = NewType(title);
                }
                break;

            case '}':
                if (mCurrentType != null)
                {
                    mListener.OnNewType(mCurrentType);
                    if (mCurrentProtocol != null)
                    {
                        mCurrentProtocol.AddType(mCurrentType);
                    }
                    mCurrentType = mLastType;
                    mLastType    = null;
                }
                else if (mCurrentProtocol != null)
                {
                    mListener.OnNewProtocol(mCurrentProtocol);
                    mCurrentProtocol = null;
                }
                break;

            case '\n':
                SpField f = NewField(str.Substring(start, pos - start));
                if (f != null && mCurrentType != null)
                {
                    mCurrentType.AddField(f);
                }
                break;
            }

            start = pos + 1;
            Scan(str, start);
        }
Example #6
0
        private SpProtocol NewProtocol(string str)
        {
            string[] words = str.Split(sSpace);
            if (words.Length != 2)
            {
                return(null);
            }

            SpProtocol protocol = new SpProtocol(words[0], int.Parse(words[1]));

            return(protocol);
        }
Example #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));
            }
        }
Example #8
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);
        }