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 : RecievePacket { byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; NetworkStream tcpStream = tcpClient.GetStream(); RecievePacket msgRecieve = Activator.CreateInstance(typeof(T)) as RecievePacket; while (tcpClient.Connected) { int curReadLength = tcpStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { byte[] refData = buffer.Take(curReadLength).ToArray(); while (msgRecieve.ParseData(ref refData)) { Debug.WriteLine($"命令类型:{msgRecieve.CommandType}"); //todo:执行command P2PCommand command = FindCommand(tcpClient, msgRecieve); if (command != null) { command.Excute(); } //重置msgRecieve msgRecieve = Activator.CreateInstance(typeof(T)) as RecievePacket; if (refData.Length <= 0) { break; } } } else { //如果tcp已关闭,需要关闭相关tcp if (tcpClient.ToClient != null && tcpClient.ToClient.Connected) { Debug.WriteLine("tcp已关闭"); tcpClient.ToClient.Close(); } break; } } }
private static void ReadTcp_Server(IAsyncResult ar) { RelationTcp_Server relation = (RelationTcp_Server)ar.AsyncState; if (relation.guid == EasyInject.Get <AppCenter>().CurrentGuid) { if (relation.readTcp.Connected && relation.readTcp.GetStream().CanRead) { int length = 0; EasyOp.Do(() => { length = relation.readTcp.GetStream().EndRead(ar); }, () => { if (length > 0) { byte[] refData = relation.buffer.Take(length).ToArray(); while (relation.msgReceive.ParseData(ref refData)) { // 执行command using (P2PCommand command = FindCommand(relation.readTcp, relation.msgReceive)) { //LogUtils.Trace($"命令类型:{relation.msgReceive.CommandType}"); if (command != null) { bool isSuccess = false; EasyOp.Do(() => { isSuccess = command.Excute(); }, e => { LogUtils.Error($"执行命令{relation.msgReceive.CommandType}时发生异常:{e}"); }); if (!isSuccess) { EasyOp.Do(() => { relation.readTcp?.SafeClose(); }); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); return; } } else { EasyOp.Do(() => { relation.readTcp?.SafeClose(); }); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); return; } } //重置msgReceive relation.msgReceive.Reset(); if (refData.Length <= 0) { break; } } if (relation.readTcp.Connected) { EasyOp.Do(() => { relation.readTcp.GetStream().BeginRead(relation.buffer, 0, relation.buffer.Length, ReadTcp_Server, relation); }, ex => { LogUtils.Debug($"Tcp连接已被断开 {relation.readTcp.RemoteEndPoint}"); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); }); } } else { EasyOp.Do(() => { relation.readTcp?.SafeClose(); }); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); } }, ex => { LogUtils.Debug($"Tcp连接已被断开 {relation.readTcp.RemoteEndPoint}"); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); }); } } else { LogUtils.Debug($"主动断开{relation.readTcp.RemoteEndPoint}连接"); EasyOp.Do(() => { relation.readTcp?.SafeClose(); }); EasyOp.Do(() => { relation.readTcp.ToClient?.SafeClose(); }); } //if (TcpCenter.Instance.ConnectedTcpList.Contains(relation.readTcp)) // TcpCenter.Instance.ConnectedTcpList.Remove(relation.readTcp); }
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}已断开"); }