Esempio n. 1
0
 public SpRpcResult()
 {
     Session  = 0;
     Protocol = null;
     Op       = SpRpcOp.Unknown;
     Data     = null;
     ud       = 0;
 }
Esempio n. 2
0
 public SpRpcResult(int s, SpProtocol proto, SpRpcOp op, SpObject data, int u)
 {
     Session  = s;
     Protocol = proto;
     Op       = op;
     Data     = data;
     ud       = u;
 }
Esempio n. 3
0
 public void Parse(string str)
 {
     this.str         = str;
     mCurrentProtocol = null;
     mCurrentType     = null;
     PreProcess();
     Scan();
 }
Esempio n. 4
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();
            }

            int ud = 0;

            if (header["ud"] != null)
            {
                ud = header["ud"].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, ud));
            }
            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, ud));
                }

                SpObject obj = mAttachTypeManager.Codec.Decode(protocol.Response, unpack_stream);
                return(new SpRpcResult(session, protocol, SpRpcOp.Response, obj, ud));
            }
        }
Esempio n. 5
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);
        }
Esempio n. 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);
        }
Esempio n. 7
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. 8
0
        private void Scan()
        {
            int start = 0;

            while (true)
            {
                int pos = str.IndexOfAny(sDelimiters, start);
                if (pos < 0)
                {
                    break;
                }

                switch (str[pos])
                {
                case '{':
                    string title = str.Substring(start, pos - start).Trim();
                    if (IsProtocol(title))
                    {
                        mCurrentProtocol = NewProtocol(title);
                        mTypes.Clear();
                    }
                    else
                    {
                        if (mCurrentType != null)
                        {
                            mTypes.Add(mCurrentType);
                        }

                        mCurrentType = NewType(title);
                        if (mTypes.Count > 0)
                        {
                            mTypes[mTypes.Count - 1].AddType(mCurrentType);
                        }
                    }

                    break;

                case '}':
                    if (mCurrentType != null)
                    {
                        mTypeManager.OnNewType(mCurrentType);
                        if (mCurrentProtocol != null)
                        {
                            mCurrentProtocol.AddType(mCurrentType);
                        }

                        if (mTypes.Count > 0)
                        {
                            mCurrentType = mTypes[mTypes.Count - 1];
                            mTypes.RemoveAt(mTypes.Count - 1);
                        }
                        else
                        {
                            mCurrentType = null;
                        }
                    }
                    else if (mCurrentProtocol != null)
                    {
                        mTypeManager.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;
            }
        }