Tasharen Entertainment Forum

Support => TNet 3 Support => Topic started by: kcavasos_fuelfx on July 30, 2014, 11:02:27 AM

Title: Server doesn't seem to close on editor stop
Post by: kcavasos_fuelfx on July 30, 2014, 11:02:27 AM
On first run, everything connects properly and I can play my game. I stop the editor's play instance. Try and start it again and this error comes up.

[TNet] Udp.Start: Only one usage of each socket address (protocol/network address/port) is normally permitted.

It seems like the server is still running after closing the player since a reboot fixes this problem for one iteration.
Is there a fix for this where I can play in editor, stop, and have the server shut down as well?
Title: Re: Server doesn't seem to close on editor stop
Post by: ArenMook on July 30, 2014, 11:15:24 AM
How do you start the server? If it's from within Unity, then hitting Stop should stop the server as well. It's done in TNServerInstance.OnDestroy():
  1.         void OnDestroy ()
  2.         {
  3.                 Disconnect();
  4.                 mUp.WaitForThreads();
  5.         }
...which is guaranteed to be called when you hit the stop button.
Title: Re: Server doesn't seem to close on editor stop
Post by: kcavasos_fuelfx on July 30, 2014, 11:59:38 AM
I'm starting the server from within Unity.

  1. //=========================================================================
  2.         // StartServer()
  3.         // Wrapper for TNet's start server stuff.
  4.         //=========================================================================
  5.         void StartServer()
  6.         {
  7.                 Debug.Log("Starting server");
  8.                 int udpPort = Random.Range(10000, 40000);
  9.                 TNLobbyClient lobby = GetComponent<TNLobbyClient>();
  10.                
  11.                 if (lobby == null)
  12.                 {
  13.                         TNServerInstance.Start(serverTcpPort, udpPort);
  14.                 }
  15.                 else
  16.                 {
  17.                         TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ?
  18.                                 TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp;
  19.                         TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, "",
  20.                                                type);
  21.                 }
  22.         }

I borrowed that code from the example scene provided. I've kind of just been slugging away seeing what works. I'm extremely new to Tnet, only have had my hands on it for a couple of days.

My intent is a 2 player game that either starts a server or joins it if there is one active:

  1. //=========================================================================
  2.         // Start()
  3.         // Initialization event for monobehaviors. Called after Awake()
  4.         //=========================================================================
  5.         void Start ()
  6.         {
  7.                 //TNServerInstance.Stop(); //Maybe this will clean up the server instance?
  8.  
  9.                 if (Application.isPlaying)
  10.                 {
  11.                         // Start resolving IPs
  12.                         Tools.ResolveIPs(null);
  13.                        
  14.                         // We don't want mobile devices to dim their screen and go to sleep while the app is running
  15.                         Screen.sleepTimeout = SleepTimeout.NeverSleep;
  16.                        
  17.                         // Make it possible to use UDP using a random port
  18.                         TNManager.StartUDP(Random.Range(10000, 50000));
  19.  
  20.                         Debug.Log("Server instance active " + TNServerInstance.isActive);
  21.                         if (TNServerInstance.isActive)
  22.                         {
  23.                                 //connect here
  24.                                 Debug.Log("Started with instance already active. Join here!");
  25.                                 //List of discovered servers
  26.                                 List<ServerList.Entry> list = TNLobbyClient.knownServers.list;
  27.                                
  28.                                 Debug.Log("Known servers " + list.Count);
  29.                                
  30.                                 if (list.Count > 0)
  31.                                 {
  32.                                         TNManager.Connect(list[0].internalAddress, list[0].internalAddress);
  33.                                 }
  34.                         }
  35.                         else
  36.                         {
  37.                                 StartServer();
  38.                         }
  39.                 }
  40.         }
Title: Re: Server doesn't seem to close on editor stop
Post by: kcavasos_fuelfx on July 31, 2014, 11:09:48 AM
Update - It doesn't have anything to do with the above code!

I've run isolated tests on the above code, and on using auto join. The issue comes in when an oculus prefab is instantiated / enabled. (In this case as part of the player object)

More on this to come when I can find what code doesn't play nice with TNet.
Title: Re: Server doesn't seem to close on editor stop
Post by: ArenMook on July 31, 2014, 12:49:56 PM
I'd be curious to know what Occulus is doing that messes up the life expectancy of scripts this way. I'm working with TNet every day (developing Windward), and can't say I've ever encountered that -- but then again I don't use Occulus for it.