|
ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
|
单例路由层,拥有共享工作线程池并将每个调用路由到正确的schedule_layer。 More...
#include <actor_manage.h>
Public Member Functions | |
| void | init (i32_threadsize apthreadnum) |
| 初始化actor管理器 | |
| void | get_type (std::vector< i16_actortype > &aactortype) |
| 获取所有调度层的actor类型集合 | |
| template<typename T, bool IS_SEND = true> | |
| void | push_task_id (const nguid &aguid, const std::shared_ptr< T > &apram) |
| bool | add_actor (actor_base *apactor, const std::function< void()> &afun) |
| 添加新actor到管理器(裸指针版本) | |
| bool | add_actor (const ptractor &apactor, const std::function< void()> &afun) |
| 添加新actor到管理器(智能指针版本) | |
| void | erase_actor (const nguid &aguid, const std::function< void()> &afun=nullptr) |
| 从管理器中移除actor | |
| bool | is_have_actor (const nguid &aguid) |
| 检查管理器是否包含指定actor | |
| void | push (const ptractor &apactor, nthread *atorthread=nullptr, bool aready=true) |
| 将处理完成的actor返回调度 | |
| void | push_task_id (const nguid &aguid, handle_pram &apram) |
| 向指定GUID的actor推送消息(单例路由层版本) | |
| void | push_task_id (const std::set< i64_actorid > &asetguid, handle_pram &apram) |
| 向多个GUID的actor批量推送消息(单例路由层版本) | |
| void | push_task_type (ENUM_ACTOR atype, handle_pram &apram) |
| 向指定类型的所有actor推送消息(全局版本) | |
| void | broadcast_task (handle_pram &apram) |
| 向所有广播actor推送消息(全局版本) | |
| void | statrt_suspend_thread () |
| 启动挂起线程 | |
| void | finish_suspend_thread () |
| 完成挂起线程 | |
| int32_t | actor_count () |
| 获取所有层的actor总数 | |
| void | get_actor_stat (msg_actor_stat &adata) |
| 获取所有层的actor统计信息 | |
Static Public Member Functions | |
| static actor_manage & | instance () |
Friends | |
| class | schedule_layer |
单例路由层,拥有共享工作线程池并将每个调用路由到正确的schedule_layer。
该类是Actor调度系统的核心,负责:
设计特点:
使用示例:
| int32_t ngl::actor_manage::actor_count | ( | ) |
获取所有层的actor总数
| bool ngl::actor_manage::add_actor | ( | actor_base * | apactor, |
| const std::function< void()> & | afun ) |
添加新actor到管理器(裸指针版本)
创建空删除器的智能指针包装,然后委托给智能指针版本。
| [in] | apactor | 要添加的actor裸指针 |
| [in] | afun | 添加完成后的回调函数 |
| bool ngl::actor_manage::add_actor | ( | const ptractor & | apactor, |
| const std::function< void()> & | afun ) |
添加新actor到管理器(智能指针版本)
根据actor的GUID计算所属调度层并委托添加。
| [in] | apactor | 要添加的actor智能指针 |
| [in] | afun | 添加完成后的回调函数 |
| void ngl::actor_manage::broadcast_task | ( | handle_pram & | apram | ) |
向所有广播actor推送消息(全局版本)
遍历所有调度层的广播表推送消息。
| [in] | apram | 消息处理参数 |
| void ngl::actor_manage::erase_actor | ( | const nguid & | aguid, |
| const std::function< void()> & | afun = nullptr ) |
从管理器中移除actor
根据GUID计算所属调度层并委托移除。
| [in] | aguid | 要移除的actor的GUID |
| [in] | afun | 移除完成后的回调函数(可选) |
| void ngl::actor_manage::finish_suspend_thread | ( | ) |
完成挂起线程
清除挂起标志,将池恢复到初始状态,并唤醒所有可能在 pop_workthreads()内阻塞的调度器。
| void ngl::actor_manage::get_actor_stat | ( | msg_actor_stat & | adata | ) |
获取所有层的actor统计信息
收集所有调度层的actor统计信息并合并。
| [out] | adata | 输出参数,存储actor统计信息 |
| void ngl::actor_manage::get_type | ( | std::vector< i16_actortype > & | aactortype | ) |
获取所有调度层的actor类型集合
收集所有LAYER_COUNT个调度层中注册的actor类型,去重后返回。
| [out] | aactortype | 输出参数,存储所有唯一的actor类型 |
| void ngl::actor_manage::init | ( | i32_threadsize | apthreadnum | ) |
初始化actor管理器
先创建共享工作线程池(每个工作线程启动一个分离线程并在信号量上阻塞), 然后创建LAYER_COUNT个调度层。要求LAYER_COUNT必须是2的幂。
| [in] | apthreadnum | 工作线程数量 |
| 断言失败如果LAYER_COUNT不是2的幂 |
| bool ngl::actor_manage::is_have_actor | ( | const nguid & | aguid | ) |
检查管理器是否包含指定actor
| [in] | aguid | 要检查的actor的GUID |
| void ngl::actor_manage::push | ( | const ptractor & | apactor, |
| nthread * | atorthread = nullptr, | ||
| bool | aready = true ) |
将处理完成的actor返回调度
如果提供了工作线程,先将其归还到池中,然后将actor返回对应调度层。
| [in] | apactor | 处理完成的actor智能指针 |
| [in] | atorthread | 使用的工作线程(可选,归还到池中) |
| [in] | aready | 是否强制入队(默认true) |
| void ngl::actor_manage::push_task_id | ( | const nguid & | aguid, |
| handle_pram & | apram ) |
向指定GUID的actor推送消息(单例路由层版本)
防止转发到尚未注册的路由actor造成无限循环。 如果目标是路由actor但尚未注册,则直接返回。
| [in] | aguid | 目标actor的GUID |
| [in] | apram | 消息处理参数 |
| void ngl::actor_manage::push_task_id | ( | const std::set< i64_actorid > & | asetguid, |
| handle_pram & | apram ) |
向多个GUID的actor批量推送消息(单例路由层版本)
按层分区目标,本地投递,然后将任何未命中的消息以单批方式转发给路由actor。
| [in] | asetguid | 目标actor的GUID集合 |
| [in] | apram | 消息处理参数 |
| void ngl::actor_manage::push_task_type | ( | ENUM_ACTOR | atype, |
| handle_pram & | apram ) |
向指定类型的所有actor推送消息(全局版本)
遍历所有调度层推送消息,并转发一次给路由actor以便远程节点接收类型广播。
| [in] | atype | 目标actor类型 |
| [in] | apram | 消息处理参数 |
| void ngl::actor_manage::statrt_suspend_thread | ( | ) |
启动挂起线程
自旋等待直到每个工作线程都归还到池中,然后冻结调度。 pop_workthreads()检查m_suspend标志并在设置后停止分发工作线程。