1 Star 0 Fork 22

zhaoyao / message_bus

forked from 橘色的喵 / message_bus 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

message_bus

  • 该模块一般用在客户端;
  • 基于消息的程序架构(如C/S架构),在发送一个request消息后可能会遇到这样的情况:
    • 等到response消息,其中response分为肯定回答和否定回答;
    • 在超时时间内没有收到回复。

业务往往需要等到响应结果以后再继续其他业务,设计方法是:采用回调的方法调用响应函数,维护一个定时器,不停检查是否有超时消息产生,并调用对应回调函数。

  • 需要使用到定时器,定时器默认间隔100ms。

  • 维护了一个超时消息的列表。

主要结构体

typedef std::function<void(std::string param1, int param2)> Callback_t;
typedef std::function<void()> TimeOutCallback_t;
struct CallbackItem_t
{
    Callback_t callback = nullptr;  
    TimeOutCallback_t timeOutCallback = nullptr;
    uint32_t timeoutInterval = 1000;    // 超时时间,milliseconds
    uint64_t timeoutStamp = 0;  // 内部使用时间戳,用户无需填写,microseconds
    std::vector<int> msgNumVec; // 需要关注的消息号
    CallbackType_t callbackType = ALWAYS;
};
typedef std::shared_ptr<CallbackItem_t> CallbackItem_ptr;
typedef std::map<int, std::vector<CallbackItem_ptr>> CallbackMap_t;
CallbackMap_t _callbackMap;   // message dispatch map
std::list<CallbackItem_ptr> _timeoutCheckList;	// timeout check list
MIT License Copyright (c) 2020 stallion5632 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个消息总线模块:当收到等待的消息时,调用回调函数;当在规定时间内没收到时,调用超时响应函数。 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/zhaoyao219/message_bus.git
git@gitee.com:zhaoyao219/message_bus.git
zhaoyao219
message_bus
message_bus
master

搜索帮助