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); }
/// <summary> /// rebuild socket send hearbeat package /// </summary> public SocketPackage ReBuildHearbeat() { var writer = NetWriter.Instance; SprotoType.package headPack = new SprotoType.package() { protoId = 2, session = NetWriter.MsgId, errorcode = 0, }; Sproto.SprotoPack pack = new Sproto.SprotoPack(); byte[] headBytes = headPack.encode(); writer.SetHeadBuffer(headBytes); writer.SetBodyData(new byte[0]); var data = writer.PostData(); NetWriter.resetData(); _hearbeatPackage = data; SocketPackage package = new SocketPackage(); package.MsgId = (int)headPack.session; package.ActionId = 2; package.Action = ActionFactory.Create(package.ActionId); package.SendTime = DateTime.Now; package.HasLoading = false; return(package); }
public BenchMark() { AddressBook address = new AddressBook(); address.person = new System.Collections.Generic.List <Person> (); Person person = new Person(); person.name = "Alice"; person.id = 10000; person.phone = new System.Collections.Generic.List <Person.PhoneNumber> (); Person.PhoneNumber num1 = new Person.PhoneNumber(); num1.number = "123456789"; num1.type = 1; person.phone.Add(num1); Person.PhoneNumber num2 = new Person.PhoneNumber(); num2.number = "87654321"; num2.type = 2; person.phone.Add(num2); address.person.Add(person); Person person1 = new Person(); person1.name = "Bob"; person1.id = 20000; person1.phone = new System.Collections.Generic.List <Person.PhoneNumber> (); Person.PhoneNumber num3 = new Person.PhoneNumber(); num3.number = "01234567890"; num3.type = 3; person1.phone.Add(num3); address.person.Add(person1); byte[] data = address.encode(); Sproto.SprotoPack spack = new Sproto.SprotoPack(); // byte[] pack_data = spack.pack (data); Sproto.SprotoStream stream = new SprotoStream(); double b = this.cur_mseconds(); for (int i = 0; i < 1000000; i++) { address.init(data); // int len = address.encode (stream); // stream.Seek (0, System.IO.SeekOrigin.Begin); // spack.pack (stream.Buffer, len); // byte[] unpack_data = spack.unpack (pack_data); // address.init (unpack_data); } double e = this.cur_mseconds(); Console.WriteLine("total: " + (e - b) / 1000 + "s"); }
public BenchMark () { AddressBook address = new AddressBook (); address.person = new System.Collections.Generic.List<Person> (); Person person = new Person (); person.name = "Alice"; person.id = 10000; person.phone = new System.Collections.Generic.List<Person.PhoneNumber> (); Person.PhoneNumber num1 = new Person.PhoneNumber (); num1.number = "123456789"; num1.type = 1; person.phone.Add (num1); Person.PhoneNumber num2 = new Person.PhoneNumber (); num2.number = "87654321"; num2.type = 2; person.phone.Add (num2); address.person.Add (person); Person person1 = new Person (); person1.name = "Bob"; person1.id = 20000; person1.phone = new System.Collections.Generic.List<Person.PhoneNumber> (); Person.PhoneNumber num3 = new Person.PhoneNumber (); num3.number = "01234567890"; num3.type = 3; person1.phone.Add (num3); address.person.Add (person1); byte[] data = address.encode (); Sproto.SprotoPack spack = new Sproto.SprotoPack (); // byte[] pack_data = spack.pack (data); Sproto.SprotoStream stream = new SprotoStream (); double b = this.cur_mseconds (); for (int i = 0; i < 1000000; i++) { address.init (data); // int len = address.encode (stream); // stream.Seek (0, System.IO.SeekOrigin.Begin); // spack.pack (stream.Buffer, len); // byte[] unpack_data = spack.unpack (pack_data); // address.init (unpack_data); } double e = this.cur_mseconds (); Console.WriteLine ("total: " + (e - b)/1000 +"s"); }
public byte[] BuildHearbeatPackage() { var writer = NetWriter.Instance; SprotoType.package headPack = new SprotoType.package() { protoId = 2, session = NetWriter.MsgId, errorcode = 0, }; Sproto.SprotoPack pack = new Sproto.SprotoPack(); byte[] headBytes = headPack.encode(); writer.SetHeadBuffer(headBytes); writer.SetBodyData(new byte[0]); var data = writer.PostData(); NetWriter.resetData(); return(data); }
private byte[] GetDataBuffer() { if (_headBuffer == null || _headBuffer.Length == 0 || _bodyBuffer == null) { return(new byte[0]); } UInt16 headLength = (UInt16)_headBuffer.Length; UInt16 bodyLength = (UInt16)_bodyBuffer.Length; //加头长度,合并body //byte[] lenBytes = BitConverter.GetBytes(Converter.GetBigEndian(headLength)); byte[] buffer = new byte[headLength + bodyLength]; //Buffer.BlockCopy(lenBytes, 0, buffer, 0, lenBytes.Length); Buffer.BlockCopy(_headBuffer, 0, buffer, 0, _headBuffer.Length); Buffer.BlockCopy(_bodyBuffer, 0, buffer, _headBuffer.Length, _bodyBuffer.Length); Sproto.SprotoPack pack = new Sproto.SprotoPack(); buffer = pack.pack(buffer); return(buffer); }
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); }