starwing / lua-protobuf

A Lua module to work with Google protobuf
MIT License
1.75k stars 387 forks source link

关于pb.decode传入指定长度的字节数组 #98

Closed 821869798 closed 4 years ago

821869798 commented 5 years ago

pb.decode 如何指定C#字节数组长度,为了避免GC,我在C#端做了缓存字节数组,有什么方法指定长度?用unsafe吗?

starwing commented 5 years ago

嗯,unsafe

821869798 commented 5 years ago

但是我要把 const char data = (const char )lua_touserdata(L, 2); 中的lua_touserdata换成lua_tostring才行, 不然传入的C#字节数组类型不是userdata导致报错: "userdata expected, got string"

starwing commented 5 years ago

你传错类型了,如果是string,那么必须是完整消息。如果要指定长度,你得搞成IntPtr再传

starwing commented 5 years ago

还有一个办法:使用slice接口来传递,slice接口接受string并且接受范围参数

songtm commented 3 years ago

@821869798 请教下这个问题你最后咋个处理的?

821869798 commented 3 years ago

@821869798 请教下这个问题你最后咋个处理的?

@songtm 以下是我的方法

local pb = require("pb") local pb_slice = require("pb.slice") local msg = pb.decode(protoName,pb_slice.new(msgData.RawData,1,msgData.RawDataLen)) --msgData.RawData 字节数组 --msgData.RawDataLen 反序列化指定的长度

songtm commented 3 years ago

@821869798 谢谢,我最终是把xlua的 push byte[] 改成直接调用xlua_pushlstring