示例#1
0
 static Rpc()
 {
     // TODO s2c protocol
     client = new SprotoRpc(Protocol.Instance);
     // TODO c2s protocol
     clientRequest = client.Attach(Protocol.Instance);
 }
示例#2
0
 public SprotoTcpSocket(SprotoMgr S2C, SprotoMgr C2S)
 {
     this.TcpSocket = new TcpClientSocket();
     this.TcpSocket.Register(this._OnConnect, this._OnClose, this._OnMessage, this._Log);
     this.Dispatcher = new ProtoDispatcher();
     this.Proto      = new SprotoRpc(S2C, C2S);
 }
    public SprotoProcesser(Socket socket, Action <SprotoRpc.RpcInfo> onEvent)
    {
        _sprotoRpc = new SprotoRpc();
        _host      = _sprotoRpc.Attach(Protocol.Instance);
        _onEvent   = onEvent;

        _transporter = new NetTransporter(socket, OnReceive);
        _transporter.Start();
    }
示例#4
0
        public SprotoTcpSocket(string s2cfile, string c2sfile, bool isbinary = false)
        {
            this.TcpSocket = new TcpClientSocket();
            this.TcpSocket.Register(this._OnConnect, this._OnClose, this._OnMessage, this._Log);
            this.Dispatcher = new ProtoDispatcher();
            SprotoMgr S2C = SprotoParser.ParseFile(s2cfile);
            SprotoMgr C2S = SprotoParser.ParseFile(c2sfile);

            this.Proto = new SprotoRpc(S2C, C2S);
        }
示例#5
0
        public TestTcpClientSocket()
        {
            string c2s =
                @"
.package {
	type 0 : integer
	session 1 : integer
	ud 2 : integer
}

C2GS_Ping 1006 {
	request {
		str 0 : string
	}
}
";
            string s2c =
                @"
.package {
	type 0 : integer
	session 1 : integer
	ud 2 : integer
}

GS2C_Pong 1108 {
	request {
		str 0 : string
		time 1 : integer
	}
}
";
            SprotoMgr C2SProto = SprotoParser.Parse(c2s);
            SprotoMgr S2CProto = SprotoParser.Parse(s2c);

            Proto = new SprotoRpc(S2CProto, C2SProto);

            TcpSocket = new TcpClientSocket();
        }
示例#6
0
 public ClientSocket(Context ctx, ProtocolBase s2c, ProtocolBase c2s)
 {
     _ctx         = ctx;
     _host        = new SprotoRpc(s2c);
     _sendRequest = _host.Attach(c2s);
 }
示例#7
0
        public static void Run()
        {
            Console.WriteLine("TestSprotoRpc.Run ...");
            string c2s =
                @".package {
	type 0 : integer
	session 1 : integer
}

foobar 1 {
	request {
		what 0 : string
	}
	response {
		ok 0 : boolean
	}
}

foo 2 {
	response {
		ok 0 : boolean
	}
}

bar 3 {
	response nil
}

