SoftMsgQueue.cs
3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
namespace HslCommunication.BasicFramework
{
/// <summary>
/// 一个简单通用的消息队列
/// </summary>
/// <typeparam name="T">类型</typeparam>
public class SoftMsgQueue<T> : SoftFileSaveBase
{
#region Constructor
/// <summary>
/// 实例化一个对象
/// </summary>
public SoftMsgQueue()
{
LogHeaderText = "SoftMsgQueue<" + typeof(T).ToString() + ">";
}
#endregion
/// <summary>
/// 所有临时存储的数据
/// </summary>
private Queue<T> all_items = new Queue<T>();
private int m_Max_Cache = 200;
/// <summary>
/// 临时消息存储的最大条数,必须大于10
/// </summary>
public int MaxCache
{
get { return m_Max_Cache; }
set { if (value > 10) m_Max_Cache = value; }
}
/// <summary>
/// 获取最新添加进去的数据
/// </summary>
public T CurrentItem
{
get
{
if (all_items.Count > 0)
{
return all_items.Peek();
}
else
{
return default(T);
}
}
}
/// <summary>
/// 将集合进行锁定
/// </summary>
private object lock_queue = new object();
/// <summary>
/// 新增一条数据
/// </summary>
public void AddNewItem(T item)
{
lock (lock_queue)
{
while (all_items.Count >= m_Max_Cache)
{
all_items.Dequeue();
}
all_items.Enqueue(item);
}
}
/// <summary>
/// 获取存储字符串
/// </summary>
/// <returns></returns>
public override string ToSaveString()
{
return JArray.FromObject(all_items).ToString();
}
/// <summary>
/// 获取加载字符串
/// </summary>
/// <param name="content"></param>
public override void LoadByString(string content)
{
JArray array = JArray.Parse(content);
all_items = (Queue<T>)array.ToObject(typeof(Queue<T>));
}
}
/// <summary>
/// 系统的消息类,用来发送消息,和确认消息的
/// </summary>
public class MessageBoard
{
/// <summary>
/// 发送方名称
/// </summary>
public string NameSend { get; set; } = "";
/// <summary>
/// 接收方名称
/// </summary>
public string NameReceive { get; set; } = "";
/// <summary>
/// 发送时间
/// </summary>
public DateTime SendTime { get; set; } = DateTime.Now;
/// <summary>
/// 发送的消息内容
/// </summary>
public string Content { get; set; } = "";
/// <summary>
/// 消息是否已经被查看
/// </summary>
public bool HasViewed { get; set; }
}
}