lichao
2021-04-02 c28cdf2fbf1565709b359c9cca6c5e29d9592dce
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
/*
 * =====================================================================================
 *
 *       Filename:  pubsub.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月24日 18时44分36秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef PUBSUB_4KGRA997
#define PUBSUB_4KGRA997
 
#include "defs.h"
#include "socket.h"
#include <string>
 
class SocketPublish
{
    typedef ShmSocket Socket;
    Socket::Shm &shm_;
    Socket::Shm &shm() { return shm_; }
 
public:
    SocketPublish(Socket::Shm &shm) :
        shm_(shm) {}
    SocketPublish() :
        SocketPublish(BHomeShm()) {}
    bool Publish(const Topic &topic, const void *data, const size_t size, const int timeout_ms);
    bool Publish(const Topic &topic, const std::string &data, const int timeout_ms)
    {
        return Publish(topic, data.data(), data.size(), timeout_ms);
    }
};
 
// socket subscribe
class SocketSubscribe : private ShmSocket
{
    typedef ShmSocket Socket;
 
public:
    SocketSubscribe(Socket::Shm &shm) :
        Socket(shm, 64) {}
    SocketSubscribe() :
        SocketSubscribe(BHomeShm()) {}
    ~SocketSubscribe() { Stop(); }
 
    typedef std::function<void(const Topic &topic, const std::string &data)> TopicDataCB;
    bool StartRecv(const TopicDataCB &tdcb, int nworker = 2);
    bool Stop() { return Socket::Stop(); }
    bool Subscribe(const std::vector<Topic> &topics, const int timeout_ms);
    bool RecvSub(Topic &topic, std::string &data, const int timeout_ms);
};
 
#endif // end of include guard: PUBSUB_4KGRA997