/// <summary> /// 解析消息数组至消息对象列表 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> /// <returns>消息对象列表</returns> private List <MessageBase> UnpackMessage(byte[] buf, int offset) { int len = buf.Length; List <MessageBase> tMsgList = new List <MessageBase>(); while (true) { if (DataConversion.ByteToNum <Int16>(buf, offset, false) == -1) { offset += 2; if (DataConversion.ByteToNum <Int16>(buf, offset, false) == -1) { offset += 2; packetBuf = (byte[])buf.Take(offset).ToArray(); break; } else { MessageBase message = MessageBase.MessageCreate(buf, ref offset); tMsgList.Add(message); } } if (offset >= (packetMaxLength - 10)) { throw new NotImplementedException(); } } if (tMsgList.Count() <= 0) { throw new NotImplementedException(); } return(tMsgList); }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> public CommsErr(byte[] buf, int offset) : base(messageId) { base.msgBuf = new byte[len]; Array.Copy(buf, 0, base.msgBuf, 0, len); offset += 2; offset += DataConversion.ByteToNum(buf, offset, ref error, false); }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> public NodeAva(byte[] buf, int offset) : base(messageId) { base.msgBuf = new byte[len]; Array.Copy(buf, 0, base.msgBuf, 0, len); offset += 2; offset += DataConversion.ByteToNum(buf, offset, ref nodeId, false); }
private void Pack() { messageBuf = DataConversion.NumToByte(msgId, false); messageBuf = DataConversion.NumToByte(nodeId, messageBuf, false); messageBuf = DataConversion.NumToByte(cartSeq, messageBuf, false); messageBuf = DataConversion.NumToByte(priority, messageBuf, false); messageBuf = DataConversion.NumToByte(laneId, messageBuf, false); base.msgBuf = messageBuf; }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> public DivertCmd(byte[] buf, int offset) : base(messageId) { offset += 2; offset += DataConversion.ByteToNum(buf, offset, ref nodeId, false); offset += DataConversion.ByteToNum(buf, offset, ref cartSeq, false); offset += DataConversion.ByteToNum(buf, offset, ref priority, false); offset += DataConversion.ByteToNum(buf, offset, ref laneId, false); Pack(); }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> public DivertReq(byte[] buf, int offset) : base(messageId) { base.msgBuf = new byte[len]; Array.Copy(buf, 0, base.msgBuf, 0, len); offset += 2; offset += DataConversion.ByteToNum(buf, offset, ref nodeId, false); offset += DataConversion.ByteToNum(buf, offset, ref cartSeq, false); offset += DataConversion.ByteToNum(buf, offset, ref attribute, false); codeStr = Encoding.ASCII.GetString(buf, offset, codeStrLen).Trim(); }
/// <summary> /// 打包报文对象至报文数组 /// </summary> private void pack() { packetBuf = DataConversion.NumToByte(cycleNum, false); packetBuf = DataConversion.NumToByte(senderId, packetBuf, false); packetBuf = DataConversion.NumToByte(receiverId, packetBuf, false); packetBuf = DataConversion.NumToByte(ack, packetBuf, false); packetBuf = DataConversion.NumToByte(transportError, packetBuf, false); packetBuf = packetBuf.Concat(PackMessage(messageList)).ToArray(); int len = 240 - packetBuf.Length; byte[] padding = Enumerable.Repeat((byte)0xff, len).ToArray(); packetBuf = packetBuf.Concat(padding).ToArray(); }
/// <summary> /// 解析报文数组至报文对象 /// </summary> /// <param name="buf">报文数组</param> public Packet(byte[] buf) { int offset = 0; if (buf.Length > packetMaxLength) { throw new NotImplementedException(); } packetBuf = (byte[])buf.Clone(); offset += DataConversion.ByteToNum(buf, offset, ref cycleNum, false); offset += DataConversion.ByteToNum(buf, offset, ref senderId, false); offset += DataConversion.ByteToNum(buf, offset, ref receiverId, false); offset += DataConversion.ByteToNum(buf, offset, ref ack, false); offset += DataConversion.ByteToNum(buf, offset, ref transportError, false); messageList = UnpackMessage(buf, offset); }
/// <summary> /// 打包消息对象列表至消息数组 /// </summary> /// <param name="tMessageList">消息对象数组</param> /// <returns>消息数组</returns> private byte[] PackMessage(List <MessageBase> tMessageList) { byte[] buf = null; int len = tMessageList.Count; if (len <= 0) { throw new NotImplementedException(); } foreach (MessageBase messageBase in tMessageList) { buf = DataConversion.NumToByte((Int16)(-1), buf, false); buf = buf.Concat(messageBase.msgBuf).ToArray(); } return(buf); }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">偏移量引用</param> /// <returns>消息对象</returns> static public MessageBase MessageCreate(byte[] buf, ref int offset) { Int16 tMsgId = 0; MessageBase messageBase; DataConversion.ByteToNum(buf, offset, ref tMsgId, false); switch (tMsgId) { case (Int16)MessageType.DivertReq: messageBase = new DivertReq(buf, offset); offset += DivertReq.len; break; case (Int16)MessageType.DivertRes: messageBase = new DivertRes(buf, offset); offset += DivertRes.len; break; case (Int16)MessageType.HeartBeat: messageBase = new HeartBeat(buf, offset); offset += HeartBeat.len; break; case (Int16)MessageType.NodeAva: messageBase = new NodeAva(buf, offset); offset += NodeAva.len; break; case (Int16)MessageType.CommsErr: messageBase = new CommsErr(buf, offset); offset += CommsErr.len; break; default: throw new NotImplementedException(); } return(messageBase); }
private void Pack() { messageBuf = DataConversion.NumToByte(msgId, false); messageBuf = DataConversion.NumToByte(period, messageBuf, false); base.msgBuf = messageBuf; }
/// <summary> /// 解析消息数组至单个消息对象 /// </summary> /// <param name="buf">消息数组</param> /// <param name="offset">数组偏移量</param> public HeartBeat(byte[] buf, int offset) : base(messageId) { offset += 2; offset += DataConversion.ByteToNum(buf, offset, ref period, false); Pack(); }