Exemplo n.º 1
0
        public RpcMessage UnpackMessage(byte[] bytes, int size)
        {
            RpcMessage message  = null;
            int        bin_size = 0;

            byte[] bin = this.S2C.Unpack(bytes, 0, size, out bin_size);
            this.reader.Seek(0, SprotoStream.SEEK_BEGIN);            // clear stream
            this.reader.Buffer = bin;

            SprotoObject header = this.S2C.Decode(this.package, this.reader);

            if (header["type"] != null)
            {
                // request
                UInt16         tag      = (UInt16)header["type"];
                SprotoProtocol protocol = this.S2C.GetProtocol(tag);
                SprotoObject   request  = null;
                if (protocol.request != null)
                {
                    request = this.S2C.Decode(protocol.request, this.reader);
                }

                message      = new RpcMessage();
                message.type = "request";
                if (header["session"] != null)
                {
                    message.session = header["session"];
                }
                if (header["ud"] != null)
                {
                    message.ud = header["ud"];
                }
                message.proto = protocol.name;
                message.tag   = protocol.tag;

                message.request = request;
            }
            else
            {
                // response
                SprotoHelper.Assert(header["session"] != null, "session not found");
                Int64 session = header["session"];
                if (this.sessions.TryGetValue(session, out message))
                {
                    //Console.WriteLine("remove session {0}",session);
                    this.sessions.Remove(session);
                }
                SprotoHelper.Assert(message != null, "unknow session");
                message.type = "response";
                if (header["ud"] != null)
                {
                    message.ud = header["ud"];
                }
                SprotoProtocol protocol = this.C2S.GetProtocol(message.tag);
                if (protocol.response != null)
                {
                    SprotoObject response = this.C2S.Decode(protocol.response, this.reader);
                    message.response = response;
                }
            }
            return(message);
        }