private void HandleAlerts(NetworkStream stream) { User user = null; lock (users) { if (users.ContainsKey(Thread.CurrentThread)) { user = users[Thread.CurrentThread]; } } if (user != null) { bool sentAlerts = false; lock (alerts) { for (int i = 0; i < alerts.Count; i++) { AlertBroadcast alertBroadcast = alerts[i]; if (alertBroadcast == null) { continue; } bool shouldSend = false; lock (alertBroadcast) { shouldSend = alertBroadcast.Receivers.Select(r => r.username).Contains(user.username); } if (shouldSend) { SendResponse(Response.From(alertBroadcast.Content), stream); Console.WriteLine("Sent an alert of type " + alertBroadcast.Content.GetType()); sentAlerts = true; } } } //Cleanup if (sentAlerts) { lock (alerts) { for (int i = alerts.Count - 1; i >= 0; i--) { alerts[i].Receivers.RemoveAll(r => r.username == user.username); if (alerts[i].Receivers.Count == 0) { alerts.RemoveAt(i); } } } } } }
private void HandleAlerts(NetworkStream stream) { string user = null; lock (users) { if (users.ContainsKey(Thread.CurrentThread)) { user = users[Thread.CurrentThread]; } } if (user != null) { bool sentAlerts = false; for (int i = 0; i < alerts.Count; i++) { AlertBroadcast alertBroadcast = alerts[i]; if (alertBroadcast == null) { continue; } bool shouldSend = false; lock (alertBroadcast) { shouldSend = alertBroadcast.Receivers.Contains(user); } if (shouldSend) { SendResponse(Response.From(alertBroadcast.Content), stream); sentAlerts = true; } } //Cleanup if (sentAlerts) { lock (alerts) { for (int i = alerts.Count - 1; i >= 0; i--) { alerts[i].Receivers.Remove(user); if (alerts[i].Receivers.Count == 0) { alerts.RemoveAt(i); } } } } } }