Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

protobuf 模块设置为全局变量,不同模块需要register不同.pb #84

Open
lilien1010 opened this issue Aug 24, 2016 · 9 comments

Comments

@lilien1010
Copy link

No description provided.

@lilien1010
Copy link
Author

protobuf 模块已经加载到全局,可是工程里面有多个 pb 文件需要解析怎么处理?难道再每次 decode之前都调用 protobuf.register() 文件?

希望能给一个demo代码,
另外protobuf.lua 能不能改成不注册为全局的,

使用如下:
local pb = require "protobuf"
local proto = pb.new(pb_file_name)

这样个模块可以使用自己独立的*.pb文件

@cloudwu
Copy link
Owner

cloudwu commented Aug 24, 2016

@lilien1010
Copy link
Author

我看代码里面
···
local P,GC

P = debug.getregistry().PROTOBUF_ENV

if P then
GC = c._gc()
else
P= c._env_new()
GC = c._gc(P)
end

M.GC = GC
···

不知道我理解错了没有,
如果在两个不同的文件里面 执行 re

test1.lua
···
local protobuf = require "protobuf"
protobuf.register_file("../../build/addressbook.pb")
···

test2.lua
···
local protobuf = require "protobuf"
protobuf.register_file("../../build/person.pb")
···
如果某个地方先执行 test1模块.再执行 test2模块
我理解因为 require 会缓存上次的模块,那么在test1.lua 里面的 protobuf 变量的环境(_env_new)其实 已经在第二次执行 register_file的时候被变更了对吗?

是不是在每次关联不同的pb文件的的之后,执行一次深拷贝?

@setlin
Copy link

setlin commented Aug 24, 2016

我个人建议把register写在一个文件中,因为pb文件有依赖关系,需要注意register的先后顺序
比如:
a.proto和b.proto
b.proto中import了a.proto

那么
ImprotPBC.lua
...
protobuf = require "protobuf"
function RegisterPBC(filename)
protobuf.register_file(filename)
end

function Init()
RegisterPBC("a.pb")
RegisterPBC("b.pb")
end

Init()
...

test1.lua
...
require "ImportPBC"
...

test2.lua
...
require "ImportPBC"
...

在程序运行中,对ImportPBC.lua文件每一次加载就是热更新了pbc

@cloudwu
Copy link
Owner

cloudwu commented Aug 24, 2016

我不喜欢 protobuffer, 不想再维护这个项目。

@lilien1010
Copy link
Author

lilien1010 commented Aug 24, 2016

@setlin 但是这样后期维护 ,会很麻烦,新的协议需要修改的老的文件,不太好。我还是打算自己改写 protobuf.lua 这个文件,通过new 的方式 把 里面的私有变量都 setmatedata,这样每次 new 都是一个新的 pbc_env,这样就不会互相干扰了

@cloudwu 也不喜欢pb啊,主要是为了兼容就有的网络协议。多谢大牛的指导

@bugwang
Copy link

bugwang commented Oct 17, 2016

protobuffer 确实感觉麻烦。
风云大神, 我看到pbc 没有oneof 结构的解析,不知道支持不。如果不支持。独自实现的话,麻烦吗,有啥建议吗?多谢

@fengsuxing
Copy link

@bugwang 我这也要用到oneof,留个联系方式一起交流吧

@wang10095
Copy link

请问各位 要想使用的不同的pb 有什么好的解决方案吗??? 遇到了相同的问题

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants