/// <summary> /// 向服务器发送客户端信息 /// </summary> /// <param name="tcpClient"></param> private void InitServerInfo(P2PTcpClient tcpClient) { if (string.IsNullOrWhiteSpace(appCenter.Config.ClientName)) { Send_0x0104 sendPacket = new Send_0x0104(); byte[] dataAr = sendPacket.PackData(); tcpClient.GetStream().WriteAsync(dataAr, 0, dataAr.Length); } else { Send_0x0101 sendPacket = new Send_0x0101(); LogUtils.Info($"客户端名称:{appCenter.Config.ClientName}"); byte[] dataAr = sendPacket.PackData(); tcpClient.GetStream().WriteAsync(dataAr, 0, dataAr.Length); } }
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}已断开"); }
/// <summary> /// 监听映射端口并转发数据(ip直接转发模式) /// </summary> /// <param name="readClient"></param> private void ListenPortMapTcpWithIp(P2PTcpClient readClient) { if (readClient.ToClient == null || !readClient.ToClient.Connected) { LogUtils.Warning($"【失败】IP数据转发:绑定的Tcp连接已断开."); readClient.SafeClose(); return; } byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; readClient.ReceiveBufferSize = P2PGlobal.P2PSocketBufferSize; NetworkStream readStream = readClient.GetStream(); NetworkStream toStream = readClient.ToClient.GetStream(); try { while (readClient.Connected) { int curReadLength = readStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { if (readClient.ToClient != null) { toStream.Write(buffer, 0, curReadLength); } else { LogUtils.Warning($"【失败】IP数据转发:目标Tcp连接已释放."); readClient.SafeClose(); break; } } else { LogUtils.Warning($"【失败】IP数据转发:Tcp连接已断开."); //如果tcp已关闭,需要关闭相关tcp try { readClient.ToClient?.SafeClose(); } finally { } break; } } } finally { LogUtils.Warning($"【失败】IP数据转发:目标Tcp连接已断开."); readClient.SafeClose(); } }
/// <summary> /// 直接转发类型的端口监听,接收新tcp回调方法 /// </summary> /// <param name="ar"></param> public void AcceptSocket_Ip(IAsyncResult ar) { ListenSt st = (ListenSt)ar.AsyncState; TcpListener listener = st.listener; PortMapItem item = st.item; Socket socket = null; try { socket = listener.EndAcceptSocket(ar); } catch (Exception ex) { LogUtils.Error(ex.ToString()); return; } listener.BeginAcceptSocket(AcceptSocket_Ip, st); P2PTcpClient tcpClient = new P2PTcpClient(socket); P2PTcpClient ipClient = null; try { ipClient = new P2PTcpClient(item.RemoteAddress, item.RemotePort); } catch (Exception ex) { tcpClient?.SafeClose(); LogUtils.Debug($"建立隧道失败:{item.LocalPort}->{item.RemoteAddress}:{item.RemotePort}{Environment.NewLine}{ex}"); } if (ipClient.Connected) { tcpClient.ToClient = ipClient; ipClient.ToClient = tcpClient; RelationTcp toRelation = new RelationTcp(); toRelation.readTcp = tcpClient; toRelation.readSs = tcpClient.GetStream(); toRelation.writeTcp = tcpClient.ToClient; toRelation.writeSs = tcpClient.ToClient.GetStream(); toRelation.buffer = new byte[P2PGlobal.P2PSocketBufferSize]; RelationTcp fromRelation = new RelationTcp(); fromRelation.readTcp = toRelation.writeTcp; fromRelation.readSs = toRelation.writeSs; fromRelation.writeTcp = toRelation.readTcp; fromRelation.writeSs = toRelation.readSs; fromRelation.buffer = new byte[P2PGlobal.P2PSocketBufferSize]; StartTransferTcp_Ip(toRelation); StartTransferTcp_Ip(fromRelation); } }
public static bool BindTcp(P2PTcpClient readTcp, P2PTcpClient toTcp) { //TcpCenter.Instance.ConnectedTcpList.Add(readTcp); //TcpCenter.Instance.ConnectedTcpList.Add(toTcp); bool ret = true; RelationTcp_Ip toRelation = new RelationTcp_Ip(); EasyOp.Do(() => { toRelation.readTcp = readTcp; toRelation.readSs = readTcp.GetStream(); toRelation.writeTcp = toTcp; toRelation.writeSs = toTcp.GetStream(); toRelation.buffer = new byte[P2PGlobal.P2PSocketBufferSize]; StartTransferTcp_Ip(toRelation); }, () => { EasyOp.Do(() => { RelationTcp_Ip fromRelation = new RelationTcp_Ip(); fromRelation.readTcp = toRelation.writeTcp; fromRelation.readSs = toRelation.writeSs; fromRelation.writeTcp = toRelation.readTcp; fromRelation.writeSs = toRelation.readSs; fromRelation.buffer = new byte[P2PGlobal.P2PSocketBufferSize]; StartTransferTcp_Ip(fromRelation); }, ex => { LogUtils.Debug($"绑定Tcp失败:{Environment.NewLine}{ex}"); EasyOp.Do(readTcp.SafeClose); ret = false; }); }, ex => { LogUtils.Debug($"绑定Tcp失败:{Environment.NewLine}{ex}"); EasyOp.Do(readTcp.SafeClose); EasyOp.Do(toTcp.SafeClose); ret = false; }); return(ret); }
/// <summary> /// 监听映射端口并转发数据(ip直接转发模式) /// </summary> /// <param name="readClient"></param> private void ListenPortMapTcpWithIp(P2PTcpClient readClient) { if (readClient.ToClient == null || !readClient.ToClient.Connected) { LogUtils.Warning($"数据转发(ip模式)失败:绑定的Tcp连接已断开."); readClient.SafeClose(); return; } TcpCenter.Instance.ConnectedTcpList.Add(readClient); byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; try { NetworkStream readStream = readClient.GetStream(); NetworkStream toStream = readClient.ToClient.GetStream(); while (readClient.Connected) { int curReadLength = readStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { toStream.Write(buffer, 0, curReadLength); } else { LogUtils.Warning($"端口映射转发(ip模式):源Tcp连接已断开."); //如果tcp已关闭,需要关闭相关tcp try { readClient.ToClient.SafeClose(); } finally { } break; } } } catch (Exception ex) { LogUtils.Warning($"端口映射转发(ip模式):目标Tcp连接已断开."); readClient.SafeClose(); } TcpCenter.Instance.ConnectedTcpList.Remove(readClient); }
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; } } }
/// <summary> /// 监听映射端口并转发数据(ip直接转发模式) /// </summary> /// <param name="readClient"></param> private void ListenPortMapTcpWithIp(P2PTcpClient readClient) { if (readClient.ToClient == null || !readClient.ToClient.Connected) { Debug.WriteLine($"[错误]端口映射:目标tcp不存在"); readClient.Close(); return; } byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; NetworkStream readStream = readClient.GetStream(); NetworkStream toStream = readClient.ToClient.GetStream(); while (readClient.Connected) { int curReadLength = readStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { if (readClient.ToClient != null && readClient.ToClient.Connected) { toStream.Write(buffer, 0, curReadLength); } else { Debug.WriteLine($"远程端口已关闭{readClient.Client.RemoteEndPoint}"); readClient.Close(); break; } } else { Debug.WriteLine($"从端口{readClient.LocalEndPoint}读取到0的数据"); //如果tcp已关闭,需要关闭相关tcp if (readClient.ToClient != null && readClient.ToClient.Connected) { readClient.ToClient.Close(); } break; } } }
public static void BindTcp(P2PTcpClient readTcp, P2PTcpClient toTcp) { TcpCenter.Instance.ConnectedTcpList.Add(readTcp); byte[] buffer = new byte[P2PGlobal.P2PSocketBufferSize]; NetworkStream readStream = readTcp.GetStream(); NetworkStream toStream = toTcp.GetStream(); while (readTcp.Connected) { int curReadLength = readStream.ReadSafe(buffer, 0, buffer.Length); if (curReadLength > 0) { bool isError = true; if (toTcp != null && toTcp.Connected) { try { toStream.Write(buffer, 0, curReadLength); isError = false; } catch { } } if (isError) { LogUtils.Warning($"Tcp连接{toTcp.RemoteEndPoint}已断开."); readTcp.SafeClose(); break; } } else { LogUtils.Warning($"Tcp连接{readTcp.RemoteEndPoint}已断开."); //如果tcp已关闭,需要关闭相关tcp toTcp?.SafeClose(); break; } } TcpCenter.Instance.ConnectedTcpList.Remove(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}已断开"); }