/// <summary> /// Sends a <see cref="UserSyncPacket"/> containing all users to the given connection. /// Usernames are stripped from users for security. /// </summary> /// <param name="connectionId">Destination connection ID</param> private void sendSyncPacket(string connectionId) { // Create a blank sync packet UserSyncPacket packet = new UserSyncPacket(); lock (userStoreLock) { // Add users to the sync packet foreach (User userRef in userStore.Users.Values) { // Get a non-reference copy of the user so we can blank out the username without affecting the original User user = userRef.Clone(); user.Username = ""; packet.Users.Add(user); } } // Pack it Any packedPacket = ProtobufPacketHelper.Pack(packet); // Send it on its way if (!netServer.TrySend(connectionId, MODULE_NAME, packedPacket.ToByteArray())) { RaiseLogEntry(new LogEventArgs("Failed to send UserSyncPacket to connection " + connectionId, LogLevel.ERROR)); } }
/// <summary> /// Handles incoming <see cref="UserSyncPacket"/>s. /// </summary> /// <param name="connectionId">Original connection ID</param> /// <param name="packet">Incoming <see cref="UserSyncPacket"/></param> private void userSyncPacketHandler(string connectionId, UserSyncPacket packet) { lock (userStoreLock) { foreach (User user in packet.Users) { if (userStore.Users.ContainsKey(user.Uuid)) { // Update/replace the user userStore.Users[user.Uuid] = user; } else { // Add the user userStore.Users.Add(user.Uuid, user); } } } }