|
@@ -0,0 +1,157 @@
|
|
1
|
+//
|
|
2
|
+// Created by 叶亮 on 2019/7/15.
|
|
3
|
+//
|
|
4
|
+
|
|
5
|
+#include "message_queue.h"
|
|
6
|
+#include "message_handler.h"
|
|
7
|
+
|
|
8
|
+MessageQueue::MessageQueue() {
|
|
9
|
+ init();
|
|
10
|
+}
|
|
11
|
+
|
|
12
|
+MessageQueue::MessageQueue(const char* queueNameParam){
|
|
13
|
+ init();
|
|
14
|
+ queueName = queueNameParam;
|
|
15
|
+}
|
|
16
|
+
|
|
17
|
+void MessageQueue::init(){
|
|
18
|
+ pthread_mutex_init(&mLock, NULL);
|
|
19
|
+ pthread_cond_init(&mCondition, NULL);
|
|
20
|
+ mNbPackets = 0;
|
|
21
|
+ mFirst = NULL;
|
|
22
|
+ mLast = NULL;
|
|
23
|
+ mAbortRequest = false;
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+MessageQueue::~MessageQueue() {
|
|
27
|
+ flush();
|
|
28
|
+ pthread_mutex_destroy(&mLock);
|
|
29
|
+ pthread_cond_destroy(&mCondition);
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+int MessageQueue::size() {
|
|
33
|
+ pthread_mutex_destroy(&mLock);
|
|
34
|
+ int size = mNbPackets;
|
|
35
|
+ pthread_mutex_unlock(&mLock);
|
|
36
|
+ return size;
|
|
37
|
+}
|
|
38
|
+
|
|
39
|
+void MessageQueue::flush() {
|
|
40
|
+ MessageNode *curNode, *nextNode;
|
|
41
|
+ Message *msg;
|
|
42
|
+ pthread_mutex_lock(&mLock);
|
|
43
|
+ for(curNode = mFirst; curNode != NULL; curNode = nextNode){
|
|
44
|
+ nextNode = curNode->next;
|
|
45
|
+ msg = curNode->msg;
|
|
46
|
+ if(NULL != msg){
|
|
47
|
+ delete msg;
|
|
48
|
+ }
|
|
49
|
+ delete curNode;
|
|
50
|
+ curNode = NULL;
|
|
51
|
+ }
|
|
52
|
+ mLast = NULL;
|
|
53
|
+ mFirst = NULL;
|
|
54
|
+ pthread_mutex_unlock(&mLock);
|
|
55
|
+}
|
|
56
|
+
|
|
57
|
+int MessageQueue::enqueueMessage(Message *msg) {
|
|
58
|
+ if(mAbortRequest){
|
|
59
|
+ delete msg;
|
|
60
|
+ return -1;
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ MessageNode *node = new MessageNode();
|
|
64
|
+ node->msg = msg;
|
|
65
|
+ node->next = NULL;
|
|
66
|
+
|
|
67
|
+ if(mLast == NULL){
|
|
68
|
+ mFirst = node;
|
|
69
|
+ }else{
|
|
70
|
+ mLast->next = node;
|
|
71
|
+ }
|
|
72
|
+ mLast = node;
|
|
73
|
+ mNbPackets++;
|
|
74
|
+ pthread_cond_signal(&mCondition);
|
|
75
|
+ pthread_mutex_unlock(&mLock);
|
|
76
|
+ return 0;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+int MessageQueue::dequeueMessage(Message **msg, bool block) {
|
|
80
|
+ MessageNode *node;
|
|
81
|
+ int ret;
|
|
82
|
+ pthread_mutex_lock(&mLock);
|
|
83
|
+ for (;;) {
|
|
84
|
+ if (mAbortRequest) {
|
|
85
|
+ ret = -1;
|
|
86
|
+ break;
|
|
87
|
+ }
|
|
88
|
+ node = mFirst;
|
|
89
|
+ if (node) {
|
|
90
|
+ mFirst = node->next;
|
|
91
|
+ if (!mFirst)
|
|
92
|
+ mLast = NULL;
|
|
93
|
+ mNbPackets--;
|
|
94
|
+ *msg = node->msg;
|
|
95
|
+ delete node;
|
|
96
|
+ node = NULL;
|
|
97
|
+ ret = 1;
|
|
98
|
+ break;
|
|
99
|
+ } else if (!block) {
|
|
100
|
+ ret = 0;
|
|
101
|
+ break;
|
|
102
|
+ } else {
|
|
103
|
+ pthread_cond_wait(&mCondition, &mLock);
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ pthread_mutex_unlock(&mLock);
|
|
107
|
+ return ret;
|
|
108
|
+}
|
|
109
|
+
|
|
110
|
+void MessageQueue::abort() {
|
|
111
|
+ pthread_mutex_lock(&mLock);
|
|
112
|
+ mAbortRequest = true;
|
|
113
|
+ pthread_cond_signal(&mCondition);
|
|
114
|
+ pthread_mutex_unlock(&mLock);
|
|
115
|
+}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+/******************* Message class *******************/
|
|
119
|
+Message::Message() {
|
|
120
|
+ handler = NULL;
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+Message::Message(int what){
|
|
124
|
+ handler = NULL;
|
|
125
|
+ this->what = what;
|
|
126
|
+}
|
|
127
|
+Message::Message(int what, int arg1, int arg2) {
|
|
128
|
+ handler = NULL;
|
|
129
|
+ this->what = what;
|
|
130
|
+ this->arg1 = arg1;
|
|
131
|
+ this->arg2 = arg2;
|
|
132
|
+}
|
|
133
|
+Message::Message(int what, void* obj) {
|
|
134
|
+ handler = NULL;
|
|
135
|
+ this->what = what;
|
|
136
|
+ this->obj = obj;
|
|
137
|
+}
|
|
138
|
+Message::Message(int what, int arg1, int arg2, void* obj) {
|
|
139
|
+ handler = NULL;
|
|
140
|
+ this->what = what;
|
|
141
|
+ this->arg1 = arg1;
|
|
142
|
+ this->arg2 = arg2;
|
|
143
|
+ this->obj = obj;
|
|
144
|
+}
|
|
145
|
+Message::~Message() {
|
|
146
|
+}
|
|
147
|
+
|
|
148
|
+int Message::execute(){
|
|
149
|
+ if (MESSAGE_QUEUE_LOOP_QUIT_FLAG == what) {
|
|
150
|
+ return MESSAGE_QUEUE_LOOP_QUIT_FLAG;
|
|
151
|
+ } else if (handler) {
|
|
152
|
+ handler->handleMessage(this);
|
|
153
|
+ return 1;
|
|
154
|
+ }
|
|
155
|
+ return 0;
|
|
156
|
+};
|
|
157
|
+
|