Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public var gameName = "GameName";
- public var serverPort = 22222;
- private var probingPublicIP = false;
- private var doneTestingNAT = false;
- private var natCapable : ConnectionTesterStatus = ConnectionTesterStatus.Undetermined;
- private var useNAT : boolean = true;
- function Awake () {
- natCapable = Network.TestConnection();
- PretestConnection();
- if (Network.HavePublicAddress()){
- Debug.Log("This machine has a public IP address");
- }else{
- Debug.Log("This machine has a private IP address");
- }
- }
- function PretestConnection() {
- while(!doneTestingNAT)
- {
- TestConnection();
- yield;
- }
- }
- function HostGame(players : int, port : int) : void{
- if( natCapable == ConnectionTesterStatus.Undetermined )
- {
- errorMessage = "The connection test isn't complete. You can't connect until it is.";
- return;
- }
- errorMessage = "Starting up a game server...";
- if( players < 1 ) { players = 1; }
- Network.InitializeServer(players, port, useNAT);
- }
- function Connect( hostInfo : HostData ) : void {
- if(hostInfo.useNat) {
- Network.Connect( hostInfo.guid );
- } else {
- Network.Connect( hostInfo );
- }
- nowConnecting = true;
- }
- function TestConnection() : void
- {
- natCapable = Network.TestConnection();
- switch( natCapable )
- {
- case ConnectionTesterStatus.Error:
- testMessage = "Problem determining NAT capabilities";
- errorMessage = "Test complete. Couldn't determine NAT punchthrough ability.";
- doneTestingNAT = true;
- break;
- case ConnectionTesterStatus.Undetermined:
- testMessage = "Undetermined NAT capabilities";
- errorMessage = "Testing connection...";
- doneTestingNAT = false;
- break;
- case ConnectionTesterStatus.NATpunchthroughFullCone:
- case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
- testMessage = "NAT can punchthrough as necessary.";
- errorMessage = "";
- doneTestingNAT = true;
- useNAT = true;
- break;
- case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
- testMessage = "Everyone except Symmetric NATs can connect.";
- errorMessage = "Test complete. Some players cannot join your games.";
- doneTestingNAT = true;
- useNAT = true;
- break;
- case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
- testMessage = "NAT Punchthrough is limited to asymmetric NAT systems.";
- errorMessage = "Test complete. Your NAT punchthrough is limited, do not host games.";
- doneTestingNAT = true;
- useNAT = true;
- break;
- case ConnectionTesterStatus.PublicIPIsConnectable:
- testMessage = "Directly connectable public IP address.";
- errorMessage = "";
- doneTestingNAT = true;
- useNAT = false;
- break;
- // This case is a bit special as we now need to check if we can
- // cicrumvent the blocking by using NAT punchthrough
- case ConnectionTesterStatus.PublicIPPortBlocked:
- testMessage = "Non-connectble public IP address (port " + serverPort + " blocked), running a server is impossible.";
- errorMessage = "Test complete. Port " + serverPort + " is blocked. A server can't be started.";
- // If no NAT punchthrough test has been performed on this public IP, force a test
- if ( !probingPublicIP )
- {
- Debug.Log( "Testing if firewall can be circumvented" );
- natCapable = Network.TestConnectionNAT();
- probingPublicIP = true;
- timer = Time.time + 10;
- useNAT = false;
- }
- // NAT punchthrough test was performed but we still get blocked
- else if( Time.time > timer )
- {
- probingPublicIP = false; // reset
- doneTestingNAT = true;
- useNAT = true;
- }
- break;
- case ConnectionTesterStatus.PublicIPNoServerStarted:
- testMessage = "Server not started, it is needed to check ability to connect with server. Restart the test when ready.";
- break;
- default:
- testMessage = "Error in test routine, got " + natCapable;
- errorMessage = "Test complete. There was an error in the connection test: " + natCapable;
- }
- if( doneTestingNAT )
- {
- Debug.Log( "Test Result: " + testMessage + "\n" +
- "NAT Capability: " + natCapable + "\n" +
- "Probing Public IP: " + probingPublicIP + "\n" +
- "Done Testing NAT: " + doneTestingNAT );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement