Skip to content

Call

基本双端互Call功能

python
class AServer(ServerBase):
    
    def Test(self):
        
        self.CallClient(PID, 'FuncName')  # 不带参数
        
        self.CallClient('FuncName', 1, b=2)  # 带参数
        
        self.CallClient('FuncName', 1, b=2, sys_target='BClient')  # 调用BClient.FuncName(1, b=2)
py
class AClient(ClientBase):

    def FuncName(self, a, b):
        print("我被调用了")
        
    def Test(self):
        
        self.CallServer('FuncName', 1, b=2) # 同上,只是C2S时无需带PID,因为服务端只有一个
        
        self.CallOTClient('FuncName', 1, b=2)
        # ↑调用其他客户端函数,会call到同游戏中其他所有连接到服务端的玩家的对应系统实例中,使用方式和CallServer一致,服务端仅做中转

Call本质是远程调用函数,对方端系统收到包后会自动解包并调用指向的函数,默认当A系统调用Call时,会发送至同名的A对应端系统,例如AServer->AClient,BClient->BServer

如果你要用AClient -> BServer,也可以,写 sys_target='BServer'

RPC

远程过程调用(英语:Remote Procedure Call,RPC)定义了更方便的链式调用,本质是Call的进一步封装。

python
class AClient(ClientBase):
    
    def Test(self):

        # 获取一个远程代理服务端实例,但什么也不做
        self.Server()
        
        # 定义要调用FuncName的行为,然后发包并调用
        self.Server().FuncName(1, b=2).send()

进阶用法

python
class AServer(ServerBase):
        
    def Test(self):

        # 获取-123456的玩家客户端(远程代理),但什么也不做
        self.Client(-123456)
        
        # 可以换成所有客户端
        self.AllClient()

        # 发包并调用
        self.AllClient().FuncName(1, b=2).send()

        # 可以链式调用, 如果Func藏得比较深
        self.AllClient().paramA.paramB.Func(1, b=2).send()

        # 是的, 你可以传入一个 callback
        def cb(x):
            print x # AClient.Func(123) 的返回值
        self.AllClient().Func(123).send(cb)

        # 可以先调a.List(), 然后再用a.List()的返回值调pop(), 最后打印返回pop()的值
        self.AllClient().a.List().pop().send(lambda x: Logger.log(x))
		
        # 现在你正在调用AClient的远程实例,你甚至可以跨系统
        self.AllClient().GetSystem('MOD', 'BClient').do_something().send()

远程值访问

python
class AServer(ServerBase):
    
    def Test(self):
        
        self.AllClient().paramA.get(cb) # 获取指定远程值, 需传入一个回调, 回调会带一个参数, 就是 paramA 的值
        
        # 设置
        self.AllClient().paramA.set(1) # 设置变量为 1
        
        self.AllClient().paramA.set(1, lambda: logging.info("set ok")) # 设置变量为 1 , 可选回调, 设置成功后可以做一些事

INFO

本文档内提到的 系统,指继承了ServerBaseClientBase的增强系统,提供了花子框架的独有功能。