using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Linq; namespace HslCommunication.BasicFramework { /// /// 一个简单通用的消息队列 /// /// 类型 public class SoftMsgQueue : SoftFileSaveBase { #region Constructor /// /// 实例化一个对象 /// public SoftMsgQueue() { LogHeaderText = "SoftMsgQueue<" + typeof(T).ToString() + ">"; } #endregion /// /// 所有临时存储的数据 /// private Queue all_items = new Queue(); private int m_Max_Cache = 200; /// /// 临时消息存储的最大条数,必须大于10 /// public int MaxCache { get { return m_Max_Cache; } set { if (value > 10) m_Max_Cache = value; } } /// /// 获取最新添加进去的数据 /// public T CurrentItem { get { if (all_items.Count > 0) { return all_items.Peek(); } else { return default(T); } } } /// /// 将集合进行锁定 /// private object lock_queue = new object(); /// /// 新增一条数据 /// public void AddNewItem(T item) { lock (lock_queue) { while (all_items.Count >= m_Max_Cache) { all_items.Dequeue(); } all_items.Enqueue(item); } } /// /// 获取存储字符串 /// /// public override string ToSaveString() { return JArray.FromObject(all_items).ToString(); } /// /// 获取加载字符串 /// /// public override void LoadByString(string content) { JArray array = JArray.Parse(content); all_items = (Queue)array.ToObject(typeof(Queue)); } } /// /// 系统的消息类,用来发送消息,和确认消息的 /// public class MessageBoard { /// /// 发送方名称 /// public string NameSend { get; set; } = ""; /// /// 接收方名称 /// public string NameReceive { get; set; } = ""; /// /// 发送时间 /// public DateTime SendTime { get; set; } = DateTime.Now; /// /// 发送的消息内容 /// public string Content { get; set; } = ""; /// /// 消息是否已经被查看 /// public bool HasViewed { get; set; } } }