public void PreventDisconnects() { string toBeSent = "3"; SendJSONMessageToAll(toBeSent, QosType.Reliable); toBeSent = "9"; GameInstanceStats gs = new GameInstanceStats(_serverName); toBeSent += JsonUtility.ToJson(gs); SendJSONMessageToMaster(toBeSent); }
private void Update() { _timer -= Time.deltaTime; if (_timer <= 0.0f) { GameServerManager.Instance.InGamePlayers = 0; GameServerManager.Instance.SendJSONMessageToAll("10", QosType.Reliable); ToggleWindows(WindowIDs.Victory, WindowIDs.None); // Set this game instance to allow connections again string toBeSent = "8"; GameInstanceStats gs = new GameInstanceStats(GameServerManager.Instance.ServerName); toBeSent += JsonUtility.ToJson(gs); GameServerManager.Instance.SendJSONMessageToMaster(toBeSent); // Clear _clients dictionary inside game server GameServerManager.Instance.clearClients(); } else { GetComponentsInChildren <Text>()[1].text = "Redirecting in " + (int)_timer + " seconds..."; } }
private IEnumerator ForwardPlayerToGameWithDelay(string serverName, ClientInfo client) { while (_gameInstances[serverName].serverID == 0) { yield return(0); } GameInstanceStats gs = new GameInstanceStats(serverName); Debug.Log("MS: " + serverName); // Inform the server of its name. byte error = 0; string jsonToBeSent = "7"; jsonToBeSent += JsonUtility.ToJson(gs); byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(_gameInstances[serverName].socketID, _gameInstances[serverName].connectionID, _gameInstances[serverName].channelID, messageBuffer, messageBuffer.Length, out error); // When the instance has finally been created and setup, forward the player to the new game server. jsonToBeSent = "14"; jsonToBeSent += JsonUtility.ToJson(new PortID(_gameInstances[serverName].serverID)); messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(client.socketID, client.ConnectionID, client.ChannelID, messageBuffer, messageBuffer.Length, out error); }
// Update is called once per frame void Update() { // Incoming Connection IDs int incomingSocketID = -1; int incomingConnectionID = -1; int incomingChannelID = -1; byte[] incomingMessageBuffer = new byte[_incomingBufferSize]; int dataSize = 0; byte error; NetworkEventType incomingNetworkEvent = NetworkTransport.Receive(out incomingSocketID, out incomingConnectionID, out incomingChannelID, incomingMessageBuffer, _incomingBufferSize, out dataSize, out error); switch (incomingNetworkEvent) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: Debug.Log("Master Server: new client connected"); ++_numberOfConnections; break; case NetworkEventType.DataEvent: string message = Encoding.UTF8.GetString(incomingMessageBuffer); int prefix = 0; int index; string newMessage = ""; // New parser now allows client commands to be > 1 digit for (index = 0; index < message.Length; ++index) { if (message[index] == '{') { break; } } prefix = Convert.ToInt32(message.Substring(0, index)); newMessage = message.Substring(index); // Have game servers register themselves. if (prefix == (int)MasterServerCommands.RegisterGameServer) { Debug.Log("register a server"); foreach (KeyValuePair <string, GameInstanceStats> instance in _gameInstances) { if (instance.Value.serverID == 0) { instance.Value.serverID = JsonUtility.FromJson <PortID>(newMessage).portID; instance.Value.socketID = incomingSocketID; instance.Value.connectionID = incomingConnectionID; instance.Value.channelID = incomingChannelID; break; } } } else if (prefix == (int)MasterServerCommands.RegisterClient) { if (!_clients.ContainsKey(incomingConnectionID)) { ClientInfo clientInfo = new ClientInfo(incomingSocketID, incomingConnectionID, incomingChannelID); _clients.Add(incomingConnectionID, clientInfo); Debug.Log(incomingConnectionID + " added to _clients"); } } //process login info else if (prefix == (int)MasterServerCommands.C_VerifyLogin) { LoginInfo info = JsonUtility.FromJson <LoginInfo>(newMessage); StartCoroutine(verifyLogin(info.username, info.password, incomingSocketID, incomingConnectionID, incomingChannelID)); } //process create account info else if (prefix == (int)MasterServerCommands.C_CreateAccount) { LoginInfo info = JsonUtility.FromJson <LoginInfo>(newMessage); StartCoroutine(CreateUser(info.username, info.password, incomingSocketID, incomingConnectionID, incomingChannelID)); } // Process game connection requests else if (prefix == (int)MasterServerCommands.C_SelectGameInstance) { string serverName = JsonUtility.FromJson <ServerInfo>(newMessage).servername; if (_gameInstances.ContainsKey(serverName.ToLower())) { // Connect client to an already existing game server. if (_gameInstances[serverName].inGamePlayers < 4 && _gameInstances[serverName].openToConnections == true) { // Tells client that game is being created string jsonToBeSent = "13"; jsonToBeSent += JsonUtility.ToJson(""); byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(incomingSocketID, incomingConnectionID, incomingChannelID, messageBuffer, messageBuffer.Length, out error); Debug.Log("Forwarding player to already established game: " + serverName); StartCoroutine(ForwardPlayerToGame(serverName.ToLower(), _clients[incomingConnectionID])); } else { Debug.Log("In Game Players: " + _gameInstances[serverName].inGamePlayers); Debug.Log("Open to new connections? " + _gameInstances[serverName].openToConnections); string jsonToBeSent = "12"; jsonToBeSent += JsonUtility.ToJson(""); byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(incomingSocketID, incomingConnectionID, incomingChannelID, messageBuffer, messageBuffer.Length, out error); } } else { if (_numberOfGameInstances == _maxGameInstances) { Debug.Log("Maximum game instances reached."); MaximumInstancesReached(_gameInstances, _clients[incomingConnectionID]); } else { // Create an instance of a game and have the client connect. _numberOfGameInstances++; _gameInstances.Add(serverName.ToLower(), new GameInstanceStats(serverName.ToLower())); if (_enableAutomaticServerInstanceExecute) { System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.FileName = _GameInstancePath; System.Diagnostics.Process.Start(startInfo); } // Tells client that game is being created string jsonToBeSent = "13"; jsonToBeSent += JsonUtility.ToJson(""); byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(incomingSocketID, incomingConnectionID, incomingChannelID, messageBuffer, messageBuffer.Length, out error); // Forwards player to game StartCoroutine(ForwardPlayerToGameWithDelay(serverName.ToLower(), _clients[incomingConnectionID])); } } } else if (prefix == (int)MasterServerCommands.VerifyOccupancy) { int inGamePlayers = JsonUtility.FromJson <GameServerInfo>(newMessage).inGamePlayers; string serverName = JsonUtility.FromJson <GameServerInfo>(newMessage).serverName; Debug.Log("Verify Occupancy: " + serverName); _gameInstances[serverName.ToLower()].inGamePlayers = inGamePlayers; } else if (prefix == (int)MasterServerCommands.GS_openToConnections) { GameInstanceStats gs = JsonUtility.FromJson <GameInstanceStats>(newMessage); Debug.Log("OpenToConnections message received | " + gs.serverName); _gameInstances[gs.serverName.ToLower()].openToConnections = true; } else if (prefix == (int)MasterServerCommands.GS_closedToConnections) { GameInstanceStats gs = JsonUtility.FromJson <GameInstanceStats>(newMessage); Debug.Log("closedToConnections message received | " + gs.serverName); _gameInstances[gs.serverName.ToLower()].openToConnections = false; } break; case NetworkEventType.DisconnectEvent: _numberOfConnections = --_numberOfConnections < 0 ? 0 : _numberOfConnections; if (_activeusernames.Contains(_clients[incomingConnectionID].username)) { _activeusernames.Remove(_clients[incomingConnectionID].username); } break; } }
void Update() { CaptureFrame(); int incomingSocketID = -1; int incomingConnectionID = -1; int incomingChannelID = -1; byte[] incomingMessageBuffer = new byte[_incomingBufferSize]; int dataSize = 0; byte error; NetworkEventType incomingNetworkEvent = NetworkTransport.Receive(out incomingSocketID, out incomingConnectionID, out incomingChannelID, incomingMessageBuffer, _incomingBufferSize, out dataSize, out error); switch (incomingNetworkEvent) { case NetworkEventType.Nothing: break; case NetworkEventType.ConnectEvent: Debug.Log("GameServer: ConnectEvent"); // Master Server Connection if (incomingConnectionID == 1) { // Tell the Master Server to Inform the Client of the connection info string jsonToBeSent = "2"; jsonToBeSent += JsonUtility.ToJson(new PortID(GS_Port)); byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent); NetworkTransport.Send(_socketID, MS_ConnectionID, TCP_ChannelID, messageBuffer, messageBuffer.Length, out error); } // Client Connections else if (incomingConnectionID > 1) { ClientInfo clientInfo = new ClientInfo(); clientInfo.socketID = incomingSocketID; clientInfo.ConnectionID = incomingConnectionID; clientInfo.ChannelID = incomingChannelID; _clients.Add(incomingConnectionID, clientInfo); _inGamePlayers = _clients.Count; // Need to inform master server of current connections every time a new client connects string jsonToBeSend = "6"; jsonToBeSend += JsonUtility.ToJson(new GameServerInfo(_inGamePlayers, _serverName)); SendJSONMessageToMaster(jsonToBeSend); } break; case NetworkEventType.DataEvent: Debug.Log("Game Server: Data Event"); string message = Encoding.UTF8.GetString(incomingMessageBuffer); int prefix = 0; int index; string newMessage = ""; // New parser now allows client commands to be > 1 digit for (index = 0; index < message.Length; ++index) { if (message[index] == '{') { break; } } prefix = Convert.ToInt32(message.Substring(0, index)); newMessage = message.Substring(index); //process user game input if (prefix == (int)GameServerCommands.PlayerInput) { PlayerIO input = JsonUtility.FromJson <PlayerIO>(newMessage); GameManager.Instance.PlayerActions(incomingConnectionID, input); } else if (prefix == (int)GameServerCommands.SetUsername) { _clients[incomingConnectionID].username = JsonUtility.FromJson <LoginInfo>(newMessage).username; _clients[incomingConnectionID].playerNum = incomingConnectionID - 2; // decremented so the range starts with 0 and not 1 _notifArea.playerEntered(JsonUtility.FromJson <LoginInfo>(newMessage).username); string jsonToBeSent = "0"; SendJSONMessage(jsonToBeSent, _clients[incomingConnectionID], QosType.Reliable); // IF the lobby is not loaded, load it. if (WindowManager.Instance.currentWindow == WindowIDs.None) { WindowManager.Instance.ToggleWindows(WindowIDs.None, WindowIDs.Lobby); } if (_lobby.gameObject.activeInHierarchy == true) { _lobby.AddPlayerToLobby(_clients[incomingConnectionID].username, _clients[incomingConnectionID].playerNum); } } else if (prefix == (int)GameServerCommands.LeaveLobby) { Debug.Log("Leave Lobby Command"); _inGamePlayers = --_inGamePlayers < 0 ? 0 : _inGamePlayers; if (_lobby.gameObject.activeInHierarchy == true) { _lobby.RemovePlayerFromLobby(_clients[incomingConnectionID].playerNum); } } else if (prefix == (int)GameServerCommands.LeaveGame) { _inGamePlayers--; if (_inGamePlayers < 0) { _inGamePlayers = 0; } if (_inGamePlayers < 1) { GameManager.Instance.ResetGameManager(); SceneManager.LoadScene("Server Game Version"); string toBeSent = "8" /*MasterServerCommands.GS_openToConnections.ToString()*/; /*UNTESTED - Should be 8*/ //Debug.Log("Testing line 257 of GameServerManager: " + Assert.Equals("8", MasterServerCommands.GS_openToConnections.ToString())); GameInstanceStats gs = new GameInstanceStats(_serverName); toBeSent += JsonUtility.ToJson(gs); SendJSONMessageToMaster(toBeSent); } } else if (prefix == (int)GameServerCommands.AssignName) { GameInstanceStats gs = JsonUtility.FromJson <GameInstanceStats>(newMessage); _serverName = gs.serverName; } break; case NetworkEventType.DisconnectEvent: if (incomingConnectionID > 1) { _inGamePlayers = --_inGamePlayers < 0 ? 0 : _inGamePlayers; if (_lobby.gameObject.activeInHierarchy == true) { _lobby.RemovePlayerFromLobby(_clients[incomingConnectionID].playerNum); } GameManager.Instance.LeaveGame(incomingConnectionID); _clients.Remove(incomingConnectionID); // Need to inform master server of current connections string jsonToBeSent1 = "6"; jsonToBeSent1 += JsonUtility.ToJson(new GameServerInfo(_inGamePlayers, _serverName)); SendJSONMessageToMaster(jsonToBeSent1); if (_inGamePlayers < 1) { GameManager.Instance.ResetGameManager(); WindowManager.Instance.ToggleWindows(WindowIDs.PlayerInfo, WindowIDs.None); SceneManager.LoadScene("Server Game Version"); } } break; } }