From 688cc843ced972fb395c04c13c9f9eb334c70144 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Sat, 28 Feb 2015 13:33:56 +0800 Subject: [PATCH] use sproto global slot --- examples/agent.lua | 8 +++++--- examples/main.lua | 2 ++ examples/protoloader.lua | 13 ++++++++++++ examples/watchdog.lua | 5 +---- lualib-src/lua-bson.c | 31 ++++++++++------------------ lualib-src/sproto/lsproto.c | 40 +++++++++++++++++++++++++++---------- lualib/sproto.lua | 4 ++-- lualib/sprotoloader.lua | 23 +++++++++++++++++++++ 8 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 examples/protoloader.lua create mode 100644 lualib/sprotoloader.lua diff --git a/examples/agent.lua b/examples/agent.lua index 98b20b2b..45541f70 100644 --- a/examples/agent.lua +++ b/examples/agent.lua @@ -2,6 +2,7 @@ local skynet = require "skynet" local netpack = require "netpack" local socket = require "socket" local sproto = require "sproto" +local sprotoloader = require "sprotoloader" local host local send_request @@ -61,9 +62,10 @@ skynet.register_protocol { end } -function CMD.start(gate, fd, proto) - host = sproto.new(proto.c2s):host "package" - send_request = host:attach(sproto.new(proto.s2c)) +function CMD.start(gate, fd) + -- slot 1,2 set at main.lua + host = sprotoloader.load(1):host "package" + send_request = host:attach(sprotoloader.load(2)) skynet.fork(function() while true do send_package(send_request "heartbeat") diff --git a/examples/main.lua b/examples/main.lua index 4a98d2eb..9da707d4 100644 --- a/examples/main.lua +++ b/examples/main.lua @@ -1,9 +1,11 @@ local skynet = require "skynet" +local sprotoloader = require "sprotoloader" local max_client = 64 skynet.start(function() print("Server start") + skynet.uniqueservice("protoloader") local console = skynet.newservice("console") skynet.newservice("debug_console",8000) skynet.newservice("simpledb") diff --git a/examples/protoloader.lua b/examples/protoloader.lua new file mode 100644 index 00000000..03df570f --- /dev/null +++ b/examples/protoloader.lua @@ -0,0 +1,13 @@ +-- module proto as examples/proto.lua +package.path = "./examples/?.lua;" .. package.path + +local skynet = require "skynet" +local sprotoparser = require "sprotoparser" +local sprotoloader = require "sprotoloader" +local proto = require "proto" + +skynet.start(function() + sprotoloader.save(proto.c2s, 1) + sprotoloader.save(proto.s2c, 2) + -- don't call skynet.exit() , because sproto.core may unload and the global slot become invalid +end) diff --git a/examples/watchdog.lua b/examples/watchdog.lua index eaedd5db..e02af169 100644 --- a/examples/watchdog.lua +++ b/examples/watchdog.lua @@ -1,8 +1,5 @@ -package.path = "./examples/?.lua;" .. package.path - local skynet = require "skynet" local netpack = require "netpack" -local proto = require "proto" local CMD = {} local SOCKET = {} @@ -12,7 +9,7 @@ local agent = {} function SOCKET.open(fd, addr) skynet.error("New client from : " .. addr) agent[fd] = skynet.newservice("agent") - skynet.call(agent[fd], "lua", "start", gate, fd, proto) + skynet.call(agent[fd], "lua", "start", gate, fd) end local function close_agent(fd) diff --git a/lualib-src/lua-bson.c b/lualib-src/lua-bson.c index ba97f841..88f1be8e 100644 --- a/lualib-src/lua-bson.c +++ b/lualib-src/lua-bson.c @@ -9,24 +9,6 @@ #include #include -#if defined(_WIN32) || defined(_WIN64) - -#include - -static void -init_winsock() { - WSADATA wsaData; - WSAStartup(MAKEWORD(2,2), &wsaData); -} - -#else - -static void -init_winsock() { -} - -#endif - #define DEFAULT_CAP 64 #define MAX_NUMBER 1024 @@ -1100,7 +1082,10 @@ static uint32_t oid_counter; static void init_oid_header() { - init_winsock(); + if (oid_counter) { + // already init + return; + } pid_t pid = getpid(); uint32_t h = 0; char hostname[256]; @@ -1116,8 +1101,12 @@ init_oid_header() { oid_header[2] = (h>>16) & 0xff; oid_header[3] = pid & 0xff; oid_header[4] = (pid >> 8) & 0xff; - - oid_counter = h ^ time(NULL) ^ (uintptr_t)&h; + + uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h; + if (c == 0) { + c = 1; + } + oid_counter = c; } static inline int diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c04f7d28..290eeef4 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -6,6 +6,7 @@ #include "lauxlib.h" #include "sproto.h" +#define MAX_GLOBALSPROTO 16 #define ENCODE_BUFFERSIZE 2050 //#define ENCODE_BUFFERSIZE 2050 @@ -458,27 +459,46 @@ lprotocol(lua_State *L) { } /* global sproto pointer for multi states */ -static void * G_sproto = NULL; -static size_t G_sproto_sz = 0; +struct sproto_bin { + void *ptr; + size_t sz; +}; + +static struct sproto_bin G_sproto[MAX_GLOBALSPROTO]; 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); + int index = luaL_optinteger(L, 2, 0); + void * tmp; + struct sproto_bin * sbin = &G_sproto[index]; + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); } - G_sproto = tmp; - G_sproto_sz = sz; + tmp = malloc(sz); + memcpy(tmp, buffer, sz); + if (sbin->ptr) { + free(sbin->ptr); + } + sbin->ptr = tmp; + sbin->sz = sz; return 0; } static int lloadproto(lua_State *L) { - lua_pushlightuserdata(L, G_sproto); - lua_pushinteger(L, G_sproto_sz); + int index = luaL_optinteger(L, 1, 0); + struct sproto_bin * sbin = &G_sproto[index]; + if (index < 0 || index >= MAX_GLOBALSPROTO) { + return luaL_error(L, "Invalid global slot index %d", index); + } + if (sbin->ptr == NULL) { + return luaL_error(L, "nil sproto at index %d", index); + } + + lua_pushlightuserdata(L, sbin->ptr); + lua_pushinteger(L, sbin->sz); return 2; } diff --git a/lualib/sproto.lua b/lualib/sproto.lua index e6acd784..4351ba1d 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -12,8 +12,8 @@ function sproto_mt:__gc() core.deleteproto(self.__cobj) end -function sproto.new(pbin) - local cobj = assert(core.newproto(pbin)) +function sproto.new(...) + local cobj = assert(core.newproto(...)) local self = { __cobj = cobj, __tcache = setmetatable( {} , weak_mt ), diff --git a/lualib/sprotoloader.lua b/lualib/sprotoloader.lua new file mode 100644 index 00000000..28f32eca --- /dev/null +++ b/lualib/sprotoloader.lua @@ -0,0 +1,23 @@ +local parser = require "sprotoparser" +local core = require "sproto.core" +local sproto = require "sproto" + +local loader = {} + +function loader.register(filename, index) + local f = assert(io.open(filename), "Can't open sproto file") + local data = f:read "a" + f:close() + local bin = parser.parse(data) + core.saveproto(bin, index) +end + +loader.save = core.saveproto + +function loader.load(index) + local bin, sz = core.loadproto(index) + return sproto.new(bin,sz) +end + +return loader +