0
点赞
收藏
分享

微信扫一扫

C++用LuaIntf调用Lua代码示例

夕阳孤草 2023-06-19 阅读 54


C++用LuaIntf调用Lua代码示例


void LuaTest::OnResponse(uint32_t uLuaRpcId,
    const std::string& sRespContent) const
{
    using LuaIntf::LuaRef;
    LuaRef require(m_pLuaState, "require");
    try {
        LuaRef handler = require.call<LuaRef>("client_rpc_response_handler");
        handler.dispatchStatic("handle", uLuaRpcId, sRespContent);
    }
    catch (const LuaIntf::LuaException& e) {
        std::cerr << "Failed to call lua client_rpc_response_handler.handle(), "
            << e.what() << std::endl;
    }
}

这是测试客户端代码,可以写Lua代码测试服务器.

Lua代码中发出一个Rpc请求时, 会在Lua中保存一个回调, 待收到应答时触发回调. 通过uLuaRpcId来索引该回调.

sRespContent 是收到的应答包, 将在lua中解包.

OnResponse() 就是调用了 Lua 代码:

require("client_rpc_response_handler").handle(uLuaRpcId, sRespContent)


举报

相关推荐

0 条评论