private bool ProcessOneMsg(byte[] msgBytes) { var head = SiMsgHelper.GetSiMsgHead(msgBytes); switch (head.u16MsgType) { case SiMacroDef.O_SILMTENB_GETFILEINFO_RES: PublishHelper.PublishMsg("topic", msgBytes); //TODO topic协商 break; case SiMacroDef.O_SILMTENB_GETFILEATTRIB_RES: break; case SiMacroDef.O_SILMTENB_SETRDWRATTRIB_RES: break; case SiMacroDef.O_SILMTENB_DELFILE_RES: break; case SiMacroDef.O_SILMTENB_GETCAPACITY_RES: break; default: //TODO 不知道是什么啊,老铁 break; } return(true); }
private void DealMsgs() { //判断数据长度是否>head的长度 if (m_nMsgLen < 4) { //已经保存的数据长度连报文头长度都不够,继续攒 return; } var head = SiMsgHelper.GetSiMsgHead(m_siMsgBuffer); if (head.u16MsgLength <= 0) //消息长度设置错误 { //按照原来的处理流程是清空掉所有的数据 var newBuf = new byte[BUFF_LEN]; //构造一个新的byte[],旧的gc m_siMsgBuffer = newBuf; m_nMsgLen = 0; return; } if (m_nMsgLen < head.u16MsgLength) //确保已经保存的数据可以解析出一个完整的消息 { //已经保存的数据不够长,继续攒 return; } var msgData = m_siMsgBuffer.Take(head.u16MsgLength).ToArray(); //取出一条消息所需的数据 ProcessOneMsg(msgData); m_nMsgLen -= head.u16MsgLength; //剩余的数据长度 //剩余的数据前移。在这里直接new一个新的数组,旧的gc回收。 var temp = new byte[BUFF_LEN]; Buffer.BlockCopy(m_siMsgBuffer, head.u16MsgLength, temp, 0, m_nMsgLen); m_siMsgBuffer = temp; DealMsgs(); //递归处理 }