public void TestSocketParse() { byte[] data = ("这是一条测试数据").ToBytes(); SendPacket sendPacket = new SendPacket(data); List <byte> dataList = new List <byte>(); dataList.AddRange(sendPacket.PackData()); dataList.AddRange(sendPacket.PackData()); data = dataList.ToArray(); ReceivePacket ReceivePacket = new ReceivePacket(); while (data.Length > 0) { if (ReceivePacket.ParseData(ref data)) { string str = ReceivePacket.GetBytes().ToStringUnicode(); Console.WriteLine(str); Assert.AreEqual("这是一条测试数据", str); ReceivePacket = new ReceivePacket(); } else { Assert.Fail(); break; } } }
public static void ListenTcp <T>(P2PTcpClient tcpClient) where T : ReceivePacket { try { Guid curGuid = Global.CurrentGuid; byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; NetworkStream tcpStream = tcpClient.GetStream(); ReceivePacket msgReceive = Activator.CreateInstance(typeof(T)) as ReceivePacket; while (tcpClient.Connected && curGuid == Global.CurrentGuid) { int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { byte[] refData = buffer.Take(curReadLength).ToArray(); while (msgReceive.ParseData(ref refData)) { LogUtils.Debug($"命令类型:{msgReceive.CommandType}"); // 执行command using (P2PCommand command = FindCommand(tcpClient, msgReceive)) { command?.Excute(); } //重置msgReceive msgReceive.Reset(); if (refData.Length <= 0) { break; } } } else { break; } } } catch (Exception ex) { LogUtils.Error($"【错误】Global_Func.ListenTcp:{Environment.NewLine}{ex}"); } if (Global.TcpMap.ContainsKey(tcpClient.ClientName)) { if (Global.TcpMap[tcpClient.ClientName].TcpClient == tcpClient) { Global.TcpMap.Remove(tcpClient.ClientName); } } //如果tcp已关闭,需要关闭相关tcp try { tcpClient.ToClient?.Close(); } catch { } LogUtils.Debug($"tcp连接{tcpClient.RemoteEndPoint}已断开"); }
public static void ListenTcp <T>(P2PTcpClient tcpClient) where T : ReceivePacket { try { Guid curGuid = AppCenter.Instance.CurrentGuid; byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; NetworkStream tcpStream = tcpClient.GetStream(); tcpClient.ReceiveBufferSize = P2PGlobal.P2PSocketBufferSize; ReceivePacket msgReceive = Activator.CreateInstance(typeof(T)) as ReceivePacket; int maxTotal = 1024 * 50 * 2; int[] recieveLength = new int[2]; int lastSecond = -1; while (tcpClient.Connected && curGuid == AppCenter.Instance.CurrentGuid) { int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { byte[] refData = buffer.Take(curReadLength).ToArray(); if (tcpClient.IsSpeedLimit) { int curSecond = DateTime.Now.Second; if (DateTime.Now.Second != lastSecond) { recieveLength[curSecond % 2] = 0; } lastSecond = curSecond; recieveLength[curSecond % 2] += curReadLength; if (recieveLength.Sum() > maxTotal) { Thread.Sleep(1000); } } while (msgReceive.ParseData(ref refData)) { LogUtils.Debug($"命令类型:{msgReceive.CommandType}"); // 执行command using (P2PCommand command = FindCommand(tcpClient, msgReceive)) { command?.Excute(); } //重置msgReceive msgReceive.Reset(); if (refData.Length <= 0) { break; } } } else { break; } } } catch (Exception ex) { LogUtils.Error($"【错误】Global_Func.ListenTcp:{Environment.NewLine}{ex}"); } if (ClientCenter.Instance.TcpMap.ContainsKey(tcpClient.ClientName)) { if (ClientCenter.Instance.TcpMap[tcpClient.ClientName].TcpClient == tcpClient) { ClientCenter.Instance.TcpMap.Remove(tcpClient.ClientName); } } //如果tcp已关闭,需要关闭相关tcp try { tcpClient.ToClient?.SafeClose(); } catch { } LogUtils.Debug($"tcp连接{tcpClient.RemoteEndPoint}已断开"); }