/// <summary>
        /// Initialize UPNP
        /// </summary>
        /// <param name="Port">The port</param>
        /// <returns>The UPNP object</returns>
        protected UPNP InitUPNP(int Port)
        {
            UPNP NewUPNP = new UPNP();

            UPNP.UPNPResult DiscoverResult = (UPNP.UPNPResult)NewUPNP.Discover();
            MDLog.Info(LOG_CAT, $"UPNP Result for Discover is {DiscoverResult}");
            UPNP.UPNPResult MappingResult = (UPNP.UPNPResult)NewUPNP.AddPortMapping(Port);
            MDLog.Info(LOG_CAT, $"UPNP Result for Mapping Port {Port} is {MappingResult}");
            ExternalAddress = NewUPNP.QueryExternalAddress();
            MDLog.Info(LOG_CAT, $"UPNP External address found [{ExternalAddress}]");
            return(NewUPNP);
        }
Exemple #2
0
    // Main menu network controls:
    public void OnHostPressed()
    {
        ClientVariables.SaveMainMenu();
        if (ClientVariables.NetworkOptions.UseUPNP)
        {
            UPNP            upnp     = new UPNP();
            UPNP.UPNPResult resultV4 = (UPNP.UPNPResult)upnp.Discover(2000, 2, "InternetGatewayDevice");
            if (resultV4 == UPNP.UPNPResult.Success)
            {
                GD.Print("Will attempt to add port-forward with upnp ip v4");
                upnp.AddPortMapping(ClientVariables.NetworkOptions.Port);
            }
            else
            {
                upnp.DiscoverIpv6 = true;
                UPNP.UPNPResult resultV6 = (UPNP.UPNPResult)upnp.Discover(2000, 2, "InternetGatewayDevice");
                if (resultV6 == UPNP.UPNPResult.Success)
                {
                    GD.Print("Will attemt to add port-forward with upnp ip v6");
                    upnp.AddPortMapping(ClientVariables.NetworkOptions.Port);
                }
            }
        }
        var   peer   = new NetworkedMultiplayerENet();
        Error result = peer.CreateServer(ClientVariables.NetworkOptions.Port);

        if (result == Error.Ok)
        {
            GetTree().NetworkPeer = peer;
            Global.GotoScene("res://Session/Session.tscn");
        }
        else
        {
            GD.Print(result);
            GD.Print("On port:" + ClientVariables.NetworkOptions.Port);
        }
    }