void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_CONNECT: PlayerConnectMsg pcMsg = JsonUtility.FromJson <PlayerConnectMsg>(recMsg); SpawnPlayers(pcMsg.newPlayer.id, pcMsg.newPlayer.cubeColor); Debug.Log("New Player joined"); break; case Commands.INVERT_BALL: GameObject.FindGameObjectWithTag("Ball").GetComponent <Ball>().invert = -1; break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); UpdatePlayers(puMsg.players); break; case Commands.OWNED_ID: OwnIDMsg idMsg = JsonUtility.FromJson <OwnIDMsg>(recMsg); playerID = idMsg.ownedPlayer.id; Debug.Log("Own id received! id: " + playerID); break; case Commands.PLAYER_DROPPED: PlayerDropMsg dropMsg = JsonUtility.FromJson <PlayerDropMsg>(recMsg); foreach (NetworkObjects.NetworkPlayer p in dropMsg.droppedPlayers) { DestroyPlayers(p.id); } break; case Commands.PLAYER_LIST: PlayerListMsg plMsg = JsonUtility.FromJson <PlayerListMsg>(recMsg); foreach (var it in plMsg.players) { SpawnPlayers(it.id, it.cubeColor); Debug.Log("Spawn player with id: " + it.id); } break; default: Debug.Log("Unrecognized message received!"); break; } }
void DropClients() { PlayerDropMsg droppedList = new PlayerDropMsg(); for (int i = 0; i < plMsg.players.Count; i++) { msgInterval[i] += Time.deltaTime; if (msgInterval[i] >= 5.0f) { int index = MatchConnectionWithId(plMsg.players[i].id); if (index >= 0) { m_Connections[index] = default(NetworkConnection); } droppedList.droppedPlayers.Add(plMsg.players[i]); plMsg.players.RemoveAt(i); msgInterval[i] = 0; i--; } } // CleanUpConnections for (int i = 0; i < m_Connections.Length; i++) { if (!m_Connections[i].IsCreated) { m_Connections.RemoveAtSwapBack(i); --i; } } if (droppedList.droppedPlayers.Count > 0) { Debug.Log("Player Dropped"); for (int i = 0; i < m_Connections.Length; i++) { SendToClient(JsonUtility.ToJson(droppedList), m_Connections[i]); } } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); ownID = hsMsg.player.id; Debug.Log("Handshake message received!"); Debug.Log("Client ID is " + ownID); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); foreach (var it in suMsg.players) { if (localPlayerList.ContainsKey(it.id)) { localPlayerList[it.id].transform.position = it.pos; } } //Debug.Log("Server update message received!"); break; case Commands.PLAYER_ADD: PlayerAddMsg aMsg = JsonUtility.FromJson <PlayerAddMsg>(recMsg); SpawnPlayer(aMsg.player.id, aMsg.player.cubeColor); Debug.Log("Add New Player, ID: " + aMsg.player.id); break; case Commands.LIST_UPDATE: ListUpdateMsg lMsg = JsonUtility.FromJson <ListUpdateMsg>(recMsg); foreach (var it in lMsg.players) { SpawnPlayer(it.id, it.cubeColor); Debug.Log("Spawn other player, ID: " + it.id); } break; case Commands.PLAYER_DROP: PlayerDropMsg dropMsg = JsonUtility.FromJson <PlayerDropMsg>(recMsg); foreach (var it in dropMsg.dplayers) { if (localPlayerList.ContainsKey(it.id)) { GameObject temp = localPlayerList[it.id]; localPlayerList.Remove(it.id); Destroy(temp); } } break; default: Debug.Log("Unrecognized message received!"); break; } }
void Update() { m_Driver.ScheduleUpdate().Complete(); PlayerDropMsg m = new PlayerDropMsg(); for (int i = 0; i < playerList.Count; ++i) { playerList[i].lastHB += Time.deltaTime; if (playerList[i].lastHB > 5f) { int index = MatchConnectionWithId(playerList[i].id); Debug.Log("Dropped Player " + playerList[i].id); if (index >= 0) { m_Connections[i] = default(NetworkConnection); } m.dplayers.Add(playerList[i]); playerList.RemoveAt(i); --i; } } // CleanUpConnections for (int i = 0; i < m_Connections.Length; i++) { if (!m_Connections[i].IsCreated) { m_Connections.RemoveAtSwapBack(i); Debug.Log("Remove Connection"); --i; } } //recalculate m_connnections if (m.dplayers.Count > 0) { for (int i = 0; i < m_Connections.Length; ++i) { SendToClient(JsonUtility.ToJson(m), m_Connections[i]); } } // AcceptNewConnections NetworkConnection c = m_Driver.Accept(); while (c != default(NetworkConnection)) { OnConnect(c); // Check if there is another new connection c = m_Driver.Accept(); } DataStreamReader stream; for (int i = 0; i < m_Connections.Length; i++) { Assert.IsTrue(m_Connections[i].IsCreated); NetworkEvent.Type cmd; cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream); while (cmd != NetworkEvent.Type.Empty) { if (cmd == NetworkEvent.Type.Data) { OnData(stream, i); } else if (cmd == NetworkEvent.Type.Disconnect) { OnDisconnect(i); } cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream); } } }