public void TryBindP2PTcp()
        {
            string ip          = BinaryUtils.ReadString(data);
            int    port        = BinaryUtils.ReadInt(data);
            string token       = BinaryUtils.ReadString(data);
            int    maxTryCount = 1;
            bool   isConnected = false;
            int    bindPort    = Convert.ToInt32(m_tcpClient.Client.LocalEndPoint.ToString().Split(':')[1]);

            Task.Factory.StartNew(() =>
            {
                P2PTcpClient p2pClient = new P2PTcpClient();
                p2pClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                p2pClient.Client.Bind(new IPEndPoint(IPAddress.Any, bindPort));
                while (maxTryCount > 0)
                {
                    maxTryCount--;
                    try
                    {
                        p2pClient.Connect(ip, port);
                        p2pClient.UpdateEndPoint();
                        isConnected = true;
                        break;
                    }
                    catch (Exception ex)
                    {
                        LogUtils.Debug($"P2P连接失败:{bindPort}\r\n{ex.ToString()}");
                        Thread.Sleep(100);
                    }
                }
                if (isConnected)
                {
                    LogUtils.Info($"命令:0x0201  内网穿透(P2P模式)连接成功 port:{bindPort} token:{token}");
                    P2PBind_DirectConnect(p2pClient, token);
                }
                else
                {
                    LogUtils.Info($"命令:0x0201  内网穿透(P2P模式)连接失败 port:{bindPort} token:{token}");
                    p2pClient.SafeClose();
                    if (TcpCenter.Instance.WaiteConnetctTcp.ContainsKey(token))
                    {
                        P2PTcpClient portClient = TcpCenter.Instance.WaiteConnetctTcp[token];
                        portClient.SafeClose();
                        TcpCenter.Instance.WaiteConnetctTcp.Remove(token);
                    }
                }
                m_tcpClient.SafeClose();
            });
        }
示例#2
0
 public void CreateTcpFromSource_DirectConnect(string token)
 {
     Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token);
     Utils.LogUtils.Debug($"命令:0x0201  正尝试建立P2P模式隧道 token:{token}");
     if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
     {
         P2PTcpClient serverClient = new P2PTcpClient();
         serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         EasyOp.Do(() =>
         {
             serverClient.Connect(appCenter.ServerAddress, appCenter.ServerPort);
         }, () =>
         {
             serverClient.IsAuth = true;
             serverClient.UpdateEndPoint();
             EasyOp.Do(() =>
             {
                 serverClient.BeginSend(sendPacket.PackData());
             },
                       () =>
             {
                 EasyOp.Do(() =>
                 {
                     Global_Func.ListenTcp <ReceivePacket>(serverClient);
                     LogUtils.Debug($"命令:0x0201 P2P模式隧道,已连接到服务器,等待下一步操作 token:{token}");
                 }, ex =>
                 {
                     LogUtils.Debug($"命令:0x0201 P2P模式隧道,服务器连接被强制断开 token:{token}:{Environment.NewLine}{ex}");
                     tcpCenter.WaiteConnetctTcp.Remove(token);
                     EasyOp.Do(serverClient.SafeClose);
                 });
             }, ex =>
             {
                 LogUtils.Debug($"命令:0x0201 P2P模式隧道,隧道打洞失败 token:{token}:{Environment.NewLine} 隧道被服务器强制断开");
                 tcpCenter.WaiteConnetctTcp.Remove(token);
             });
         }, ex =>
         {
             LogUtils.Debug($"命令:0x0201 P2P模式隧道,无法连接服务器 token:{token}:{Environment.NewLine}{ex}");
             tcpCenter.WaiteConnetctTcp.Remove(token);
         });
     }
     else
     {
         LogUtils.Debug($"命令:0x0201 接收到建立P2P模式隧道命令,但已超时. token:{token}");
     }
 }
