namespace XFEExtension.NetCore.CyberComm; /// /// 通信群组控制器 /// public class CyberCommGroupController { private readonly List _commGroups; /// /// 刷新 /// public void Refresh() { for (var i = _commGroups.Count - 1; i >= 0; i--) { if (_commGroups[i].Count == 0) { _commGroups.Remove(_commGroups[i]); } } } /// /// 添加群组 /// /// public void AddGroup(CyberCommGroup commGroup) { _commGroups.Add(commGroup); } /// /// 移除群组 /// /// public void RemoveGroup(CyberCommGroup commGroup) { _commGroups.Remove(commGroup); } /// /// 移除指定索引的群组 /// /// public void RemoveGroupAt(int index) { _commGroups.RemoveAt(index); } /// /// 清空群组 /// public void Clear() { _commGroups.Clear(); } /// /// 群组数量 /// public int Count { get { return _commGroups.Count; } } /// /// 索引器 /// /// /// public CyberCommGroup this[int index] { get { return _commGroups[index]; } } /// /// 索引器 /// /// /// public CyberCommGroup? this[string groupId] { get { foreach (var commGroup in _commGroups) { if (commGroup.GroupId == groupId) { return commGroup; } } return null; } } /// /// 通信群组控制器发送文本消息 /// /// 目标群组的ID /// 发送的文本消息 public async Task SendGroupTextMessage(string groupId, string message) { var commGroup = this[groupId]; if (commGroup is not null) { await commGroup.SendGroupTextMessage(message); } } /// /// 通信群组控制器发送二进制消息 /// /// 目标群组的ID /// 发送的二进制消息 public async Task SendGroupBinaryMessage(string groupId, byte[] bytes) { var commGroup = this[groupId]; if (commGroup is not null) { await commGroup.SendGroupBinaryMessage(bytes); } } /// /// 通信群组控制器 /// public CyberCommGroupController() { _commGroups = []; } }