private void NetWorkStateCallBack(NetWorkState state) { SkynetLogger.Info(Channel.NetDevice, "Gate WS NetWorkStateCallBack:" + state); if (state == NetWorkState.Connected) { //TODO:发送 与 gate 握手消息成功后 开启 心跳操作 SpObject handshakeRequset = new SpObject(); handshakeRequset.Insert("uid", _req.uid); handshakeRequset.Insert("secret", _req.secret); handshakeRequset.Insert("subid", _req.subid); _client.Request("handshake", handshakeRequset, (SpObject obj) => { { int role = obj["role"].AsInt(); if (role == 0) { SpObject bornRequest = new SpObject(); bornRequest.Insert("name", "helloworld"); bornRequest.Insert("head", "1111111111"); bornRequest.Insert("job", "1"); _client.Request("born", bornRequest, (SpObject bornObj) => { SkynetLogger.Info(Channel.NetDevice, "born resp is ok"); }); } else { SkynetLogger.Info(Channel.NetDevice, "is has role"); } } }); } }
private SpObject CreateObject () { SpObject obj = new SpObject (); obj.Insert ("a", new SpObject ("hello")); obj.Insert ("b", new SpObject (1000000)); obj.Insert ("c", new SpObject (true)); SpObject d = new SpObject (); d.Insert ("a", "world"); d.Insert ("c", -1); obj.Insert ("d", d); SpObject e = new SpObject (); e.Append ("ABC"); e.Append ("def"); obj.Insert ("e", e); SpObject f = new SpObject (); f.Append (-3); f.Append (-2); f.Append (-1); f.Append (0); f.Append (1); f.Append (2); obj.Insert ("f", f); SpObject g = new SpObject (); g.Append (true); g.Append (false); g.Append (true); obj.Insert ("g", g); SpObject h = new SpObject (); { SpObject t = new SpObject (); t.Insert ("b", 100); h.Append (t); } { SpObject t = new SpObject (); t.Insert ("b", -100); t.Insert ("c", false); h.Append (t); } { SpObject t = new SpObject (); t.Insert ("b", 0); SpObject he = new SpObject (); he.Append ("test"); t.Insert ("e", he); h.Append (t); } obj.Insert ("h", h); return obj; }
private SpObject CreateObject() { SpObject obj = new SpObject(); SpObject person = new SpObject(); { SpObject p = new SpObject(); p.Insert("name", "Alice"); p.Insert("id", 10000); SpObject phone = new SpObject(); { SpObject p1 = new SpObject(); p1.Insert("number", "123456789"); p1.Insert("type", 1); phone.Append(p1); } { SpObject p1 = new SpObject(); p1.Insert("number", "87654321"); p1.Insert("type", 2); phone.Append(p1); } p.Insert("phone", phone); person.Append(p); } { SpObject p = new SpObject(); p.Insert("name", "Bob"); p.Insert("id", 20000); SpObject phone = new SpObject(); { SpObject p1 = new SpObject(); p1.Insert("number", "01234567890"); p1.Insert("type", 3); phone.Append(p1); } p.Insert("phone", phone); person.Append(p); } obj.Insert("person", person); return(obj); }
private SpObject CreateObject () { SpObject obj = new SpObject (); SpObject person = new SpObject (); { SpObject p = new SpObject (); p.Insert ("name", "Alice"); p.Insert ("id", 10000); SpObject phone = new SpObject (); { SpObject p1 = new SpObject (); p1.Insert ("number", "123456789"); p1.Insert ("type", 1); phone.Append (p1); } { SpObject p1 = new SpObject (); p1.Insert ("number", "87654321"); p1.Insert ("type", 2); phone.Append (p1); } p.Insert ("phone", phone); person.Insert (p["id"].AsInt (), p); } { SpObject p = new SpObject (); p.Insert ("name", "Bob"); p.Insert ("id", 20000); SpObject phone = new SpObject (); { SpObject p1 = new SpObject (); p1.Insert ("number", "01234567890"); p1.Insert ("type", 3); phone.Append (p1); } p.Insert ("phone", phone); person.Insert (p["id"].AsInt (), p); } obj.Insert ("person", person); return obj; }
private void TestFoobar () { Util.Log ("client request foobar"); SpObject foobar_request = new SpObject (); foobar_request.Insert ("what", "foo"); SpStream req = client.Request ("foobar", foobar_request, 1); Util.Assert (req.Length == 11); Util.Log ("request foobar size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch (req); Util.Assert (dispatch_result.Arg["what"].AsString ().Equals ("foo")); Util.DumpObject (dispatch_result.Arg); Util.Log ("server response"); SpObject foobar_response = new SpObject (); foobar_response.Insert ("ok", true); SpStream resp = server.Response (dispatch_result.Session, foobar_response); Util.Assert (resp.Length == 7); Util.Log ("response package size = " + resp.Length); Util.Log ("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch (resp); Util.Assert (dispatch_result.Arg["ok"].AsBoolean () == true); Util.DumpObject (dispatch_result.Arg); }
private void TestFoo() { SpStream req = client.Request("foo", null, 2); Util.Assert(req.Length == 4); Util.Log("request foo size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch(req); SpObject foobar_response = new SpObject(); foobar_response.Insert("ok", false); SpStream resp = server.Response(dispatch_result.Session, foobar_response); Util.Assert(resp.Length == 7); Util.Log("response package size = " + resp.Length); Util.Log("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch(resp); Util.Assert(dispatch_result.Arg["ok"].AsBoolean() == false); Util.DumpObject(dispatch_result.Arg); }
private void TestFoobar() { Util.Log("client request foobar"); SpObject foobar_request = new SpObject(); foobar_request.Insert("what", "foo"); SpStream req = client.Request("foobar", foobar_request, 1); Util.Assert(req.Length == 11); Util.Log("request foobar size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch(req); Util.Assert(dispatch_result.Arg["what"].AsString().Equals("foo")); Util.DumpObject(dispatch_result.Arg); Util.Log("server response"); SpObject foobar_response = new SpObject(); foobar_response.Insert("ok", true); SpStream resp = server.Response(dispatch_result.Session, foobar_response); Util.Assert(resp.Length == 7); Util.Log("response package size = " + resp.Length); Util.Log("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch(resp); Util.Assert(dispatch_result.Arg["ok"].AsBoolean() == true); Util.DumpObject(dispatch_result.Arg); }
private SpObject CreateObject2() { SpObject obj = new SpObject(); obj.Insert("a", "hello"); obj.Insert("b", 1000000); obj.Insert("c", true); SpObject d = new SpObject(); d.Insert("a", "world"); d.Insert("c", -1); obj.Insert("d", d); obj.Insert("e", new SpObject(SpObject.ArgType.Array, "ABC", "def")); obj.Insert("f", new SpObject(SpObject.ArgType.Array, -3, -2, -1, 0, 1, 2)); obj.Insert("g", new SpObject(SpObject.ArgType.Array, true, false, true)); SpObject h = new SpObject(); { SpObject t = new SpObject(); t.Insert("b", 100); h.Append(t); } { SpObject t = new SpObject(); t.Insert("b", -100); t.Insert("c", false); h.Append(t); } { SpObject t = new SpObject(); t.Insert("b", 0); SpObject he = new SpObject(); he.Append("test"); t.Insert("e", he); h.Append(t); } obj.Insert("h", h); return(obj); }
void Join() { var joinRequest = new SpObject(); joinRequest.Insert("session", 0); joinRequest.Insert("model", "fight"); _client.Request("join", joinRequest, (SpObject obj) => { var udpSession = new BattleSession { session = obj["session"].AsInt(), host = obj["host"].AsString(), port = obj["port"].AsInt(), secret = obj["secret"].AsString() }; Signals.Get <UdpSignal>().Dispatch(udpSession); }); }
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; }
private void NetWorkStateCallBack(NetWorkState state) { SkynetLogger.Info(Channel.Udp, "Gate Udp NetWorkStateCallBack:" + state); if (state != NetWorkState.Connected) { return; } //TODO:发送 与 gate 握手消息成功后 开启 心跳操作 var handshakeRequset = new SpObject(); handshakeRequset.Insert("uid", "ddddddddddd"); _client.Request("handshake", handshakeRequset, (SpObject obj) => { SkynetLogger.Info(Channel.Udp, "udp handshake resp"); }); }
public SpStream Response(int session, SpObject args) { SpObject header = new SpObject(); header.Insert("session", session); SpStream encode_stream = new SpStream(); mHostTypeManager.Codec.Encode(mHeaderType, header, encode_stream); if (session != 0 && mSessions.ContainsKey(session)) { mHostTypeManager.Codec.Encode(mSessions[session].Response, args, encode_stream); } SpStream pack_stream = new SpStream(); encode_stream.Position = 0; SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; return(pack_stream); }
private SpObject CreateObject() { SpObject obj = new SpObject(); obj.Insert("a", new SpObject("hello")); obj.Insert("b", new SpObject(1000000)); obj.Insert("c", new SpObject(true)); SpObject d = new SpObject(); d.Insert("a", "world"); d.Insert("c", -1); obj.Insert("d", d); SpObject e = new SpObject(); e.Append("ABC"); e.Append("def"); obj.Insert("e", e); SpObject f = new SpObject(); f.Append(-3); f.Append(-2); f.Append(-1); f.Append(0); f.Append(1); f.Append(2); obj.Insert("f", f); SpObject g = new SpObject(); g.Append(true); g.Append(false); g.Append(true); obj.Insert("g", g); SpObject h = new SpObject(); { SpObject t = new SpObject(); t.Insert("b", 100); h.Append(t); } { SpObject t = new SpObject(); t.Insert("b", -100); t.Insert("c", false); h.Append(t); } { SpObject t = new SpObject(); t.Insert("b", 0); SpObject he = new SpObject(); he.Append("test"); t.Insert("e", he); h.Append(t); } obj.Insert("h", h); return(obj); }
private void TestFoo () { SpStream req = client.Request ("foo", null, 2); Util.Assert (req.Length == 4); Util.Log ("request foo size = " + req.Length); req.Position = 0; SpRpcResult dispatch_result = server.Dispatch (req); SpObject foobar_response = new SpObject (); foobar_response.Insert ("ok", false); SpStream resp = server.Response (dispatch_result.Session, foobar_response); Util.Assert (resp.Length == 7); Util.Log ("response package size = " + resp.Length); Util.Log ("client dispatch"); resp.Position = 0; dispatch_result = client.Dispatch (resp); Util.Assert (dispatch_result.Arg["ok"].AsBoolean () == false); Util.DumpObject (dispatch_result.Arg); }
private SpObject DecodeInternal (SpType type) { if (mStream == null || type == null) return null; // buildin type decoding should not be here if (mTypeManager.IsBuildinType (type)) return null; SpObject obj = new SpObject (); List<int> tags = new List<int> (); int current_tag = 0; short fn = mStream.ReadInt16 (); for (short i = 0; i < fn; i++) { int value = (int)mStream.ReadUInt16 (); if (value == 0) { tags.Add (current_tag); current_tag++; } else { if (value % 2 == 0) { SpField f = type.GetFieldByTag (current_tag); if (f == null) return null; value = value / 2 - 1; if (f.Type == mTypeManager.Integer) { obj.Insert (f.Name, value); } else if (f.Type == mTypeManager.Boolean) { obj.Insert (f.Name, (value == 0 ? false : true)); } else { return null; } current_tag++; } else { current_tag += (value + 1) / 2; } } } for (int c = 0; c < tags.Count; c++) { int tag = tags[c]; SpField f = type.GetFieldByTag (tag); if (f == null) return null; if (f.IsTable) { int size = mStream.ReadInt32 (); if (f.Type == mTypeManager.Integer) { byte n = mStream.ReadByte (); int count = (size - 1) / n; SpObject tbl = new SpObject (); for (int i = 0; i < count; i++) { switch (n) { case 4: tbl.Insert (i, mStream.ReadInt32 ()); break; case 8: tbl.Insert (i, mStream.ReadInt64 ()); break; default: return null; } } obj.Insert (f.Name, tbl); } else if (f.Type == mTypeManager.Boolean) { SpObject tbl = new SpObject (); for (int i = 0; i < size; i++) { tbl.Insert (i, mStream.ReadBoolean ()); } obj.Insert (f.Name, tbl); } else if (f.Type == mTypeManager.String) { SpObject tbl = new SpObject (); int k = 0; while (size > 0) { int str_len = mStream.ReadInt32 (); size -= 4; tbl.Insert (k, Encoding.UTF8.GetString (mStream.ReadBytes (str_len), 0, str_len)); k++; size -= str_len; } obj.Insert (f.Name, tbl); } else if (f.Type == null) { // unknown type mStream.ReadBytes (size); } else { SpObject tbl = new SpObject (); int k = 0; while (size > 0) { int obj_len = mStream.ReadInt32 (); size -= 4; SpObject o = DecodeInternal (f.Type); if (f.KeyName != null) { tbl.Insert (o.AsTable ()[f.KeyName].Value, o); } else { tbl.Insert (k, o); } k++; size -= obj_len; } obj.Insert (f.Name, tbl); } } else { int size = mStream.ReadInt32 (); if (f.Type == mTypeManager.Integer) { switch (size) { case 4: obj.Insert (f.Name, mStream.ReadInt32 ()); break; case 8: obj.Insert (f.Name, mStream.ReadInt64 ()); break; default: return null; } } else if (f.Type == mTypeManager.Boolean) { // boolean should not be here return null; } else if (f.Type == mTypeManager.String) { obj.Insert (f.Name, Encoding.UTF8.GetString (mStream.ReadBytes (size), 0, size)); } else if (f.Type == null) { // unknown type mStream.ReadBytes (size); } else { obj.Insert (f.Name, DecodeInternal (f.Type)); } } } return obj; }
private SpObject DecodeInternal(SpType type) { if (mStream == null || type == null) { return(null); } // buildin type decoding should not be here if (mTypeManager.IsBuildinType(type)) { return(null); } SpObject obj = new SpObject(); List <int> tags = new List <int> (); int current_tag = 0; short fn = mStream.ReadInt16(); for (short i = 0; i < fn; i++) { int value = (int)mStream.ReadUInt16(); if (value == 0) { tags.Add(current_tag); current_tag++; } else { if (value % 2 == 0) { SpField f = type.GetFieldByTag(current_tag); if (f == null) { return(null); } value = value / 2 - 1; if (f.Type == mTypeManager.Integer) { obj.Insert(f.Name, value); } else if (f.Type == mTypeManager.Boolean) { obj.Insert(f.Name, (value == 0 ? false : true)); } else { return(null); } current_tag++; } else { current_tag += (value + 1) / 2; } } } for (int c = 0; c < tags.Count; c++) { int tag = tags[c]; SpField f = type.GetFieldByTag(tag); if (f == null) { return(null); } if (f.IsTable) { int size = mStream.ReadInt32(); if (f.Type == mTypeManager.Integer) { byte n = mStream.ReadByte(); int count = (size - 1) / n; SpObject tbl = new SpObject(); for (int i = 0; i < count; i++) { switch (n) { case 4: tbl.Insert(i, mStream.ReadInt32()); break; case 8: tbl.Insert(i, mStream.ReadInt64()); break; default: return(null); } } obj.Insert(f.Name, tbl); } else if (f.Type == mTypeManager.Boolean) { SpObject tbl = new SpObject(); for (int i = 0; i < size; i++) { tbl.Insert(i, mStream.ReadBoolean()); } obj.Insert(f.Name, tbl); } else if (f.Type == mTypeManager.String) { SpObject tbl = new SpObject(); int k = 0; while (size > 0) { int str_len = mStream.ReadInt32(); size -= 4; tbl.Insert(k, Encoding.UTF8.GetString(mStream.ReadBytes(str_len), 0, str_len)); k++; size -= str_len; } obj.Insert(f.Name, tbl); } else if (f.Type == null) { // unknown type mStream.ReadBytes(size); } else { SpObject tbl = new SpObject(); int k = 0; while (size > 0) { int obj_len = mStream.ReadInt32(); size -= 4; SpObject o = DecodeInternal(f.Type); if (f.KeyName != null) { tbl.Insert(o.AsTable()[f.KeyName].Value, o); } else { tbl.Insert(k, o); } k++; size -= obj_len; } obj.Insert(f.Name, tbl); } } else { int size = mStream.ReadInt32(); if (f.Type == mTypeManager.Integer) { switch (size) { case 4: obj.Insert(f.Name, mStream.ReadInt32()); break; case 8: obj.Insert(f.Name, mStream.ReadInt64()); break; default: return(null); } } else if (f.Type == mTypeManager.Boolean) { // boolean should not be here return(null); } else if (f.Type == mTypeManager.String) { obj.Insert(f.Name, Encoding.UTF8.GetString(mStream.ReadBytes(size), 0, size)); } else if (f.Type == null) { // unknown type mStream.ReadBytes(size); } else { obj.Insert(f.Name, DecodeInternal(f.Type)); } } } return(obj); }
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); }
private SpObject CreateObject2 () { SpObject obj = new SpObject (); obj.Insert ("a", "hello"); obj.Insert ("b", 1000000); obj.Insert ("c", true); SpObject d = new SpObject (); d.Insert ("a", "world"); d.Insert ("c", -1); obj.Insert ("d", d); obj.Insert ("e", new SpObject (SpObject.ArgType.Array, "ABC", "def")); obj.Insert ("f", new SpObject (SpObject.ArgType.Array, -3, -2, -1, 0, 1, 2)); obj.Insert ("g", new SpObject (SpObject.ArgType.Array, true, false, true)); SpObject h = new SpObject (); { SpObject t = new SpObject (); t.Insert ("b", 100); h.Append (t); } { SpObject t = new SpObject (); t.Insert ("b", -100); t.Insert ("c", false); h.Append (t); } { SpObject t = new SpObject (); t.Insert ("b", 0); SpObject he = new SpObject (); he.Append ("test"); t.Insert ("e", he); h.Append (t); } obj.Insert ("h", h); return obj; }
public SpStream Response (int session, SpObject args) { SpObject header = new SpObject (); header.Insert ("session", session); SpStream encode_stream = new SpStream (); mHostTypeManager.Codec.Encode (mHeaderType, header, encode_stream); if (session != 0 && mSessions.ContainsKey (session)) { mHostTypeManager.Codec.Encode (mSessions[session].Response, args, encode_stream); } SpStream pack_stream = new SpStream (); encode_stream.Position = 0; SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; return pack_stream; }
private void TestUserInfo(){ Util.Log ("client request userinfo===================="); SpObject userinfo_request = new SpObject (); userinfo_request.Insert ("who", 3000); SpStream req = client.Request ("userinfo",userinfo_request,5); //Util.Assert (req.Length == 3); Util.Log ("request userinfo size ============"+req.Length); req.Position = 0; SpRpcResult dispath_result = server.Dispatch (req); //Util.Assert (dispath_result.Arg[]); Util.Log("respone args============="+dispath_result.Arg["who"].AsInt()); Util.DumpObject (dispath_result.Arg); Util.Log ("server respnse============================"); SpObject userinfo_response = new SpObject (); //ok 0 : integer //uid 1 : integer //rolename 2 : string //choserole 3 : integer //level 4 : integer //exp 5 : integer //gold 6 : integer userinfo_response.Insert ("ok",1); userinfo_response.Insert ("uid",3000); userinfo_response.Insert ("rolename","cutely"); userinfo_response.Insert ("choserole",1); userinfo_response.Insert ("level",1); userinfo_response.Insert ("exp",11); userinfo_response.Insert ("gold",8888); SpStream resp = server.Response (dispath_result.Session, userinfo_response); Util.Log ("response package size============"+resp.Length); Util.Log ("client dispatch"); resp.Position = 0; dispath_result = client.Dispatch (resp); Util.DumpObject (dispath_result.Arg); Util.Log ("response args==================================="); Util.Log ("state======="+dispath_result.Arg["ok"].AsInt()); Util.Log ("UID========="+dispath_result.Arg["uid"].AsInt()); Util.Log ("roleName===="+dispath_result.Arg["rolename"].AsString()); Util.Log ("choserole==="+dispath_result.Arg["choserole"].AsInt()); Util.Log ("level======="+dispath_result.Arg["level"].AsInt()); Util.Log ("exp========="+dispath_result.Arg["exp"].AsInt()); Util.Log ("gold========"+dispath_result.Arg["gold"].AsInt()); }