mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 12:20:41 +00:00
add load/save proto for multi states use
This commit is contained in:
2
Makefile
2
Makefile
@@ -13,7 +13,7 @@ LUA_STATICLIB := 3rd/lua/liblua.a
|
|||||||
LUA_LIB ?= $(LUA_STATICLIB)
|
LUA_LIB ?= $(LUA_STATICLIB)
|
||||||
LUA_INC ?= 3rd/lua
|
LUA_INC ?= 3rd/lua
|
||||||
|
|
||||||
$(LUA_STATICLIB) :
|
$(LUA_STATICLIB) :
|
||||||
cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT)
|
cd 3rd/lua && $(MAKE) CC='$(CC) -std=gnu99' $(PLAT)
|
||||||
|
|
||||||
# jemalloc
|
# jemalloc
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "msvcint.h"
|
#include "msvcint.h"
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
@@ -41,8 +42,15 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
|
|||||||
static int
|
static int
|
||||||
lnewproto(lua_State *L) {
|
lnewproto(lua_State *L) {
|
||||||
size_t sz = 0;
|
size_t sz = 0;
|
||||||
void * buffer = (void *)luaL_checklstring(L,1,&sz);
|
void * buffer;
|
||||||
struct sproto * sp = sproto_create(buffer, sz);
|
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) {
|
if (sp) {
|
||||||
lua_pushlightuserdata(L, sp);
|
lua_pushlightuserdata(L, sp);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -449,6 +457,31 @@ lprotocol(lua_State *L) {
|
|||||||
return 3;
|
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
|
int
|
||||||
luaopen_sproto_core(lua_State *L) {
|
luaopen_sproto_core(lua_State *L) {
|
||||||
#ifdef luaL_checkversion
|
#ifdef luaL_checkversion
|
||||||
@@ -461,6 +494,8 @@ luaopen_sproto_core(lua_State *L) {
|
|||||||
{ "querytype", lquerytype },
|
{ "querytype", lquerytype },
|
||||||
{ "decode", ldecode },
|
{ "decode", ldecode },
|
||||||
{ "protocol", lprotocol },
|
{ "protocol", lprotocol },
|
||||||
|
{ "loadproto", lloadproto },
|
||||||
|
{ "saveproto", lsaveproto },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
luaL_newlib(L,l);
|
luaL_newlib(L,l);
|
||||||
|
|||||||
Reference in New Issue
Block a user