示例#3
0
        public void CreateTcpFromDest_DirectConnect(string token)
        {
            int port = BinaryUtils.ReadInt(data);

            Utils.LogUtils.Debug($"命令:0x0201  正尝试建立P2P模式隧道 token:{token}");
            P2PTcpClient serverClient = new P2PTcpClient();

            serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            EasyOp.Do(() =>
            {
                serverClient.Connect(appCenter.ServerAddress, appCenter.ServerPort);
            },
                      () =>
            {
                serverClient.IsAuth       = true;
                serverClient.P2PLocalPort = port;
                serverClient.UpdateEndPoint();
                Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token);

                EasyOp.Do(() =>
                {
                    serverClient.BeginSend(sendPacket.PackData());
                },
                          () =>
                {
                    EasyOp.Do(() =>
                    {
                        Global_Func.ListenTcp <ReceivePacket>(serverClient);
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,已连接到服务器,等待下一步操作 token:{token}");
                    }, ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,服务器连接被强制断开 token:{token}:{Environment.NewLine}{ex}");
                        EasyOp.Do(serverClient.Close);
                    });
                }, ex =>
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,隧道打洞失败 token:{token}:{Environment.NewLine} 隧道被服务器强制断开");
                });
            }, ex =>
            {
                LogUtils.Debug($"命令:0x0201 P2P模式隧道,无法连接服务器 token:{token}:{Environment.NewLine}{ex}");
            });
        }
 public void CreateTcpFromSource_DirectConnect(string token)
 {
     Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token);
     Utils.LogUtils.Info($"命令:0x0201  正尝试内网穿透(P2P模式)token:{token}");
     if (TcpCenter.Instance.WaiteConnetctTcp.ContainsKey(token))
     {
         P2PTcpClient serverClient = new P2PTcpClient();
         serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         serverClient.Connect(ConfigCenter.Instance.ServerAddress, ConfigCenter.Instance.ServerPort);
         serverClient.IsAuth = true;
         serverClient.UpdateEndPoint();
         serverClient.Client.Send(sendPacket.PackData());
         AppCenter.Instance.StartNewTask(() => { Global_Func.ListenTcp <ReceivePacket>(serverClient); });
     }
     else
     {
         LogUtils.Warning($"命令:0x0201 接收到内网穿透(P2P模式)命令,但已超时. token:{token}");
     }
 }
 public void CreateTcpFromDest_DirectConnect(string token)
 {
     try
     {
         int port = BinaryUtils.ReadInt(data);
         Models.Send.Send_0x0201_Bind sendPacket = new Models.Send.Send_0x0201_Bind(token);
         Utils.LogUtils.Info($"命令:0x0201  正尝试内网穿透(P2P模式) token:{token}");
         P2PTcpClient serverClient = new P2PTcpClient();
         serverClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
         serverClient.Connect(ConfigCenter.Instance.ServerAddress, ConfigCenter.Instance.ServerPort);
         serverClient.IsAuth       = true;
         serverClient.P2PLocalPort = port;
         serverClient.UpdateEndPoint();
         serverClient.Client.Send(sendPacket.PackData());
         AppCenter.Instance.StartNewTask(() => { Global_Func.ListenTcp <ReceivePacket>(serverClient); });
     }
     catch (Exception ex)
     {
         LogUtils.Error($"命令:0x0201 尝试内网穿透(P2P模式) 发生错误:{Environment.NewLine}{ex.Message}");
     }
 }
