【问题标题】:Lua RPC and userdataLua RPC 和用户数据
【发布时间】:2012-05-30 21:31:04
【问题描述】:

我目前在我的程序中使用 luarpc 进行进程间通信。现在的问题是,由于我的 tolua++ 绑定将类实例存储为用户数据,我无法使用这些函数中的任何一个,导致 luarpc 无法处理用户数据。我现在的问题是,如果您知道用户数据始终只有一个指针(4 个字节)并且附加了一个用于调用和索引操作的元表,是否可以(以及如何)传输用户数据。

【问题讨论】:

    标签: c++ lua rpc lua-userdata


    【解决方案1】:

    你不能。

    用户数据是指针还是对象都没有关系。不能通过它们任意 RPC 的原因是数据没有存储在 Lua 中。因此 LuaRPC 无法正常传输。

    指向地址空间的指针对于其他进程绝对没有价值;如果它在另一台机器上运行,则更是如此。您必须实际传输数据本身才能使 RPC 工作。 LuaRPC 可以进行这种传输,但仅限于它可以理解的数据。而它唯一能理解的数据就是存储在 Lua 中的数据。

    【讨论】:

    • 好吧,我不想在另一台机器上解释它。我只想能够调用远程对象的功能。因此我只需要一种方法将包含元表的 ptr 传输到另一台机器,如果我调用 userdata:GetX() 之类的函数,它必须将其传输回来并调用 corret 函数
    • @ACB:LuaRPC 无法知道你的意图。它不知道您打算将此指针用作标识符。所以为了安全起见,是不允许的。你需要解决这个问题。
    • 我知道,但实际上我想更改它,以便它适用于这种特殊情况,假设它始终只是一个标识符。我的问题是我将如何解决这个问题,因为我不知道 lua 实际如何处理调用用户数据,我不知道我需要来回传递什么才能使其工作。我也会对使 tolua++ 将 pts 存储为数字或其他内容的解决方案感到高兴。我只是希望能够调用这些函数
    • 好的,我已经得到 local remote = rpc.remoteobj:getremoteinstance()``rpc.remoteobj.work(remote) 工作现在有没有办法将其更改为 rpc.remote:work()
    • hmm ok m = { __index = rpc.remoteobj }; rpc.setmetatable(remote,m);-- lua wont allow changing metatable of userdata remote:work() 现在也可以工作,但我将如何从 c++ 内部做到这一点?
    【解决方案2】:

    好的,我现在开始工作了。我所做的是对于用户数据参数/返回,我将实际的 ptr + 元表名称(类型名称)发送给客户端。然后,客户端使用 __index 方法附加一个元表,该方法创建一个具有类型名的新助手,并在您要访问的字段中附加一个助手。然后,当您从该 userdata 调用或读取字段时,客户端将用于调用 typetable 的字段和 userdata 的数据发送到服务器。

    读取变量:

        lua_pushlightuserdata(L,msg.read<void*>());
    #ifndef RPC_SERVER
        luaL_getmetatable(L,"rpc.userdata");
        int len = msg.read<int>();
        char* s = new char[len];
        msg.read((uint8*)s,len);
        s[len] = '\0';
        lua_pushlstring(L,s,len);
        lua_setfield(L,-2,"__name");
        lua_pushlightuserdata(L,TlsGetValue(transporttls));
        lua_setfield(L,-2,"__transport");
        lua_setmetatable(L,-2);
    #endif
    

    写变量:

        else
        {
            msg.append<RPCType>(RPC_USERDATA);
            msg.append<void*>(lua_touserdata(L,idx));
    #ifdef RPC_SERVER
            lua_getmetatable(L,idx);
            lua_rawget(L,LUA_REGISTRYINDEX);
            const char* s = lua_tostring(L,-1);
            int len = lua_strlen(L,-1);
            msg.append<int>(len);
            msg.append(s,len);
    #endif
            lua_settop(L,stack_at_start);
        }
    

    用户数据索引:

    checkNumArgs(L,2);
    ASSERT(lua_isuserdata(L,1) && isMetatableType(L,1,"rpc.userdata"));
    
    if(lua_type(L,2) != LUA_TSTRING)
        return luaL_error( L, "can't index a handle with a non-string" );
    const char* s = lua_tostring(L,2);
    if(strlen(s) > MAX_PATH - 1)
        return luaL_error(L,"string to long");
    
    int stack = lua_gettop(L);
    lua_getmetatable(L,1);
    lua_getfield(L,-1,"__name");
    const char* name = lua_tostring(L,-1);
    if(strlen(name) > MAX_PATH - 1)
        return luaL_error(L,"string to long");
    lua_pop(L,1); // remove name
    
    lua_getfield(L,-1,"__transport");
    Transport* t = reinterpret_cast<Transport*>(lua_touserdata(L,-1));
    lua_pop(L,1);
    
    Helper* h  = Helper::create(L,t,name);
    Helper::append(L,h,s);
    return 1;
    

    我或多或少地重写了完整的 rpc 库以使用命名管道和窗口,但我认为代码应该为任何人提供足够的信息来实现它。

    这允许如下代码:

    local remote = rpc.remoteobj:getinstance()
    remote:dosmthn()
    

    在客户端。它目前不允许添加新字段,但这就是我现在所需要的:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2015-01-14
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      相关资源
      最近更新 更多