lichao
2021-03-30 491d98b3ba32cafed5682552bd870ca0ef93275c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * =====================================================================================
 *
 *       Filename:  socket.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月30日 15时48分58秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
 
#include "socket.h"
#include <chrono>
#include "msg.h"
#include "defs.h"
#include "bh_util.h"
 
using namespace bhome_msg;
using namespace bhome_shm;
using namespace std::chrono_literals;
 
namespace {
 
int GetSocketDefaultLen(ShmSocket::Type type) 
{
    switch (type) {
        case ShmSocket::eSockRequest : return 12;
        case ShmSocket::eSockReply : return 64;
        case ShmSocket::eSockPublish : return 0;
        case ShmSocket::eSockSubscribe : return 64;
        default: return 0;
    }
}
 
 
}
 
ShmSocket::ShmSocket(Type type, bhome_shm::SharedMemory &shm)
    : shm_(shm),
      type_(type),
      run_(false)
{
    int len = GetSocketDefaultLen(type);
    if (len != 0) {
        mq_.reset(new Queue(shm_, len));
 
        auto RecvProc = [this](){
            while (run_) {
                try {
                    std::unique_lock<std::mutex> lk(mutex_);
                    if (cv_recv_cb_.wait_for(lk, 100ms, [this](){ return HasRecvCB(); })) {
                        BHMsg msg;
                        if (mq_->Recv(msg, 100)) {
                            this->onRecv_(msg);
                        }
                    }
                } catch (...) {}
            }
        };
        run_.store(true);
        workers_.emplace_back(RecvProc);
    }
}
ShmSocket::ShmSocket(Type type)
    : ShmSocket(type, BHomeShm())
{
}
 
ShmSocket::~ShmSocket()
{
    Stop();
}
 
bool ShmSocket::Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms)
{
    if (type_ != eSockPublish) {
        return false;
    }
    assert(!mq_);
    try {
        MsgI imsg;
        if (!imsg.MakeRC(shm_, MakePub(topic, data, size))) {
            return false;
        }
        DEFER1(imsg.Release(shm_));
        return Queue::Send(shm_, kBHBusQueueId, imsg, timeout_ms);
        
    } catch (...) {
        return false;
    }
}
 
bool ShmSocket::Subscribe(const std::vector<std::string> &topics, const int timeout_ms)
{
    if (type_ != eSockSubscribe) {
        return false;
    }
    assert(mq_);
    try {
        return mq_->Send(kBHBusQueueId, MakeSub(mq_->Id(), topics), timeout_ms);
    } catch (...) {
        return false;
    }
}
 
bool ShmSocket::SetRecvCallback(const RecvCB &onRecv)
{
    std::lock_guard<std::mutex> lock(mutex_);
    onRecv_ = onRecv;
    cv_recv_cb_.notify_one();
    return true;
}
 
bool ShmSocket::HasRecvCB()
{
    return static_cast<bool>(onRecv_);
}
 
void ShmSocket::Stop()
{
    run_ = false;
    for (auto &t : workers_) {
        if (t.joinable()) {
            t.join();
        }
    }
}