private void OnData(DataStreamReader stream)
    {
        NativeArray <byte> message = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(message);
        string returnData = Encoding.ASCII.GetString(message.ToArray());

        NetworkHeader header = new NetworkHeader();

        header = JsonUtility.FromJson <NetworkHeader>(returnData);

        switch (header.cmd)
        {
        case Commands.NEW_CLIENT:
        {
            Debug.Log("New client");
            NewPlayer np = JsonUtility.FromJson <NewPlayer>(returnData);
            Debug.Log("NEW_CLIENT: " + np.player.ToString());
            SpawnPlayers(np.player);
            break;
        }

        case Commands.UPDATE:
        {
            UpdatedPlayer up = JsonUtility.FromJson <UpdatedPlayer>(returnData);
            UpdatePlayers(up.update);
            break;
        }

        case Commands.CLIENT_DROPPED:
        {
            DisconnectedPlayer dp = JsonUtility.FromJson <DisconnectedPlayer>(returnData);
            DestroyPlayers(dp.disconnect);
            Debug.Log("Client dropped");
            break;
        }

        case Commands.CLIENT_LIST:
        {
            ConnectedPlayer cp = JsonUtility.FromJson <ConnectedPlayer>(returnData);
            SpawnPlayers(cp.connect);
            Debug.Log("Client list");
            break;
        }

        case Commands.OWN_ID:
        {
            NewPlayer p = JsonUtility.FromJson <NewPlayer>(returnData);
            myID = p.player.id;
            SpawnPlayers(p.player);
            Debug.Log("OWN_ID: " + myID);
            break;
        }

        default:
            Debug.Log("Error");
            break;
        }
    }
Esempio n. 2
0
    private void CleanupClients()
    {
        List <Client> dropped = new List <Client>();

        for (int i = 0; i < clients.Count; i++)
        {
            clients[i].interval += Time.deltaTime;

            if (clients[i].interval >= 2.0f)
            {
                int connectionIndex = FindMatchingConnection(clients[i].id);
                if (connectionIndex >= 0)
                {
                    m_Connections[connectionIndex] = default(NetworkConnection);
                }
                dropped.Add(clients[i]);
                Destroy(players[i]);
                clients.RemoveAt(i);
                players.RemoveAt(i);
                i--;
            }
        }

        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        if (dropped.Count > 0)
        {
            DisconnectedPlayer drop = new DisconnectedPlayer(dropped);
            foreach (Client client in clients)
            {
                SendData(drop, client);
            }
        }
    }
Esempio n. 3
0
        public void DisconnectPlayer(RealPlayer player)
        {
            lock (syncObject)
            {
                int playerNum = players.FindIndex(p => p.ID == player.ID);

                if (playerNum == -1)
                {
                    return;
                }

                players[playerNum] = new DisconnectedPlayer(player);

                if (playerNum == currentPlayerNum)
                {
                    stepNextPlayerNoLock();
                }
                else
                {
                    sendToAllPlayers(p => p.SendGamePlayersSnapshot());
                }
            }
        }
Esempio n. 4
0
    private void OnData(DataStreamReader stream)
    {
        NativeArray <byte> message = new NativeArray <byte>(stream.Length, Allocator.Temp);

        stream.ReadBytes(message);
        string returnData = Encoding.ASCII.GetString(message.ToArray());

        NetworkHeader header = new NetworkHeader();

        try
        {
            header = JsonUtility.FromJson <NetworkHeader>(returnData);
        }
        catch (System.ArgumentException e)
        {
            Debug.LogError(e.ToString() + "\nFailed To Load. Disconnect");
            Disconnect();
            return;
        }

        try
        {
            switch (header.cmd)
            {
            case Commands.NEW_CLIENT:
            {
                Debug.Log("A Client Appears!");
                NewPlayer np = JsonUtility.FromJson <NewPlayer>(returnData);
                Debug.Log(np.player.ToString());
                SpawnPlayers(np.player);
                break;
            }

            case Commands.UPDATE:
            {
                UpdatedPlayer up = JsonUtility.FromJson <UpdatedPlayer>(returnData);
                UpdatePlayers(up.update);
                break;
            }

            case Commands.CLIENT_DROPPED:
            {
                DisconnectedPlayer dp = JsonUtility.FromJson <DisconnectedPlayer>(returnData);
                DestroyPlayers(dp.disconnect);
                Debug.Log("A Client Disappers!");
                break;
            }

            case Commands.CLIENT_LIST:
            {
                ConnectedPlayer cp = JsonUtility.FromJson <ConnectedPlayer>(returnData);
                SpawnPlayers(cp.connect);
                Debug.Log("Client_List");
                break;
            }

            case Commands.OWN_ID:
            {
                NewPlayer p = JsonUtility.FromJson <NewPlayer>(returnData);
                myID = p.player.id;
                SpawnPlayers(p.player);
                Debug.Log("PlayerID");
                break;
            }

            default:
                Debug.Log("Error!");
                break;
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError(e.ToString() + "\nMessage Contents Failed to Load. Disconnect");
            Disconnect();
            return;
        }
    }