private void ConnectServer() { try { Global.P2PServerTcp = new P2PTcpClient(Global.ServerAddress, Global.ServerPort); } catch { Global.P2PServerTcp = null; LogUtils.Error($"{DateTime.Now.ToString("【失败】[HH:mm:ss]")}连接服务器:{Global.ServerAddress}:{Global.ServerPort}"); } if (Global.P2PServerTcp != null) { LogUtils.Show($"{DateTime.Now.ToString("【成功】[HH:mm:ss]")}连接服务器:{Global.ServerAddress}:{Global.ServerPort}"); Global.P2PServerTcp.IsAuth = true; //接收数据 Global.TaskFactory.StartNew(() => { //向服务器发送客户端信息 InitServerInfo(Global.P2PServerTcp); //监听来自服务器的消息 Global_Func.ListenTcp <ReceivePacket>(Global.P2PServerTcp); }); } }
internal void ConnectServer() { try { tcpCenter.P2PServerTcp = new P2PTcpClient(appCenter.Config.ServerAddress, appCenter.Config.ServerPort); } catch { LogUtils.Error($"连接服务器失败:{appCenter.Config.ServerAddress}:{appCenter.Config.ServerPort}"); return; } LogUtils.Info($"已连接服务器:{appCenter.Config.ServerAddress}:{appCenter.Config.ServerPort}", false); tcpCenter.P2PServerTcp.IsAuth = true; //向服务器发送客户端信息 InitServerInfo(tcpCenter.P2PServerTcp); //监听来自服务器的消息 Global_Func.ListenTcp <ReceivePacket>(tcpCenter.P2PServerTcp); }
internal void ConnectServer() { try { TcpCenter.Instance.P2PServerTcp = new P2PTcpClient(ConfigCenter.Instance.ServerAddress, ConfigCenter.Instance.ServerPort); } catch { LogUtils.Error($"{DateTime.Now.ToString("HH:mm:ss")} 无法连接服务器:{ConfigCenter.Instance.ServerAddress}:{ConfigCenter.Instance.ServerPort}"); return; } LogUtils.Info($"{DateTime.Now.ToString("HH:mm:ss")} 已连接服务器:{ConfigCenter.Instance.ServerAddress}:{ConfigCenter.Instance.ServerPort}", false); TcpCenter.Instance.P2PServerTcp.IsAuth = true; AppCenter.Instance.StartNewTask(() => { //向服务器发送客户端信息 InitServerInfo(TcpCenter.Instance.P2PServerTcp); //监听来自服务器的消息 Global_Func.ListenTcp <ReceivePacket>(TcpCenter.Instance.P2PServerTcp); }); }
private void ConnectServer() { try { P2PTcpClient p2PTcpClient = new P2PTcpClient(Global.ServerAddress, Global.ServerPort); ConsoleUtils.Write($"服务器{Global.ServerAddress}:{Global.ServerPort}连接成功."); p2PTcpClient.IsAuth = true; Global.P2PServerTcp = p2PTcpClient; //接收数据 Global.TaskFactory.StartNew(() => { //向服务器发送客户端信息 InitServerInfo(p2PTcpClient); //监听来自服务器的消息 Global_Func.ListenTcp <RecievePacket>(p2PTcpClient); }); } catch { ConsoleUtils.Write($"服务器{Global.ServerAddress}:{Global.ServerPort}连接失败!"); } }
/// <summary> /// 端口映射监听端口的新tcp回调方法 /// </summary> /// <param name="ar"></param> public void AcceptSocket_Server(IAsyncResult ar) { ListenSt st = (ListenSt)ar.AsyncState; TcpListener listener = st.listener; PortMapItem item = st.item; Socket socket = null; try { //获取当前接入的tcp socket = listener.EndAcceptSocket(ar); } catch (Exception ex) { LogUtils.Error(ex.ToString()); return; } EasyOp.Do(() => { listener.BeginAcceptSocket(AcceptSocket_Server, st); }, () => { EasyOp.Do(() => { if (tcpCenter.P2PServerTcp != null && tcpCenter.P2PServerTcp.Connected) { P2PTcpClient tcpClient = new P2PTcpClient(socket); //加入待连接集合 P2PResult result = new P2PResult(); tcpCenter.WaiteConnetctTcp.Add(tcpClient.Token, result); //tcpCenter.WaiteConnetctTcp.Add(tcpClient.Token, tcpClient); //发送消息给服务端,开始内网穿透 Send_0x0201_Apply packet = new Send_0x0201_Apply(tcpClient.Token, item.RemoteAddress, item.RemotePort, item.P2PType); LogUtils.Debug(string.Format("正在建立{0}隧道 token:{1} client:{2} port:{3}", item.P2PType == 0 ? "中转模式" : "P2P模式", tcpClient.Token, item.RemoteAddress, item.RemotePort)); byte[] dataAr = packet.PackData(); EasyOp.Do(() => { tcpCenter.P2PServerTcp.BeginSend(dataAr); }, () => { //等待指定超时时间后,判断是否连接成功 Monitor.Enter(result.block); if (Monitor.Wait(result.block, AppConfig.P2PTimeout)) { if (tcpCenter.WaiteConnetctTcp[tcpClient.Token].IsError) { LogUtils.Debug(tcpCenter.WaiteConnetctTcp[tcpClient.Token].ErrorMsg); tcpClient.SafeClose(); } else { P2PTcpClient destTcp = tcpCenter.WaiteConnetctTcp[tcpClient.Token].Tcp; tcpClient.IsAuth = destTcp.IsAuth = true; destTcp.ToClient = tcpClient; tcpClient.ToClient = destTcp; if (item.P2PType == 0) { Global_Func.ListenTcp <Packet_0x0202>(tcpClient); LogUtils.Debug($"中转模式隧道,连接成功 token:{tcpClient.Token}"); } else { if (Global_Func.BindTcp(tcpClient, destTcp)) { LogUtils.Debug($"P2P模式隧道,连接成功 token:{tcpClient.Token}"); } else { LogUtils.Debug($"P2P模式隧道,连接失败 token:{tcpClient.Token}"); EasyOp.Do(tcpClient.SafeClose); EasyOp.Do(destTcp.SafeClose); } } } } else { LogUtils.Debug($"建立隧道失败:token:{tcpClient.Token} {item.LocalPort}->{item.RemoteAddress}:{item.RemotePort} {AppConfig.P2PTimeout / 1000}秒无响应,已超时."); EasyOp.Do(tcpClient.SafeClose); } tcpCenter.WaiteConnetctTcp.Remove(tcpClient.Token); Monitor.Exit(result.block); //if (tcpCenter.WaiteConnetctTcp.ContainsKey(tcpClient.Token)) //{ // LogUtils.Debug($"建立隧道失败:token:{tcpClient.Token} {item.LocalPort}->{item.RemoteAddress}:{item.RemotePort} {AppConfig.P2PTimeout / 1000}秒无响应,已超时."); // tcpCenter.WaiteConnetctTcp[tcpClient.Token].tcp?.SafeClose(); // tcpCenter.WaiteConnetctTcp.Remove(tcpClient.Token); //} }, ex => { EasyOp.Do(tcpClient.SafeClose); LogUtils.Debug($"建立隧道失败,无法连接服务器:token:{tcpClient.Token} {item.LocalPort}->{item.RemoteAddress}:{item.RemotePort}."); }); } else { LogUtils.Debug($"建立隧道失败:未连接到服务器!"); socket.Close(); } }, ex => { LogUtils.Debug("处理新tcp连接时发生错误:" + Environment.NewLine + ex.ToString()); }); }, ex => { LogUtils.Error("监听端口发生错误:" + listener.LocalEndpoint.ToString() + Environment.NewLine + ex.ToString()); }); }
public bool CreateP2PConnect(PortMapItem item, Socket socket) { bool ret = false; if (tcpCenter.P2PServerTcp != null && tcpCenter.P2PServerTcp.Connected) { P2PTcpClient tcpClient = new P2PTcpClient(socket); //加入待连接集合 P2PResult result = new P2PResult(); tcpCenter.WaiteConnetctTcp.Add(tcpClient.Token, result); //tcpCenter.WaiteConnetctTcp.Add(tcpClient.Token, tcpClient); //发送消息给服务端,开始内网穿透 Send_0x0201_Apply packet = new Send_0x0201_Apply(tcpClient.Token, item.RemoteAddress, item.RemotePort, item.P2PType); LogUtils.Debug(string.Format("正在建立{0}隧道 token:{1} client:{2} port:{3}", item.P2PType == 0 ? "中转模式" : "P2P模式", tcpClient.Token, item.RemoteAddress, item.RemotePort)); byte[] dataAr = packet.PackData(); EasyOp.Do(() => { tcpCenter.P2PServerTcp.BeginSend(dataAr); }, () => { Monitor.Enter(result.block); if (Monitor.Wait(result.block, AppConfig.P2PTimeout)) { if (tcpCenter.WaiteConnetctTcp[tcpClient.Token].IsError) { LogUtils.Debug(tcpCenter.WaiteConnetctTcp[tcpClient.Token].ErrorMsg); //tcpClient.SafeClose(); } else { P2PTcpClient destTcp = tcpCenter.WaiteConnetctTcp[tcpClient.Token].Tcp; tcpClient.IsAuth = destTcp.IsAuth = true; destTcp.ToClient = tcpClient; tcpClient.ToClient = destTcp; if (item.P2PType == 0) { Global_Func.ListenTcp <Packet_0x0202>(tcpClient); ret = true; LogUtils.Debug($"中转模式隧道,连接成功 token:{tcpClient.Token}"); } else { if (Global_Func.BindTcp(tcpClient, destTcp)) { ret = true; LogUtils.Debug($"P2P模式隧道,连接成功 token:{tcpClient.Token}"); } else { LogUtils.Debug($"P2P模式隧道,连接失败 token:{tcpClient.Token}"); EasyOp.Do(destTcp.SafeClose); } } } } else { LogUtils.Debug($"建立隧道失败:token:{tcpClient.Token} {item.LocalPort}->{item.RemoteAddress}:{item.RemotePort} {AppConfig.P2PTimeout / 1000}秒无响应,已超时."); } tcpCenter.WaiteConnetctTcp.Remove(tcpClient.Token); Monitor.Exit(result.block); }, ex => { LogUtils.Debug($"建立隧道失败,无法连接服务器:token:{tcpClient.Token} {item.LocalPort}->{item.RemoteAddress}:{item.RemotePort}."); }); } else { LogUtils.Debug($"建立隧道失败:未连接到服务器!"); } return(ret); }