Пример #1
0
        public void ReceiveHandshakeResponseBody(ref PacketReader reader)
        {
            // Allocation here is ok (and unavoidable). This doesn't happen very often (just once in normal circumstances) and
            // we're creating lists of peers - so at the very least we need to allocate the lists.
            var clientsByRooms = new Dictionary <string, List <ushort> >();
            var clients        = new List <ClientInfo>();

            reader.ReadHandshakeResponseBody(clients, clientsByRooms);

            // Load player IDs
            PlayerIds.Load(clients);

            //Remove all player objects who are not in the handshake response
            //Normally we would only receive one handshake response, but it's still valid to receive more
            var currentClients = new List <ClientInfo <TPeer?> >();

            GetClients(currentClients);
            for (var i = 0; i < currentClients.Count; i++)
            {
                if (PlayerIds.GetName(currentClients[i].PlayerId) != currentClients[i].PlayerName)
                {
                    RemoveClient(currentClients[i]);
                }
            }

            //Create a client object for every player which does not already exist
            foreach (var client in clients)
            {
                GetOrCreateClientInfo(client.PlayerId, client.PlayerName, client.CodecSettings, null);
            }

            //Clear all rooms
            ClearRooms();

            //Add remote clients into rooms according to `clientsByRooms` which we deserialized from packet
            foreach (var item in clientsByRooms)
            {
                foreach (var client in item.Value)
                {
                    ClientInfo <TPeer?> info;
                    if (!TryGetClientInfoById(client, out info))
                    {
                        Log.Warn("Attempted to add an unknown client '{0}' into room '{1}'", client, item.Key);
                    }
                    else
                    {
                        JoinRoom(item.Key, info);
                    }
                }
            }

            //Send back a response with the complete current state of this client and follow up with deltas every time the state changes
            SendClientState();
        }