예제 #1
0
        public RpcPackage PackRequest(string proto, SprotoObject request = null, Int64 session = 0, SprotoObject ud = null)
        {
            //Console.WriteLine("PackRequest {0} {1} {2}",proto,request,session);
            SprotoProtocol protocol = this.C2S.GetProtocol(proto);
            UInt16         tag      = protocol.tag;
            SprotoObject   header   = this.NewPackageHeader(this.C2S, tag, session, ud);

            this.writer.Seek(0, SprotoStream.SEEK_BEGIN);            // clear stream
            SprotoStream writer = this.C2S.Encode(header, this.writer);

            if (request != null)
            {
                string expect = protocol.request;
                if (request.type != expect)
                {
                    SprotoHelper.Error("[SprotoRpc.Request] expect '{0}' got '{1}'", expect, request.type);
                }
                writer = this.C2S.Encode(request, writer);
            }
            RpcPackage package = new RpcPackage();

            package.data = this.C2S.Pack(writer.Buffer, 0, writer.Position, out package.size);
            if (session != 0)
            {
                SprotoHelper.Assert(!this.sessions.ContainsKey(session), String.Format("repeat session: {0}", session));
                RpcMessage message = new RpcMessage();
                message.session        = session;
                message.proto          = proto;
                message.request        = request;
                message.tag            = tag;
                this.sessions[session] = message;
            }
            return(package);
        }
예제 #2
0
        public RpcPackage PackResponse(string proto, SprotoObject response = null, Int64 session = 0, SprotoObject ud = null)
        {
            //Console.WriteLine("PackResponse {0} {1} {2}",proto,response,session);
            SprotoProtocol protocol = this.S2C.GetProtocol(proto);
            SprotoObject   header   = this.NewPackageHeader(this.S2C, 0, session, ud);

            this.writer.Seek(0, SprotoStream.SEEK_BEGIN);            // clear stream
            SprotoStream writer = this.S2C.Encode(header, this.writer);

            if (response != null)
            {
                string expect = protocol.response;
                if (response.type != expect)
                {
                    SprotoHelper.Error("[SprotoRpc.Response] expect '{0}' got '{1}'", expect, response.type);
                }
                writer = this.S2C.Encode(response, writer);
            }
            RpcPackage package = new RpcPackage();

            package.data = this.S2C.Pack(writer.Buffer, 0, writer.Position, out package.size);
            return(package);
        }