public bool Request(string proto, SpObject args, int session, SpStream stream) { SpStream encode_stream = EncodeRequest(proto, args, session); encode_stream.Position = 0; return(SpPacker.Pack(encode_stream, stream)); }
public SpRpcResult() { Session = 0; Protocol = null; Op = SpRpcOp.Unknown; Arg = null; }
public SpRpcResult(int s, SpProtocol p, SpRpcOp o, SpObject a) { Session = s; Protocol = p; Op = o; Arg = a; }
void VerifySucess(SpObject sp) { SkynetLogger.Info(Channel.NetDevice, "is OnVerifySucess"); _client.StartHeartBeatService(); //TODO: 请求各模块信息 }
public void Append (SpObject obj) { if (IsTable () == false) { mType = ArgType.Table; mValue = new Dictionary<object, SpObject> (); } Insert (AsTable ().Count, obj); }
public void Insert (object key, SpObject obj) { if (IsTable () == false) { mType = ArgType.Table; mValue = new Dictionary<object, SpObject> (); } AsTable ()[key] = obj; }
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 static string DumpObject(SpObject obj, int tab) { string str = ""; if (obj != null) { if (obj.IsTable()) { str = GetTab(tab) + "<table>\n"; foreach (KeyValuePair <object, SpObject> entry in obj.AsTable()) { str += GetTab(tab + 1) + "<key : " + entry.Key + ">\n"; str += DumpObject(entry.Value, tab + 1); } } else if (obj.Value == null) { str = GetTab(tab) + "<null>\n"; } else { str = GetTab(tab) + obj.Value.ToString() + "\n"; } } return(str); }
public void Request(string proto, SpObject msg, Action <SpObject> action) { var spRes = Protocol.Pack(proto, _session, msg); _connector.RawSend(spRes.Buffer, spRes.Length); _session++; }
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); }
public SpStream Encode(SpType type, SpObject obj) { SpStream stream = new SpStream(); if (Encode(type, obj, stream) == false) { if (stream.IsOverflow()) { if (stream.Position > MAX_SIZE) { return(null); } int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream(size); if (Encode(type, obj, stream) == false) { return(null); } } else { return(null); } } return(stream); }
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 CheckObj(SpObject obj) { Util.DumpObject(obj); Util.Assert(obj["a"].AsString().Equals("hello")); Util.Assert(obj["b"].AsInt() == 1000000); Util.Assert(obj["c"].AsBoolean() == true); Util.Assert(obj["d"]["a"].AsString().Equals("world")); Util.Assert(obj["d"]["c"].AsInt() == -1); Util.Assert(obj["e"][0].AsString().Equals("ABC")); Util.Assert(obj["e"][1].AsString().Equals("def")); Util.Assert(obj["f"][0].AsInt() == -3); Util.Assert(obj["f"][1].AsInt() == -2); Util.Assert(obj["f"][2].AsInt() == -1); Util.Assert(obj["f"][3].AsInt() == 0); Util.Assert(obj["f"][4].AsInt() == 1); Util.Assert(obj["f"][5].AsInt() == 2); Util.Assert(obj["g"][0].AsBoolean() == true); Util.Assert(obj["g"][1].AsBoolean() == false); Util.Assert(obj["g"][2].AsBoolean() == true); Util.Assert(obj["h"][0]["b"].AsInt() == 100); Util.Assert(obj["h"][1]["b"].AsInt() == -100); Util.Assert(obj["h"][1]["c"].AsBoolean() == false); Util.Assert(obj["h"][2]["b"].AsInt() == 0); Util.Assert(obj["h"][2]["e"][0].AsString().Equals("test")); }
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); }
public void InvokeCallBack(int id, SpObject data) { if (!_callBackMap.ContainsKey(id)) { return; } _callBackMap[id].Invoke(data); }
public bool Encode (SpType type, SpObject obj, SpStream stream) { if (type == null || obj == null || stream == null) return false; mStream = stream; bool success = EncodeInternal (type, obj); return (success && stream.IsOverflow () == false); }
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; }
public void Insert(object key, SpObject obj) { if (IsTable() == false) { mType = ArgType.Table; mValue = new Dictionary <object, SpObject> (); } AsTable()[key] = obj; }
public void Append(SpObject obj) { if (IsTable() == false) { mType = ArgType.Table; mValue = new Dictionary <object, SpObject> (); } Insert(AsTable().Count, obj); }
public Benchmark () { manager = LoadProto (); obj = CreateObject (); manager.Codec.Encode ("AddressBook", obj, encode_stream); encode_stream.Position = 0; SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; SpPacker.Unpack (pack_stream, unpack_stream); }
public Benchmark() { manager = LoadProto(); obj = CreateObject(); manager.Codec.Encode("AddressBook", obj, encode_stream); encode_stream.Position = 0; SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; SpPacker.Unpack(pack_stream, unpack_stream); }
public void Request(string proto, SpObject msg, Action <SpObject> action) { if (_socket.ReadyState != WebSocketState.Open) { return; } _eventManager.AddCallBack(_session, action); var spStream = _protocol.Pack(proto, _session, msg); _socket.Send(spStream.Buffer, 0, spStream.Length); ++_session; }
public bool Encode(SpType type, SpObject obj, SpStream stream) { if (type == null || obj == null || stream == null) { return(false); } mStream = stream; bool success = EncodeInternal(type, obj); return(success && stream.IsOverflow() == false); }
public SpStream Pack(string proto, int session, SpObject args) { _stream.Reset(); if (proto != "heartbeat") { SkynetLogger.Info(Channel.NetDevice, "Send Request : " + proto + ", session : " + session);; } _rpc.Request(proto, args, session, _stream); return(_stream); }
public bool Request (string proto, SpObject args, int session, SpStream stream) { Util.Log ("requst input==================================="); Util.Log ("proto======================="+proto); Util.DumpObject (args); Util.DumpStream (stream); SpStream encode_stream = EncodeRequest (proto, args, session); Util.Log ("dump encode stream==============================="); Util.DumpStream (encode_stream); encode_stream.Position = 0; return SpPacker.Pack (encode_stream, stream); }
public static SpStream Pack(string proto, int session, SpObject args) { _stream.Reset(); SkynetLogger.Info(Channel.Udp, "Send Request : " + proto + ", session : " + session); _stream.Write(_session); _stream.Write(_secret); _rpc.Request(proto, args, session, _stream); return(_stream); }
public void SendRequest(string protocolName, SpObject spObject) { mSession++; SpStream spStream = new SpStream(); spStream.Write((short)0); mRpc.Request(protocolName, spObject, mSession, spStream); int len = spStream.Length - 2; spStream.Buffer[0] = (byte)((len >> 8) & 0xff); spStream.Buffer[1] = (byte)(len & 0xff); mSocket.BeginSend(spStream.Buffer, 0, spStream.Length, SocketFlags.None, new AsyncCallback(SendRequestCallback), this); }
public void InvokeOnEvent(string route, SpObject msg) { if (!_eventMap.ContainsKey(route)) { return; } var list = _eventMap[route]; foreach (var action in list) { action.Invoke(msg); } }
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(); } 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)); } 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)); } SpObject obj = mAttachTypeManager.Codec.Decode(protocol.Response, unpack_stream); return(new SpRpcResult(session, protocol, SpRpcOp.Response, obj)); } }
private void CheckObj(SpObject obj) { Util.DumpObject(obj); Util.Assert(obj["person"][10000]["id"].AsInt() == 10000); Util.Assert(obj["person"][10000]["name"].AsString().Equals("Alice")); Util.Assert(obj["person"][10000]["phone"][0]["type"].AsInt() == 1); Util.Assert(obj["person"][10000]["phone"][0]["number"].AsString().Equals("123456789")); Util.Assert(obj["person"][10000]["phone"][1]["type"].AsInt() == 2); Util.Assert(obj["person"][10000]["phone"][1]["number"].AsString().Equals("87654321")); Util.Assert(obj["person"][20000]["id"].AsInt() == 20000); Util.Assert(obj["person"][20000]["name"].AsString().Equals("Bob")); Util.Assert(obj["person"][20000]["phone"][0]["type"].AsInt() == 3); Util.Assert(obj["person"][20000]["phone"][0]["number"].AsString().Equals("01234567890")); }
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; }
public void Login(SpObject arg) { //read value from arg string token = arg["token"].Value.ToString(); string challenge = arg["chanllenge"].Value.ToString(); //use secert to encrypt token byte[] token_code = Convert.FromBase64String(token); byte[] secret_code = Convert.FromBase64String(secret); byte[] desencode_token_code = DES.Encode(secret_code,token_code); token = Convert.ToBase64String(desencode_token_code); //send sproto package Send("login",new SpObject(SpObject.ArgType.Table, "session",session, "token",token)); }
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 void Run() { SpTypeManager manager = LoadProto(); SpObject obj = CreateObject(); CheckObj(obj); Util.Log("Encode"); SpStream encode_stream = new SpStream(); manager.Codec.Encode("AddressBook", obj, encode_stream); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Decode"); encode_stream.Position = 0; SpObject newObj = manager.Codec.Decode("AddressBook", encode_stream); CheckObj(newObj); Util.Log("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream(); SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream(); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream(unpack_stream); Util.Log("Decode"); unpack_stream.Position = 0; newObj = manager.Codec.Decode("AddressBook", unpack_stream); CheckObj(newObj); }
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); }
private void Send(string proto, SpObject args) { mSendStream.Reset(); mSession++; Util.Log("Send Request : " + proto + ", session : " + mSession); if (args != null) { Util.DumpObject(args); } mSendStream.Write((short)0); mRpc.Request(proto, args, mSession, mSendStream); int len = mSendStream.Length - 2; mSendStream.Buffer[0] = (byte)((len >> 8) & 0xff); mSendStream.Buffer[1] = (byte)(len & 0xff); mSocket.Send(mSendStream.Buffer, mSendStream.Length, SocketFlags.None); }
private void TestStr(string s) { SpObject obj = new SpObject(SpObject.ArgType.Table, "a", s); Util.Log("------------------TestStr----------------------------"); Util.Log(s); Util.Log("Encode"); SpStream encode_stream = new SpStream(2); bool ret = manager.Codec.Encode("ss", obj, encode_stream); Util.Assert(ret == false); encode_stream = new SpStream(encode_stream.Position); ret = manager.Codec.Encode("ss", obj, encode_stream); Util.Assert(ret == true); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream(); SpPacker.Pack(encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream(); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream(unpack_stream); Util.Log("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode("ss", unpack_stream); string ds = dobj["a"].AsString(); Util.Log(ds); Util.Assert(s == ds); }
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; }
public SpStream Encode (SpType type, SpObject obj) { SpStream stream = new SpStream (); if (Encode (type, obj, stream) == false) { if (stream.IsOverflow ()) { if (stream.Position > MAX_SIZE) return null; int size = stream.Position; size = ((size + 7) / 8) * 8; stream = new SpStream (size); if (Encode (type, obj, stream) == false) return null; } else { return null; } } return stream; }
private static string DumpObject (SpObject obj, int tab) { string str = ""; if (obj != null) { if (obj.IsTable ()) { str = GetTab (tab) + "<table>\n"; foreach (KeyValuePair<object, SpObject> entry in obj.AsTable ()) { str += GetTab (tab + 1) + "<key : " + entry.Key + ">\n"; str += DumpObject (entry.Value, tab + 1); } } else if (obj.Value == null) { str = GetTab (tab) + "<null>\n"; } else { str = GetTab (tab) + obj.Value.ToString () + "\n"; } } return str; }
private void TestStr (string s) { SpObject obj = new SpObject (SpObject.ArgType.Table, "a", s); Util.Log ("------------------TestStr----------------------------"); Util.Log (s); Util.Log ("Encode"); SpStream encode_stream = new SpStream (2); bool ret = manager.Codec.Encode ("ss", obj, encode_stream); Util.Assert (ret == false); encode_stream = new SpStream (encode_stream.Position); ret = manager.Codec.Encode ("ss", obj, encode_stream); Util.Assert (ret == true); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream (); SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream (); SpPacker.Unpack (pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream (unpack_stream); Util.Log ("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode ("ss", unpack_stream); string ds = dobj["a"].AsString (); Util.Log (ds); Util.Assert (s == ds); }
private bool EncodeInternal (SpType type, SpObject obj) { if (mStream == null || type == null || obj == null) { return false; } // buildin type decoding should not be here if (mTypeManager.IsBuildinType (type)) { return false; } int begin = mStream.Position; // fn. will be update later short fn = 0; mStream.Write (fn); List<KeyValuePair<SpObject, SpField>> objs = new List<KeyValuePair<SpObject, SpField>> (); int current_tag = -1; Dictionary<int, SpField>.ValueCollection.Enumerator en = type.Fields.Values.GetEnumerator (); while (en.MoveNext ()) { SpField f = en.Current; if (f == null) { return false; } SpObject o = obj[f.Name]; if (o == null || IsTypeMatch (f, o) == false) continue; if (f.Tag <= current_tag) { return false; } if (f.Tag - current_tag > 1) { mStream.Write ((short)(2 * (f.Tag - current_tag - 1) - 1)); fn++; } bool standalone = true; if (f.IsTable == false) { if (f.Type == mTypeManager.Boolean) { int value = o.AsBoolean () ? 1 : 0; mStream.Write ((short)((value + 1) * 2)); standalone = false; } else if (f.Type == mTypeManager.Integer) { int value = o.AsInt (); if (value >= 0 && value < 0x7fff) { mStream.Write ((short)((value + 1) * 2)); standalone = false; } } } if (standalone) { objs.Add (new KeyValuePair<SpObject, SpField> (o, f)); mStream.Write ((short)0); } fn++; current_tag = f.Tag; } List<KeyValuePair<SpObject, SpField>>.Enumerator e = objs.GetEnumerator (); while (e.MoveNext ()) { KeyValuePair<SpObject, SpField> entry = e.Current; if (entry.Value.IsTable) { int array_begin = mStream.Position; int size = 0; mStream.Write (size); if (entry.Value.Type == mTypeManager.Integer) { byte len = 4; Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator (); while (enumerator.MoveNext ()) { SpObject o = enumerator.Current.Value; if (o.IsLong ()) { len = 8; break; } } mStream.Write (len); enumerator = entry.Key.AsTable ().GetEnumerator (); while (enumerator.MoveNext ()) { SpObject o = enumerator.Current.Value; if (len == 4) { mStream.Write (o.AsInt ()); } else { mStream.Write (o.AsLong ()); } } } else if (entry.Value.Type == mTypeManager.Boolean) { Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator (); while (enumerator.MoveNext ()) { SpObject o = enumerator.Current.Value; mStream.Write ((byte)(o.AsBoolean () ? 1 : 0)); } } else if (entry.Value.Type == mTypeManager.String) { Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator (); while (enumerator.MoveNext ()) { SpObject o = enumerator.Current.Value; byte[] b = Encoding.UTF8.GetBytes (o.AsString ()); mStream.Write (b.Length); mStream.Write (b); } } else { Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator (); while (enumerator.MoveNext ()) { SpObject o = enumerator.Current.Value; int obj_begin = mStream.Position; int obj_size = 0; mStream.Write (obj_size); if (EncodeInternal (entry.Value.Type, o) == false) { return false; } int obj_end = mStream.Position; obj_size = (int)(obj_end - obj_begin - 4); mStream.Position = obj_begin; mStream.Write (obj_size); mStream.Position = obj_end; } } int array_end = mStream.Position; size = (int)(array_end - array_begin - 4); mStream.Position = array_begin; mStream.Write (size); mStream.Position = array_end; } else { if (entry.Key.IsString ()) { byte[] b = Encoding.UTF8.GetBytes (entry.Key.AsString ()); mStream.Write (b.Length); mStream.Write (b); } else if (entry.Key.IsInt ()) { mStream.Write ((int)4); mStream.Write (entry.Key.AsInt ()); } else if (entry.Key.IsLong ()) { mStream.Write ((int)8); mStream.Write (entry.Key.AsLong ()); } else if (entry.Key.IsBoolean ()) { // boolean should not be here return false; } else { int obj_begin = mStream.Position; int obj_size = 0; mStream.Write (obj_size); if (EncodeInternal (entry.Value.Type, entry.Key) == false) { return false; } int obj_end = mStream.Position; obj_size = (int)(obj_end - obj_begin - 4); mStream.Position = obj_begin; mStream.Write (obj_size); mStream.Position = obj_end; } } } int end = mStream.Position; mStream.Position = begin; mStream.Write (fn); mStream.Position = end; return true; }
public static void DumpObject (SpObject obj) { Log (DumpObject (obj, 0)); }
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; }
public SpRpcResult () { Session = 0; Protocol = null; Op = SpRpcOp.Unknown; Arg = null; }
public SpRpcResult (int s, SpProtocol p, SpRpcOp o, SpObject a) { Session = s; Protocol = p; Op = o; Arg = a; }
public SpStream Encode (string proto, SpObject obj) { return Encode (mTypeManager.GetType (proto), 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); }
public SpStream Request (string proto, SpObject args, int session) { SpStream encode_stream = EncodeRequest (proto, args, session); encode_stream.Position = 0; return SpPacker.Pack (encode_stream); }
public bool Encode (string proto, SpObject obj, byte[] buffer, int offset, int size) { return Encode (proto, obj, new SpStream (buffer, offset, size)); }
public bool Encode (string proto, SpObject obj, byte[] buffer) { return Encode (proto, obj, buffer, 0, buffer.Length); }
private void TestNest () { SpObject clist = new SpObject (SpObject.ArgType.Table, "character", new SpObject (SpObject.ArgType.Array, new SpObject (SpObject.ArgType.Table, "id", 1, "attribute", new SpObject (SpObject.ArgType.Table, "level", 1, "templateId", 1001, "ability", new SpObject (SpObject.ArgType.Table, "hp", 2530, "att", 2310 ) ) ), new SpObject (SpObject.ArgType.Table, "id", 2, "attribute", new SpObject (SpObject.ArgType.Table, "level", 1, "templateId", 1002, "ability", new SpObject (SpObject.ArgType.Table, "hp", 1320, "att", 2090 ) ) ) ) ); Util.Log ("------------------TEST CHARACTER----------------------------"); Util.DumpObject (clist); Util.Log ("Encode"); SpStream encode_stream = new SpStream (); manager.Codec.Encode ("clist", clist, encode_stream); encode_stream.Position = 0; Util.DumpStream (encode_stream); Util.Log ("Pack"); encode_stream.Position = 0; SpStream pack_stream = new SpStream (); SpPacker.Pack (encode_stream, pack_stream); pack_stream.Position = 0; Util.DumpStream (pack_stream); Util.Log ("Unpack"); pack_stream.Position = 0; SpStream unpack_stream = new SpStream (); SpPacker.Unpack (pack_stream, unpack_stream); unpack_stream.Position = 0; Util.DumpStream (unpack_stream); Util.Log ("Decode"); unpack_stream.Position = 0; SpObject dobj = manager.Codec.Decode ("clist", unpack_stream); Util.DumpObject (dobj); }
private void CheckObj (SpObject obj) { Util.DumpObject (obj); Util.Assert (obj["person"][10000]["id"].AsInt () == 10000); Util.Assert (obj["person"][10000]["name"].AsString ().Equals ("Alice")); Util.Assert (obj["person"][10000]["phone"][0]["type"].AsInt () == 1); Util.Assert (obj["person"][10000]["phone"][0]["number"].AsString ().Equals ("123456789")); Util.Assert (obj["person"][10000]["phone"][1]["type"].AsInt () == 2); Util.Assert (obj["person"][10000]["phone"][1]["number"].AsString ().Equals ("87654321")); Util.Assert (obj["person"][20000]["id"].AsInt () == 20000); Util.Assert (obj["person"][20000]["name"].AsString ().Equals ("Bob")); Util.Assert (obj["person"][20000]["phone"][0]["type"].AsInt () == 3); Util.Assert (obj["person"][20000]["phone"][0]["number"].AsString ().Equals ("01234567890")); }
public bool Encode (string proto, SpObject obj, SpStream stream) { return Encode (mTypeManager.GetType (proto), obj, stream); }
public SpStream Request (string proto, SpObject args) { return Request (proto, args, 0); }
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 bool IsTypeMatch (SpField f, SpObject o) { if (f == null || f.Type == null || o == null) return false; if (f.IsTable && o.IsTable ()) { return true; } else if (f.Type == mTypeManager.String) { if (o.IsString ()) return true; } else if (f.Type == mTypeManager.Boolean) { if (o.IsBoolean ()) return true; } else if (f.Type == mTypeManager.Integer) { if (o.IsInt () || o.IsLong ()) return true; } else if (o.IsTable ()) { return true; } return false; }