public static byte[] BuildContent(IdType idType, MsgType msgType, OpsType opsType, int attr, int serial, byte[] core) { var coreLen = core == null ? 0 : core.Length; var buffer = Unpooled.Buffer(coreLen + 20); var msgId = Convert.ToInt32(idType); var msgIdBytes = BytesUtil.Int32ToBytes(msgId); buffer.WriteBytes(msgIdBytes); var timestamp = TimeUtil.GetTimeStamp(); var timeBytes = BytesUtil.Int32ToBytes(timestamp); buffer.WriteBytes(timeBytes); var msgTypeCode = Convert.ToInt16(msgType); var msgTypeBytes = BytesUtil.Int16ToBytes(msgTypeCode); buffer.WriteBytes(msgTypeBytes); var opsTypeCode = Convert.ToInt16(opsType); var opsTypeBytes = BytesUtil.Int16ToBytes(opsTypeCode); buffer.WriteBytes(opsTypeBytes); var attrBytes = BytesUtil.Int16ToBytes((Int16)attr); buffer.WriteBytes(attrBytes); var length = coreLen + 4; var lenBytes = BytesUtil.Int16ToBytes((Int16)length); buffer.WriteBytes(lenBytes); var serialBytes = BytesUtil.Int32ToBytes(serial); buffer.WriteBytes(serialBytes); if (core != null) { buffer.WriteBytes(core); } return(buffer.Array); }
public static BaseMessage Decode(IByteBuffer msg) { var head = msg.ReadByte(); var addr = new byte[4]; msg.ReadBytes(addr); var dst = BytesUtil.BytesToHex(addr); msg.ReadBytes(addr); var src = BytesUtil.BytesToHex(addr); var version = (int)msg.ReadByte(); var ctrlValue = msg.ReadShort(); var ctrlType = ctrlValue >> 15 & 0x1; var isEncrypt = (ctrlValue >> 14 & 1) == 1; var hasSignature = (ctrlValue >> 13 & 1) == 1; var hasStarter = (ctrlValue >> 12 & 1) == 1; var hasEndSign = (ctrlValue >> 11 & 1) == 1; var logicSignal = ctrlValue >> 9 & 3; var ackType = ctrlValue >> 7 & 3; var ackStatus = ctrlValue >> 5 & 3; var ctrl = new Control(); ctrl.SrcValue = ctrlValue; ctrl.ControlType = ctrlType; ctrl.IsEncrypt = isEncrypt; ctrl.HasSignature = hasSignature; ctrl.HasStarter = hasStarter; ctrl.HasEndSign = hasEndSign; ctrl.LogicSignal = logicSignal; ctrl.AckType = ackType; ctrl.AckStatus = ackStatus; var send = (int)msg.ReadByte(); var receive = (int)msg.ReadByte(); var contentLength = msg.ReadUnsignedShort(); var header = new BaseHeader { Head = head, Dst = dst, Src = src, Ver = version, Control = ctrl, Send = send, Receive = receive, Length = contentLength, }; var content = PooledByteBufferAllocator.Default.Buffer(contentLength); msg.ReadBytes(content); return(new BaseMessage { Header = header, Content = content }); }