public override int encode (SprotoStream stream) { base.serialize.open (stream); if (base.has_field.has_field (0)) { base.serialize.write_obj (this.person, 0); } return base.serialize.close (); }
private void test_array() { /* * .Test { * var1 0: *boolean * var2 4: *integer * var3 5: *string * } * */ SprotoTypeSerialize serialize = new SprotoTypeSerialize (20); List<Int64> data = new List<Int64> (); data.Add (4); data.Add (0x1123); data.Add (0x1122334455); data.Add (-0x778899aabb); data.Add (-6); List<bool> b_data = new List<bool> (); b_data.Add (true); b_data.Add (false); b_data.Add (true); List<string> str_data = new List<string> (); str_data.Add ("abc"); str_data.Add ("1234"); str_data.Add ("fgcbvb"); byte[] test_result_data = { 0X04, 0X00, 0X00, 0X00, 0X05, 0X00, 0X00, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X01, 0X00, 0X01, 0X29, 0X00, 0X00, 0X00, 0X08, 0X04, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X23, 0X11, 0X00, 0X00, 0X00, 0X00, 0X00, 0X00, 0X55, 0X44, 0X33, 0X22, 0X11, 0X00, 0X00, 0X00, 0X45, 0X55, 0X66, 0X77, 0X88, 0Xff, 0Xff, 0Xff, 0Xfa, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0Xff, 0X19, 0X00, 0X00, 0X00, 0X03, 0X00, 0X00, 0X00, 0X61, 0X62, 0X63, 0X04, 0X00, 0X00, 0X00, 0X31, 0X32, 0X33, 0X34, 0X06, 0X00, 0X00, 0X00, 0X66, 0X67, 0X63, 0X62, 0X76, 0X62, }; SprotoStream stream = new SprotoStream (); serialize.open (stream); serialize.write_boolean (b_data, 0); serialize.write_integer (data, 4); serialize.write_string (str_data, 5); int len = serialize.close (); byte[] buffer = new byte[len]; stream.Seek (0, System.IO.SeekOrigin.Begin); stream.Read (buffer, 0, len); Console.Write ("====== array dump ========"); dump_bytes (buffer); assert(buffer, test_result_data); }
public byte[] encode () { SprotoStream stream = new SprotoStream (); this.encode (stream); int len = stream.Position; byte[] buffer = new byte[len]; stream.Seek (0, SeekOrigin.Begin); stream.Read (buffer, 0, len); return buffer; }
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"); }
private void WriteUInt64(SprotoStream writer, UInt64 v) { //Console.WriteLine("WriteUInt64: {0}",v); writer.WriteByte((byte)(v & 0xff)); writer.WriteByte((byte)((v >> 8) & 0xff)); writer.WriteByte((byte)((v >> 16) & 0xff)); writer.WriteByte((byte)((v >> 24) & 0xff)); writer.WriteByte((byte)((v >> 32) & 0xff)); writer.WriteByte((byte)((v >> 40) & 0xff)); writer.WriteByte((byte)((v >> 48) & 0xff)); writer.WriteByte((byte)((v >> 56) & 0xff)); }
private List <string> DecodeStringList(SprotoStream reader) { List <string> list = new List <string>(); List <byte[]> bytes_list = this.DecodeBinaryList(reader); for (int i = 0; i < bytes_list.Count; i++) { byte[] bytes = bytes_list[i]; list.Add(Encoding.UTF8.GetString(bytes)); } return(list); }
public byte[] encode() { SprotoStream stream = new SprotoStream(); this.encode(stream); int len = stream.Position; byte[] buffer = new byte[len]; stream.Seek(0, SeekOrigin.Begin); stream.Read(buffer, 0, len); return(buffer); }
public override int encode (SprotoStream stream) { base.serialize.open (stream); if (base.has_field.has_field (0)) { base.serialize.write_integer (this.type, 0); } if (base.has_field.has_field (1)) { base.serialize.write_integer (this.session, 1); } return base.serialize.close (); }
private UInt64 ReadUInt64(SprotoStream reader) { UInt64 number; int len = 8; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = reader.ReadByte(); } number = BitConverter.ToUInt64(bytes, 0); //Console.WriteLine("ReadUInt64: {0}",number); return(number); }
private UInt32 PeekUInt32(SprotoStream reader) { UInt32 number; int len = 4; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = reader.Buffer[reader.Position + i]; } number = BitConverter.ToUInt32(bytes, 0); //Console.WriteLine("PeekUInt32: {0}",number); return(number); }
private UInt16 ReadUInt16(SprotoStream reader) { // little-endian UInt16 number; int len = 2; byte[] bytes = new byte[len]; for (int i = 0; i < len; i++) { bytes[i] = reader.ReadByte(); } number = BitConverter.ToUInt16(bytes, 0); //Console.WriteLine("ReadUInt16: {0}",number); return(number); }
private UInt32 EncodeBinaryList(List <byte[]> binary_list, SprotoStream writer) { UInt32 size = 0; for (int i = 0; i < binary_list.Count; i++) { byte[] binary = binary_list[i]; int length_pos = writer.Position; writer.Seek(SprotoCodec.SIZEOF_LENGTH, SprotoStream.SEEK_CUR); UInt32 length = this.EncodeBinary(binary, writer); this.FillSize(writer, length_pos, length); size += (SprotoCodec.SIZEOF_LENGTH + length); } return(size); }
private UInt32 EncodeStringList(List <string> string_list, SprotoStream writer) { UInt32 size = 0; for (int i = 0; i < string_list.Count; i++) { string str = string_list[i]; int length_pos = writer.Position; writer.Seek(SprotoCodec.SIZEOF_LENGTH, SprotoStream.SEEK_CUR); UInt32 length = this.EncodeString(str, writer); this.FillSize(writer, length_pos, length); size += (SprotoCodec.SIZEOF_LENGTH + length); } return(size); }
private List <bool> DecodeBooleanList(SprotoStream reader) { List <bool> list = new List <bool>(); UInt32 size = this.ReadUInt32(reader); for (; size > 0; size--) { bool ok = this.DecodeBoolean(reader); list.Add(ok); } if (size != 0) { SprotoHelper.Error("[SprotoCodec.DecodeBooleanList] fail"); } return(list); }
public SprotoObject Decode(SprotoMgr sprotomgr, string typename, SprotoStream reader) { if (SprotoHelper.IsBuildInType(typename)) { SprotoHelper.Error("[SprotoCodec.Decode] expect a 'non-buildin-type' got '{0}'", typename); } SprotoType type = sprotomgr.GetType(typename); if (null == type) { SprotoHelper.Error("[SprotoCodec.Decode] occur a unknow-type '{0}'", typename); } SprotoObject obj = this.DecodeSprotoObject(sprotomgr, type, reader); return(obj); }
public override int encode (SprotoStream stream) { base.serialize.open (stream); if (base.has_field.has_field (0)) { base.serialize.write_string (this.a, 1); } if (base.has_field.has_field (1)) { base.serialize.write_boolean (this.b, 3); } if (base.has_field.has_field (2)) { base.serialize.write_integer (this.c, 5); } return base.serialize.close (); }
private UInt32 EncodeInteger(Int64 integer, SprotoStream writer) { UInt32 sizeof_uint32 = sizeof(UInt32); UInt32 sizeof_integer = this.RealSizeOfInteger(integer); if (sizeof_integer == sizeof_uint32) { UInt32 number = (UInt32)integer; this.WriteUInt32(writer, number); } else { UInt64 number = (UInt64)integer; this.WriteUInt64(writer, number); } return(sizeof_integer); }
public static SprotoMgr ParseFromBinaryFile(string filename) { FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read); SprotoStream ms = new SprotoStream(); byte[] buf = new byte[1024]; int len = stream.Read(buf, 0, buf.Length); while (len > 0) { ms.Write(buf, 0, buf.Length); len = stream.Read(buf, 0, buf.Length); } stream.Close(); byte[] bytes = ms.Buffer; int length = (int)ms.Position; return(SprotoParser.ParseFromBinary(bytes, length)); }
private void test_field() { SprotoTypeSerialize serialize = new SprotoTypeSerialize (20); /* * .Test { * var1 0: integer * var2 1: string * var3 5: intger * var4 7: boolean * var5 9: integer * var6 10: boolean * var7 12: intger * } * */ byte[] test_result_data = { 0X0b, 0X00, 0X00, 0X00, 0X00, 0X00, 0X05, 0X00, 0Xac, 0X88, 0X01, 0X00, 0X04, 0X00, 0X01, 0X00, 0X00, 0X00, 0X02, 0X00, 0X01, 0X00, 0X46, 0X22, 0X04, 0X00, 0X00, 0X00, 0Xde, 0Xff, 0Xff, 0Xff, 0X0b, 0X00, 0X00, 0X00, 0X74, 0X65, 0X73, 0X74, 0X5f, 0X73, 0X74, 0X72, 0X69, 0X6e, 0X67, 0X08, 0X00, 0X00, 0X00, 0X66, 0X55, 0X44, 0X33, 0X22, 0X11, 0X00, 0X00 }; SprotoStream stream = new SprotoStream (); serialize.open (stream); serialize.write_integer (-34, 0); serialize.write_string ("test_string", 1); serialize.write_integer (0x4455, 5); serialize.write_boolean (true, 7); serialize.write_integer (0x112233445566, 9); serialize.write_boolean (false, 10); serialize.write_integer (0x1122, 12); int len = serialize.close (); byte[] buffer = new byte[len]; stream.Seek (0, System.IO.SeekOrigin.Begin); stream.Read (buffer, 0, len); Console.WriteLine ("======== encode buffer ==========="); dump_bytes (buffer); assert(buffer, test_result_data); }
private Int64 DecodeInteger(SprotoStream reader) { UInt32 sizeof_integer = this.ReadUInt32(reader); Int64 integer; if (sizeof_integer == 4) { Int32 number = (Int32)this.ReadUInt32(reader); integer = (Int64)number; } else { if (sizeof_integer != 8) { SprotoHelper.Error("[SprotoCodec.DecodeInteger] invalid integer size '{0}'", sizeof_integer); } integer = (Int64)this.ReadUInt64(reader); } return(integer); }
public SprotoStream Encode(SprotoMgr sprotomgr, SprotoObject obj, SprotoStream writer = null) { if (null == writer) { writer = new SprotoStream(); } string typename = obj.type; if (SprotoHelper.IsBuildInType(typename)) { SprotoHelper.Error("[SprotoCodec.Encode] expect a 'non-buildin-type sprotoobj' got '{0}'", typename); } SprotoType type = sprotomgr.GetType(typename); if (null == type) { SprotoHelper.Error("[SprotoCodec.Encode] occur a unknow-type '{0}'", typename); } this.EncodeSprotoObject(sprotomgr, type, obj, writer); return(writer); }
private List <byte[]> DecodeBinaryList(SprotoStream reader) { List <byte[]> list = new List <byte[]>(); UInt32 size = this.ReadUInt32(reader); while (size > 0) { byte[] bytes = this.DecodeBinary(reader); UInt32 elem_size = (UInt32)(bytes.Length + SprotoCodec.SIZEOF_LENGTH); size = size - elem_size; if (size < 0) { SprotoHelper.Error("[SprotoCodec.DecodeBinaryList] fail"); } list.Add(bytes); } if (size != 0) { SprotoHelper.Error("[SprotoCodec.DecodeBinaryList] fail"); } return(list); }
public RpcPackage PackResponse(string proto, SprotoObject response = null, Int64 session = 0, SprotoObject ud = null) { //Console.WriteLine("PackResponse {0} {1} {2}",proto,response,session); SprotoProtocol protocol = this.S2C.GetProtocol(proto); SprotoObject header = this.NewPackageHeader(this.S2C, 0, session, ud); this.writer.Seek(0, SprotoStream.SEEK_BEGIN); // clear stream SprotoStream writer = this.S2C.Encode(header, this.writer); if (response != null) { string expect = protocol.response; if (response.type != expect) { SprotoHelper.Error("[SprotoRpc.Response] expect '{0}' got '{1}'", expect, response.type); } writer = this.S2C.Encode(response, writer); } RpcPackage package = new RpcPackage(); package.data = this.S2C.Pack(writer.Buffer, 0, writer.Position, out package.size); return(package); }
public abstract int encode(SprotoStream stream);
private UInt32 EncodeSprotoObjectList(SprotoMgr sprotomgr, List <SprotoObject> list, SprotoStream writer) { UInt32 size = 0; for (int i = 0; i < list.Count; i++) { SprotoObject elemobj = list[i]; SprotoType elemtype = sprotomgr.GetType(elemobj.type); int elemsize_pos = writer.Position; writer.Seek(SprotoCodec.SIZEOF_LENGTH, SprotoStream.SEEK_CUR); UInt32 elemsize = this.EncodeSprotoObject(sprotomgr, elemtype, elemobj, writer); this.FillSize(writer, elemsize_pos, elemsize); size += (SprotoCodec.SIZEOF_LENGTH + elemsize); } return(size); }
public byte[] PackEncode(SprotoObject obj, out int size, SprotoStream writer = null) { SprotoStream stream = this.Encode(obj, writer); return(this.Pack(stream.Buffer, 0, stream.Position, out size)); }
public SprotoObject Decode(string typename, SprotoStream reader) { return(this.Codec.Decode(this, typename, reader)); }
public SprotoStream Encode(SprotoObject obj, SprotoStream writer = null) { return(this.Codec.Encode(this, obj, writer)); }
private List <SprotoObject> DecodeSprotoObjectList(SprotoMgr sprotomgr, SprotoType type, SprotoStream reader) { List <SprotoObject> list = new List <SprotoObject>(); UInt32 size = this.ReadUInt32(reader); while (size > 0) { UInt32 elem_size = this.ReadUInt32(reader); UInt32 need_size = elem_size + SprotoCodec.SIZEOF_LENGTH; size = size - need_size; if (size < 0) { SprotoHelper.Error("[SprotoCodec.DecodeSprotoObjectList] fail"); } SprotoObject elemobj = this.DecodeSprotoObject(sprotomgr, type, reader); list.Add(elemobj); } if (size != 0) { SprotoHelper.Error("[SprotoCodec.DecodeSprotoObjectList] fail"); } return(list); }
private UInt32 EncodeString(string str, SprotoStream writer) { byte[] bytes = Encoding.UTF8.GetBytes(str); return(this.EncodeBinary(bytes, writer)); }
private bool DecodeBoolean(SprotoStream reader) { byte b = reader.ReadByte(); return(Convert.ToBoolean(b)); }
public override int encode (SprotoStream stream) { base.serialize.open (stream); if (base.has_field.has_field (0)) { base.serialize.write_string (this.a, 0); } if (base.has_field.has_field (1)) { base.serialize.write_integer (this.b, 1); } if (base.has_field.has_field (2)) { base.serialize.write_boolean (this.c, 2); } if (base.has_field.has_field (3)) { base.serialize.write_obj (this.d, 3); } if (base.has_field.has_field (4)) { base.serialize.write_string (this.e, 4); } if (base.has_field.has_field (5)) { base.serialize.write_integer (this.f, 5); } if (base.has_field.has_field (6)) { base.serialize.write_boolean (this.g, 6); } if (base.has_field.has_field (7)) { base.serialize.write_obj (this.h, 7); } return base.serialize.close (); }
private void test_array() { /* * .Test { * var1 0: *boolean * var2 4: *integer * var3 5: *string * } * */ SprotoTypeSerialize serialize = new SprotoTypeSerialize (20); List<Int64> data = new List<Int64> (); data.Add (4); data.Add (0x1123); data.Add (0x1122334455); data.Add (-0x778899aabb); data.Add (-6); List<bool> b_data = new List<bool> (); b_data.Add (true); b_data.Add (false); b_data.Add (true); List<string> str_data = new List<string> (); str_data.Add ("中文显示"); str_data.Add ("1234"); str_data.Add ("fgcbvb"); byte[] test_result_data = { 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x29, 0x00, 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x00, 0x00, 0x45, 0x55, 0x66, 0x77, 0x88, 0xff, 0xff, 0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x22, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0xe4, 0xb8, 0xad, 0xe6, 0x96, 0x87, 0xe6, 0x98, 0xbe, 0xe7, 0xa4, 0xba, 0x04, 0x00, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0x06, 0x00, 0x00, 0x00, 0x66, 0x67, 0x63, 0x62, 0x76, 0x62, }; SprotoStream stream = new SprotoStream (); serialize.open (stream); serialize.write_boolean (b_data, 0); serialize.write_integer (data, 4); serialize.write_string (str_data, 5); int len = serialize.close (); byte[] buffer = new byte[len]; stream.Seek (0, System.IO.SeekOrigin.Begin); stream.Read (buffer, 0, len); Console.Write ("====== array dump ========"); dump_bytes (buffer); assert(buffer, test_result_data); }
private string DecodeString(SprotoStream reader) { byte[] bytes = this.DecodeBinary(reader); return(Encoding.UTF8.GetString(bytes)); }
public override int encode (SprotoStream stream) { base.serialize.open (stream); return base.serialize.close (); }
public override int encode (SprotoStream stream) { base.serialize.open (stream); if (base.has_field.has_field (0)) { base.serialize.write_string (this.name, 0); } if (base.has_field.has_field (1)) { base.serialize.write_integer (this.id, 1); } if (base.has_field.has_field (2)) { base.serialize.write_obj (this.d1, 2); } if (base.has_field.has_field (3)) { base.serialize.write_obj (this.d2, 3); } if (base.has_field.has_field (4)) { base.serialize.write_obj (this.d3, 4); } if (base.has_field.has_field (5)) { base.serialize.write_obj (this.a1, 5); } return base.serialize.close (); }
private UInt32 EncodeSprotoObjectDict <T> (SprotoMgr sprotomgr, Dictionary <T, SprotoObject> dict, SprotoField field, SprotoStream writer) { UInt32 size = 0; List <T> keys = new List <T>(dict.Keys); keys.Sort(); // keep encode stable foreach (var key in keys) { SprotoObject elemobj = dict[key]; if (elemobj.Get(field.key) == null) { SprotoHelper.Error("[SprotoCodec.EncodeSprotoObjectDict] exist null mainindex '{0}' in field '{1}'", field.key, field.name); } SprotoType elemtype = sprotomgr.GetType(elemobj.type); int elemsize_pos = writer.Position; writer.Seek(SprotoCodec.SIZEOF_LENGTH, SprotoStream.SEEK_CUR); UInt32 elemsize = this.EncodeSprotoObject(sprotomgr, elemtype, elemobj, writer); this.FillSize(writer, elemsize_pos, elemsize); size += (SprotoCodec.SIZEOF_LENGTH + elemsize); } return(size); }
public abstract int encode (SprotoStream stream);
// Parser from binary(*.spb) private static void _ParseFromBinary(SprotoMgr sprotomgr, byte[] bytes, int length) { SprotoMgr meta = SprotoParser.Parse(meta_proto); SprotoStream reader = new SprotoStream(); reader.Write(bytes, 0, length); reader.Seek(0, SprotoStream.SEEK_BEGIN); SprotoObject group = meta.Decode("group", reader); List <SprotoObject> types = null; List <SprotoObject> protocols = null; if (group.Get("type") != null) { types = group.Get("type"); foreach (SprotoObject meta_type in types) { SprotoType type = new SprotoType(); type.name = meta_type["name"]; if (meta_type.Has("fields")) { List <SprotoObject> fields = meta_type["fields"]; foreach (SprotoObject meta_field in fields) { SprotoField field = new SprotoField(); field.name = meta_field["name"]; field.tag = (UInt16)meta_field["tag"]; field.is_array = false; if (meta_field.Has("array")) { field.is_array = (bool)meta_field["array"]; } Int64 type_id; if (meta_field.Has("buildin")) { type_id = meta_field["buildin"]; field.type = SprotoParser.BuildInTypeId2Name[type_id]; if (type_id == 0 && meta_field.Has("type")) { // fixed float field.digit = (UInt16)meta_field["type"]; } else if (type_id == 2 && meta_field.Has("type")) { // binary field.type = "binary"; } } else { type_id = meta_field["type"]; SprotoObject t = types[(int)type_id]; field.type = t["name"]; // map if (meta_field.Has("key")) { SprotoHelper.Assert(field.is_array); UInt16 map_index = (UInt16)meta_field["key"]; List <SprotoObject> t_fields = t["fields"]; string name = null; foreach (SprotoObject f in t_fields) { if (f["tag"] == map_index) { name = f["name"]; break; } } SprotoHelper.Assert(name != null, String.Format("map index {0} cann't find in type '{1}'", map_index, field.type)); field.key = name; } } type.AddField(field); } } sprotomgr.AddType(type); } } if (group.Get("protocol") != null) { protocols = group.Get("protocol"); foreach (SprotoObject meta_protocol in protocols) { SprotoProtocol protocol = new SprotoProtocol(); protocol.name = meta_protocol["name"]; protocol.tag = (UInt16)meta_protocol["tag"]; if (meta_protocol["request"] != null) { Int64 request = meta_protocol["request"]; protocol.request = types[(int)request]["name"]; } if (meta_protocol["response"] != null) { Int64 response = meta_protocol["response"]; protocol.response = types[(int)response]["name"]; } bool confirm = meta_protocol["confirm"]; if (confirm) { protocol.response = null; } sprotomgr.AddProtocol(protocol); } } }
private object DecodeBuildInType(SprotoField field, SprotoStream reader) { object obj = null; switch (field.type) { case "integer": if (field.is_array) { List <Int64> integer_list = this.DecodeIntegerList(reader); if (field.digit == 0) { obj = integer_list; } else { List <double> double_list = new List <double>(); integer_list.ForEach(v => double_list.Add((double)v / MathPow(10, field.digit))); obj = double_list; } } else { Int64 integer = this.DecodeInteger(reader); if (field.digit == 0) { obj = integer; } else { obj = (double)integer / MathPow(10, field.digit); } } break; case "boolean": if (field.is_array) { obj = this.DecodeBooleanList(reader); } else { SprotoHelper.Error("[SprotoCodec.DecodeBuildInType] 'boolean' should decode in header part"); //obj = this.DecodeBoolean(reader); } break; case "string": if (field.is_array) { obj = this.DecodeStringList(reader); } else { obj = this.DecodeString(reader); } break; case "binary": if (field.is_array) { obj = this.DecodeBinaryList(reader); } else { obj = this.DecodeBinary(reader); } break; default: SprotoHelper.Error("[SprotoCodec.DecodeBuildInType] invalid buildin-type '{0}'", field.type); break; } return(obj); }
public static byte[] DumpToBinary(SprotoMgr sprotomgr) { SprotoMgr meta = SprotoParser.Parse(meta_proto); SprotoObject group = meta.NewSprotoObject("group"); // make result stable List <SprotoType> types = new List <SprotoType>(sprotomgr.Types.Values); types.Sort(delegate(SprotoType lhs, SprotoType rhs) { return(string.Compare(lhs.name, rhs.name)); }); if (sprotomgr.Types.Count != 0) { List <SprotoObject> meta_types = new List <SprotoObject>(); foreach (SprotoType type in types) { SprotoObject meta_type = meta.NewSprotoObject("type"); meta_type["name"] = type.name; if (type.fields.Count != 0) { List <SprotoField> fields = new List <SprotoField>(type.fields.Values); fields.Sort(delegate(SprotoField lhs, SprotoField rhs) { return(lhs.tag < rhs.tag ? -1 : 1); }); List <SprotoObject> meta_fields = new List <SprotoObject>(); foreach (SprotoField field in fields) { SprotoObject meta_field = meta.NewSprotoObject("type.field"); meta_field["name"] = field.name; meta_field["tag"] = field.tag; if (SprotoParser.BuildInTypeName2Id.ContainsKey(field.type)) { meta_field["buildin"] = SprotoParser.BuildInTypeName2Id[field.type]; if (field.type == "binary") { meta_field["type"] = 1; } else if (field.type == "integer" && field.digit > 0) { meta_field["type"] = field.digit; } } else { meta_field["type"] = types.IndexOf(sprotomgr.Types[field.type]); } if (field.is_array) { meta_field["array"] = true; if (field.key != null) { SprotoType t = sprotomgr.Types[field.type]; SprotoField f = t.fields[field.key]; meta_field["key"] = f.tag; } } meta_fields.Add(meta_field); } meta_type["fields"] = meta_fields; } meta_types.Add(meta_type); } group["type"] = meta_types; } if (sprotomgr.Protocols.Count != 0) { // make result stable List <SprotoProtocol> protocols = new List <SprotoProtocol>(sprotomgr.Protocols.Values); protocols.Sort(delegate(SprotoProtocol lhs, SprotoProtocol rhs) { return(lhs.tag < rhs.tag ? -1 : 1); }); List <SprotoObject> meta_protocols = new List <SprotoObject>(); foreach (SprotoProtocol protocol in protocols) { SprotoObject meta_protocol = meta.NewSprotoObject("protocol"); meta_protocol["name"] = protocol.name; meta_protocol["tag"] = protocol.tag; if (protocol.request != null) { meta_protocol["request"] = types.IndexOf(sprotomgr.Types[protocol.request]); } if (protocol.response != null) { meta_protocol["response"] = types.IndexOf(sprotomgr.Types[protocol.response]); } else { meta_protocol["confirm"] = true; } meta_protocols.Add(meta_protocol); } group["protocol"] = meta_protocols; } SprotoStream writer = new SprotoStream(); meta.Encode(group, writer); byte[] result = new byte[writer.Position]; for (int i = 0; i < writer.Position; i++) { result[i] = writer.Buffer[i]; } return(result); }
private Dictionary <T, SprotoObject> DecodeSprotoObjectDict <T> (SprotoMgr sprotomgr, SprotoType type, SprotoField field, SprotoStream reader) { Dictionary <T, SprotoObject> dict = new Dictionary <T, SprotoObject>(); UInt32 size = this.ReadUInt32(reader); while (size > 0) { UInt32 elem_size = this.ReadUInt32(reader); UInt32 need_size = elem_size + SprotoCodec.SIZEOF_LENGTH; size = size - need_size; if (size < 0) { SprotoHelper.Error("[SprotoCodec.DecodeSprotoObjectDict] fail"); } SprotoObject elemobj = this.DecodeSprotoObject(sprotomgr, type, reader); SprotoObject keyobj = elemobj.Get(field.key); T key = (T)keyobj.val; dict[key] = elemobj; } if (size != 0) { SprotoHelper.Error("[SprotoCodec.DecodeSprotoObjectDict] fail"); } return(dict); }