blackhole 4 {
}
";

            string s2c =
                @".package {
	type 0 : integer
	session 1 : integer
}
";
            SprotoMgr S2C    = SprotoParser.Parse(s2c);
            SprotoMgr C2S    = SprotoParser.Parse(c2s);
            SprotoRpc Client = new SprotoRpc(S2C, C2S);
            SprotoRpc Server = new SprotoRpc(C2S, S2C);
            // test proto foobar
            SprotoObject request = Client.C2S.NewSprotoObject("foobar.request");

            request["what"] = "foo";
            RpcPackage request_package = Client.PackRequest("foobar", request, 1);

            Console.WriteLine("Client request foobar: data={0},size={1}", request_package.data, request_package.size);
            RpcMessage message = Server.UnpackMessage(request_package.data, request_package.size);

            SprotoHelper.Assert(message.proto == "foobar");
            SprotoHelper.Assert(message.type == "request", "not a request");
            SprotoHelper.Assert(message.request != null);
            SprotoHelper.Assert(message.request["what"] == "foo");
            SprotoHelper.Assert(message.session == 1);
            SprotoObject response = Client.C2S.NewSprotoObject("foobar.response");

            response["ok"] = true;
            RpcPackage response_package = Server.PackResponse(message.proto, response, message.session);

            Console.WriteLine("Server resonse foobar: data={0},size={1}", response_package.data, response_package.size);
            message = Client.UnpackMessage(response_package.data, response_package.size);
            SprotoHelper.Assert(message.proto == "foobar");
            SprotoHelper.Assert(message.type == "response", "not a response");
            SprotoHelper.Assert(message.response != null);
            SprotoHelper.Assert(message.response["ok"] == true);
            SprotoHelper.Assert(message.session == 1);

            // test proto foo
            request_package = Client.PackRequest("foo", null, 2);
            Console.WriteLine("Client request foo: data={0},size={1}", request_package.data, request_package.size);
            message = Server.UnpackMessage(request_package.data, request_package.size);

            SprotoHelper.Assert(message.proto == "foo");
            SprotoHelper.Assert(message.type == "request", "not a request");
            SprotoHelper.Assert(message.request == null);             // no request data
            SprotoHelper.Assert(message.session == 2);
            response         = Client.C2S.NewSprotoObject("foo.response");
            response["ok"]   = false;
            response_package = Server.PackResponse(message.proto, response, message.session);
            Console.WriteLine("Server resonse foo: data={0},size={1}", response_package.data, response_package.size);
            message = Client.UnpackMessage(response_package.data, response_package.size);
            SprotoHelper.Assert(message.proto == "foo");
            SprotoHelper.Assert(message.type == "response", "not a response");
            SprotoHelper.Assert(message.response != null);
            SprotoHelper.Assert(message.response["ok"] == false);
            SprotoHelper.Assert(message.session == 2);

            // test proto bar
            request_package = Client.PackRequest("bar", null, 3);
            Console.WriteLine("Client request bar: data={0},size={1}", request_package.data, request_package.size);
            message = Server.UnpackMessage(request_package.data, request_package.size);

            SprotoHelper.Assert(message.proto == "bar");
            SprotoHelper.Assert(message.type == "request", "not a request");
            SprotoHelper.Assert(message.request == null);             // no request data
            SprotoHelper.Assert(message.session == 3);
            response_package = Server.PackResponse(message.proto, null, message.session);
            Console.WriteLine("Server resonse bar: data={0},size={1}", response_package.data, response_package.size);
            message = Client.UnpackMessage(response_package.data, response_package.size);
            SprotoHelper.Assert(message.proto == "bar");
            SprotoHelper.Assert(message.type == "response", "not a response");
            SprotoHelper.Assert(message.response == null);             // no response data
            SprotoHelper.Assert(message.session == 3);

            // test proto blackhole
            request_package = Client.PackRequest("blackhole", null, 0);
            Console.WriteLine("Client request blackhole: data={0},size={1}", request_package.data, request_package.size);
            message = Server.UnpackMessage(request_package.data, request_package.size);

            SprotoHelper.Assert(message.proto == "blackhole");
            SprotoHelper.Assert(message.type == "request", "not a request");
            SprotoHelper.Assert(message.request == null);             // no request data
            SprotoHelper.Assert(message.session == 0);
            // session == 0 mean: can donn't response

            Client.C2S.Dump();
            Console.WriteLine("TestSprotoRpc.Run ok");
        }
