#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <queue>
#include <vector>
#include <atomic>
#include <string>
using namespace std;
//前置声明
#pragma once
class PointInfo;
/* 常量定义 */
#define MESSAGE_QUEUE_SIZE 10000     // 消息队列容量

// 修改后的 deal_message_t 结构
struct deal_message_t {
    std::string device_id;      // 使用 std::string 代替 char 数组
    std::string mac;            // 使用 std::string 代替 char 数组
    char* data;                 // 接收到的数据
    size_t length;              // 数据长度
    std::vector<PointInfo> points; // 关联的测点信息
};

/* 线程安全的消息队列 */
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();
    }
};

void process_received_message(string mac, string id, const char* data, size_t length);