Kaynağa Gözat

归拢公共类

chenzubin 2 ay önce
ebeveyn
işleme
0c9844c748

+ 17 - 0
app/src/main/cpp/common/coomon_utils.h

@@ -0,0 +1,17 @@
1
+//
2
+// Created by 叶亮 on 2019/7/16.
3
+//
4
+
5
+#ifndef ANDROIDRECORDER_COOMON_UTILS_H
6
+#define ANDROIDRECORDER_COOMON_UTILS_H
7
+
8
+#include <sys/time.h>
9
+
10
+static inline long getCurrentTime()
11
+{
12
+    struct timeval tv;
13
+    gettimeofday(&tv, NULL);
14
+    return tv.tv_sec * 1000 + tv.tv_usec/1000;
15
+}
16
+
17
+#endif //ANDROIDRECORDER_COOMON_UTILS_H

+ 21 - 0
app/src/main/cpp/common/message_handler.cpp

@@ -0,0 +1,21 @@
1
+//
2
+// Created by 叶亮 on 2019/7/15.
3
+//
4
+#include "message_handler.h"
5
+
6
+Handler::Handler(MessageQueue *queue) {
7
+    this->mQueue = queue;
8
+}
9
+
10
+Handler::~Handler() {
11
+
12
+}
13
+
14
+int Handler::postMessage(Message *msg) {
15
+    msg->handler = this;
16
+    return mQueue->enqueueMessage(msg);
17
+}
18
+
19
+int Handler::getQueueSize() {
20
+    return mQueue->size();
21
+}

+ 26 - 0
app/src/main/cpp/common/message_handler.h

@@ -0,0 +1,26 @@
1
+//
2
+// Created by 叶亮 on 2019/7/15.
3
+//
4
+
5
+#ifndef ANDROIDRECORDER_HANDLER_H
6
+#define ANDROIDRECORDER_HANDLER_H
7
+
8
+#include "message_queue.h"
9
+class Message;
10
+class MessageQueue;
11
+
12
+class Handler{
13
+private:
14
+    MessageQueue* mQueue;
15
+
16
+public:
17
+    Handler(MessageQueue* mQueue);
18
+    ~Handler();
19
+
20
+    int postMessage(Message* msg);
21
+    int getQueueSize();
22
+    virtual void handleMessage(Message* msg){};
23
+
24
+};
25
+
26
+#endif //ANDROIDRECORDER_HANDLER_H

+ 157 - 0
app/src/main/cpp/common/message_queue.cpp

@@ -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
+

+ 84 - 0
app/src/main/cpp/common/message_queue.h

@@ -0,0 +1,84 @@
1
+//
2
+// Created by 叶亮 on 2019/7/15.
3
+//
4
+
5
+#ifndef ANDROIDRECORDER_MESSAGE_QUEUE_H
6
+#define ANDROIDRECORDER_MESSAGE_QUEUE_H
7
+
8
+#include <pthread.h>
9
+#include <sys/types.h>
10
+#include "message_handler.h"
11
+#include "../log_utils.h"
12
+
13
+#define MESSAGE_QUEUE_LOOP_QUIT_FLAG        19900909
14
+
15
+class Handler;
16
+
17
+class Message{
18
+private:
19
+    int what;
20
+    int arg1;
21
+    int arg2;
22
+    void *obj;
23
+
24
+public:
25
+    Message();
26
+    Message(int what);
27
+    Message(int what, int arg1, int arg2);
28
+    Message(int what, void *obj);
29
+    Message(int what, int arg1, int arg2, void *obj);
30
+    ~Message();
31
+
32
+    int execute();
33
+    int getWhat(){
34
+        return what;
35
+    }
36
+
37
+    int getArg1(){
38
+        return arg1;
39
+    }
40
+
41
+    int getArg2(){
42
+        return arg2;
43
+    }
44
+
45
+    void* getObj(){
46
+        return obj;
47
+    }
48
+
49
+    Handler *handler;
50
+};
51
+
52
+typedef struct MessageNode{
53
+    Message *msg;
54
+    struct MessageNode *next;
55
+    MessageNode(){
56
+        msg = NULL;
57
+        next = NULL;
58
+    }
59
+} MessageNode;
60
+
61
+class MessageQueue{
62
+private :
63
+    MessageNode* mFirst;
64
+    MessageNode* mLast;
65
+    int mNbPackets;
66
+    bool mAbortRequest;
67
+    pthread_mutex_t mLock;
68
+    pthread_cond_t mCondition;
69
+    const char * queueName;
70
+
71
+public:
72
+    MessageQueue();
73
+    MessageQueue(const char* queueNameParam);
74
+    ~MessageQueue();
75
+
76
+    void init();
77
+    void flush();
78
+    int enqueueMessage(Message* msg);
79
+    int dequeueMessage(Message **msg, bool block);
80
+    int size();
81
+    void abort();
82
+};
83
+
84
+#endif //ANDROIDRECORDER_MESSAGE_QUEUE_H