示例#8
0
		public override void run() {
			SprotoRpc client = new SprotoRpc ();
			SprotoRpc service = new SprotoRpc (Protocol.Instance);
			SprotoRpc.RpcRequest clientRequest = client.Attach (Protocol.Instance);

			// ===============foobar=====================
			// request

			SprotoType.foobar.request obj = new SprotoType.foobar.request ();
			obj.what = "foo";
			byte[] req = clientRequest.Invoke<Protocol.foobar> (obj, 1);
			assert (req, new byte[] {0X55, 0X02, 0X04, 0X04, 0X01, 0Xc4, 0X03, 0X66, 0X6f, 0X01, 0X6f});

			// dispatch
			SprotoRpc.RpcInfo sinfo = service.Dispatch (req);
			assert (sinfo.type == SprotoRpc.RpcType.REQUEST);
			assert (sinfo.requestObj.GetType () == typeof(SprotoType.foobar.request));
			assert (sinfo.Response != null);
			SprotoType.foobar.request req_obj = (SprotoType.foobar.request)sinfo.requestObj;
			assert (req_obj.what == "foo");

			// response
			SprotoType.foobar.response obj2 = new SprotoType.foobar.response ();
			obj2.ok = true;
			byte[] resp = sinfo.Response (obj2);
			assert (resp, new byte[] {0X55, 0X02, 0X01, 0X04, 0X01, 0X01, 0X04});

			// dispatch
			sinfo = client.Dispatch (resp);
			assert (sinfo.type == SprotoRpc.RpcType.RESPONSE);
			assert (sinfo.session == 1);
			assert (((SprotoType.foobar.response)sinfo.responseObj).ok == true);
	
			// ================foo====================
			// request
			req =  clientRequest.Invoke<Protocol.foo> (null, 2);
			assert (req, new byte[] {0X15, 0X02, 0X06, 0X06});

			// dispatch
			sinfo = service.Dispatch (req);
			assert (sinfo.type == SprotoRpc.RpcType.REQUEST);
			assert (sinfo.tag == Protocol.foo.Tag);
			assert (sinfo.requestObj == null);

			// response
			SprotoType.foo.response obj3 = new SprotoType.foo.response();
			obj3.ok = false;
			resp = sinfo.Response (obj3);
			assert (resp, new byte[] {0X55, 0X02, 0X01, 0X06, 0X01, 0X01, 0X02});

			// dispatch
			sinfo = client.Dispatch (resp);
			assert (sinfo.type == SprotoRpc.RpcType.RESPONSE);
			assert (sinfo.session == 2);
			assert (((SprotoType.foo.response)sinfo.responseObj).ok == false);

			// ================bar====================
			// request
			req = clientRequest.Invoke<Protocol.bar> ();
			assert (req, new byte[] { 0X05, 0X01, 0X08, });

			// dispatch
			sinfo = service.Dispatch (req);
			assert (sinfo.type == SprotoRpc.RpcType.REQUEST);
			assert (sinfo.requestObj == null);
			assert (sinfo.tag == Protocol.bar.Tag);
			assert (sinfo.Response == null);

			// ================blackhole====================
			// request
			req = clientRequest.Invoke<Protocol.blackhole> ();
			assert (req, new byte[]{ 0X05, 0X01, 0X0a });
		}
