跳至内容

Endpoint

EndpointContext

EndpointContext(
    caller: int,
    connection_id: int,
    address: str,
    group: str,
    user_data: dict[str, typing.Any],
    timestamp: float,
    request: Any,
    systems: SystemCaller,
    guard_state: dict[str, typing.Any] = <factory>,
    client_limits: list[list[int]] = <factory>,
    server_limits: list[list[int]] = <factory>,
    max_row_sub: int = 0,
    max_index_sub: int = 0,
    max_table_sub: int = 0,
) -> None

Source: hetu/endpoint/context.py:21

Endpoint调用时的上下文,由engine创建并作为 ctx 参数传入Endpoint函数; SystemContext 继承自此类。包含调用方身份、当前连接的用户数据, 以及消息发送和订阅数量限制等。

Attributes

  • caller (int) — 调用方的user id;未登录为0;执行过 elevate() 后为传入的 user_id 。

  • connection_id (int) — 调用方的connection id。

  • address (str) — 调用方的IP地址。

  • group (str) — 所属组名,用于判断是否admin(is_admin())、GM(is_gm())。

  • user_data (dict[str, Any]) — 当前连接的用户数据,可自由设置,在所有System间共享。

  • timestamp (float) — 调用时间戳。

  • request (Any) — framework原始请求对象(aiohttp 的 web.Request),unsafe,普通业务代码请避免直接使用。

  • systems (SystemCaller) — 全局System管理器,unsafe,可通过 ctx.systems.call(...) 调用其他System。

  • guard_state (dict[str, Any]) — 每连接的 guard 私有状态(如 rate_limit 的窗口计数);纯内存、不跨连接。

  • client_limits (list[list[int]]) — 客户端消息发送限制(次数)。

  • server_limits (list[list[int]]) — 服务端消息发送限制(次数)。

  • max_row_sub (int) — 行订阅数量限制。

  • max_index_sub (int) — 索引订阅数量限制。

  • max_table_sub (int) — 整表订阅数量限制。

Methods

is_admin

is_admin()

Source: hetu/endpoint/context.py:74

是否管理员连接(group 以 “admin” 开头):可调用 ADMIN、GM 权限的 System/Endpoint, 可订阅 ADMIN 组件,订阅时不受 RLS 限制。这是后台管理工具用的 root 级权限,不要给游戏 客户端的连接,游戏里的管理权限用 GM(is_gm())。 Whether this is an admin connection (group starts with “admin”): root-level, for back-office tools only, never for game-client connections (those use GM).

is_gm

is_gm()

Source: hetu/endpoint/context.py:84

是否 GM 连接(group 以 “gm” 开头):登录后可调用 Permission.GM 的 System/Endpoint, 读数据和普通玩家一样。 Whether this is a GM connection (group starts with “gm”).

configure

configure(client_limits, server_limits, max_row_sub, max_index_sub, max_table_sub=0)

Source: hetu/endpoint/context.py:92

配置当前连接的限流与订阅配额。

此方法通常在连接建立后调用,用于把 websocket/app 配置中的连接级限制 写入 Context,供后续消息收发和订阅逻辑直接读取。

Parameters

  • client_limits (Any) — 客户端向服务端发送消息的频率限制。每项格式为 [最大消息数, 统计时间(秒)];空列表表示不限制。 一般对应配置项 CLIENT_SEND_LIMITS。

  • server_limits (Any) — 服务端向客户端发送消息的频率限制。每项格式同上; 一般对应配置项 SERVER_SEND_LIMITS。

  • max_row_sub (Any) — 当前连接允许的最大行订阅数量。一般对应配置项 MAX_ROW_SUBSCRIPTION。

  • max_index_sub (Any) — 当前连接允许的最大索引订阅数量。一般对应配置项 MAX_INDEX_SUBSCRIPTION。

  • max_table_sub (Any) — 当前连接允许的最大整表订阅数量。一般对应配置项 MAX_TABLE_SUBSCRIPTION。

Notes

本方法只负责保存传入值,不做校验、排序或拷贝。 client_limits 和 server_limits 应按统计时间从小到大排列, 因为后续限流检查会使用最后一项的时间窗口作为计数重置基准。

rls_check

rls_check(
    component: type[BaseComponent],
    row: numpy.record | numpy.ndarray | numpy.rec.recarray | dict,
) -> bool

Source: hetu/endpoint/context.py:132

检查当前用户对某个component的权限


ResponseToClient

ResponseToClient(message: list | dict)

Source: hetu/endpoint/response.py:13

Bases: EndpointResponse

回报message给客户端,注意必须是json可以序列化的数据

Attributes

  • message (Any) — No description.

elevate

elevate(ctx: hetu.endpoint.context.Context, user_id: int, kick_logged_in=True)

Source: hetu/endpoint/connection.py:147

提升到User权限。如果该连接已提权,或user_id已在其他连接登录,返回False。 如果成功,则ctx.caller会被设置为user_id,同时事务结束,之后将无法调用ctx[Components]。

kick_logged_in: 如果user_id已在其他连接登录,则标记该连接断开并返回True。服务器随后主动断开该连接, 断开前先发 WebSocket close 码 CLOSE_KICKED (4001)、原因 “kicked”,客户端可据此提示。 被顶掉的连接的 on_disconnect 以匿名身份(ctx.caller == 0)运行,不会动已在新连接 上的这个用户。

Notes

本方法是一个独立的事务,如果你在System中(另一个事务中)调用此方法,父事务回退时不会回退此方法的结果。