zoukankan      html  css  js  c++  java
  • lua pbc

    先要将proto文件编译成.pb文件,然后再动态绑定实现lua protobuffer,这就需要了解云风做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob/master/binding/lua/README.md

    具体的方式是,下载pbc的项目;在pbc/binding/lua下面编译出protobuf.so放在LUA_PATH下面,或者将protobuf.lua放在LUA_PATH下,就可以调用protobuf中的库方法
    cd pbc/binding/lua
    make

    如果提示-fPIC
    -->删去pbc-master/build/o下的所有文件
    -->修改pbc-master/Makefile 中的CFLAGS = -O2为CFLAGS = -O2 -fPIC
    然后就在pbc-master目录下make
    cd pbc/binding/lua
    make

    如果提示liblua.a XXX -fPIC
    那么唯有重新编译一次lua了。
    -->cd lua-5.1.4/src
    -->修改Makefile中的CFLAGS= -O2 -Wall $(MYCFLAGS)为CFLAGS= -O2 -Wall $(MYCFLAGS) -fPIC
    -->cd ..
    -->make linux
    -->sudo make install

    cd pbc/binding/lua
    make
    -->编译成功
    具体的调用方法在pbc的项目中有例子说明,主要的思路是:
    1. require "protobuf"
    2. 注册pb文件,利用该文件decode或者encode它的protobuffer对象,见下面的例子:
       echo "package test" > test.proto
       echo "message Mytest {optional sint64 testid=1;}" >> test.proto
       protoc --descriptor_set_out test.pb test.proto
      以上方法生成了pb文件,下面的方法是用lua对pb的decode和encode过程
       pb = require "protobuf"
       protobuf.register_file("./test.pb")
       stringbuffer = pb.encode("test.Mytest",
                       {
                             testid = 10;                    })
       result = pb.encode("test.Mytest", stringbuffer)
       print("result="..result.testid)

    这只是云风提供的方法之一,lua对pb的decode和encode过程的方法之二如下:
    file = io.open("./test.pb", "rb")
    buffer = file:read "*a"
    file:close()
      pb.register(buffer)
      stringbuffer = pb.encode("test.Mytest",
    {
    testid =100;
    })
    result = pb.decode("test.Mytest", stringbuffer)
    print("result="..result.testid)
    云风还提供了第三种方法,是用Lua parser,因为这种方法需要用到lua 的 Lpeg模块,暂时还没有研究这个功能,等后面再补上。
  • 相关阅读:
    openswitch db files
    openstack中虚拟机和其网络的联系方法 instance and network
    python操作db2和mysql ,ibm_db
    yum安装mariadb
    python 连接 db2
    db2操作 连接、备份、恢复db2
    su su
    linux 后台运行进程 fg bg ctrl+z nohup
    mysql 命令行
    IDEA-使用技巧
  • 原文地址:https://www.cnblogs.com/ghost240/p/3253092.html
Copyright © 2011-2022 走看看