Пример #1
0
    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);
            Debug.Log("Handshake message received!");
            break;

        case Commands._ID:
            //new message to update player ID
            PlayerUpdateMsg puIDMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            Debug.Log("Player connected with ID " + puIDMsg.player.id);
            MsgPlayerInfo.player.id = puIDMsg.player.id;
            ID = MsgPlayerInfo.player.id;
            break;

        case Commands.PLAYER_UPDATE:
            //new message to to update player position from server
            PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            Debug.Log("Player position: " + puMsg.player.pos);
            break;

        case Commands.SERVER_UPDATE:
            //message to manage server updates.
            ServerUpdateMsg ServerUpdateMessage = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("Server Update sent.");
            for (int i = 0; i < ServerUpdateMessage.players.Count; ++i)
            {
                if (ConnectedPlayers.ContainsKey(ServerUpdateMessage.players[i].id))
                {
                    //assign respective player position  as per the server
                    ConnectedPlayers[ServerUpdateMessage.players[i].id].transform.position = ServerUpdateMessage.players[i].pos;
                    //handles dropped clients on server.
                    if (ServerUpdateMessage.players[i].isDropped)
                    {
                        ConnectedPlayers[ServerUpdateMessage.players[i].id].GetComponent <PlayerInput>().isDropped = true;
                    }
                }
            }
            break;

        case Commands.CURRENT_PLAYERS:
            //messsage to manage current players in server
            //instantiates a cube & sets the ID and position
            ServerUpdateMsg MsgCurrentPlayers = JsonUtility.FromJson <ServerUpdateMsg>(recMsg);
            Debug.Log("server received Current Players info.");
            for (int i = 0; i < MsgCurrentPlayers.players.Count; ++i)
            {
                GameObject PlayerCube = Instantiate(PlayerPrefab);
                ConnectedPlayers[MsgCurrentPlayers.players[i].id] = PlayerCube;
                PlayerCube.transform.position = MsgCurrentPlayers.players[i].pos;
            }
            break;

        case Commands.ADD_PLAYER:
            //new message to manage add player
            //instantiate a cube & added set the ID.
            PlayerUpdateMsg MsgAddPlayer  = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg);
            GameObject      NewPlayerCube = Instantiate(PlayerPrefab);
            ConnectedPlayers[MsgAddPlayer.player.id] = NewPlayerCube;

            break;

        case Commands.DROPPED_PLAYERS:
            //new message to manage dropped clients
            //if dropped client list has ids in it destroy and remove it.
            DroppedPlayersMsg DroppedMessage = JsonUtility.FromJson <DroppedPlayersMsg>(recMsg);
            Debug.Log("Dropped Client");
            for (int i = 0; i < DroppedMessage.DROPPEDPLAYERSLIST.Count; ++i)
            {
                if (ConnectedPlayers.ContainsKey(DroppedMessage.DROPPEDPLAYERSLIST[i]))
                {
                    Destroy(ConnectedPlayers[DroppedMessage.DROPPEDPLAYERSLIST[i]]);
                    ConnectedPlayers.Remove(DroppedMessage.DROPPEDPLAYERSLIST[i]);
                }
            }
            break;

        default:
            Debug.Log("Unrecognized message received!");
            break;
        }
    }
Пример #2
0
    void Update()
    {
        //list to maintain dropped clients
        List <string> DroppedClientsList = new List <string>();

        m_Driver.ScheduleUpdate().Complete();
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);
            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, m_Connections[i]);

                    heartBeat[m_Connections[i].InternalId.ToString()] = Time.time;
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }

        foreach (KeyValuePair <string, float> item in heartBeat)
        {
            if (Time.time - item.Value >= 5f)
            {
                //add the clent to the dropped client list
                DroppedClientsList.Add(item.Key);
            }
        }

        //check for droppedclientlist for being empty, if not..
        if (DroppedClientsList.Count != 0)
        {
            for (int i = 0; i < DroppedClientsList.Count; ++i)
            {
                //remove from the dictionaries.
                ConnectedPlayers.Remove(DroppedClientsList[i]);
                heartBeat.Remove(DroppedClientsList[i]);
            }

            DroppedPlayersMsg MsgDroppedPlayers = new DroppedPlayersMsg();
            MsgDroppedPlayers.DROPPEDPLAYERSLIST = DroppedClientsList;


            for (int i = 0; i < m_Connections.Length; i++)
            {
                if (DroppedClientsList.Contains(m_Connections[i].InternalId.ToString()) == true)
                {
                    continue;
                }
                //send dropped players message to client
                Assert.IsTrue(m_Connections[i].IsCreated);
                SendToClient(JsonUtility.ToJson(MsgDroppedPlayers), m_Connections[i]);
            }
        }
    }