您的位置:首页 > 编程语言 > C语言/C++

C++消息分发模块框架的的突发奇想,不足之处欢迎指正

2016-04-14 09:13 537 查看
//以下仅仅是一个初步构想,还有不足之处
#pragma once
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <list>
#include <boost/thread.hpp>
/*
*              ui界面层
*                  |
*                  |
*                  |
*             service业务层(dispatcher分发模块)-----db数据库层
*                  |
*                  |
*                  |
*              core通信层
*
*/

struct msg_param_t
{
std::string  id;
std::string  sender;
std::string  receiver;
std::string  body;
void*        rsv;
};

typedef std::string msg_name_t;
typedef std::string msg_body_t;

//! 模板类,T为回调对象类型,每种msg 类型T中都需要定义相应的handle函数
//           R代表请求的类型指针,这里使用泛型表示(暴露给上层)
template<typename T, typename R>
class msg_dispatcher_t
{
typedef R                                  rcv_ptr_t;                              //! 消息的接收者
typedef std::string                        json_value_t;                           //! 传输的内容
typedef int (msg_dispatcher_t<T, R>::*reg_func_t)(const json_value_t&, rcv_ptr_t); //! 消息对应的解析函数
typedef std::map<std::string, reg_func_t>  reg_func_queue_t;                       //! 消息队列
public:
//! 所有的消息都在构造时注册解析函数
msg_dispatcher_t(T& _msg_handler) : m_msg_handler_(_msg_handler){
m_reg_func_[m_msg_handler_.msg_name()] = &_msg_handler.handle;
}

//! 接口函数,使用者只需单点接入dispatch,消息会自动派发到msg_handler特定的handle函数
//json body 需要先解析
int dispath(const json_value_t& _json, rcv_ptr_t _rcv){
reg_func_queue_t::const_iterator it = m_reg_func_.find(_json);
if (it != m_reg_func_.end())
{
reg_func_t func = it->second;
//(this->*func)(/*json*/, _rcv);
//解析后,处理消息
}
}
private:
T&                      m_msg_handler_;
reg_func_queue_t        m_reg_func_;
};

//具体的消息处理层(暴露给上层)
class msg_handler_t
{
public:
typedef int rcv_ptr_t;

public:
void handle(const msg_param_t& _param,  rcv_ptr_t _rcv){
std::cout << "msg_handle" << std::endl;
}
std::string msg_name() { return "msg_name_test"; }
};

//此类为便于接收者的使用,应该设置为单实例(暴露给上下层)
class msg_dispatcher_factory_t{
public:
//提供消息和处理函数的注册
bool register_msg_handle();

//提供消息处理的查找功能
template<typename T, typename R>
msg_dispatcher_t<T, R> find_msg_handle(const msg_name_t& _msg_name);

protected:
msg_dispatcher_factory_t();
~msg_dispatcher_factory_t();

private:
//消息工厂队列 包括消息处理的对象指针,处理函数
};

//此函数只暴露给底层,而不暴露给上层,由底层来进行消息的转发
class msg_dispatcher_impl_t{
public:
explicit msg_dispatcher_impl_t(msg_dispatcher_factory_t* _factory){
run_loop();
}

//放入消息队列,这里尾部放入消息,头部处理消息
void push_msg(msg_param_t _msg){
//加锁,然后放入队列
}

//消息处理,也就是分发消息
bool run_loop(){
//Sleep(200);
//先从消息队列头部取出消息(需加锁),然后解析消息
msg_param_t  param = m_msg_queue_.front();
//从消息工厂获取具体消息对应的处理函数, id不应该作为消息的查找,应该有消息分类
//msg_dispatcher_t<T, R> dispatcher = m_dispatcher_factory_->find_msg_handle(param.id);
//将相应消息投递到具体的模块去处理,这里的参数要单独处理
//dispatcher.dispath(param.body, param.receiver);
}
private:

private:
msg_dispatcher_factory_t*  m_dispatcher_factory_;  //可以做成单实例, 使用公共接口来处理
boost::thread*             m_thread_;
boost::mutex               m_mtx_;
std::list<msg_param_t>     m_msg_queue_;
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: