Ejemplo n.º 1
0
        public void P2PBind_DirectConnect(P2PTcpClient p2pClient, string token)
        {
            try
            {
                if (m_tcpClient.P2PLocalPort > 0)
                {
                    int         port    = m_tcpClient.P2PLocalPort;
                    PortMapItem destMap = ConfigCenter.Instance.PortMapList.FirstOrDefault(t => t.LocalPort == port && string.IsNullOrEmpty(t.LocalAddress));

                    P2PTcpClient portClient = null;

                    if (destMap != null)
                    {
                        if (destMap.MapType == PortMapType.ip)
                        {
                            portClient = new P2PTcpClient(destMap.RemoteAddress, destMap.RemotePort);
                        }
                        else
                        {
                            portClient = new P2PTcpClient("127.0.0.1", port);
                        }
                    }
                    else
                    {
                        portClient = new P2PTcpClient("127.0.0.1", port);
                    }


                    portClient.IsAuth   = p2pClient.IsAuth = true;
                    portClient.ToClient = p2pClient;
                    p2pClient.ToClient  = portClient;
                    AppCenter.Instance.StartNewTask(() => { Global_Func.BindTcp(p2pClient, portClient); });
                    AppCenter.Instance.StartNewTask(() => { Global_Func.BindTcp(portClient, p2pClient); });
                }
                else
                {
                    if (TcpCenter.Instance.WaiteConnetctTcp.ContainsKey(token))
                    {
                        P2PTcpClient portClient = TcpCenter.Instance.WaiteConnetctTcp[token];
                        TcpCenter.Instance.WaiteConnetctTcp.Remove(token);
                        portClient.IsAuth   = p2pClient.IsAuth = true;
                        portClient.ToClient = p2pClient;
                        p2pClient.ToClient  = portClient;
                        AppCenter.Instance.StartNewTask(() => { Global_Func.BindTcp(p2pClient, portClient); });
                        AppCenter.Instance.StartNewTask(() => { Global_Func.BindTcp(portClient, p2pClient); });
                    }
                    else
                    {
                        LogUtils.Warning($"命令:0x0201 接收到内网穿透命令,但已超时. token:{token}");
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public void P2PBind_DirectConnect(P2PTcpClient p2pClient, string token)
        {
            if (m_tcpClient.P2PLocalPort > 0)
            {
                //B端
                int         port    = m_tcpClient.P2PLocalPort;
                PortMapItem destMap = appCenter.PortMapList.FirstOrDefault(t => t.LocalPort == port && string.IsNullOrEmpty(t.LocalAddress));

                P2PTcpClient portClient = null;

                EasyOp.Do(() =>
                {
                    if (destMap != null)
                    {
                        if (destMap.MapType == PortMapType.ip)
                        {
                            portClient = new P2PTcpClient(destMap.RemoteAddress, destMap.RemotePort);
                        }
                        else
                        {
                            portClient = new P2PTcpClient("127.0.0.1", port);
                        }
                    }
                    else
                    {
                        portClient = new P2PTcpClient("127.0.0.1", port);
                    }
                },
                          () =>
                {
                    portClient.IsAuth   = p2pClient.IsAuth = true;
                    portClient.ToClient = p2pClient;
                    p2pClient.ToClient  = portClient;
                    EasyOp.Do(() =>
                    {
                        if (Global_Func.BindTcp(p2pClient, portClient))
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接成功 token:{token}");
                        }
                        else
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}");
                        }
                    },
                              ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                    });
                },
                          ex =>
                {
                    LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接目标端口失败 token:{token}:{Environment.NewLine}{ex}");
                    EasyOp.Do(p2pClient.SafeClose);
                });
            }
            else
            {
                //A端,发起端
                if (tcpCenter.WaiteConnetctTcp.ContainsKey(token))
                {
                    P2PTcpClient portClient = tcpCenter.WaiteConnetctTcp[token];
                    tcpCenter.WaiteConnetctTcp.Remove(token);
                    portClient.IsAuth   = p2pClient.IsAuth = true;
                    portClient.ToClient = p2pClient;
                    p2pClient.ToClient  = portClient;
                    EasyOp.Do(() =>
                    {
                        if (Global_Func.BindTcp(p2pClient, portClient))
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接成功 token:{token}");
                        }
                        else
                        {
                            LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}");
                        }
                    },
                              ex =>
                    {
                        LogUtils.Debug($"命令:0x0201 P2P模式隧道,连接失败 token:{token}:{Environment.NewLine}{ex}");
                    });
                }
                else
                {
                    LogUtils.Debug($"命令:0x0201 接收到建立隧道命令,但已超时. token:{token}");
                    EasyOp.Do(p2pClient.SafeClose);
                }
            }
        }