using System; using System.Linq; using Fort23.UTool; using NetCore.Protocol; namespace NetCore.ContentParse { public class ByteParse : IContentParse { // private byte[] buffData = new byte[655350]; //记录半包数据 private bool isBufferData; private byte[] lastBuffData; private int lastCount; private ushort lastXueLieHao; //数据结束 //尾包 private byte[] weiBaoBuffData; private byte[] ShortToByte(short number) { byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray(); return numberBytes; } private byte[] UShortToByte(ushort number) { byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray(); return numberBytes; } private byte[] IntToByte(int number, bool isReverse = false) { if (isReverse) { byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray(); return numberBytes; } else { byte[] numberBytes = BitConverter.GetBytes(number); return numberBytes; } } private short ByteToShort(byte[] numberBytes, bool isReverse = false) { if (isReverse) { short converted = BitConverter.ToInt16(numberBytes.Reverse().ToArray(), 0); return converted; } else { short converted = BitConverter.ToInt16(numberBytes, 0); return converted; } } private ushort ByteToUshort(byte[] numberBytes, bool isReverse = false) { if (isReverse) { ushort converted = BitConverter.ToUInt16(numberBytes.Reverse().ToArray(), 0); return converted; } else { ushort converted = BitConverter.ToUInt16(numberBytes, 0); return converted; } } private int ByteToInt(byte[] numberBytes, bool isReverse = false) { if (isReverse) { int converted = BitConverter.ToInt32(numberBytes.Reverse().ToArray(), 0); return converted; } else { int converted = BitConverter.ToInt32(numberBytes, 0); return converted; } } public byte[] AssembleByte(byte[] content) { byte[] mdata = null; // LogTool.Log("回复消息" + buffer.Length); mdata = new byte[content.Length + 4]; byte[] zcd = IntToByte(content.Length); mdata[0] = zcd[0]; mdata[1] = zcd[1]; mdata[2] = zcd[2]; mdata[3] = zcd[3]; Array.Copy(content, 0, mdata, 4, content.Length); return mdata; } public void ParseByte(byte[] buffData, IProtocol iProtocol, IContentReceiver contentReceiver) { int count = buffData.Length; try { int currCount = 0; int startIndex = 0; // bool isEnd = false; while (currCount < count) { // isEnd = true; byte[] data = null; ushort xueLieHao = 1; int cdShort = 0; if (!isBufferData) { if (weiBaoBuffData != null) { int maxCount = 6553500; if (count + weiBaoBuffData.Length > maxCount) { maxCount = count + weiBaoBuffData.Length; } // LogTool.Log("修复包太短__" + count + "___" + weiBaoBuffData.Length); byte[] newBuff = new byte[maxCount]; Array.Copy(weiBaoBuffData, 0, newBuff, 0, weiBaoBuffData.Length); Array.Copy(buffData, 0, newBuff, weiBaoBuffData.Length, count); buffData = newBuff; count += weiBaoBuffData.Length; // LogTool.Log("修复包后太短__" + count); if (count < 4) { weiBaoBuffData = new byte[count]; // LogTool.Log("包太短222__" + count); Array.Copy(buffData, currCount, weiBaoBuffData, 0, count); break; } else { weiBaoBuffData = null; } } else { if (count - currCount < 4) { weiBaoBuffData = new byte[count]; // LogTool.Log("包太短__" + count); Array.Copy(buffData, currCount, weiBaoBuffData, 0, count); break; } } } if (!isBufferData) { byte[] dataBuffLanl = new byte[4]; dataBuffLanl[0] = buffData[currCount]; currCount++; dataBuffLanl[1] = buffData[currCount]; currCount++; dataBuffLanl[2] = buffData[currCount]; currCount++; dataBuffLanl[3] = buffData[currCount]; currCount++; cdShort = (ByteToInt(dataBuffLanl, false)); // LogTool.Log("数据到来" + cdShort + "__" + count + "_" + xueLieHao); if ((currCount + cdShort) > count) //处理半包情况 { lastBuffData = new byte[count - currCount]; lastCount = cdShort; lastXueLieHao = xueLieHao; Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length); // LogTool.Log("数据只有一半" + count + "__" + currCount + "___" + cdShort); isBufferData = true; break; } else { // LogTool.Log(currCount + "_收到长度:" + cdShort + "_Max" + count); data = new byte[cdShort]; Array.Copy(buffData, currCount, data, 0, data.Length); } } else if (lastCount - lastBuffData.Length > count) { // LogTool.Log("数据只有一半的一半" + count + "__" + lastCount + "___" + lastBuffData.Length); byte[] newLastBuffData = new byte[lastBuffData.Length + count]; Array.Copy(lastBuffData, 0, newLastBuffData, 0, lastBuffData.Length); Array.Copy(buffData, 0, newLastBuffData, lastBuffData.Length, count); lastBuffData = newLastBuffData; break; } else if (lastBuffData != null) //处理半包情况 { isBufferData = false; // LogTool.Log("修复半包" + lastCount + "__" + count + "___" + // (lastCount - lastBuffData.Length)); xueLieHao = lastXueLieHao; data = new byte[lastCount]; cdShort = lastCount - lastBuffData.Length; Array.Copy(lastBuffData, 0, data, 0, lastBuffData.Length); Array.Copy(buffData, currCount, data, lastBuffData.Length, cdShort); } // SimulationCombat cr = SimulationCombat.Parser.ParseFrom(data); // RequestBuffer buffer = new RequestBuffer(); // buffer.combatRequest = cr; // buffer.xlh = xueLieHao; // // allCount++; // // LogTool.Log("shoudao "+allCount); // // buffer.httpListenerContext = httpListenerContext; // LogTool.Log("收到中转服请求" + cr.CombatType + "___" + xueLieHao); // AddBuffer(buffer); currCount += cdShort; startIndex = currCount; contentReceiver.AddContent(data); } // if (isEnd&&currCount < count&&!isBufferData)//尾包 // { // LogTool.Log("尾巴处理"+currCount+"___"+count); // isWeiBao = true; // lastBuffData = new byte[count - currCount]; // Array.Copy(buffData, currCount, lastBuffData, 0, lastBuffData.Length); // // } } catch (Exception e) { LogTool.Error(e); // RequestBuffer buffer = new RequestBuffer(); // buffer.combatRequest = cr; // LogTool.Log(e); // // Send(null, null); // LogTool.Log(""); } } } }