示例#6
0
        public void TryBindP2PTcp()
        {
            string       ip        = BinaryUtils.ReadString(data);
            int          port      = BinaryUtils.ReadInt(data);
            string       token     = BinaryUtils.ReadString(data);
            int          bindPort  = Convert.ToInt32(m_tcpClient.Client.LocalEndPoint.ToString().Split(':')[1]);
            P2PTcpClient p2pClient = new P2PTcpClient();

            p2pClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            EasyOp.Do(() =>
            {
                p2pClient.Client.Bind(new IPEndPoint(IPAddress.Any, bindPort));
            },
                      () =>
            {
                int tryCount = 3;
                while (!p2pClient.Connected && tryCount > 0)
                {
                    EasyOp.Do(() =>
                    {
                        p2pClient.Connect(ip, port);
                        p2pClient.UpdateEndPoint();
                    }, ex => {
                        LogUtils.Trace($"命令:0x0201 P2P模式隧道,端口打洞错误{ex}");
                    });
                    tryCount--;
                }
                if (p2pClient.Connected)
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,端口复用成功 port:{bindPort} token:{token}");
                    P2PBind_DirectConnect(p2pClient, token);
                }
                else
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,打洞失败 token:{token}");
                    EasyOp.Do(p2pClient.SafeClose);
                    //如果是发起端,清空集合
                    if (m_tcpClient.P2PLocalPort <= 0)
                    {
                        if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                        {
                            P2PTcpClient portClient = tcpCenter.WaiteConnetctTcp[token];
                            EasyOp.Do(portClient.SafeClose);
                            tcpCenter.WaiteConnetctTcp.Remove(token);
                        }
                    }
                }
                EasyOp.Do(m_tcpClient.SafeClose);
            },
                      ex =>
            {
                LogUtils.Debug($"命令:0x0201 P2P模式隧道,端口复用失败 token:{token}:{Environment.NewLine}{ex}");
                //如果是发起端,清空集合
                if (m_tcpClient.P2PLocalPort <= 0)
                {
                    if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                    {
                        tcpCenter.WaiteConnetctTcp.Remove(token);
                    }
                }
            });
        }
示例#7
0
        protected virtual void TryBindP2PTcp()
        {
            string       ip        = BinaryUtils.ReadString(data);
            int          port      = BinaryUtils.ReadInt(data);
            string       token     = BinaryUtils.ReadString(data);
            int          bindPort  = Convert.ToInt32(m_tcpClient.Client.LocalEndPoint.ToString().Split(':')[1]);
            P2PTcpClient p2pClient = new P2PTcpClient();

            p2pClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            EasyOp.Do(() =>
            {
                p2pClient.Client.Bind(new IPEndPoint(IPAddress.Any, bindPort));
            },
                      () =>
            {
                if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                {
                    int p2pMode = tcpCenter.WaiteConnetctTcp[token].P2PType;
                    if ((p2pMode & 1) > 0)
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,开始端口复用打洞");
                        int tryCount = 3;
                        while (!p2pClient.Connected && tryCount > 0)
                        {
                            EasyOp.Do(() =>
                            {
                                p2pClient.Connect(ip, port);
                                p2pClient.UpdateEndPoint();
                            }, ex =>
                            {
                                LogUtils.Trace($"命令:0x0201 P2P模式隧道,端口打洞错误{ex}");
                            });
                            tryCount--;
                        }
                    }
                    if ((p2pMode & 2) > 0)
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,开始端口预测打洞");
                        p2pClient = TryRadomPort(ip, port);
                    }
                }

                if (p2pClient != null && p2pClient.Connected)
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,打洞成功 port:{bindPort} token:{token}");
                    P2PBind_DirectConnect(p2pClient, token);
                }
                else
                {
                    EasyOp.Do(() => { p2pClient.SafeClose(); });
                    //如果是发起端,清空集合
                    if (m_tcpClient.P2PLocalPort <= 0)
                    {
                        if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                        {
                            tcpCenter.WaiteConnetctTcp[token].ErrorMsg = $"命令:0x0201 P2P模式隧道,打洞失败 token:{token}";
                            tcpCenter.WaiteConnetctTcp[token].PulseBlock();
                        }
                    }
                    else
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,打洞失败 token:{token}");
                    }
                }
                EasyOp.Do(() => { m_tcpClient.SafeClose(); });
            },
                      ex =>
            {
                //如果是发起端,清空集合
                if (m_tcpClient.P2PLocalPort <= 0)
                {
                    if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                    {
                        tcpCenter.WaiteConnetctTcp[token].ErrorMsg = $"命令:0x0201 P2P模式隧道,端口复用失败 token:{token}:{Environment.NewLine}{ex}";
                        tcpCenter.WaiteConnetctTcp[token].PulseBlock();
                    }
                }
                else
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,端口复用失败 token:{token}:{Environment.NewLine}{ex}");
                }
            });
        }