ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::schedule_layer Class Reference

独立调度单元,拥有调度线程和分配到此层的所有actor查找表。 More...

#include <actor_manage.h>

Public Member Functions

 schedule_layer ()
 schedule_layer构造函数
void get_type (std::set< i16_actortype > &aactortype)
 获取当前层所有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, bool aready=true)
 将处理完成的actor返回调度层
bool push_task_id (const nguid &aguid, handle_pram &apram, bool aismass=true)
 向指定GUID的actor推送消息
bool 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推送消息
int32_t actor_count ()
 获取当前层actor数量
void get_actor_stat (std::map< ENUM_ACTOR, msg_actor > &astatmap)
 获取当前层actor统计信息

Static Public Member Functions

static nguid get_clientguid ()
 获取客户端路由actor的GUID
static nguid nodetypebyguid ()
 获取路由actor的GUID

Detailed Description

独立调度单元,拥有调度线程和分配到此层的所有actor查找表。

该类是分层调度架构的核心组件,负责:

  • 管理分配到此层的所有Actor
  • 维护调度线程和就绪队列
  • 处理消息路由和Actor调度
  • 支持广播消息分发

设计特点:

  • 工作线程从actor_manage的共享池中借用
  • 所有公共方法都是线程安全的(由m_mutex保护)
  • 构造一次,永不销毁
  • 调度线程在构造时分离,在进程生命周期内运行
  • 未实现优雅关闭(原因见文件级注释)

Constructor & Destructor Documentation

◆ schedule_layer()

ngl::schedule_layer::schedule_layer ( )

schedule_layer构造函数

立即启动调度线程。线程在m_sem信号量上阻塞直到actor被推入, 因此与actor_manage::init()不存在竞争。线程是分离的,在进程 生命周期内运行,没有join或停止逻辑。

Member Function Documentation

◆ actor_count()

int32_t ngl::schedule_layer::actor_count ( )

获取当前层actor数量

Returns
当前层中注册的actor总数
Note
读取锁保护,线程安全

◆ add_actor()

bool ngl::schedule_layer::add_actor ( const ptractor & apactor,
const std::function< void()> & afun )

向调度层添加新actor

将actor注册到ID索引表、类型索引表和广播表中, 并通知路由actor新actor的存在以便远程节点发现。 路由actor(ACTOR_CLIENT/ACTOR_SERVER)立即可用无需同步。

Parameters
[in]apactor要添加的actor智能指针
[in]afun添加完成后的回调函数
Returns
true表示添加成功,false表示GUID重复
Note
写入锁保护,线程安全

◆ broadcast_task()

void ngl::schedule_layer::broadcast_task ( handle_pram & apram)

向当前层所有广播actor推送消息

遍历广播表中的所有可用actor并推送消息。

Parameters
[in]apram消息处理参数
Note
写入锁保护,跳过不可用的actor

◆ erase_actor()

void ngl::schedule_layer::erase_actor ( const nguid & aguid,
const std::function< void()> & afun = nullptr )

从调度层移除actor

从所有查找表中移除actor,通知路由actor进行远程注销。 根据actor当前状态决定立即执行回调还是延迟到工作线程返回后执行。

Parameters
[in]aguid要移除的actor的GUID
[in]afun移除完成后的回调函数(可选)
Note
写入锁保护,处理三种状态:就绪队列中/空闲/运行中

◆ get_actor_stat()

void ngl::schedule_layer::get_actor_stat ( std::map< ENUM_ACTOR, msg_actor > & astatmap)

获取当前层actor统计信息

收集当前层所有actor类型和各区域的actor ID列表。

Parameters
[out]astatmap输出参数,按actor类型存储统计信息
Note
读取锁保护,线程安全

◆ get_clientguid()

nguid ngl::schedule_layer::get_clientguid ( )
static

获取客户端路由actor的GUID

Returns
路由actor的GUID

◆ get_type()

void ngl::schedule_layer::get_type ( std::set< i16_actortype > & aactortype)

获取当前层所有actor类型

收集当前调度层中所有注册的actor类型到集合中。

Parameters
[out]aactortype输出参数,存储所有actor类型
Note
读取锁保护,线程安全

◆ is_have_actor()

bool ngl::schedule_layer::is_have_actor ( const nguid & aguid)

检查调度层是否包含指定actor

Parameters
[in]aguid要检查的actor的GUID
Returns
true表示actor存在于当前层,false表示不存在
Note
读取锁保护,线程安全

◆ nodetypebyguid()

nguid ngl::schedule_layer::nodetypebyguid ( )
static

获取路由actor的GUID

根据当前节点类型返回对应的路由actor GUID。

Returns
路由actor的GUID(actor_server或actor_client)

◆ push()

void ngl::schedule_layer::push ( const ptractor & apactor,
bool aready = true )

将处理完成的actor返回调度层

根据actor状态决定下一步操作:

  • 如果actor已被删除,执行延迟回调
  • 如果仍有消息等待,重新入队就绪队列
  • 否则返回空闲状态
    Parameters
    [in]apactor处理完成的actor智能指针
    [in]aready是否强制入队(默认true,仅当队列非空时入队)
    Note
    处理延迟删除回调,释放actor资源

◆ push_task_id() [1/2]

bool ngl::schedule_layer::push_task_id ( const nguid & aguid,
handle_pram & apram,
bool aismass = true )

向指定GUID的actor推送消息

在本地查找actor并推送消息。如果本地未找到且aismass为true, 将消息转发给路由actor进行远程投递。

Parameters
[in]aguid目标actor的GUID
[in]apram消息处理参数
[in]aismass是否允许转发到路由actor(默认true)
Returns
true表示消息被转发到路由actor,false表示本地处理或失败
Note
写入锁保护查找actor,推送操作在锁内完成

◆ push_task_id() [2/2]

bool ngl::schedule_layer::push_task_id ( const std::set< i64_actorid > & asetguid,
handle_pram & apram )

向多个GUID的actor批量推送消息

遍历GUID集合,逐一调用单GUID版本的push_task_id, 不允许转发到路由actor。

Parameters
[in]asetguid目标actor的GUID集合
[in]apram消息处理参数
Returns
true表示至少有一条消息需要转发到路由actor

◆ push_task_type()

void ngl::schedule_layer::push_task_type ( ENUM_ACTOR atype,
handle_pram & apram )

向指定类型的所有actor推送消息

遍历当前层中指定类型的所有可用actor并推送消息。

Parameters
[in]atype目标actor类型
[in]apram消息处理参数
Note
写入锁保护,跳过不可用的actor

The documentation for this class was generated from the following files: