2025-06-20 09:25:17 +08:00
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
#include <atomic>
|
2025-06-24 10:33:31 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
|
using namespace std;
|
2025-06-20 09:25:17 +08:00
|
|
|
|
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
|
|
|
|
|
#define MESSAGE_QUEUE_SIZE 10000 // <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
2025-06-24 10:33:31 +08:00
|
|
|
|
// <20>ĺ<DEB8><C4BA><EFBFBD> deal_message_t <20>ṹ
|
|
|
|
|
|
struct deal_message_t {
|
|
|
|
|
|
std::string device_id; // ʹ<><CAB9> std::string <20><><EFBFBD><EFBFBD> char <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
std::string mac; // ʹ<><CAB9> std::string <20><><EFBFBD><EFBFBD> char <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
char* data; // <20><><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
size_t length; // <20><><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
|
|
|
|
|
std::vector<PointInfo> points; // <20><><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
|
};
|
2025-06-20 09:25:17 +08:00
|
|
|
|
|
|
|
|
|
|
/* <20>̰߳<DFB3>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> */
|
|
|
|
|
|
class SafeMessageQueue {
|
|
|
|
|
|
private:
|
|
|
|
|
|
std::queue<deal_message_t> queue;
|
|
|
|
|
|
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
|
|
std::atomic<int> count{ 0 };
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
bool push(const deal_message_t& msg) {
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
if (queue.size() >= MESSAGE_QUEUE_SIZE) {
|
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
queue.push(msg);
|
|
|
|
|
|
count++;
|
|
|
|
|
|
pthread_cond_signal(&cond);
|
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool pop(deal_message_t& msg) {
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
|
|
while (queue.empty()) {
|
|
|
|
|
|
pthread_cond_wait(&cond, &mutex);
|
|
|
|
|
|
}
|
|
|
|
|
|
msg = queue.front();
|
|
|
|
|
|
queue.pop();
|
|
|
|
|
|
count--;
|
|
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
|
|
return count.load();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-25 10:54:09 +08:00
|
|
|
|
void process_received_message(string mac, string id, const char* data, size_t length);
|