From 318ee300e3d5b6a61bf9e38d323ca0dc70d145cd Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 28 Feb 2015 11:13:10 +0800 Subject: [PATCH] add load/save proto for multi states use --- Makefile | 2 +- lualib-src/sproto/lsproto.c | 39 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 3c2ead00..e553d6c9 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ LUA_STATICLIB := 3rd/lua/liblua.a LUA_LIB ?= $(LUA_STATICLIB) LUA_INC ?= 3rd/lua -$(LUA_STATICLIB) : +$(LUA_STATICLIB) : cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT) # jemalloc diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index e5378713..c04f7d28 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -1,4 +1,5 @@ #include +#include #include "msvcint.h" #include "lua.h" @@ -41,8 +42,15 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { static int lnewproto(lua_State *L) { size_t sz = 0; - void * buffer = (void *)luaL_checklstring(L,1,&sz); - struct sproto * sp = sproto_create(buffer, sz); + void * buffer; + struct sproto * sp; + if (lua_isuserdata(L,1)) { + buffer = lua_touserdata(L,1); + sz = luaL_checkinteger(L,2); + } else { + buffer = (void *)luaL_checklstring(L,1,&sz); + } + sp = sproto_create(buffer, sz); if (sp) { lua_pushlightuserdata(L, sp); return 1; @@ -449,6 +457,31 @@ lprotocol(lua_State *L) { return 3; } +/* global sproto pointer for multi states */ +static void * G_sproto = NULL; +static size_t G_sproto_sz = 0; + +static int +lsaveproto(lua_State *L) { + size_t sz; + void * buffer = (void *)luaL_checklstring(L,1,&sz); + void * tmp = malloc(sz); + memcpy(tmp, buffer, sz); + if (G_sproto) { + free(G_sproto); + } + G_sproto = tmp; + G_sproto_sz = sz; + return 0; +} + +static int +lloadproto(lua_State *L) { + lua_pushlightuserdata(L, G_sproto); + lua_pushinteger(L, G_sproto_sz); + return 2; +} + int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion @@ -461,6 +494,8 @@ luaopen_sproto_core(lua_State *L) { { "querytype", lquerytype }, { "decode", ldecode }, { "protocol", lprotocol }, + { "loadproto", lloadproto }, + { "saveproto", lsaveproto }, { NULL, NULL }, }; luaL_newlib(L,l);