/// <summary> /// 新增发布主题 /// </summary> /// <param name="data"></param> /// <returns></returns> public static TopicMessage UnPackNewTopic(byte[] data, int count) { TopicMessage message = new TopicMessage(); byte[] tmp = new byte[count - 1];//除去数据标识 Array.Copy(data, 1, tmp, 0, tmp.Length); Span <byte> bytes = new Span <byte>(tmp); var lenSP = bytes.Slice(0, 4); int len = BitConverter.ToInt32(lenSP.ToArray(), 0); lenSP = bytes.Slice(4, 4); int topicLen = BitConverter.ToInt32(lenSP.ToArray(), 0); lenSP = bytes.Slice(8, topicLen); string topic = Encoding.Default.GetString(lenSP.ToArray()); lenSP = bytes.Slice(8 + topicLen, 8); long id = BitConverter.ToInt32(lenSP.ToArray(), 0); lenSP = bytes.Slice(16 + topicLen, count - 1 - 16 - topicLen); string addr = Encoding.Default.GetString(lenSP.ToArray()); message.Address = addr; message.NodeId = id; message.TopicName = topic; return(message); }
/// <summary> /// 解析订阅信息 /// </summary> /// <param name="data"></param> /// <returns></returns> public static TopicMessage UnPackSubscribeMsg(byte[] data) { byte[] tmp = new byte[data.Length - 1]; Array.Copy(data, 1, tmp, 0, data.Length - 1); Span <byte> span = tmp; var sp = span.Slice(0, 4); int len = BitConverter.ToInt32(sp.ToArray(), 0); sp = span.Slice(4, 4); int topicLen = BitConverter.ToInt32(sp.ToArray(), 0); sp = span.Slice(8, topicLen); string topic = Encoding.Default.GetString(sp.ToArray()); sp = span.Slice(8 + topicLen); string addr = Encoding.Default.GetString(sp.ToArray()); TopicMessage message = new TopicMessage() { Address = addr, TopicName = topic }; return(message); }