/// <summary> /// 解包当前已经接收到的原始数据,结果存放进了已接收消息队列, 返回true如果接收到了消息. /// </summary> /// <param name="packeter"> 打包方法. </param> /// <param name="length"> [out] 接收到数据长度. </param> /// /// <returns> 解包出来的消息条数(注意不是长度). </returns> internal int UnpackReceiveData(IPacket packeter, out int length) { lock (this._lockReserveData) { //拼接所有的已接受数据 byte[] alldata = _reserveQueuePacked.GetDataOnce(_reserveData); _reserveData = null;//清空已经无用的_reserveData if (alldata == null) { length = 0; //长度为0 //这个情形在客户端狂发速度过快的时候容易出现,但是不影响接收,所以去掉这个日志 //DxDebug.LogWarning("Token.UnpackReceiveData(): alldata为null!"); return(0); } length = alldata.Length; //传出这个数据长度 FindPacketResult findPacketResult = packeter.FindPacket(alldata, 0); //解包 _reserveData = findPacketResult.reserveData; //更新reserveData if (findPacketResult.dataArr != null) //将结果加入队列 { //记录下一共找到的有效消息条数 int msgCount = findPacketResult.dataArr.Length; for (int i = 0; i < findPacketResult.dataArr.Length; i++)//结果是一个消息数组 { byte[] data = findPacketResult.dataArr[i]; if (data == null) { //这里是否会频繁发生? DxDebug.LogWarning("Token.UnpackReceiveData(): 结果中的data为null!"); break; } //如果不是心跳包才加入接收消息队列 if (!Config.CompareHeartBeat(findPacketResult.dataArr[i]))//Config中的静态函数判断 { if (!_receiveQueue.EnqueueMaxLimit(findPacketResult.dataArr[i])) { DxDebug.LogWarning("Token.UnpackReceiveData():接收已解包的数据队列 丢弃了一段数据"); } } else { DxDebug.LogFileOnly("Token.UnpackReceiveData():接收到了心跳包 TokenID:" + this.ID); } } LastMsgReceTickTime = DateTime.Now.Ticks; //记录最近一次接收到消息的时间 //DxDebug.Log("某个token接收到了 " + findPacketResult.data.Length + "条消息"); return(msgCount); } else { DxDebug.LogWarning("Token.UnpackReceiveData():接收到数据,经过FindPacket(),但是没有找到有效消息!"); return(0); } } }
FindPacketResult IPacket.FindPacket(byte[] sData, int startIndex) { // DxDebug.LogConsole("DPacket.FindPacket(): sData长度:" + sData.Length + " startIndex: " + startIndex); List <byte[]> packsList = new List <byte[]>(); FindPacketResult findPacketResult = new FindPacketResult(); findPacketResult.dataArr = null; findPacketResult.reserveData = null; //从头至尾依次检查 int checkStart = startIndex; while (checkStart < sData.Length) { DPacket.OnceFindResult result = FindPacketOnce(sData, checkStart); if (result.startIndex != checkStart) { DxDebug.LogWarning("DPacket.FindPacket():丢弃了一段数据,丢弃起始" + checkStart + "丢弃长度" + (result.startIndex - checkStart)); } if (result.e == DPacket.PacketError.Succeed) { packsList.Add(UnPack(sData, result)); //解包,添加到队列 // DxDebug.Log("DPacket.FindPacket():解包成功"); checkStart = result.startIndex + result.length; //解包成功,直接跳下一个长度位置 } else if (result.e == DPacket.PacketError.PacketReceiveing) { findPacketResult.reserveData = new byte[sData.Length - result.startIndex]; Buffer.BlockCopy(sData, result.startIndex, findPacketResult.reserveData, 0, findPacketResult.reserveData.Length); DxDebug.Log("DPacket.FindPacket():解包:数据可能还没有接收完"); break; } else if (result.e == DPacket.PacketError.PacketLengthTooShort) { findPacketResult.reserveData = new byte[sData.Length - result.startIndex]; Buffer.BlockCopy(sData, result.startIndex, findPacketResult.reserveData, 0, findPacketResult.reserveData.Length); DxDebug.Log("DPacket.FindPacket():解包:数据太短"); break; } } findPacketResult.dataArr = packsList.ToArray(); return(findPacketResult); }
FindPacketResult IPacket.FindPacket(byte[] sData, int startIndex) { FindPacketResult result = new FindPacketResult(); if (sData.Length - startIndex < sizeof(int)) { result.dataArr = null; result.reserveData = sData; } int index = startIndex; List <byte[]> listData = new List <byte[]>(); while (index < sData.Length) { //得到一个长度 int length = BitConverter.ToInt32(sData, index); if (sData.Length - index - sizeof(int) < length)//表示还没有接收完 { if (index == 0) { result.reserveData = sData; } else { result.reserveData = new byte[sData.Length - index]; Buffer.BlockCopy(sData, index, result.reserveData, 0, result.reserveData.Length); } } else { byte[] data = new byte[length]; Buffer.BlockCopy(sData, index + sizeof(int), data, 0, data.Length); listData.Add(data); } index += length + sizeof(int); } result.dataArr = listData.ToArray(); return(result); }