/*
|
* =====================================================================================
|
*
|
* Filename: socket.h
|
*
|
* Description:
|
*
|
* Version: 1.0
|
* Created: 2021年03月30日 15时49分19秒
|
* Revision: none
|
* Compiler: gcc
|
*
|
* Author: Li Chao (),
|
* Organization:
|
*
|
* =====================================================================================
|
*/
|
|
#ifndef SOCKET_GWTJHBPO
|
#define SOCKET_GWTJHBPO
|
|
#include "shm_queue.h"
|
#include <atomic>
|
#include <condition_variable>
|
#include <functional>
|
#include <memory>
|
#include <mutex>
|
#include <thread>
|
#include <vector>
|
|
class ShmSocket
|
{
|
typedef bhome_shm::ShmMsgQueue Queue;
|
|
public:
|
enum Type {
|
eSockRequest,
|
eSockReply,
|
eSockSubscribe,
|
eSockPublish,
|
};
|
typedef std::function<void(bhome_msg::BHMsg &msg)> RecvCB;
|
|
ShmSocket(Type type);
|
ShmSocket(Type type, bhome_shm::SharedMemory &shm);
|
~ShmSocket();
|
|
// bool Request(const std::string &topic, const void *data, const size_t size, onReply);
|
bool RequestAndWait() { return false; } // call Request, and wait onReply notify cv
|
|
// bool HandleRequest(onData);
|
bool ReadRequest(); // exclude with HandleRequest
|
bool SendReply(); // exclude with HandleRequest
|
|
bool Publish(const std::string &topic, const void *data, const size_t size, const int timeout_ms);
|
bool Subscribe(const std::vector<std::string> &topics, const int timeout_ms);
|
bool RecvSub(std::string &topic, std::string &data, const int timeout_ms);
|
bool SetRecvCallback(const RecvCB &onRecv);
|
|
private:
|
bool HasRecvCB();
|
void Stop();
|
|
bhome_shm::SharedMemory &shm_;
|
Type type_;
|
std::vector<std::thread> workers_;
|
std::mutex mutex_;
|
std::condition_variable cv_recv_cb_;
|
std::atomic<bool> run_;
|
RecvCB onRecv_;
|
|
std::unique_ptr<Queue> mq_;
|
};
|
|
#endif // end of include guard: SOCKET_GWTJHBPO
|