/** * Broadcast to all connected clients */ public void Broadcast(string msg, bool flag = false, Client client = null) { if (flag && client == null) { return; // Add some security for NullReferenceException } foreach (var item in ClientList) { var broadcastClient = item.Value; if (broadcastClient == client) { continue; // Stop if it is the same socket } if (broadcastClient.Lobby != null) { continue; // Stop if the client is in a Lobby } NetworkStream broadcastStream; try { broadcastStream = broadcastClient.Socket.GetStream(); } catch (InvalidOperationException) { // Remove client if we can't get its stream PendingDisconnection.Add(broadcastClient); continue; } WriteManager.Run(broadcastStream, Wrapper.Type.Message, flag ? client.Info.Name + " says : " + msg : msg); } // Check disconnection after all broadcast CheckDisconnection(); }
/** * Remove a client */ public void RemoveClient(Client client) { PendingDisconnection.Add(client); }