Leaf-Server官方教程: Leaf ChanRPC

Posted Kaitiren

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leaf-Server官方教程: Leaf ChanRPC 相关的知识,希望对你有一定的参考价值。

Leaf ChanRPC

由于 Leaf 中,每个模块跑在独立的 goroutine 上,为了模块间方便的相互调用就有了基于 channel 的 RPC 机制。一个 ChanRPC 需要在游戏服务器初始化的时候进行注册(注册过程不是 goroutine 安全的),例如 LeafServer 中 game 模块注册了 NewAgent 和 CloseAgent 两个 ChanRPC:

package internal

import (
"github.com/name5566/leaf/gate"
)

func init() {
skeleton.RegisterChanRPC("NewAgent", rpcNewAgent)
skeleton.RegisterChanRPC("CloseAgent", rpcCloseAgent)
}

func rpcNewAgent(args []interface{}) {

}

func rpcCloseAgent(args []interface{}) {

}

使用 skeleton 来注册 ChanRPC。RegisterChanRPC 的第一个参数是 ChanRPC 的名字,第二个参数是 ChanRPC 的实现。这里的 NewAgent 和 CloseAgent 会被 LeafServer 的 gate 模块在连接建立和连接中断时调用。ChanRPC 的调用方有 3 种调用模式:

  • 同步模式,调用并等待 ChanRPC 返回
  • 异步模式,调用并提供回调函数,回调函数会在 ChanRPC 返回后被调用
  • Go 模式,调用并立即返回,忽略任何返回值和错误gate 模块这样调用 game 模块的 NewAgent ChanRPC(这仅仅是一个示例ÿ

以上是关于Leaf-Server官方教程: Leaf ChanRPC 的主要内容,如果未能解决你的问题,请参考以下文章

Leaf-Server官方教程: Leaf ChanRPC

Leaf-Server官方教程: Leaf ChanRPC

Leaf-Server官方教程:Leaf安装搭建与模块运行

Leaf-Server官方教程:Leaf安装搭建与模块运行

Leaf-Server官方教程: Leaf Go

Leaf-Server官方教程: Leaf Go / Leaf timer / Leaf recordfile