/// <summary> /// Event Handler for Application Event UnregisterHost /// </summary> /// <param name="netMsg"></param> void OnUnregisterHost(NetworkMessage netMsg) { if (IsConnectionVerified(netMsg.conn)) { MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; var msg = netMsg.ReadMessage <MessageTypes.UnregisterHostMessage>(); // find the instance if (ConnectedHosts.RemoveHost(msg.hostName, msg.hostPassword)) { result = MessageTypes.CustomEventType.NoError; } else { Debug.LogError("<MasterServer> OnHostUnregister Host not found: " + msg.hostName); } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnHostUnregister result: " + result); } // tell other hosts? var response = new MessageTypes.UnregisterHostResponseMessage(); response.resultCode = (int)result; netMsg.conn.Send(MessageTypes.UnregisterHostResponse, response); UpdateUI(); } }
/// <summary> /// Event Handler for Application Event AddPlayer /// If a HostPassword is set on the HostInstance, it is required to add a new player to that host /// </summary> /// <param name="netMsg"></param> void OnAddPlayer(NetworkMessage netMsg) { if (IsConnectionVerified(netMsg.conn)) { MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; var msg = netMsg.ReadMessage <MessageTypes.AddPlayerMessage>(); if (string.IsNullOrEmpty(msg.playerKey)) { result = MessageTypes.CustomEventType.KeyEmpty; } else if (!Players.IsPlayerCountValid(MaxPlayerLimitForAllHosts)) { result = MessageTypes.CustomEventType.MaxPlayersExceeded; } else if (!string.IsNullOrEmpty(msg.hostName) && ConnectedHosts.Connected.ContainsKey(msg.hostName)) { // check given host first for quick check, then check all hosts if (ConnectedHosts.FindPlayer(msg.hostName, msg.playerKey) != null) { result = MessageTypes.CustomEventType.DuplicateDetected; } else if (ConnectedHosts.FindPlayer(msg.playerKey) != null) { result = MessageTypes.CustomEventType.DuplicateDetected; } else if (!ConnectedHosts.Connected[msg.hostName].VerifyPassword(msg.hostPassword)) { result = MessageTypes.CustomEventType.HostPasswordFail; } else { result = MessageTypes.CustomEventType.NoError; ConnectedHosts.AddPlayer(msg.hostName, msg.playerKey, msg.playerValue, msg.flags); } } else { result = MessageTypes.CustomEventType.HostUnknown; } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnAddPlayer result: " + result); } var response = new MessageTypes.AddPlayerResponseMessage(); response.resultCode = (int)result; response.playerKey = msg.playerKey; netMsg.conn.Send(MessageTypes.AddPlayerResponse, response); UpdateUI(); } }
/// <summary> /// Event Handler for Application Event ListHosts /// </summary> /// <param name="netMsg"></param> void OnListHosts(NetworkMessage netMsg) { Debug.Log("<MasterServer> OnListHosts invoked"); if (IsConnectionVerified(netMsg.conn)) { var msg = netMsg.ReadMessage <MessageTypes.ListHostsMessage>(); MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; // only make this check is the master server has a password! if (string.IsNullOrEmpty(MasterServerPassword) || (!string.IsNullOrEmpty(msg.serverPassword) && MasterServerPassword.Equals(msg.serverPassword))) { // successful result = MessageTypes.CustomEventType.NoError; } else { result = MessageTypes.CustomEventType.ServerPasswordFail; } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnListHosts result: " + result); } var response = new MessageTypes.ListHostsResponseMessage(); // if there are hosts connected, build the array of HostStubs to send to the client if (ConnectedHosts.Connected.Count > 0) { response.resultCode = (int)MessageTypes.CustomEventType.NoError; var stubs = new List <HostStub>(); // loop through the connected hosts, get info for each host, get the players for each host, and build stubs to send back foreach (var host in ConnectedHosts.Connected) { if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnListHosts Adding Host: " + host.Value.HostName); } stubs.Add(new HostStub(host.Value.ConnectedPlayers.GetPlayerStubs().ToArray(), host.Value.HostName, host.Value.Comment, host.Value.PlayerLimit)); } response.hosts = stubs.ToArray(); } else { response.resultCode = (int)MessageTypes.CustomEventType.NoHostsRegistered; } netMsg.conn.Send((short)MessageTypes.ListHostsResponse, response); UpdateUI(); } }
/// <summary> /// Event Handler for Application Event PlayerChangeHost /// </summary> /// <param name="netMsg"></param> void OnUpdatePlayerHost(NetworkMessage netMsg) { if (IsConnectionVerified(netMsg.conn)) { var msg = netMsg.ReadMessage <MessageTypes.UpdatePlayerHostMessage>(); MessageTypes.CustomEventType result = MessageTypes.CustomEventType.UnknownError; if (string.IsNullOrEmpty(msg.playerKey)) { result = MessageTypes.CustomEventType.KeyEmpty; } else if (!string.IsNullOrEmpty(msg.hostName) && ConnectedHosts.Connected.ContainsKey(msg.hostName)) { PlayerInstance player = null; // check given host first for quick check, then check all hosts if (!ConnectedHosts.Connected[msg.hostName].VerifyPassword(msg.hostPassword)) { result = MessageTypes.CustomEventType.HostPasswordFail; } else if ((player = ConnectedHosts.RemovePlayer(msg.playerKey)) != null) { // successful removal, now add to new Host player.HostLocation = ConnectedHosts.Connected[msg.hostName]; player.HostLoginTime = System.DateTime.UtcNow; ConnectedHosts.AddPlayer(msg.hostName, player); result = MessageTypes.CustomEventType.NoError; } else { result = MessageTypes.CustomEventType.DuplicateDetected; } } else { result = MessageTypes.CustomEventType.HostUnknown; } if (MasterServer.ViewDebugMessages) { Debug.Log("<MasterServer> OnUpdatePlayerHost result: " + result); } var response = new MessageTypes.UpdatePlayerHostResponseMessage(); response.resultCode = (int)result; response.playerKey = msg.playerKey; netMsg.conn.Send((short)MessageTypes.UpdatePlayerHostResponse, response); UpdateUI(); } }