Esempio n. 1
0
        /// <summary>
        /// This causes the server to switch scenes and sets the networkSceneName.
        /// <para>Clients that connect to this server will automatically switch to this scene. This is called autmatically if onlineScene or offlineScene are set, but it can be called from user code to switch scenes again while the game is in progress. This automatically sets clients to be not-ready. The clients must call NetworkClient.Ready() again to participate in the new scene.</para>
        /// </summary>
        /// <param name="newSceneName"></param>
        public override void ServerChangeScene(string newSceneName)
        {
            if (newSceneName == RoomScene)
            {
                foreach (NobleRoomPlayer roomPlayer in roomSlots)
                {
                    if (roomPlayer == null)
                    {
                        continue;
                    }

                    // find the game-player object for this connection, and destroy it
                    NetworkIdentity identity = roomPlayer.GetComponent <NetworkIdentity>();

                    if (NetworkServer.active)
                    {
                        // re-add the room object
                        roomPlayer.GetComponent <NobleRoomPlayer>().readyToBegin = false;
                        NobleServer.ReplacePlayerForConnection(identity.connectionToClient, roomPlayer.gameObject);
                    }
                }

                allPlayersReady = false;
            }

            base.ServerChangeScene(newSceneName);
        }
Esempio n. 2
0
        /// <summary>Called when hosting starts.</summary>
        /// <remarks>
        /// If you override this method you must call the base method or everything will explode.
        /// </remarks>
        override public void OnStartServer()
        {
            base.OnStartServer();

            if (!isLANOnly)
            {
                try
                {
                    NobleServer.relayLifetime           = relayLifetime;
                    NobleServer.maxAllocationResends    = maxRelayRefreshAttempts;
                    NobleServer.allocationResendTimeout = relayRefreshTimeout;
                    NobleServer.relayRefreshTime        = relayRefreshTime;
                    ushort port = NobleServer.GetTransportPort();
                    NobleServer.InitializeHosting(port, region, OnServerPrepared, OnFatalError, forceRelayConnection);

                    return;
                }
                catch (SocketException)
                {
                    isLANOnly = true;
                    Logger.Log("Failed to resolve relay server address. Starting in LAN only mode", Logger.Level.Warn);
                }
            }

            // If we made it to here then either this is a LAN only host or Noble Connect failed.
            // Start in LAN only mode.
            OnStartServerLANOnly();
        }
Esempio n. 3
0
        /// <summary>
        /// Called on the server when a client adds a new player with ClientScene.AddPlayer.
        /// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
        /// </summary>
        /// <param name="conn">Connection from client.</param>
        public override void OnServerAddPlayer(NetworkConnection conn)
        {
            if (IsSceneActive(RoomScene))
            {
                if (roomSlots.Count == maxConnections)
                {
                    return;
                }

                allPlayersReady = false;

                if (LogFilter.Debug)
                {
                    Debug.LogFormat("NetworkRoomManager.OnServerAddPlayer playerPrefab:{0}", roomPlayerPrefab.name);
                }

                GameObject newRoomGameObject = OnRoomServerCreateRoomPlayer(conn);
                if (newRoomGameObject == null)
                {
                    newRoomGameObject = Instantiate(roomPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity);
                }

                NobleServer.AddPlayerForConnection(conn, newRoomGameObject);
            }
            else
            {
                OnRoomServerAddPlayer(conn);
            }
        }
Esempio n. 4
0
        IEnumerator CleanUpStoppedServer()
        {
            // We wait to give the disconnect message time to go out
            // If we didn't wait here clients wouldn't recognize the server disconnect until after a long timeout
            yield return(0);

            NobleServer.Dispose();
        }
Esempio n. 5
0
        public void OnStartServerLANOnly()
        {
            IPAddress localIP = GetALANAddress();

            networkPort              = NobleServer.GetTransportPort();
            networkAddress           = localIP.ToString();
            NobleServer.HostEndPoint = new IPEndPoint(localIP, networkPort);
            OnServerPrepared(localIP.ToString(), (ushort)networkPort);
        }
Esempio n. 6
0
        /// <summary>Start a server and request a HostEndPoint from the NobleConnectServices</summary>
        /// <remarks>
        /// OnServerPrepared will be called when the HostEndPoint has been retrieved and the host
        /// is ready to receive relay / punchthrough connections.
        /// </remarks>
        new public void StartServer()
        {
            isLANOnly = false;

            if (networkPort == 0)
            {
                networkPort = UnityEngine.Random.Range(49152, 65536);
            }
            NobleServer.SetTransportPort((ushort)networkPort);

            base.StartServer();
        }
Esempio n. 7
0
 /// <summary>Updates the NobleClient and NobleServer.</summary>
 /// <remarks>
 /// If you override this method you must call the base method or everything will explode.
 /// </remarks>
 virtual public void Update()
 {
     NobleServer.Update();
     if (client != null)
     {
         client.Update();
     }
     if (client != null)
     {
         if (isNetworkActive && !client.isConnecting && !client.isConnected && !isDisconnecting)
         {
             StopClient();
         }
     }
 }
Esempio n. 8
0
        /// <summary>Clean up the client and server.</summary>
        /// <remarks>
        /// If you override this method you must call the base method or resources will not be properly cleaned up.
        /// </remarks>
        override public void OnDestroy()
        {
            base.OnDestroy();

            // Only Dispose static NobleServer is this is "main" network manager
            // This fixes an issue where having a network manager in every scene
            // and destroying the unneeded one after a scene transition
            // would cause a disconnect
            if (NetworkManager.singleton == (this as NetworkManager))
            {
                NobleServer.Dispose();
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
Esempio n. 9
0
        void SceneLoadedForPlayer(NetworkConnection conn, GameObject roomPlayer)
        {
            if (LogFilter.Debug)
            {
                Debug.LogFormat("NetworkRoom SceneLoadedForPlayer scene: {0} {1}", SceneManager.GetActiveScene().path, conn);
            }

            if (IsSceneActive(RoomScene))
            {
                // cant be ready in room, add to ready list
                PendingPlayer pending;
                pending.conn       = conn;
                pending.roomPlayer = roomPlayer;
                pendingPlayers.Add(pending);
                return;
            }

            GameObject gamePlayer = OnRoomServerCreateGamePlayer(conn, roomPlayer);

            if (gamePlayer == null)
            {
                // get start position from base class
                Transform startPos = GetStartPosition();
                gamePlayer = startPos != null
                    ? Instantiate(playerPrefab, startPos.position, startPos.rotation)
                    : Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
            }

            if (!OnRoomServerSceneLoadedForPlayer(conn, roomPlayer, gamePlayer))
            {
                return;
            }

            // replace room player with game player
            NobleServer.ReplacePlayerForConnection(conn, gamePlayer, true);
        }
Esempio n. 10
0
 /// <summary>Called on the server when a client disconnects</summary>
 /// <remarks>
 /// If you override this method you must call the base method or resources will not be properly cleaned up.
 /// </remarks>
 /// <param name="conn">The NetworkConnection of the disconnecting client</param>
 override public void OnServerDisconnect(NetworkConnection conn)
 {
     NobleServer.OnServerDisconnect(conn);
     base.OnServerDisconnect(conn);
 }