public unpack ( byte data, int len ) : byte[] | ||
data | byte | |
len | int | |
Résultat | byte[] |
public bool TryParse(byte[] data, out PackageHead head, out byte[] bodyBytes) { bodyBytes = new byte[0]; head = null; int pos = 0; if (data == null || data.Length == 0) { return(false); } Sproto.SprotoPack pack = new Sproto.SprotoPack(); var bin = pack.unpack(data); SprotoTypeDeserialize d = new SprotoTypeDeserialize(bin); SprotoTypeReader sReader = new SprotoTypeReader(bin, 0, bin.Length); SprotoType.package headpack = new SprotoType.package(); headpack.init(sReader); head = new PackageHead(); head.ActionId = headpack.protoId; head.MsgId = headpack.session; head.StatusCode = headpack.errorcode; if (sReader.Length - sReader.Position > 0) { bodyBytes = new byte[sReader.Length - sReader.Position]; sReader.Read(bodyBytes, 0, bodyBytes.Length); } return(true); }
protected override void ProcessStream(uint msgid, MemoryStream stream) { try { var meta = _metaSet.GetByID(msgid); if (meta != MessageMetaSet.NullMeta) { object msg = null; if (stream != null) { pCache[0] = spack.unpack(stream.ToArray()); msg = meta.type.GetConstructor(tCache).Invoke(pCache); } if (DebugMessage) { ReflectMessage(msgid, msg); } _dispatcher.Invoke(msgid, msg); } } catch (Exception e) { Debug.LogError(e.ToString()); } }
public override void run() { SprotoPack extract = new SprotoPack (); byte[] data = new byte[] { 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, }; byte[] result_pack_data = new byte[] { 0X41, 0X08, 0X04, 0X00, 0Xc4, 0X05, 0X68, 0X65, 0X8f, 0X6c, 0X6c, 0X6f, 0X04, 0X40, 0X8b, 0X42, 0X0f, 0X1b, 0X04, 0X22, 0X01, 0X05, 0Xe2, 0X05, 0X77, 0X6f, 0X72, 0Xc7, 0X6c, 0X64, 0X04, 0Xff, 0Xff, 0X47, 0Xff, 0Xff, 0X0e, 0X03, 0X3c, 0X41, 0X42, 0X43, 0X03, 0X1e, 0X64, 0X65, 0X66, 0X19, 0Xff, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0X1f, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X22, 0X01, 0X02, 0Xa2, 0X03, 0X01, 0X01, 0X11, 0X3e, 0X06, 0X55, 0X02, 0X01, 0Xca, 0X02, 0X10, 0X10, 0X45, 0X03, 0X01, 0X02, 0Xf1, 0X04, 0X9c, 0Xff, 0Xff, 0Xff, 0X51, 0X16, 0X04, 0X01, 0X45, 0X02, 0X03, 0X08, 0Xc4, 0X04, 0X74, 0X65, 0X03, 0X73, 0X74, }; byte[] result_unpack_data = new byte[] { 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, }; byte[] pack_data = extract.pack (data); dump_bytes (pack_data); assert (pack_data, result_pack_data); byte[] unpack_data = extract.unpack (pack_data); assert (unpack_data, result_unpack_data); byte[] data2 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 0, 7, 8, 1, 2, 3, 4, 5, 0, }; Console.WriteLine ("======pack======="); byte[] pack_data1 = extract.pack (data2); dump_bytes (pack_data1); Console.WriteLine ("=======unpack======"); byte[] unpack_data1 = extract.unpack (pack_data1); dump_bytes (unpack_data1); }
/// <summary> /// sproto消息 /// </summary> void OnSprotoMessage(ClientSession session, ByteBuffer buffer) { byte[] data = buffer.ReadBytes(); //------------------------------------------------------------- SprotoPack spack = new SprotoPack(); byte[] pack_data = spack.pack(data); // pack byte[] unpack_data = spack.unpack(pack_data); // unpack AddressBook addr = new AddressBook(unpack_data); // decode Console.WriteLine("OnSprotoMessage id=>>" + addr.person.Count); buffer.Close(); buffer = null; //------------------------------------------------------------- ByteBuffer newBuffer = new ByteBuffer(); newBuffer.WriteByte((byte)ProtocalType.SPROTO); newBuffer.WriteBytes(data); //添加数据 SocketUtil.SendMessage(session, Protocal.Login, newBuffer); }
public override void run() { Foobar obj = new Foobar (); obj.a = "hello"; obj.b = 1000000; obj.c = true; obj.d = new Foobar.Nest (); obj.d.a = "world"; obj.d.c = -1; obj.e = new System.Collections.Generic.List<string> {"ABC", "def"}; obj.f = new System.Collections.Generic.List<long> { -3, -2, -1, 0 , 1, 2}; obj.g = new System.Collections.Generic.List<bool> { true, false, true}; obj.h = new System.Collections.Generic.List<Foobar> (); Foobar tmp = new Foobar (); tmp.b = 100; obj.h.Add (tmp); obj.h.Add (new Foobar ()); tmp = new Foobar (); tmp.b = -100; tmp.c = false; obj.h.Add (tmp); tmp = new Foobar (); tmp.b = 0; tmp.e = new System.Collections.Generic.List<string> { "test" }; obj.h.Add (tmp); byte[] data = obj.encode (); byte[] result_data = new byte[] { 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, }; assert (data, result_data); Sproto.SprotoPack spack = new SprotoPack (); byte[] pack_data = spack.pack (data); byte[] result_pack_data = new byte[] { 0X41, 0X08, 0X04, 0X00, 0Xc4, 0X05, 0X68, 0X65, 0X8f, 0X6c, 0X6c, 0X6f, 0X04, 0X40, 0X8b, 0X42, 0X0f, 0X1b, 0X04, 0X22, 0X01, 0X05, 0Xe2, 0X05, 0X77, 0X6f, 0X72, 0Xc7, 0X6c, 0X64, 0X04, 0Xff, 0Xff, 0X47, 0Xff, 0Xff, 0X0e, 0X03, 0X3c, 0X41, 0X42, 0X43, 0X03, 0X1e, 0X64, 0X65, 0X66, 0X19, 0Xff, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0X1f, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X22, 0X01, 0X02, 0Xa2, 0X03, 0X01, 0X01, 0X11, 0X3e, 0X06, 0X55, 0X02, 0X01, 0Xca, 0X02, 0X10, 0X10, 0X45, 0X03, 0X01, 0X02, 0Xf1, 0X04, 0X9c, 0Xff, 0Xff, 0Xff, 0X51, 0X16, 0X04, 0X01, 0X45, 0X02, 0X03, 0X08, 0Xc4, 0X04, 0X74, 0X65, 0X03, 0X73, 0X74, }; assert (pack_data, result_pack_data); byte[] unpack_data = spack.unpack (pack_data); byte[] result_unpack_data = new byte[] { 0X08, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, }; assert (unpack_data, result_unpack_data); Foobar foo = new Foobar (unpack_data); assert (foo.a == "hello"); assert (foo.b == 1000000); assert (foo.c == true); assert (foo.d.a == "world"); assert (foo.d.c == -1); assert (foo.e [0] == "ABC"); assert (foo.e [1] == "def"); assert (foo.f, new Int64[] { -3, -2, -1, 0 , 1, 2}); assert (foo.g, new bool[] {true, false, true }); assert (foo.h [0].b == 100); assert (foo.h [2].b == -100); assert (foo.h [2].c == false); assert (foo.h [3].b == 0); assert (foo.h [3].e [0] == "test"); // test extend proto byte[] extend_data = new byte[] { 0X0a, 0X00, 0X00, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X70, 0X4a, 0X05, 0X00, 0X00, 0X00, 0X68, 0X65, 0X6c, 0X6c, 0X6f, 0X04, 0X00, 0X00, 0X00, 0X40, 0X42, 0X0f, 0X00, 0X1b, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X77, 0X6f, 0X72, 0X6c, 0X64, 0X04, 0X00, 0X00, 0X00, 0Xff, 0Xff, 0Xff, 0Xff, 0X0e, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X41, 0X42, 0X43, 0X03, 0X00, 0X00, 0X00, 0X64, 0X65, 0X66, 0X19, 0X00, 0X00, 0X00, 0X04, 0Xfd, 0Xff, 0Xff, 0Xff, 0Xfe, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X00, 0X00, 0X00, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X3e, 0X00, 0X00, 0X00, 0X06, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0Xca, 0X00, 0X02, 0X00, 0X00, 0X00, 0X00, 0X00, 0X10, 0X00, 0X00, 0X00, 0X03, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X04, 0X00, 0X00, 0X00, 0X9c, 0Xff, 0Xff, 0Xff, 0X16, 0X00, 0X00, 0X00, 0X04, 0X00, 0X01, 0X00, 0X02, 0X00, 0X03, 0X00, 0X00, 0X00, 0X08, 0X00, 0X00, 0X00, 0X04, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, 0X05, 0X00, 0X00, 0X00, 0X7a, 0X69, 0X78, 0X75, 0X6e, 0x95, 0x27 }; long size = foo.init (extend_data); Console.WriteLine ("szie: " + size); }