Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public var gameName = "GameName";
  2. public var serverPort =  22222;
  3.  
  4. private var probingPublicIP = false;
  5. private var doneTestingNAT = false;
  6. private var natCapable : ConnectionTesterStatus = ConnectionTesterStatus.Undetermined;
  7. private var useNAT : boolean = true;
  8.  
  9. function Awake () {
  10.     natCapable = Network.TestConnection();
  11.     PretestConnection();
  12.     if (Network.HavePublicAddress()){
  13.         Debug.Log("This machine has a public IP address");
  14.     }else{
  15.         Debug.Log("This machine has a private IP address");
  16.     }
  17. }
  18.  
  19. function PretestConnection() {
  20.   while(!doneTestingNAT)
  21.   {
  22.     TestConnection();
  23.     yield;
  24.   }
  25. }
  26.  
  27. function HostGame(players : int, port : int) : void{
  28.   if( natCapable == ConnectionTesterStatus.Undetermined )
  29.   {
  30.     errorMessage = "The connection test isn't complete.  You can't connect until it is.";
  31.     return;
  32.   }
  33.   errorMessage = "Starting up a game server...";
  34.   if( players < 1 ) { players = 1; }
  35.   Network.InitializeServer(players, port, useNAT);
  36. }
  37.  
  38. function Connect( hostInfo : HostData ) : void {
  39.     if(hostInfo.useNat) {
  40.         Network.Connect( hostInfo.guid );
  41.     } else {
  42.         Network.Connect( hostInfo );
  43.     }
  44.     nowConnecting = true;
  45. }
  46.  
  47. function TestConnection() : void
  48. {
  49.   natCapable = Network.TestConnection();
  50.  
  51.   switch( natCapable )
  52.   {
  53.     case ConnectionTesterStatus.Error:
  54.       testMessage = "Problem determining NAT capabilities";
  55.       errorMessage = "Test complete.  Couldn't determine NAT punchthrough ability.";
  56.       doneTestingNAT = true;
  57.       break;
  58.            
  59.     case ConnectionTesterStatus.Undetermined:
  60.       testMessage = "Undetermined NAT capabilities";
  61.       errorMessage = "Testing connection...";
  62.       doneTestingNAT = false;
  63.       break;
  64.      
  65.     case ConnectionTesterStatus.NATpunchthroughFullCone:
  66.     case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
  67.       testMessage = "NAT can punchthrough as necessary.";
  68.       errorMessage = "";
  69.       doneTestingNAT = true;
  70.       useNAT = true;
  71.       break;
  72.      
  73.     case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
  74.         testMessage = "Everyone except Symmetric NATs can connect.";
  75.         errorMessage = "Test complete.  Some players cannot join your games.";
  76.         doneTestingNAT = true;
  77.         useNAT = true;
  78.         break;
  79.    
  80.     case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
  81.         testMessage = "NAT Punchthrough is limited to asymmetric NAT systems.";
  82.         errorMessage = "Test complete.  Your NAT punchthrough is limited, do not host games.";
  83.         doneTestingNAT = true;
  84.         useNAT = true;
  85.         break;
  86.                    
  87.     case ConnectionTesterStatus.PublicIPIsConnectable:
  88.         testMessage = "Directly connectable public IP address.";
  89.         errorMessage = "";
  90.         doneTestingNAT = true;
  91.         useNAT = false;
  92.         break;
  93.        
  94.     // This case is a bit special as we now need to check if we can
  95.     // cicrumvent the blocking by using NAT punchthrough
  96.     case ConnectionTesterStatus.PublicIPPortBlocked:
  97.         testMessage = "Non-connectble public IP address (port " + serverPort + " blocked), running a server is impossible.";
  98.         errorMessage = "Test complete.  Port " + serverPort + " is blocked.  A server can't be started.";
  99.         // If no NAT punchthrough test has been performed on this public IP, force a test
  100.         if ( !probingPublicIP )
  101.         {
  102.           Debug.Log( "Testing if firewall can be circumvented" );
  103.           natCapable = Network.TestConnectionNAT();
  104.           probingPublicIP = true;
  105.           timer = Time.time + 10;
  106.           useNAT = false;
  107.         }
  108.         // NAT punchthrough test was performed but we still get blocked
  109.         else if( Time.time > timer )
  110.         {
  111.           probingPublicIP = false;      // reset
  112.           doneTestingNAT = true;
  113.           useNAT = true;
  114.         }
  115.         break;
  116.        
  117.     case ConnectionTesterStatus.PublicIPNoServerStarted:
  118.         testMessage = "Server not started, it is needed to check ability to connect with server. Restart the test when ready.";
  119.         break;
  120.    
  121.     default:
  122.         testMessage = "Error in test routine, got " + natCapable;
  123.         errorMessage = "Test complete.  There was an error in the connection test: " + natCapable;
  124.   }
  125.  
  126.   if( doneTestingNAT )
  127.   {
  128.     Debug.Log( "Test Result: " + testMessage + "\n" +
  129.                "NAT Capability: " + natCapable + "\n" +
  130.                "Probing Public IP: " + probingPublicIP + "\n" +
  131.                "Done Testing NAT: " + doneTestingNAT );
  132.   }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement