public static byte[] Unpack(byte[] data, int len = 0) { SprotoStream origin = new SprotoStream(); int idx = 0; len = len == 0 ? data.Length : len; while (idx < len) { int mapz = data[idx++]; SprotoStream group = new SprotoStream(GROUP_SZ); byte padding = 0; for (int i = 0; i < GROUP_SZ && idx < len; ++i) { if ((mapz & ((1 << i) & 0xff)) != 0) { group[i] = data[idx++]; } } // To judge whether it is a unsaturated group. if (idx == len - 1 && data[idx] < GROUP_SZ) { padding = data[idx++]; } origin.Write(group.Buffer, 0, GROUP_SZ - padding); } return(origin.Buffer.Take <byte>(origin.Position).ToArray()); }
public static byte[] Pack(byte[] data, int len = 0) { SprotoStream packed = new SprotoStream(); int idx = 0; int i = 0; len = len == 0 ? data.Length : len; while (idx < len) { byte mapz = 0; SprotoStream group = new SprotoStream(GROUP_SZ); for (i = 0; i < GROUP_SZ && idx < len; ++i) { if (data[idx] != 0) { mapz |= (byte)((1 << i) & 0xff); group.Write(data, idx, 1); } ++idx; } packed.WriteByte(mapz); packed.Write(group.Buffer, 0, group.Position); } // If it is an unsaturated group, then fill a byte of padding. if (i < GROUP_SZ) { byte padding = (byte)(GROUP_SZ - i); packed.WriteByte(padding); } return(packed.Buffer.Take <byte>(packed.Position).ToArray()); }
private UInt32 EncodeBinary(byte[] bytes, SprotoStream writer) { UInt32 length = (UInt32)bytes.Length; writer.Write(bytes, 0, bytes.Length); return(length); }
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)); }
// 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); } } }