转自: http://www.voidcn.com/article/p-vmuovdgn-bam.html

(1)lua实现protobuf的简介

需要读者对google的protobuf有一定的了解。
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.   https://developers.google.com/protocol-buffers/docs/overview
 

但遗憾的是,官网的probocol buffers并不支持lua,云风大侠做的pbc的项目,地址为:https://github.com/cloudwu/pbc/blob/master/binding/lua/README.md. 实现了protobuf对lua的支持。

主要步骤如下:

第一步: You specify how you want the information you're serializing to be structured by defining protocol buffer message types in .proto files(定义.proto)

第二步: Once you've defined your messages, you run the protocol buffer compiler for your application's language on your .proto file to generate data access classes.(编译.proto文件,生成.pb文件)

第三步: 使用pbc库提供的函数,实现protobuf.

(2)安装protobuf

下载地址:https://code.google.com/p/protobuf/

安装protobuf后,protobuf提供了protoc指令,可以编译.proto文件

#protobuf
cd $WORKDIR
tar xfz protobuf-2.4.1.tar.gz
cd protobuf-2.4.1
./configure || exit $?
make -j3 || exit $?
make install || exit $?

使用方法:protoc --descriptor_set_out test.pb test.proto

使用protoc -h 可以查看protoc的其他选项

把 多个proto 文件编译到一个.pb文件的方法: protoc --descriptor_set_out=$confdir/common.cmd.pb  $protodir/*.proto

(3) 安装pbc

rm -rf pbc-master
tar xfz pbc-master.tar.gz cd pbc-master
make || exit $?
cd binding/lua/ && make || exit $?
cp -rf protobuf.so $WORKDIR/lualib/

把编译生成的protobu.so文件放到LUA_PATH目录里面,当require "protobuf"的时候,可以找到这个文件。放在哪里不重要,重要的是让lua找到它。

除此之外,还有一步非常重要:

把 pbc/binding/lua/protobuf.lua 赋值到LUA_PATH目录中,当require "protobuf"的时候,可以找到这个文件。

http://www.cnblogs.com/ghost240/p/3253092.html 这篇文章中说,

LUA_PATH下,就可以调用protobuf中的库方法 是错的!在pbc/binding/lua下面编译出protobuf.so放在LUA_PATH下面,或者将protobuf.lua放在

(4)如何使用pbc

a: 定义一个common.proto文件

	package Common;

	message accountRegister
{
optional string accountid = 1;
optional string hashedpwd = 2;
}

b:编码common.proto,生成common.pb文件

protoc --descriptor_set_out=/common.pb  common.proto

c: 初始化protobuf

require("protobuf")   特别需要注意

	-- 生成的pb文件
local pbfile ="/path/to/common.pb" local pbdesc = io.open(pbfile, "rb")
if not pbdesc then
echoError("failed to load protobuf description file:" .. pbfile)
return
end local pbbuffer = pbdesc:read("*a")
protobuf.register(pbbuffer)
pbdesc:close()

d:  编码 protobuf.encode(ptype, msg)

ptype = 'Common.accountRegister' 千万不要忘了 Common前缀

msg = {accountid = '123', hashedpwd = '123456'}  msg的声明的格式必须要和proto文件里声明的一样,否则编码不通过

e: 解码 protobuf.decode(pbtype, msgbuf) 返回msg 和err,具体的返回内容请参考pbc的文件

(转)lua protobuffer的实现的更多相关文章

  1. lua pbc

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

  2. 实战:一种在http请求中使用protobuffer+nginx+lua收集打点日志的方案

    背景 app打点日志的上报和收集,是互联网公司的基本需求. 一.方案选择 1.1 protobuffer vs json 探究一种以最高效的方式上报和解析打点数据是一个系统性的问题,需要解决的子问题有 ...

  3. cocos2d-x lua 中使用protobuf并对http进行处理

    cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...

  4. lua执行字节码的过程介绍

    前面一篇文章中介绍了lua给下面代码生成最终的字节码的整个过程,这次我们来看看lua vm执行这些字节码的过程. foo = "bar" local a, b = "a& ...

  5. lua 学习笔记(1)

    一.lua函数赋值与函数调用         在lua中函数名也是作为一种变量出现的,即函数和所有其他值一样都是匿名的,当要使用某个函数时,需要将该函数赋值给一个变量,这样在函数块的其他地方就可以通过 ...

  6. 在redis中使用lua脚本让你的灵活性提高5个逼格

    在redis的官网上洋洋洒洒的大概提供了200多个命令,貌似看起来很多,但是这些都是别人预先给你定义好的,但你却不能按照自己的意图进行定制, 所以是不是感觉自己还是有一种被束缚的感觉,有这个感觉就对了 ...

  7. 使用Nginx+Lua代理Hadoop HA

    一.Hadoop HA的Web页面访问 Hadoop开启HA后,会同时存在两个Master组件提供服务,其中正在使用的组件称为Active,另一个作为备份称为Standby,例如HDFS的NameNo ...

  8. 打印Lua的Table对象

    小伙伴们再也不用为打印lua的Table对象而苦恼了, 本人曾也苦恼过,哈哈 不过今天刚完成了这个东西, 以前在网上搜过打印table的脚本,但是都感觉很不理想,于是,自己造轮子了~ 打印的效果,自己 ...

  9. lua解析赋值类型代码的过程

    我们来看看lua vm在解析下面源码并生成bytecode时的整个过程: foo = "bar" local a, b = "a", "b" ...

随机推荐

  1. Apache Flink:详细入门

    Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时(Flink Runtime),提供支持流处理和批处理两种类型应用的功能.现有的开源计算 ...

  2. Lambda表达式底层分析

    一.我们先看下C#代码下Lamdba表达式的写法 // <summary> /// 写入日志委托 /// </summary> /// <param name=" ...

  3. SQlite 学习资料

      很有用的开源跨平台数据库,可以作为客户端的小型内存数据库使用,据说它有N多用户(Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFo ...

  4. jquery 弥补ie6不支持input:hover状态

    <!doctype html><html>    <head>    <meta charset="utf-8">    <t ...

  5. 四(2)、springcloud之Ribbon负载均衡

    2.Ribbon负载均衡 ​ Ribbon在工作时分成两步第一步先选择 EurekaServer ,它优先选择在同一个区域内负载较少的server. 第二步再根据用户指定的策略,在从server取到的 ...

  6. bigger is greater

    题目: Lexicographical order is often known as alphabetical order when dealing with strings. A string i ...

  7. nuxt 利用lru-cache 做服务器数据请求缓存

    // 运行与服务端的js // node.js lru-cache import LRU from 'lru-cache' const lruCache = LRU({ // 缓存队列长度 max: ...

  8. GCloud SDK 遇到的错误记录

    eclipse 环境 1.调用 SetAppInfo 方法返回 -1 语音id 和 key 设置正确 ,各种检测都没问题 解决办法 把安卓工程目录下 obj 文件价删除 ,把sdk 替换成以前能用的老 ...

  9. Spring入门(四)Spring-test模块

    自动化转配bean的测试案例分析 package soundsystem; import static org.junit.Assert.*; import org.junit.Rule; impor ...

  10. 安装php 在阿里云yum源的环境

    yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-postgresql ph ...