using System; using System.Collections.Generic; using System.ComponentModel; using UnityEngine; namespace Mirror { /// /// The NetworkServer uses a NetworkServerSimple for basic network functionality and adds more game-like functionality. /// /// /// NetworkServer handles remote connections from remote clients via a NetworkServerSimple instance, and also has a local connection for a local client. /// The NetworkServer is a singleton. It has static convenience functions such as NetworkServer.SendToAll() and NetworkServer.Spawn() which automatically use the singleton instance. /// The NetworkManager uses the NetworkServer, but it can be used without the NetworkManager. /// The set of networked objects that have been spawned is managed by NetworkServer. Objects are spawned with NetworkServer.Spawn() which adds them to this set, and makes them be created on clients. Spawned objects are removed automatically when they are destroyed, or than they can be removed from the spawned set by calling NetworkServer.UnSpawn() - this does not destroy the object. /// There are a number of internal messages used by NetworkServer, these are setup when NetworkServer.Listen() is called. /// public static class NetworkServer { static bool initialized; static int maxConnections; // original HLAPI has .localConnections list with only m_LocalConnection in it // (for backwards compatibility because they removed the real localConnections list a while ago) // => removed it for easier code. use .localConnection now! public static NetworkConnection localConnection { get; private set; } /// /// A list of local connections on the server. /// public static Dictionary connections = new Dictionary(); /// /// Dictionary of the message handlers registered with the server. /// The key to the dictionary is the message Id. /// public static Dictionary handlers = new Dictionary(); /// /// If you enable this, the server will not listen for incoming connections on the regular network port. /// This can be used if the game is running in host mode and does not want external players to be able to connect - making it like a single-player game. Also this can be useful when using AddExternalConnection(). /// public static bool dontListen; /// /// Checks if the server has been started. /// This will be true after NetworkServer.Listen() has been called. /// public static bool active { get; private set; } /// /// True is a local client is currently active on the server. /// This will be true for "Hosts" on hosted server games. /// public static bool localClientActive { get; private set; } /// /// Reset the NetworkServer singleton. /// public static void Reset() { active = false; } /// /// This shuts down the server and disconnects all clients. /// public static void Shutdown() { if (initialized) { DisconnectAll(); if (dontListen) { // was never started, so dont stop } else { Transport.activeTransport.ServerStop(); } Transport.activeTransport.OnServerDisconnected.RemoveListener(OnDisconnected); Transport.activeTransport.OnServerConnected.RemoveListener(OnConnected); Transport.activeTransport.OnServerDataReceived.RemoveListener(OnDataReceived); Transport.activeTransport.OnServerError.RemoveListener(OnError); initialized = false; } dontListen = false; active = false; NetworkIdentity.ResetNextNetworkId(); } static void Initialize() { if (initialized) return; initialized = true; if (LogFilter.Debug) Debug.Log("NetworkServer Created version " + Version.Current); //Make sure connections are cleared in case any old connections references exist from previous sessions connections.Clear(); Transport.activeTransport.OnServerDisconnected.AddListener(OnDisconnected); Transport.activeTransport.OnServerConnected.AddListener(OnConnected); Transport.activeTransport.OnServerDataReceived.AddListener(OnDataReceived); Transport.activeTransport.OnServerError.AddListener(OnError); } internal static void RegisterMessageHandlers() { RegisterHandler(OnClientReadyMessage); RegisterHandler(OnCommandMessage); RegisterHandler(OnRemovePlayerMessage); RegisterHandler(NetworkTime.OnServerPing); } /// /// Start the server, setting the maximum number of connections. /// /// Maximum number of allowed connections /// public static bool Listen(int maxConns) { Initialize(); maxConnections = maxConns; // only start server if we want to listen if (!dontListen) { Transport.activeTransport.ServerStart(); if (LogFilter.Debug) Debug.Log("Server started listening"); } active = true; RegisterMessageHandlers(); return true; } /// /// This accepts a network connection and adds it to the server. /// This connection will use the callbacks registered with the server. /// /// Network connection to add. /// True if added. public static bool AddConnection(NetworkConnection conn) { if (!connections.ContainsKey(conn.connectionId)) { // connection cannot be null here or conn.connectionId // would throw NRE connections[conn.connectionId] = conn; conn.SetHandlers(handlers); return true; } // already a connection with this id return false; } /// /// This removes an external connection added with AddExternalConnection(). /// /// The id of the connection to remove. /// True if the removal succeeded public static bool RemoveConnection(int connectionId) { return connections.Remove(connectionId); } // called by LocalClient to add itself. dont call directly. internal static void SetLocalConnection(ULocalConnectionToClient conn) { if (localConnection != null) { Debug.LogError("Local Connection already exists"); return; } localConnection = conn; OnConnected(localConnection); } internal static void RemoveLocalConnection() { if (localConnection != null) { localConnection.Disconnect(); localConnection.Dispose(); localConnection = null; } localClientActive = false; RemoveConnection(0); } internal static void ActivateLocalClientScene() { if (localClientActive) return; // ClientScene for a local connection is becoming active. any spawned objects need to be started as client objects localClientActive = true; foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) { if (!identity.isClient) { if (LogFilter.Debug) Debug.Log("ActivateClientScene " + identity.netId + " " + identity); identity.OnStartClient(); } } } // this is like SendToReady - but it doesn't check the ready flag on the connection. // this is used for ObjectDestroy messages. [EditorBrowsable(EditorBrowsableState.Never), Obsolete("use SendToObservers instead")] static bool SendToObservers(NetworkIdentity identity, short msgType, MessageBase msg) { if (LogFilter.Debug) Debug.Log("Server.SendToObservers id:" + msgType); if (identity != null && identity.observers != null) { // pack message into byte[] once byte[] bytes = MessagePacker.PackMessage((ushort)msgType, msg); // send to all observers bool result = true; foreach (KeyValuePair kvp in identity.observers) { result &= kvp.Value.SendBytes(bytes); } return result; } return false; } // this is like SendToReady - but it doesn't check the ready flag on the connection. // this is used for ObjectDestroy messages. static bool SendToObservers(NetworkIdentity identity, T msg) where T: IMessageBase { if (LogFilter.Debug) Debug.Log("Server.SendToObservers id:" + typeof(T)); if (identity != null && identity.observers != null) { // pack message into byte[] once byte[] bytes = MessagePacker.Pack(msg); bool result = true; foreach (KeyValuePair kvp in identity.observers) { result &= kvp.Value.SendBytes(bytes); } return result; } return false; } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use SendToAll instead.")] public static bool SendToAll(int msgType, MessageBase msg, int channelId = Channels.DefaultReliable) { if (LogFilter.Debug) Debug.Log("Server.SendToAll id:" + msgType); // pack message into byte[] once byte[] bytes = MessagePacker.PackMessage((ushort)msgType, msg); // send to all bool result = true; foreach (KeyValuePair kvp in connections) { result &= kvp.Value.SendBytes(bytes, channelId); } return result; } /// /// Send a message structure with the given type number to all connected clients. /// This applies to clients that are ready and not-ready. /// /// Message type. /// Message structure. /// Transport channel to use /// public static bool SendToAll(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase { if (LogFilter.Debug) Debug.Log("Server.SendToAll id:" + typeof(T)); // pack message into byte[] once byte[] bytes = MessagePacker.Pack(msg); bool result = true; foreach (KeyValuePair kvp in connections) { result &= kvp.Value.SendBytes(bytes, channelId); } return result; } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use SendToReady instead.")] public static bool SendToReady(NetworkIdentity identity, short msgType, MessageBase msg, int channelId = Channels.DefaultReliable) { if (LogFilter.Debug) Debug.Log("Server.SendToReady msgType:" + msgType); if (identity != null && identity.observers != null) { // pack message into byte[] once byte[] bytes = MessagePacker.PackMessage((ushort)msgType, msg); // send to all ready observers bool result = true; foreach (KeyValuePair kvp in identity.observers) { if (kvp.Value.isReady) { result &= kvp.Value.SendBytes(bytes, channelId); } } return result; } return false; } /// /// Send a message structure with the given type number to only clients which are ready. /// See Networking.NetworkClient.Ready. /// /// Message type. /// /// Message structure. /// Send to observers including self.. /// Transport channel to use /// public static bool SendToReady(NetworkIdentity identity, T msg, bool includeSelf = true, int channelId = Channels.DefaultReliable) where T : IMessageBase { if (LogFilter.Debug) Debug.Log("Server.SendToReady msgType:" + typeof(T)); if (identity != null && identity.observers != null) { // pack message into byte[] once byte[] bytes = MessagePacker.Pack(msg); bool result = true; foreach (KeyValuePair kvp in identity.observers) { bool isSelf = kvp.Value == identity.connectionToClient; if ((!isSelf || includeSelf) && kvp.Value.isReady) { result &= kvp.Value.SendBytes(bytes, channelId); } } return result; } return false; } /// /// Send a message structure with the given type number to only clients which are ready. /// See Networking.NetworkClient.Ready. /// /// Message type. /// /// Message structure. /// Transport channel to use /// public static bool SendToReady(NetworkIdentity identity, T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase { return SendToReady(identity, msg, true, channelId); } /// /// Disconnect all currently connected clients, including the local connection. /// This can only be called on the server. Clients will receive the Disconnect message. /// public static void DisconnectAll() { DisconnectAllConnections(); localConnection = null; active = false; localClientActive = false; } /// /// Disconnect all currently connected clients except the local connection. /// This can only be called on the server. Clients will receive the Disconnect message. /// public static void DisconnectAllConnections() { foreach (NetworkConnection conn in connections.Values) { conn.Disconnect(); // call OnDisconnected unless local player in host mode if (conn.connectionId != 0) OnDisconnected(conn); conn.Dispose(); } connections.Clear(); } // The user should never need to pump the update loop manually internal static void Update() { if (!active) return; // update all server objects foreach (KeyValuePair kvp in NetworkIdentity.spawned) { if (kvp.Value != null && kvp.Value.gameObject != null) { kvp.Value.MirrorUpdate(); } else { // spawned list should have no null entries because we // always call Remove in OnObjectDestroy everywhere. Debug.LogWarning("Found 'null' entry in spawned list for netId=" + kvp.Key + ". Please call NetworkServer.Destroy to destroy networked objects. Don't use GameObject.Destroy."); } } } static void OnConnected(int connectionId) { if (LogFilter.Debug) Debug.Log("Server accepted client:" + connectionId); // connectionId needs to be > 0 because 0 is reserved for local player if (connectionId <= 0) { Debug.LogError("Server.HandleConnect: invalid connectionId: " + connectionId + " . Needs to be >0, because 0 is reserved for local player."); Transport.activeTransport.ServerDisconnect(connectionId); return; } // connectionId not in use yet? if (connections.ContainsKey(connectionId)) { Transport.activeTransport.ServerDisconnect(connectionId); if (LogFilter.Debug) Debug.Log("Server connectionId " + connectionId + " already in use. kicked client:" + connectionId); return; } // are more connections allowed? if not, kick // (it's easier to handle this in Mirror, so Transports can have // less code and third party transport might not do that anyway) // (this way we could also send a custom 'tooFull' message later, // Transport can't do that) if (connections.Count < maxConnections) { // get ip address from connection string address = Transport.activeTransport.ServerGetClientAddress(connectionId); // add player info NetworkConnection conn = new NetworkConnection(address, connectionId); OnConnected(conn); } else { // kick Transport.activeTransport.ServerDisconnect(connectionId); if (LogFilter.Debug) Debug.Log("Server full, kicked client:" + connectionId); } } static void OnConnected(NetworkConnection conn) { if (LogFilter.Debug) Debug.Log("Server accepted client:" + conn.connectionId); // add connection and invoke connected event AddConnection(conn); conn.InvokeHandler(new ConnectMessage()); } static void OnDisconnected(int connectionId) { if (LogFilter.Debug) Debug.Log("Server disconnect client:" + connectionId); if (connections.TryGetValue(connectionId, out NetworkConnection conn)) { conn.Disconnect(); RemoveConnection(connectionId); if (LogFilter.Debug) Debug.Log("Server lost client:" + connectionId); OnDisconnected(conn); } } static void OnDisconnected(NetworkConnection conn) { conn.InvokeHandler(new DisconnectMessage()); if (LogFilter.Debug) Debug.Log("Server lost client:" + conn.connectionId); } static void OnDataReceived(int connectionId, ArraySegment data) { if (connections.TryGetValue(connectionId, out NetworkConnection conn)) { conn.TransportReceive(data); } else { Debug.LogError("HandleData Unknown connectionId:" + connectionId); } } static void OnError(int connectionId, Exception exception) { // TODO Let's discuss how we will handle errors Debug.LogException(exception); } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use RegisterHandler instead.")] public static void RegisterHandler(int msgType, NetworkMessageDelegate handler) { if (handlers.ContainsKey(msgType)) { if (LogFilter.Debug) Debug.Log("NetworkServer.RegisterHandler replacing " + msgType); } handlers[msgType] = handler; } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use RegisterHandler instead.")] public static void RegisterHandler(MsgType msgType, NetworkMessageDelegate handler) { RegisterHandler((int)msgType, handler); } /// /// Register a handler for a particular message type. /// There are several system message types which you can add handlers for. You can also add your own message types. /// /// Message type /// Function handler which will be invoked for when this message type is received. public static void RegisterHandler(Action handler) where T: IMessageBase, new() { int msgType = MessagePacker.GetId(); if (handlers.ContainsKey(msgType)) { if (LogFilter.Debug) Debug.Log("NetworkServer.RegisterHandler replacing " + msgType); } handlers[msgType] = MessagePacker.MessageHandler(handler); } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use UnregisterHandler instead.")] public static void UnregisterHandler(int msgType) { handlers.Remove(msgType); } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use UnregisterHandler instead.")] public static void UnregisterHandler(MsgType msgType) { UnregisterHandler((int)msgType); } /// /// Unregisters a handler for a particular message type. /// /// Message type public static void UnregisterHandler() where T : IMessageBase { int msgType = MessagePacker.GetId(); handlers.Remove(msgType); } /// /// Clear all registered callback handlers. /// public static void ClearHandlers() { handlers.Clear(); } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use SendToClient instead.")] public static void SendToClient(int connectionId, int msgType, MessageBase msg) { if (connections.TryGetValue(connectionId, out NetworkConnection conn)) { conn.Send(msgType, msg); return; } Debug.LogError("Failed to send message to connection ID '" + connectionId + ", not found in connection list"); } /// /// Send a message to the client which owns the given connection ID. /// It accepts the connection ID as a parameter as well as a message and MsgType. Remember to set the client up for receiving the messages by using NetworkClient.RegisterHandler. Also, for user messages you must use a MsgType with a higher ID number than MsgType.Highest. /// /// Message type /// Client connection ID. /// Message struct to send public static void SendToClient(int connectionId, T msg) where T : IMessageBase { if (connections.TryGetValue(connectionId, out NetworkConnection conn)) { conn.Send(msg); return; } Debug.LogError("Failed to send message to connection ID '" + connectionId + ", not found in connection list"); } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use SendToClientOfPlayer instead.")] public static void SendToClientOfPlayer(NetworkIdentity identity, int msgType, MessageBase msg) { if (identity != null) { identity.connectionToClient.Send(msgType, msg); } else { Debug.LogError("SendToClientOfPlayer: player has no NetworkIdentity: " + identity.name); } } /// /// send this message to the player only /// /// Message type /// /// public static void SendToClientOfPlayer(NetworkIdentity identity, T msg) where T: IMessageBase { if (identity != null) { identity.connectionToClient.Send(msg); } else { Debug.LogError("SendToClientOfPlayer: player has no NetworkIdentity: " + identity.name); } } /// /// This replaces the player object for a connection with a different player object. The old player object is not destroyed. /// If a connection already has a player object, this can be used to replace that object with a different player object. This does NOT change the ready state of the connection, so it can safely be used while changing scenes. /// /// Connection which is adding the player. /// Player object spawned for the player. /// /// public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId) { if (GetNetworkIdentity(player, out NetworkIdentity identity)) { identity.assetId = assetId; } return InternalReplacePlayerForConnection(conn, player); } /// /// This replaces the player object for a connection with a different player object. The old player object is not destroyed. /// If a connection already has a player object, this can be used to replace that object with a different player object. This does NOT change the ready state of the connection, so it can safely be used while changing scenes. /// /// Connection which is adding the player. /// Player object spawned for the player. /// public static bool ReplacePlayerForConnection(NetworkConnection conn, GameObject player) { return InternalReplacePlayerForConnection(conn, player); } /// /// When an AddPlayer message handler has received a request from a player, the server calls this to associate the player object with the connection. /// When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail. /// /// Connection which is adding the player. /// Player object spawned for the player. /// /// public static bool AddPlayerForConnection(NetworkConnection conn, GameObject player, Guid assetId) { if (GetNetworkIdentity(player, out NetworkIdentity identity)) { identity.assetId = assetId; } return AddPlayerForConnection(conn, player); } static void SpawnObserversForConnection(NetworkConnection conn) { if (LogFilter.Debug) Debug.Log("Spawning " + NetworkIdentity.spawned.Count + " objects for conn " + conn.connectionId); if (!conn.isReady) { // client needs to finish initializing before we can spawn objects // otherwise it would not find them. return; } // let connection know that we are about to start spawning... conn.Send(new ObjectSpawnStartedMessage()); // add connection to each nearby NetworkIdentity's observers, which // internally sends a spawn message for each one to the connection. foreach (NetworkIdentity identity in NetworkIdentity.spawned.Values) { if (identity.gameObject.activeSelf) //TODO this is different // try with far away ones in ummorpg! { if (LogFilter.Debug) Debug.Log("Sending spawn message for current server objects name='" + identity.name + "' netId=" + identity.netId + " sceneId=" + identity.sceneId); bool visible = identity.OnCheckObserver(conn); if (visible) { identity.AddObserver(conn); } } } // let connection know that we finished spawning, so it can call // OnStartClient on each one (only after all were spawned, which // is how Unity's Start() function works too) conn.Send(new ObjectSpawnFinishedMessage()); } /// /// When an AddPlayer message handler has received a request from a player, the server calls this to associate the player object with the connection. /// When a player is added for a connection, the client for that connection is made ready automatically. The player object is automatically spawned, so you do not need to call NetworkServer.Spawn for that object. This function is used for "adding" a player, not for "replacing" the player on a connection. If there is already a player on this playerControllerId for this connection, this will fail. /// /// Connection which is adding the player. /// Player object spawned for the player. /// public static bool AddPlayerForConnection(NetworkConnection conn, GameObject player) { NetworkIdentity identity = player.GetComponent(); if (identity == null) { Debug.Log("AddPlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player); return false; } identity.Reset(); // cannot have a player object in "Add" version if (conn.playerController != null) { Debug.Log("AddPlayer: player object already exists"); return false; } // make sure we have a controller before we call SetClientReady // because the observers will be rebuilt only if we have a controller conn.playerController = identity; // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients) identity.connectionToClient = conn; // set ready if not set yet SetClientReady(conn); if (SetupLocalPlayerForConnection(conn, identity)) { return true; } if (LogFilter.Debug) Debug.Log("Adding new playerGameObject object netId: " + identity.netId + " asset ID " + identity.assetId); FinishPlayerForConnection(identity, player); if (identity.localPlayerAuthority) { identity.SetClientOwner(conn); } return true; } static bool SetupLocalPlayerForConnection(NetworkConnection conn, NetworkIdentity identity) { if (LogFilter.Debug) Debug.Log("NetworkServer SetupLocalPlayerForConnection netID:" + identity.netId); if (conn is ULocalConnectionToClient) { if (LogFilter.Debug) Debug.Log("NetworkServer AddPlayer handling ULocalConnectionToClient"); // Spawn this player for other players, instead of SpawnObject: if (identity.netId == 0) { // it is allowed to provide an already spawned object as the new player object. // so dont spawn it again. identity.OnStartServer(true); } identity.RebuildObservers(true); SendSpawnMessage(identity, null); // Set up local player instance on the client instance and update local object map NetworkClient.AddLocalPlayer(identity); identity.SetClientOwner(conn); // Trigger OnAuthority identity.ForceAuthority(true); // Trigger OnStartLocalPlayer identity.SetLocalPlayer(); return true; } return false; } static void FinishPlayerForConnection(NetworkIdentity identity, GameObject playerGameObject) { if (identity.netId == 0) { // it is allowed to provide an already spawned object as the new player object. // so dont spawn it again. Spawn(playerGameObject); } } internal static bool InternalReplacePlayerForConnection(NetworkConnection conn, GameObject player) { NetworkIdentity identity = player.GetComponent(); if (identity == null) { Debug.LogError("ReplacePlayer: playerGameObject has no NetworkIdentity. Please add a NetworkIdentity to " + player); return false; } //NOTE: there can be an existing player if (LogFilter.Debug) Debug.Log("NetworkServer ReplacePlayer"); // is there already an owner that is a different object?? if (conn.playerController != null) { conn.playerController.SetNotLocalPlayer(); conn.playerController.clientAuthorityOwner = null; } conn.playerController = identity; // Set the connection on the NetworkIdentity on the server, NetworkIdentity.SetLocalPlayer is not called on the server (it is on clients) identity.connectionToClient = conn; //NOTE: DONT set connection ready. // add connection to observers AFTER the playerController was set. // by definition, there is nothing to observe if there is no player // controller. // // IMPORTANT: do this in AddPlayerForConnection & ReplacePlayerForConnection! SpawnObserversForConnection(conn); if (LogFilter.Debug) Debug.Log("NetworkServer ReplacePlayer setup local"); if (SetupLocalPlayerForConnection(conn, identity)) { return true; } if (LogFilter.Debug) Debug.Log("Replacing playerGameObject object netId: " + player.GetComponent().netId + " asset ID " + player.GetComponent().assetId); FinishPlayerForConnection(identity, player); if (identity.localPlayerAuthority) { identity.SetClientOwner(conn); } return true; } static bool GetNetworkIdentity(GameObject go, out NetworkIdentity identity) { identity = go.GetComponent(); if (identity == null) { Debug.LogError("GameObject " + go.name + " doesn't have NetworkIdentity."); return false; } return true; } /// /// Sets the client to be ready. /// When a client has signaled that it is ready, this method tells the server that the client is ready to receive spawned objects and state synchronization updates. This is usually called in a handler for the SYSTEM_READY message. If there is not specific action a game needs to take for this message, relying on the default ready handler function is probably fine, so this call wont be needed. /// /// The connection of the client to make ready. public static void SetClientReady(NetworkConnection conn) { if (LogFilter.Debug) Debug.Log("SetClientReadyInternal for conn:" + conn.connectionId); // set ready conn.isReady = true; // client is ready to start spawning objects if (conn.playerController != null) SpawnObserversForConnection(conn); } internal static void ShowForConnection(NetworkIdentity identity, NetworkConnection conn) { if (conn.isReady) SendSpawnMessage(identity, conn); } internal static void HideForConnection(NetworkIdentity identity, NetworkConnection conn) { ObjectHideMessage msg = new ObjectHideMessage { netId = identity.netId }; conn.Send(msg); } /// /// Marks all connected clients as no longer ready. /// All clients will no longer be sent state synchronization updates. The player's clients can call ClientManager.Ready() again to re-enter the ready state. This is useful when switching scenes. /// public static void SetAllClientsNotReady() { foreach (NetworkConnection conn in connections.Values) { SetClientNotReady(conn); } } /// /// Sets the client of the connection to be not-ready. /// Clients that are not ready do not receive spawned objects or state synchronization updates. They client can be made ready again by calling SetClientReady(). /// /// The connection of the client to make not ready. public static void SetClientNotReady(NetworkConnection conn) { if (conn.isReady) { if (LogFilter.Debug) Debug.Log("PlayerNotReady " + conn); conn.isReady = false; conn.RemoveObservers(); conn.Send(new NotReadyMessage()); } } // default ready handler. static void OnClientReadyMessage(NetworkConnection conn, ReadyMessage msg) { if (LogFilter.Debug) Debug.Log("Default handler for ready message from " + conn); SetClientReady(conn); } // default remove player handler static void OnRemovePlayerMessage(NetworkConnection conn, RemovePlayerMessage msg) { if (conn.playerController != null) { Destroy(conn.playerController.gameObject); conn.playerController = null; } else { Debug.LogError("Received remove player message but connection has no player"); } } // Handle command from specific player, this could be one of multiple players on a single client static void OnCommandMessage(NetworkConnection conn, CommandMessage msg) { if (!NetworkIdentity.spawned.TryGetValue(msg.netId, out NetworkIdentity identity)) { Debug.LogWarning("Spawned object not found when handling Command message [netId=" + msg.netId + "]"); return; } // Commands can be for player objects, OR other objects with client-authority // -> so if this connection's controller has a different netId then // only allow the command if clientAuthorityOwner if (conn.playerController != null && conn.playerController.netId != identity.netId) { if (identity.clientAuthorityOwner != conn) { Debug.LogWarning("Command for object without authority [netId=" + msg.netId + "]"); return; } } if (LogFilter.Debug) Debug.Log("OnCommandMessage for netId=" + msg.netId + " conn=" + conn); identity.HandleCommand(msg.componentIndex, msg.functionHash, new NetworkReader(msg.payload)); } internal static void SpawnObject(GameObject obj) { if (!active) { Debug.LogError("SpawnObject for " + obj + ", NetworkServer is not active. Cannot spawn objects without an active server."); return; } NetworkIdentity identity = obj.GetComponent(); if (identity == null) { Debug.LogError("SpawnObject " + obj + " has no NetworkIdentity. Please add a NetworkIdentity to " + obj); return; } identity.Reset(); identity.OnStartServer(false); if (LogFilter.Debug) Debug.Log("SpawnObject instance ID " + identity.netId + " asset ID " + identity.assetId); identity.RebuildObservers(true); //SendSpawnMessage(objNetworkIdentity, null); } internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnection conn) { if (identity.serverOnly) return; if (LogFilter.Debug) Debug.Log("Server SendSpawnMessage: name=" + identity.name + " sceneId=" + identity.sceneId.ToString("X") + " netid=" + identity.netId); // for easier debugging // one writer for owner, one for observers NetworkWriter ownerWriter = NetworkWriterPool.GetWriter(); NetworkWriter observersWriter = NetworkWriterPool.GetWriter(); // serialize all components with initialState = true // (can be null if has none) identity.OnSerializeAllSafely(true, ownerWriter, out int ownerWritten, observersWriter, out int observersWritten); // convert to ArraySegment to avoid reader allocations // (need to handle null case too) ArraySegment ownerSegment = ownerWritten > 0 ? ownerWriter.ToArraySegment() : default; ArraySegment observersSegment = observersWritten > 0 ? observersWriter.ToArraySegment() : default; // 'identity' is a prefab that should be spawned if (identity.sceneId == 0) { SpawnPrefabMessage msg = new SpawnPrefabMessage { netId = identity.netId, owner = conn?.playerController == identity, assetId = identity.assetId, // use local values for VR support position = identity.transform.localPosition, rotation = identity.transform.localRotation, scale = identity.transform.localScale }; // conn is != null when spawning it for a client if (conn != null) { // use owner segment if 'conn' owns this identity, otherwise // use observers segment bool isOwner = identity.connectionToClient == conn; msg.payload = isOwner ? ownerSegment : observersSegment; conn.Send(msg); } // conn is == null when spawning it for the local player else { // send ownerWriter to owner // (spawn no matter what, even if no components were // serialized because the spawn message contains more data. // components might still be updated later on.) msg.payload = ownerSegment; SendToClientOfPlayer(identity, msg); // send observersWriter to everyone but owner // (spawn no matter what, even if no components were // serialized because the spawn message contains more data. // components might still be updated later on.) msg.payload = observersSegment; SendToReady(identity, msg, false); } } // 'identity' is a scene object that should be spawned again else { SpawnSceneObjectMessage msg = new SpawnSceneObjectMessage { netId = identity.netId, owner = conn?.playerController == identity, sceneId = identity.sceneId, // use local values for VR support position = identity.transform.localPosition, rotation = identity.transform.localRotation, scale = identity.transform.localScale }; // conn is != null when spawning it for a client if (conn != null) { // use owner segment if 'conn' owns this identity, otherwise // use observers segment bool isOwner = identity.connectionToClient == conn; msg.payload = isOwner ? ownerSegment : observersSegment; conn.Send(msg); } // conn is == null when spawning it for the local player else { // send ownerWriter to owner // (spawn no matter what, even if no components were // serialized because the spawn message contains more data. // components might still be updated later on.) msg.payload = ownerSegment; SendToClientOfPlayer(identity, msg); // send observersWriter to everyone but owner // (spawn no matter what, even if no components were // serialized because the spawn message contains more data. // components might still be updated later on.) msg.payload = observersSegment; SendToReady(identity, msg, false); } } NetworkWriterPool.Recycle(ownerWriter); NetworkWriterPool.Recycle(observersWriter); } /// /// This destroys all the player objects associated with a NetworkConnections on a server. /// This is used when a client disconnects, to remove the players for that client. This also destroys non-player objects that have client authority set for this connection. /// /// The connections object to clean up for. public static void DestroyPlayerForConnection(NetworkConnection conn) { // => destroy what we can destroy. HashSet tmp = new HashSet(conn.clientOwnedObjects); foreach (uint netId in tmp) { if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) { Destroy(identity.gameObject); } } if (conn.playerController != null) { DestroyObject(conn.playerController, true); conn.playerController = null; } } /// /// Spawn the given game object on all clients which are ready. /// This will cause a new object to be instantiated from the registered prefab, or from a custom spawn function. /// /// Game object with NetworkIdentity to spawn. public static void Spawn(GameObject obj) { if (VerifyCanSpawn(obj)) { SpawnObject(obj); } } static bool CheckForPrefab(GameObject obj) { #if UNITY_EDITOR #if UNITY_2018_3_OR_NEWER return UnityEditor.PrefabUtility.IsPartOfPrefabAsset(obj); #elif UNITY_2018_2_OR_NEWER return (UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(obj) == null) && (UnityEditor.PrefabUtility.GetPrefabObject(obj) != null); #else return (UnityEditor.PrefabUtility.GetPrefabParent(obj) == null) && (UnityEditor.PrefabUtility.GetPrefabObject(obj) != null); #endif #else return false; #endif } static bool VerifyCanSpawn(GameObject obj) { if (CheckForPrefab(obj)) { Debug.LogErrorFormat("GameObject {0} is a prefab, it can't be spawned. This will cause errors in builds.", obj.name); return false; } return true; } /// /// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client. /// This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object. /// /// The object to spawn. /// The player object to set Client Authority to. /// public static bool SpawnWithClientAuthority(GameObject obj, GameObject player) { NetworkIdentity identity = player.GetComponent(); if (identity == null) { Debug.LogError("SpawnWithClientAuthority player object has no NetworkIdentity"); return false; } if (identity.connectionToClient == null) { Debug.LogError("SpawnWithClientAuthority player object is not a player."); return false; } return SpawnWithClientAuthority(obj, identity.connectionToClient); } /// /// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client. /// This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object. /// /// The object to spawn. /// The connection to set Client Authority to. /// public static bool SpawnWithClientAuthority(GameObject obj, NetworkConnection conn) { if (!conn.isReady) { Debug.LogError("SpawnWithClientAuthority NetworkConnection is not ready!"); return false; } Spawn(obj); NetworkIdentity identity = obj.GetComponent(); if (identity == null || !identity.isServer) { // spawning the object failed. return false; } return identity.AssignClientAuthority(conn); } /// /// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client. /// This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object. /// /// The object to spawn. /// The assetId of the object to spawn. Used for custom spawn handlers. /// The connection to set Client Authority to. /// public static bool SpawnWithClientAuthority(GameObject obj, Guid assetId, NetworkConnection conn) { Spawn(obj, assetId); NetworkIdentity identity = obj.GetComponent(); if (identity == null || !identity.isServer) { // spawning the object failed. return false; } return identity.AssignClientAuthority(conn); } /// /// This spawns an object like NetworkServer.Spawn() but also assigns Client Authority to the specified client. /// This is the same as calling NetworkIdentity.AssignClientAuthority on the spawned object. /// /// The object to spawn. /// The assetId of the object to spawn. Used for custom spawn handlers. public static void Spawn(GameObject obj, Guid assetId) { if (VerifyCanSpawn(obj)) { if (GetNetworkIdentity(obj, out NetworkIdentity identity)) { identity.assetId = assetId; } SpawnObject(obj); } } static void DestroyObject(NetworkIdentity identity, bool destroyServerObject) { if (LogFilter.Debug) Debug.Log("DestroyObject instance:" + identity.netId); NetworkIdentity.spawned.Remove(identity.netId); identity.clientAuthorityOwner?.RemoveOwnedObject(identity); ObjectDestroyMessage msg = new ObjectDestroyMessage { netId = identity.netId }; SendToObservers(identity, msg); identity.ClearObservers(); if (NetworkClient.active && localClientActive) { identity.OnNetworkDestroy(); } // when unspawning, dont destroy the server's object if (destroyServerObject) { UnityEngine.Object.Destroy(identity.gameObject); } identity.MarkForReset(); } /// /// Destroys this object and corresponding objects on all clients. /// In some cases it is useful to remove an object but not delete it on the server. For that, use NetworkServer.UnSpawn() instead of NetworkServer.Destroy(). /// /// Game object to destroy. public static void Destroy(GameObject obj) { if (obj == null) { if (LogFilter.Debug) Debug.Log("NetworkServer DestroyObject is null"); return; } if (GetNetworkIdentity(obj, out NetworkIdentity identity)) { DestroyObject(identity, true); } } /// /// This takes an object that has been spawned and un-spawns it. /// The object will be removed from clients that it was spawned on, or the custom spawn handler function on the client will be called for the object. /// Unlike when calling NetworkServer.Destroy(), on the server the object will NOT be destroyed. This allows the server to re-use the object, even spawn it again later. /// /// The spawned object to be unspawned. public static void UnSpawn(GameObject obj) { if (obj == null) { if (LogFilter.Debug) Debug.Log("NetworkServer UnspawnObject is null"); return; } if (GetNetworkIdentity(obj, out NetworkIdentity identity)) { DestroyObject(identity, false); } } /// /// Obsolete: Use instead. /// [EditorBrowsable(EditorBrowsableState.Never), Obsolete("Use NetworkIdentity.spawned[netId] instead.")] public static GameObject FindLocalObject(uint netId) { if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity)) { return identity.gameObject; } return null; } static bool ValidateSceneObject(NetworkIdentity identity) { if (identity.gameObject.hideFlags == HideFlags.NotEditable || identity.gameObject.hideFlags == HideFlags.HideAndDontSave) return false; #if UNITY_EDITOR if (UnityEditor.EditorUtility.IsPersistent(identity.gameObject)) return false; #endif // If not a scene object return identity.sceneId != 0; } /// /// This causes NetworkIdentity objects in a scene to be spawned on a server. /// NetworkIdentity objects in a scene are disabled by default. Calling SpawnObjects() causes these scene objects to be enabled and spawned. It is like calling NetworkServer.Spawn() for each of them. /// /// Success if objects where spawned. public static bool SpawnObjects() { if (!active) return true; NetworkIdentity[] identities = Resources.FindObjectsOfTypeAll(); foreach (NetworkIdentity identity in identities) { if (ValidateSceneObject(identity)) { if (LogFilter.Debug) Debug.Log("SpawnObjects sceneId:" + identity.sceneId.ToString("X") + " name:" + identity.gameObject.name); identity.Reset(); identity.gameObject.SetActive(true); } } foreach (NetworkIdentity identity in identities) { if (ValidateSceneObject(identity)) { Spawn(identity.gameObject); // these objects are server authority - even if "localPlayerAuthority" is set on them identity.ForceAuthority(true); } } return true; } } }