hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
 
#ifndef SHM_QUEUE_TRANSCEIER_H_
#define SHM_QUEUE_TRANSCEIER_H_
 
#include <shmc/shm_queue.h>
#include "shm_control_feature.h"
#include "logger/log.h"
 
#define MAX_IMAGE_SIZE (1920*1088*2)
const  char *kShmFeatureKey = "0x100ff";
static int   mShmReadOK   = 0;
static int   mShmWriteOK  = 0;
static shmc::ShmQueue<shmc::SVIPC>  mShmQueue;
static shmc::ShmQueue<shmc::SVIPC> *mShmReadQueue = nullptr;
 
pthread_mutex_t gMutex = PTHREAD_MUTEX_INITIALIZER;
 
void shm_queue_send_buffer(std::string buffer) {
    shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel lv, const char *s) {
        LOG_INFO("[%d] %s\n", lv, s);
    });
    if (mShmWriteOK == 0) {
        mShmWriteOK = mShmQueue.InitForWrite(kShmFeatureKey, MAX_IMAGE_SIZE);
    }
    if (mShmWriteOK > 0) {
        pthread_mutex_lock(&gMutex);
        mShmQueue.Push(buffer);
        pthread_mutex_unlock(&gMutex);
    }
}
 
void shm_queue_recv_buffer(std::string *buffer) {
    bool hasRetry = false;
    shmc::SetLogHandler(shmc::kDebug, [](shmc::LogLevel lv, const char *s) {
        LOG_INFO("[%d] %s\n", lv, s);
    });
RETRY:
    if (mShmReadQueue == nullptr) {
        mShmReadQueue = new shmc::ShmQueue<shmc::SVIPC>();
    }
    if (mShmReadOK == 0) {
        mShmReadOK = mShmReadQueue->InitForRead(kShmFeatureKey);
    }
    if (mShmReadOK > 0) {
        pthread_mutex_lock(&gMutex);
        mShmReadQueue->Pop(buffer);
        pthread_mutex_unlock(&gMutex);
        if (buffer->length() == 0 && hasRetry == false) {
            hasRetry = true;
            delete mShmReadQueue;
            mShmReadQueue = nullptr;
            mShmReadOK = 0;
            LOG_ERROR("shm_queue_recv_buffer RETRY %s", kShmFeatureKey);
            goto RETRY;
        }
    } else {
        LOG_ERROR("shm_queue_recv_buffer read error");
    }
}
 
#endif