event-dispatcher.hh
typedef boost::function<void(const boost::system::error_code& error)> Callback
回调函数的类型定义
static Component* instantiate(const Component_context*, size_t n_threads);
生成一个Event-dispatcher对象
void post(const Event&);
立刻发送一个事件
std::unique_ptr<boost::asio::deadline_timer>
post(const Callback& callback, const timeval& duration);
定义一个带有时间控制的消息/* Posts 'callback' to be called after the given 'duration' elapses. The
* caller must not destroy the returned Timer, but may use it to cancel or
* reschedule the timer, up until the point where the timer is actually
* invoked. */
void dispatch(const Event&) const
立刻发送一个消息出去
boost::asio::io_service& get_io_service()
得到事件发送的io_service
template <typename T>
inline
void register_event()
注册事件,使用方法为:
register_event<Event类>();
bool register_handler(const Component_name&,
const Event_name&,
const Event_handler&);
为事件注册句柄,带组件名
void register_handler(const Event_name&,
const Event_handler&, int order);
为事件注册句柄,同一个事件有多个句柄的情况下使用这个语句,可以为句柄定义优先级,句柄按顺序先后依次执行
event-dispatcher.cc
1.bool
Event_dispatcher::register_handler(
const Component_name& component_name,
const Event_name& event_name,
const Event_handler& h);
首先查看的是注册的事件有没有存在,不存在返回false;接下来根据组件在对应事件中的优先级,把句柄按优先级插入到call_chain_map;
2.Event_dispatcher::dispatch(const Event& event) const
的实现是查找call_chain_map,找到事件对应的句柄,依次执行,直到执行完所有句柄或遇到句柄返回STOP
3.register_event(const Event_name& name)是为新的事件插入到priority_map中;
4.priority_map保存着响应事件时,应组件的执行优先级;其数据结构是两级字典:【事件名【组件名,优先级】】;通过读取注册文件(etc/nox.meta.json),根据注册文件中组件的顺序,给组件设置优先级;