void TestConnection()
    {
        natCapable = Network.TestConnection();

        switch (natCapable)
        {
        case ConnectionTesterStatus.Error:
            testMessage = "Problem determining NAT capabilities";
            doneTesting = true;
            break;

        case ConnectionTesterStatus.Undetermined:
            testMessage = "Testing NAT capabilities";
            doneTesting = false;
            break;

        case ConnectionTesterStatus.PublicIPIsConnectable:
            testMessage = "Directly connectable public IP address";
            useNat      = false;
            doneTesting = true;
            break;

        case ConnectionTesterStatus.PublicIPPortBlocked:
            testMessage = "Non-connectable public IP address (port " + serverPort + " blocked) running a server is impossible.";
            useNat      = false;

            if (!probingPublicIP)
            {
                Debug.Log("Testing if firewall can be circumvented");

                natCapable      = Network.TestConnectionNAT();
                probingPublicIP = true;
                timer           = Time.time + 10;
            }
            else if (Time.time > timer)
            {
                probingPublicIP = false;
                useNat          = true;
                doneTesting     = true;
            }
            break;

        case ConnectionTesterStatus.PublicIPNoServerStarted:
            testMessage = "Public IP address but server not inititalized, it must be started to check server accessiblity. Restart connect test when ready.";
            doneTesting = true;
            break;

        case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
            testMessage = "Limited NAT punchthrough capabilities. Cannot " +
                          "connect to all types of NAT servers. Running a server " +
                          "is ill advised as not everyone can connect.";
            useNat      = true;
            doneTesting = true;
            break;

        default:
            testMessage = "Error in test routine, got " + natCapable;

            if (string.Compare("Limited", 0, natCapable.ToString(), 0, 7) == 0)
            {
                useNat      = true;
                doneTesting = true;
                break;
            }
            break;
        }
        Debug.Log(testMessage);
    }
Beispiel #2
0
    // Test Connection is used to check the connection to the Game Server (GS) to determine if it's behind a router/FW
    // with a different public IP and private IP.  This helps us determine if we need to try using NAT Punch Through to connect
    // Network.useNat will be set based on the tested findings.  This is used on the client to figure out how to connect
    // On the GS we check !Network.HavePublicAddress() and pass it into the MGS when we register so it know if NPT is required
    void TestConnection()
    {
        // Start/Poll the connection test, report the results in a label and react to the results accordingly
        natCapable = Network.TestConnection();

        switch (natCapable)
        {
        case ConnectionTesterStatus.Error:
            testMessage = "Problem determining NAT capabilities";
            doneTesting = true;
            break;

        case ConnectionTesterStatus.Undetermined:
            testMessage = "Testing NAT capabilities";
            doneTesting = false;
            break;

        case ConnectionTesterStatus.PublicIPIsConnectable:
            testMessage = "Directly connectable public IP address.";
            useNat      = false;
            doneTesting = true;
            break;

        // This case is a bit special as we now need to check if we can
        // use the blocking by using NAT punchthrough
        case ConnectionTesterStatus.PublicIPPortBlocked:
            testMessage = "Non-connectble public IP address (port " + serverPort + "	blocked),"
                          + " running a server is impossible.";
            useNat = false;
            // If no NAT punchthrough test has been performed on this public IP, force a test
            if (!probingPublicIP)
            {
                //Debug.Log("Testing if firewall can be circumnvented");
                natCapable      = Network.TestConnectionNAT();
                probingPublicIP = true;
                timer           = Time.time + 10;
            }
            // NAT punchthrough test was performed but we still get blocked
            else if (Time.time > timer)
            {
                probingPublicIP = false;                         // reset
                useNat          = true;
                doneTesting     = true;
            }
            break;

        case ConnectionTesterStatus.PublicIPNoServerStarted:
            testMessage = "Public IP address but server not initialized,"
                          + "it must be started to check server accessibility. Restart connection test when ready.";
            doneTesting = true;
            break;

        default:
            testMessage = "Error in test routine, got " + natCapable;
            if (string.Compare("Limited", 0, natCapable.ToString(), 0, 7) == 0)
            {
                useNat = true;
            }
            doneTesting = true;
            break;
        }         // end switch
                  //Debug.Log(testMessage);
    }