/*
|
* =====================================================================================
|
*
|
* 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
|