void Update() { m_Driver.ScheduleUpdate().Complete(); // Clean up connections for (int i = 0; i < m_Connections.Length; i++) { if (!m_Connections[i].IsCreated) { m_Connections.RemoveAtSwapBack(i); --i; } } // Accept new connections NetworkConnection c; while ((c = m_Driver.Accept()) != default(NetworkConnection) && Clients.Count < 4) { m_Connections.Add(c); //Debug.Log("Accepted a connection"); //Send Welcome Message to Client var color = Lobby.ColorList[Clients.Count]; uint encodedColor = (uint)DataUtilities.EncodeColor(color); //uint colourInt = ((uint)colour.r << 24 | (uint)colour.g << 16 | (uint)colour.b << 8 | (uint)colour.a); ServerClient newClient = new ServerClient(c.InternalId, "", color); newClient.StartHealth = Lobby.PlayerStartHealth; Clients.Add(newClient); var message = new WelcomeMessage { PlayerID = c.InternalId, Colour = encodedColor, }; var writer = m_Driver.BeginSend(c); message.SerializeObject(ref writer); m_Driver.EndSend(writer); } DataStreamReader reader; for (int i = 0; i < m_Connections.Length; i++) { if (!m_Connections[i].IsCreated) { continue; } NetworkEvent.Type cmd; while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out reader)) != NetworkEvent.Type.Empty) { if (cmd == NetworkEvent.Type.Data) { var messageType = (Message.MessageType)reader.ReadUShort(); switch (CurrentGameState) { case GameState.Lobby: LobbyData.HandleMessages(messageType, reader, i); break; case GameState.InGame: GameData.HandleMessages(messageType, reader, i); break; default: break; } } else if (cmd == NetworkEvent.Type.Disconnect) { for (int j = 0; j < m_Connections.Length; j++) { var leftMessage = new PlayerLeft { PlayerDisconnectID = Clients[i].ClientID, }; var write = m_Driver.BeginSend(m_Connections[j]); leftMessage.SerializeObject(ref write); m_Driver.EndSend(write); } Clients.RemoveAt(i); Lobby.LobbyUI.GetComponent <PlayerLobbyList>().RemoveFromList(i); m_Connections[i] = default(NetworkConnection); } } } }
public override void HandleMessages(Message.MessageType messageType, DataStreamReader reader, int ConnectionID) { if (IsClient)//Clientside handling { switch (messageType) { case Message.MessageType.NewPlayer: var newPlayer = new NewPlayer(); newPlayer.DeserializeObject(ref reader); //Debug.Log(newPlayer.PlayerName + " " + newPlayer.PlayerID + " " + newPlayer.Colour); ServerClient _newClient = new ServerClient(newPlayer.PlayerID, newPlayer.PlayerName, DataUtilities.DecodeColor((int)newPlayer.Colour)); ClientBehaviour.Instance.Clients.Add(_newClient); if (!ClientBehaviour.Instance.IsHost) { ClientBehaviour.Instance.Lobby.PrintMessage(newPlayer.PlayerName + ", joined the game.", Color.green); ClientBehaviour.Instance.Lobby.LobbyUI.GetComponent <PlayerLobbyList>().UpdateLobby(_newClient); } break; case Message.MessageType.Welcome: var welcomeMessage = new WelcomeMessage(); welcomeMessage.DeserializeObject(ref reader); ClientBehaviour.Instance.PlayerID = welcomeMessage.PlayerID; if (!ClientBehaviour.Instance.IsHost) { ClientBehaviour.Instance.Lobby.PrintMessage("Succesfully joined the server.", Color.green); } //Send Name to Server. var nameMessage = new SetNameMessage { Name = ClientBehaviour.Instance.PlayerName }; var writer = ClientBehaviour.Instance.m_Driver.BeginSend(ClientBehaviour.Instance.m_Connection); nameMessage.SerializeObject(ref writer); ClientBehaviour.Instance.m_Driver.EndSend(writer); if (!ClientBehaviour.Instance.IsHost) { ClientBehaviour.Instance.Lobby.PrintMessage("Succesfully Connected to the Server.", Color.green); } break; case Message.MessageType.SetName: break; case Message.MessageType.RequestDenied: var deniedMessage = new RequestDenied(); deniedMessage.DeserializeObject(ref reader); Debug.Log("Message ID: " + deniedMessage.DeniedMessageID + " was denied by the server."); break; case Message.MessageType.PlayerLeft: var playerLeft = new PlayerLeft(); playerLeft.DeserializeObject(ref reader); ClientBehaviour.Instance.Clients.RemoveAt(playerLeft.PlayerDisconnectID); if (!ClientBehaviour.Instance.IsHost) { ClientBehaviour.Instance.Lobby.PrintMessage("Player " + playerLeft.PlayerDisconnectID + " has disconnected.", Color.yellow); } break; case Message.MessageType.StartGame: var startGame = new StartGame(); startGame.DeserializeObject(ref reader); for (int i = 0; i < ClientBehaviour.Instance.Clients.Count; i++) { ClientBehaviour.Instance.Clients[i].StartHealth = startGame.StartHP; //Debug.Log(startGame.StartHP); } if (!ClientBehaviour.Instance.IsHost) { ClientBehaviour.Instance.Lobby.PrintMessage("Game Starting...", Color.blue); } ClientBehaviour.Instance.CurrentGameState = GameState.InGame; ClientBehaviour.Instance.ClientUI = GameObject.Instantiate(Resources.Load("ClientUI") as GameObject).GetComponent <ClientUI>(); ClientBehaviour.Instance.ClientUI.PlayerInfo.text = ClientBehaviour.Instance.PlayerName; ClientBehaviour.Instance.ClientUI.HealthInfo.text = startGame.StartHP + "/" + startGame.StartHP + " HP"; GameObject.DontDestroyOnLoad(ClientBehaviour.Instance.ClientUI.gameObject); UnityEngine.SceneManagement.SceneManager.LoadScene("GameScene"); break; case Message.MessageType.None: default: //Debug.Log("Received Server Data | " + ConnectionID); break; } } else //Serverside handling { switch (messageType) { case Message.MessageType.NewPlayer: break; case Message.MessageType.Welcome: break; case Message.MessageType.SetName: var _message = new SetNameMessage(); _message.DeserializeObject(ref reader); //Debug.Log("Welcome User:"******"Received Client Data | " + ConnectionID); break; } } }