lua c结构体转table

lua c结构体转table

本周有个任务,是将json数据和C结构体数据进行互相转换,初一看很简单,有两个方案去做。但是都是很麻烦,要么做起来简单,要么工作量大,要么工作量少,难度极高,最后找到一个折中的办法,就是通过修改源码来提供捷径。


问题描述:

公司的游戏服务器是由c++实现的,而客户端是android或者ios上实现的,这时的数据交互就是打包二进制数据流进行

但是呢,现在公司要做个web端的客户端,并且要使web端的客户端和c++数据的客户端进行交互,众所周知,
web端对于二进制数据的处理并不是很友好,所以要建立一个中间站,来进行数据转换,将c++的struct数据转换成
json数据,将json数据转换成struct数据。那么怎么进行转换呢?  

实现lua获取struct结构体成员的接口

打开Luajit源码(网址https://github.com/LuaJIT/LuaJIT)中的lib_ffi.c文件,
添加以下代码,然后重新编译,windows平台和linux平台的编译方法请各自百度吧
    LJLIB_CF(ffi_keys)  LJLIB_REC(ffi_xof FF_ffi_keys)
    {
      CTState *cts = ctype_cts(L);
      CTypeID id = ffi_checkctype(L, cts, NULL);
      CType *ct = lj_ctype_rawref(cts, id);
      lua_newtable(L);
      if (ctype_isstruct(ct->info) && ct->size != CTSIZE_INVALID) {
        uint32_t i = 0;
        while(ct->sib) {
          ct = ctype_get(cts, ct->sib);
          lua_pushnumber(L, ++i);
          lua_pushstring(L, strdata(strref(ct->name)));
          lua_settable(L, -3);
        }
      }
      return 1;
    }
编译运行成功以后,写个例子实验以下吧
local ffi = require("ffi");
-- 声明结构体
ffi.cdef[[
struct test{
    int first;
    char second;
    char *third;
    void *fouth;
    struct test *point;
};
]]
-- 获取结构体成员
prinit(ffi.keys("struct test"));
运行结果:
> ffi = require("ffi")
> ffi.cdef[[
>> struct test{
>> int first;
>> char second;
>> char *third;
>> void *fouth;
>> struct test* next;
>> };
>> ]]
> ffi.keys("struct test")
{ 'first', 'second', 'third', 'fouth', 'next' }
>
有了结构体的成员名称,接下来就可以通过遍历成员名称的方式,逐个给结构赋值了。

简单struct结构体转json数据

有了以上获取结构体成员的功能,我们简单的将结构体转化为json数据吧
    local ffi = require("ffi");
    local json = require("json");
    -- 声明结构体
    ffi.cdef[[
    struct test{
        int first;
        int second;
    };
    ]]
    var jdata = {};
    var cdata = ffi.new("struct test");
    cdata.first = 5;
    cdata.second = 10;

    var members = ffi.keys("struct test")
    for i,key in ipairs(members) do
        jdata[key] = cdata[key];
    end

    p(jdata);
输出:
    { a = 5, b = 10 }

复杂struct结构体转json

上面的struct 只有一层的结构,数据转换比较简单。 
但是当结构体内有数组或者结构体的时候,怎么办呢?那就要用ffi.typeof获取类型, 
并分析类型是结构体还是数组,这个我也做好了有现成的。

代码文件再此:https://download.csdn.net/download/c_creator/10339126

使用:
    ffi.cdef[[
        struct testA{
            int a;
            int b;
        };
        struct testB{
            int a;
            int b;
            struct testA;
        };
    ]]
    -- 二进制数据强转struct数据类型
    local cdata = ffi.cast("struct testB" .. "*", data);
        -- 解析cdata数据为table
        local tbl_data = parseCData.parseCData(cdata);
        -- table数据转json
        local jdata = json.encode(tbl_data)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值