Replies: 3 comments 10 replies
-
|
lua 5.5 支持了 external string ,稍后我加一个接口直接把 bson encode 成 string 。 不过这里跨服务传输,我觉得 encode 成 lightuserdata 接收方 free 可能更好一点。 |
Beta Was this translation helpful? Give feedback.
3 replies
-
|
使用指针也是需要简单改下才能正常运行:
diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c
index 29d6c70..21914c6 100644
--- a/lualib-src/lua-bson.c
+++ b/lualib-src/lua-bson.c
@@ -327,6 +327,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth)
case LUA_TNUMBER:
append_number(bs, L, key, sz);
break;
+ case LUA_TLIGHTUSERDATA:
case LUA_TUSERDATA: {
append_key(bs, L, BSON_DOCUMENT, key, sz);
int32_t * doc = (int32_t*)lua_touserdata(L,-1);
@@ -978,6 +979,20 @@ lencode(lua_State *L) {
return 1;
}
+static int
+lmeta(lua_State *L) {
+ lua_settop(L,1);
+ bson_meta(L);
+ return 1;
+}
+
+static int
+lto_lightuserdata(lua_State *L) {
+ void *p = lua_touserdata(L, 1);
+ lua_pushlightuserdata(L, p);
+ return 1;
+}
+
static int
encode_bson_byorder(lua_State *L) {
int n = lua_gettop(L);
@@ -1340,6 +1355,8 @@ luaopen_bson(lua_State *L) {
{ "objectid", lobjectid },
{ "int64", lint64 },
{ "decode", ldecode },
+ { "meta", lmeta },
+ { "to_lightuserdata", lto_lightuserdata },
{ NULL, NULL },
};
生命周期还是原来的 userdata 的生命周期,使用 skynet.call 应该是没问题的,使用 skynet.send 就可能会被提前释放。 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
我现在想实现一个 mongodb service 连接池子,所有的 db 操作都通过 skynet.call 或者 skynet.send 传输到这些池子里来执行。对于普通的接口没有什么问题,但是对于 raw_safe_insert 这样的接口有点问题, bson_encode 的结果无法正常通过 lua-seri 序列化。因此我先对 bson_encode 的结果执行 tostring ,然后在接收方再通过对这个字符串重新构造一个 userdata 结果来实现。
示例代码:
调用服务
池子服务
通过新增一个 bson_meta 接口来重新构造 userdata :
我的问题是:还有没别的更合适的方案?
Beta Was this translation helpful? Give feedback.
All reactions