示例#9
0
        public static void Run()
        {
            string c2s =
                @"
.package {
	type 0 : integer
	session 1 : integer
}

.Person {
	id 0 : integer			# int type
	name 1 : string			# string type
	age 2 : integer			# int type
	isman 3 : boolean		# bool type
	emails 4 : *string		# string list
	children 5 : *Person	# Person list
	luckydays 6 : *integer  # integer list
}


get 1 {
	request {
		id 0 : integer
	}
	response Person
}
";
            string s2c =
                @"
.package {
	type 0 : integer
	session 1 : integer
}
";
            SprotoMgr S2C    = SprotoParser.Parse(s2c);
            SprotoMgr C2S    = SprotoParser.Parse(c2s);
            SprotoRpc Client = new SprotoRpc(S2C, C2S);
            SprotoRpc Server = new SprotoRpc(C2S, S2C);
            // create a request
            SprotoObject request = Client.C2S.NewSprotoObject("get.request");

            request["id"] = 1;
            RpcPackage request_package = Client.PackRequest("get", request, 1);

            RpcMessage message = Server.UnpackMessage(request_package.data, request_package.size);
            // create a response
            //SprotoObject response = Client.C2S.NewSprotoObject("Person");
            SprotoObject response = Server.S2C.NewSprotoObject("Person");

            response["id"]     = 1;
            response["name"]   = "sundream";
            response["age"]    = 26;
            response["emails"] = new List <string> {
                "*****@*****.**",
            };
            //List<SprotoObject> children = new List<SprotoObject>();
            // no children
            //response["children"] = children;
            response["luckydays"] = new List <Int64> {
                0, 6
            };
            RpcPackage response_package = Server.PackResponse("get", response, 1);

            message = Client.UnpackMessage(response_package.data, response_package.size);
            Console.WriteLine("proto={0},tag={1},ud={2},session={3},type={4},request={5},response={6}",
                              message.proto, message.tag, message.ud, message.session, message.type, message.request, message.response);
        }
示例#10
0
        public override void run()
        {
            SprotoRpc client  = new SprotoRpc();
            SprotoRpc service = new SprotoRpc(Protocol.Instance);

            SprotoRpc.RpcRequest clientRequest = client.Attach(Protocol.Instance);

            // ===============foobar=====================
            // request

            SprotoType.foobar.request obj = new SprotoType.foobar.request();
            obj.what = "foo";
            byte[] req = clientRequest.Invoke <Protocol.foobar> (obj, 1);
            assert(req, new byte[] { 0X55, 0X02, 0X04, 0X04, 0X01, 0Xc4, 0X03, 0X66, 0X6f, 0X01, 0X6f });

            // dispatch
            SprotoRpc.RpcInfo sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.requestObj.GetType() == typeof(SprotoType.foobar.request));
            assert(sinfo.Response != null);
            SprotoType.foobar.request req_obj = (SprotoType.foobar.request)sinfo.requestObj;
            assert(req_obj.what == "foo");

            // response
            SprotoType.foobar.response obj2 = new SprotoType.foobar.response();
            obj2.ok = true;
            byte[] resp = sinfo.Response(obj2);
            assert(resp, new byte[] { 0X55, 0X02, 0X01, 0X04, 0X01, 0X01, 0X04 });

            // dispatch
            sinfo = client.Dispatch(resp);
            assert(sinfo.type == SprotoRpc.RpcType.RESPONSE);
            assert(sinfo.session == 1);
            assert(((SprotoType.foobar.response)sinfo.responseObj).ok == true);

            // ================foo====================
            // request
            req = clientRequest.Invoke <Protocol.foo> (null, 2);
            assert(req, new byte[] { 0X15, 0X02, 0X06, 0X06 });

            // dispatch
            sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.tag == Protocol.foo.Tag);
            assert(sinfo.requestObj == null);

            // response
            SprotoType.foo.response obj3 = new SprotoType.foo.response();
            obj3.ok = false;
            resp    = sinfo.Response(obj3);
            assert(resp, new byte[] { 0X55, 0X02, 0X01, 0X06, 0X01, 0X01, 0X02 });

            // dispatch
            sinfo = client.Dispatch(resp);
            assert(sinfo.type == SprotoRpc.RpcType.RESPONSE);
            assert(sinfo.session == 2);
            assert(((SprotoType.foo.response)sinfo.responseObj).ok == false);

            // ================bar====================
            // request
            req = clientRequest.Invoke <Protocol.bar> ();
            assert(req, new byte[] { 0X05, 0X01, 0X08, });

            // dispatch
            sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.requestObj == null);
            assert(sinfo.tag == Protocol.bar.Tag);
            assert(sinfo.Response == null);

            // ================blackhole====================
            // request
            req = clientRequest.Invoke <Protocol.blackhole> ();
            assert(req, new byte[] { 0X05, 0X01, 0X0a });
        }