123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
-
- 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("");
- }
- }
- }
- }
|