// using System.Net.Sockets; // using NetCore.ContentParse; // using NetCore.NetServerCoreBasic; // using NetCore.Protocol; // // namespace GameServer.NetLink; // // public class TCPLink : INetContainer // { // public void Start() // { // } // // public void Init(IContentParse iContentParse, IProtocol iProtocol) // { // } // // public void AddGetData(object data) // { // } // // // private byte[] buffData = new byte[6553500]; // private Socket _tcpClient; // private Thread thread; // public BinaryReader br; // public BinaryWriter bw; // public string url; // private bool isRun; // private long jg; // private Thread heartThread; // private Queue _sendBuffers = new Queue(); // private int allCount; // private SocketAsyncEventArgs sae; // // //记录半包数据 // private bool isBufferData; // private byte[] lastBuffData; // private int lastCount; // // private ushort lastXueLieHao; // //数据结束 // // //尾包 // private byte[] weiBaoBuffData; // // // // // public class RequestBuffer // { // // public HttpListenerContext httpListenerContext; // public ushort xlh; // public SimulationCombat combatRequest; // } // // public enum SendBufferType // { // Data, // Connect, // } // // public class SendBuffer // { // public RequestBuffer RequestBuffer; // public SimulationCombatResponse SimulationFightTogetherResponse; // public SendBufferType SendBufferType; // // /// // /// 版本 // /// // public ushort ver; // } // // public Queue requestBuffer = new Queue(); // // public void Init(string ip, int port) // { // this.url = ip; // LogTool.Log("开启协议"); // _tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // _tcpClient.Connect(ip, port); // // isRun = true; // LogTool.Log("启动成功" + url); // heartThread = new Thread(HrartUpdate); // heartThread.Start(); // thread = new Thread(TheUpdate); // thread.Start(); // } // // private void HrartUpdate() // { // while (isRun) // { // if (_tcpClient != null) // { // try // { // SendBuffer sbs = null; // lock (_sendBuffers) // { // if (!_sendBuffers.TryDequeue(out sbs)) // { // } // } // // if (sbs != null) // { // byte[] data = GetSendData(sbs); // _tcpClient.Send(data); // } // // long currTime = DateTime.Now.Ticks / 10000; // double kk = currTime - jg; // if (kk > 5 * 1000) // { // jg = currTime; // byte[] cdByte = IntToByte(1); // byte[] mdata = new byte[5]; // mdata[0] = cdByte[0]; // mdata[1] = cdByte[1]; // mdata[2] = cdByte[2]; // mdata[3] = cdByte[3]; // mdata[4] = 2; // _tcpClient.Send(mdata); // } // } // catch (Exception e) // { // Console.WriteLine(e); // // isRun = false; // Dispose(); // } // } // // // Thread.Sleep(1); // } // } // // 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) // { // byte[] numberBytes = BitConverter.GetBytes(number).Reverse().ToArray(); // 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; // } // } // // // private void TheUpdate() // { // while (isRun) // { // // if (sae == null) // // { // // SendAe(); // // } // // try // { // // SocketAsyncEventArgs sae=new SocketAsyncEventArgs(); // // sae.SetBuffer(buffData); // // int count = _tcpClient.Receive(buffData); // // // if (count <= 4) // // { // // Thread.Sleep(1); // // continue; // // } // // LogTool.Log("数据来了" + count); // // byte[] currBuferr=new byte[count]; // // Array.Copy(buffData, 0, currBuferr, 0,count); // // currBuferr = currBuferr.Reverse().ToArray(); // 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 (isWeiBao&&lastBuffData!=null) // // { // // LogTool.Log("修复尾包"+lastBuffData.Length+"__"+count); // // isWeiBao = false; // // int maxCount = 6553500; // // if (count + lastBuffData.Length > maxCount) // // { // // maxCount = count + lastBuffData.Length; // // } // // byte[] newBuffData = new byte[6553500]; // // Array.Copy(lastBuffData, 0, newBuffData, 0, lastBuffData.Length); // // Array.Copy(buffData, 0, newBuffData, lastBuffData.Length, count); // // count += lastBuffData.Length; // // buffData = newBuffData; // // lastBuffData = null; // // } // 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 < 6) // { // weiBaoBuffData = new byte[count]; // // LogTool.Log("包太短222__" + count); // Array.Copy(buffData, currCount, weiBaoBuffData, 0, count); // break; // } // else // { // weiBaoBuffData = null; // } // } // else // { // if (count - currCount < 6) // { // weiBaoBuffData = new byte[count]; // // LogTool.Log("包太短__" + count); // Array.Copy(buffData, currCount, weiBaoBuffData, 0, count); // break; // } // } // } // // if (!isBufferData) // { // byte[] cdByte = new byte[2]; // cdByte[0] = buffData[currCount]; // currCount++; // cdByte[1] = buffData[currCount]; // currCount++; // xueLieHao = ByteToUshort(cdByte, true); // // 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, true)); // // 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; // } // // // 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) // { // // RequestBuffer buffer = new RequestBuffer(); // // buffer.combatRequest = cr; // LogTool.Log(e); // // Send(null, null); // // LogTool.Log(""); // } // } // catch (Exception e) // { // LogTool.Log(e); // break; // } // } // // LogTool.Log("服务器执行完毕"); // } // // // public void AddBuffer(RequestBuffer buffer) // { // lock (requestBuffer) // { // requestBuffer.Enqueue(buffer); // } // } // // public RequestBuffer GetRequestBuffer() // { // lock (requestBuffer) // { // if (requestBuffer.TryDequeue(out RequestBuffer buffer)) // { // return buffer; // } // // return null; // } // } // // private byte[] GetSendData(SendBuffer sendBuffer) // { // byte[] mdata = null; // if (sendBuffer != null) // { // if (sendBuffer.SimulationFightTogetherResponse != null) // { // byte[] data = null; // int size = sendBuffer.SimulationFightTogetherResponse.CalculateSize(); // data = new byte[size]; // CodedOutputStream cos = new CodedOutputStream(data); // sendBuffer.SimulationFightTogetherResponse.WriteTo(cos); // cos.CheckNoSpaceLeft(); // LogTool.Log("回复消息" + sendBuffer.RequestBuffer.xlh + "__" + data.Length); // mdata = new byte[data.Length + 7]; // byte[] zcd = IntToByte((mdata.Length - 4)); // // // mdata[0] = zcd[0]; // mdata[1] = zcd[1]; // mdata[2] = zcd[2]; // mdata[3] = zcd[3]; // // // LogTool.Log( "mdata"+[0]+"_"+ mdata[1]+"_"+ mdata[2]+"_"+ mdata[3]); // // mdata[4] = 1; // byte[] cdByte = UShortToByte(sendBuffer.RequestBuffer.xlh); // mdata[5] = cdByte[0]; // mdata[6] = cdByte[1]; // // Array.Copy(data, 0, mdata, 7, data.Length); // } // else // { // if (sendBuffer.SendBufferType == SendBufferType.Connect) // { // byte[] cdByte = IntToByte(3); // mdata = new byte[7]; // mdata[0] = cdByte[0]; // mdata[1] = cdByte[1]; // mdata[2] = cdByte[2]; // mdata[3] = cdByte[3]; // mdata[4] = 0; // cdByte = UShortToByte(sendBuffer.ver); // mdata[5] = cdByte[0]; // mdata[6] = cdByte[1]; // } // else // { // byte[] cdByte = IntToByte(3); // mdata = new byte[7]; // mdata[0] = cdByte[0]; // mdata[1] = cdByte[1]; // mdata[2] = cdByte[2]; // mdata[3] = cdByte[3]; // // mdata[4] = 3; // cdByte = UShortToByte(sendBuffer.RequestBuffer.xlh); // mdata[5] = cdByte[0]; // mdata[6] = cdByte[1]; // } // } // } // else // { // LogTool.Log("回复消息" + 125); // // byte[] cdByte = IntToByte(1); // mdata = new byte[7]; // mdata[0] = cdByte[0]; // mdata[1] = cdByte[1]; // mdata[2] = cdByte[2]; // mdata[3] = cdByte[3]; // // mdata[4] = 4; // } // // // return mdata; // } // // public void Send(RequestBuffer buffer, SimulationCombatResponse resp, // SendBufferType sendBufferType = SendBufferType.Data) // { // lock (_sendBuffers) // { // SendBuffer sendBuffer = new SendBuffer(); // sendBuffer.RequestBuffer = buffer; // sendBuffer.SimulationFightTogetherResponse = resp; // sendBuffer.SendBufferType = sendBufferType; // _sendBuffers.Enqueue(sendBuffer); // } // } // // public void Send(SendBuffer sendBuffer) // { // lock (_sendBuffers) // { // // SendBuffer sendBuffer = new SendBuffer(); // // sendBuffer.RequestBuffer = buffer; // // sendBuffer.SimulationFightTogetherResponse = resp; // // sendBuffer.SendBufferType = sendBufferType; // _sendBuffers.Enqueue(sendBuffer); // } // } // // public void Dispose() // { // isRun = false; // if (thread != null) // { // _tcpClient.Close(); // // thread.Abort(); // } // // if (heartThread != null) // { // heartThread.Abort(); // } // // heartThread = null; // thread = null; // } // } // // } // }