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 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)); } }
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 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); }
public double UnpackAndDecode() { double begin = GetMs(); for (int i = 0; i < BENCHUMARK_RUN_TIMES; i++) { encode_stream.Position = 0; pack_stream.Position = 0; unpack_stream.Position = 0; //manager.Codec.Encode ("AddressBook", obj, encode_stream); //SpPacker.Pack (encode_stream, pack_stream); SpPacker.Unpack(pack_stream, unpack_stream); unpack_stream.Position = 0; manager.Codec.Decode("AddressBook", unpack_stream); } double end = GetMs(); return((end - begin) / 1000); }
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); }
public void Run() { SpTypeManager manager = LoadProto(); // different ways to create SpObject Util.Log("Object Create Check"); CheckObj(CreateObject()); CheckObj(CreateObject2()); SpObject obj = CreateObject3(); CheckObj(obj); Util.Log("Encode"); SpStream small_stream = new SpStream(32); bool success = manager.Codec.Encode("foobar", obj, small_stream); Util.Assert(success == false); Util.Log("encode failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); SpStream encode_stream = manager.Codec.Encode("foobar", obj); encode_stream.Position = 0; Util.DumpStream(encode_stream); Util.Log("Decode "); encode_stream.Position = 0; SpObject ooo = manager.Codec.Decode("foobar", encode_stream); CheckObj(ooo); Util.Log("Pack"); encode_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Pack(encode_stream, small_stream); Util.Assert(success == false); Util.Log("pack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); SpStream pack_stream = SpPacker.Pack(encode_stream); pack_stream.Position = 0; Util.DumpStream(pack_stream); Util.Log("Unpack"); pack_stream.Position = 0; small_stream.Position = 0; success = SpPacker.Unpack(pack_stream, small_stream); Util.Assert(success == false); Util.Log("unpack failed! require size : " + small_stream.Position); small_stream.Position = 0; Util.DumpStream(small_stream); pack_stream.Position = 0; SpStream decode_stream = SpPacker.Unpack(pack_stream); decode_stream.Position = 0; Util.DumpStream(decode_stream); Util.Log("Decode "); decode_stream.Position = 0; SpObject newObj = manager.Codec.Decode("foobar", decode_stream); CheckObj(newObj); }