mmwabstract.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "mmwabstract.h"
  2. #include "muduo/net/EventLoopThread.h"
  3. using namespace muduo::net;
  4. namespace mmw {
  5. /**
  6. * @brief 根据topic发布一条消息
  7. * @param topic[in] 要发布消息的topic
  8. * @param content[in] 消息的具体内容
  9. * @return true 成功
  10. * @return false 失败
  11. */
  12. bool PublisherAbstract::publish(const std::string& topic, const std::string& content)
  13. {
  14. return publish(topic.data(), topic.size(), content.data(), content.size());
  15. }
  16. /**
  17. * @brief SubscriberAbstract类的析构函数
  18. */
  19. SubscriberAbstract::~SubscriberAbstract(void)
  20. {
  21. workFlags_ = false;
  22. }
  23. /**
  24. * @brief 此函数内部开启一个线程来运行readyMessage函数,来接收订阅到的数据所以,
  25. * 此类的派生类需要重写readyMessage函数
  26. * @param func[in] 读取回调函数
  27. * @return true 启动成功
  28. * @return false 启动失败
  29. */
  30. bool SubscriberAbstract::readStart(ReadCallBack func)
  31. {
  32. std::call_once(onceFlag_, [=] {
  33. eventLoopthreadPtr_ = std::make_shared<muduo::net::EventLoopThread>();
  34. workFlags_ = true;
  35. eventLoopthreadPtr_->startLoop()->runInLoop([=] {
  36. auto func_ = std::move(func);
  37. while (workFlags_) {
  38. if (func_) {
  39. readyMessage(func_);
  40. }
  41. }
  42. });
  43. });
  44. return true;
  45. }
  46. }