| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547 | // 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<SendBuffer> _sendBuffers = new Queue<SendBuffer>();//     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;////         /// <summary>//         /// 版本//         /// </summary>//         public ushort ver;//     }////     public Queue<RequestBuffer> requestBuffer = new Queue<RequestBuffer>();////     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;//     }// }//// }// }
 |