-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMessageQueue.cs
136 lines (86 loc) · 3.08 KB
/
CMessageQueue.cs
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
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Ground_Floor
{
class CMessageQueue
{
private CMessage ReplyMessage;
private CMessage MessageToStore;
private Mutex LocRcvdMutex;
private Mutex LocSentMutex;
private List<CMessage> SentMessageQueue;
private List<CMessage> RcvdMessageQueue;
private bool _isQueueForMessagesToSend;
public CMessageQueue(bool isQueueForMessagesToSend)
{
_isQueueForMessagesToSend = isQueueForMessagesToSend;
RcvdMessageQueue = new List<CMessage>();
SentMessageQueue = new List<CMessage>();
ReplyMessage = new CMessage();
LocRcvdMutex = new Mutex();
LocSentMutex = new Mutex();
}
public CMessage PushMessage(CMessage IncommingMessage,bool ToSendBuffer)
{
List<CMessage> MessageQueue;
Mutex locMutex;
if (ToSendBuffer) {
MessageQueue = SentMessageQueue;
locMutex = LocSentMutex;
}
else {
MessageQueue = RcvdMessageQueue;
locMutex = LocRcvdMutex;
}
if (IncommingMessage.Command_Type() == CMessageConstants.CMD_TYPE_REPLY)
{
AckMessage(IncommingMessage);
ReplyMessage.SetMessage(0, CMessageConstants.CMD_TYPE_NONE, IncommingMessage.GetSourceThreadNo());
}
else {
MessageToStore = new CMessage();
MessageToStore.CopyMessage(IncommingMessage);
locMutex.WaitOne();
MessageQueue.Add(MessageToStore);
locMutex.ReleaseMutex();
ReplyMessage.SetMessage(0, CMessageConstants.CMD_TYPE_REPLY,IncommingMessage.GetSourceThreadNo());
ReplyMessage.AddIntPrm(IncommingMessage.Command_Id());
ReplyMessage.AddIntPrm(IncommingMessage.CheckSum());
}
return ReplyMessage;
}
public void AckMessage(CMessage IncommingMessage)
{
int Cmd_Id;
int ChkSm;
CMessage Message;
for (int iCpt=0;iCpt<SentMessageQueue.Count;iCpt++){
Message = SentMessageQueue[iCpt];
Cmd_Id = Message.Command_Id();
ChkSm = Message.CheckSum();
if ((Cmd_Id == IncommingMessage.GetPrmInt(0)) && (ChkSm == IncommingMessage.GetPrmInt(1)))
{
LocSentMutex.WaitOne();
SentMessageQueue.RemoveAt(iCpt);
LocSentMutex.ReleaseMutex();
}
}
}
public CMessage PullMessage()
{
CMessage ReturnMessage = new CMessage();
if (RcvdMessageQueue.Count>0) {
ReturnMessage.CopyMessage(RcvdMessageQueue[0]);
LocRcvdMutex.WaitOne();
RcvdMessageQueue.RemoveAt(0);
LocRcvdMutex.ReleaseMutex();
return ReturnMessage;
}
else {
return null;
}
}
}
}