Skip to content

Commit 688cc84

Browse files
committed
use sproto global slot
1 parent 318ee30 commit 688cc84

File tree

8 files changed

+85
-39
lines changed

8 files changed

+85
-39
lines changed

examples/agent.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local skynet = require "skynet"
22
local netpack = require "netpack"
33
local socket = require "socket"
44
local sproto = require "sproto"
5+
local sprotoloader = require "sprotoloader"
56

67
local host
78
local send_request
@@ -61,9 +62,10 @@ skynet.register_protocol {
6162
end
6263
}
6364

64-
function CMD.start(gate, fd, proto)
65-
host = sproto.new(proto.c2s):host "package"
66-
send_request = host:attach(sproto.new(proto.s2c))
65+
function CMD.start(gate, fd)
66+
-- slot 1,2 set at main.lua
67+
host = sprotoloader.load(1):host "package"
68+
send_request = host:attach(sprotoloader.load(2))
6769
skynet.fork(function()
6870
while true do
6971
send_package(send_request "heartbeat")

examples/main.lua

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
local skynet = require "skynet"
2+
local sprotoloader = require "sprotoloader"
23

34
local max_client = 64
45

56
skynet.start(function()
67
print("Server start")
8+
skynet.uniqueservice("protoloader")
79
local console = skynet.newservice("console")
810
skynet.newservice("debug_console",8000)
911
skynet.newservice("simpledb")

examples/protoloader.lua

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- module proto as examples/proto.lua
2+
package.path = "./examples/?.lua;" .. package.path
3+
4+
local skynet = require "skynet"
5+
local sprotoparser = require "sprotoparser"
6+
local sprotoloader = require "sprotoloader"
7+
local proto = require "proto"
8+
9+
skynet.start(function()
10+
sprotoloader.save(proto.c2s, 1)
11+
sprotoloader.save(proto.s2c, 2)
12+
-- don't call skynet.exit() , because sproto.core may unload and the global slot become invalid
13+
end)

examples/watchdog.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
package.path = "./examples/?.lua;" .. package.path
2-
31
local skynet = require "skynet"
42
local netpack = require "netpack"
5-
local proto = require "proto"
63

74
local CMD = {}
85
local SOCKET = {}
@@ -12,7 +9,7 @@ local agent = {}
129
function SOCKET.open(fd, addr)
1310
skynet.error("New client from : " .. addr)
1411
agent[fd] = skynet.newservice("agent")
15-
skynet.call(agent[fd], "lua", "start", gate, fd, proto)
12+
skynet.call(agent[fd], "lua", "start", gate, fd)
1613
end
1714

1815
local function close_agent(fd)

lualib-src/lua-bson.c

+10-21
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@
99
#include <string.h>
1010
#include <stdbool.h>
1111

12-
#if defined(_WIN32) || defined(_WIN64)
13-
14-
#include <winsock2.h>
15-
16-
static void
17-
init_winsock() {
18-
WSADATA wsaData;
19-
WSAStartup(MAKEWORD(2,2), &wsaData);
20-
}
21-
22-
#else
23-
24-
static void
25-
init_winsock() {
26-
}
27-
28-
#endif
29-
3012
#define DEFAULT_CAP 64
3113
#define MAX_NUMBER 1024
3214

@@ -1100,7 +1082,10 @@ static uint32_t oid_counter;
11001082

11011083
static void
11021084
init_oid_header() {
1103-
init_winsock();
1085+
if (oid_counter) {
1086+
// already init
1087+
return;
1088+
}
11041089
pid_t pid = getpid();
11051090
uint32_t h = 0;
11061091
char hostname[256];
@@ -1116,8 +1101,12 @@ init_oid_header() {
11161101
oid_header[2] = (h>>16) & 0xff;
11171102
oid_header[3] = pid & 0xff;
11181103
oid_header[4] = (pid >> 8) & 0xff;
1119-
1120-
oid_counter = h ^ time(NULL) ^ (uintptr_t)&h;
1104+
1105+
uint32_t c = h ^ time(NULL) ^ (uintptr_t)&h;
1106+
if (c == 0) {
1107+
c = 1;
1108+
}
1109+
oid_counter = c;
11211110
}
11221111

11231112
static inline int

lualib-src/sproto/lsproto.c

+29-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "lauxlib.h"
77
#include "sproto.h"
88

9+
#define MAX_GLOBALSPROTO 16
910
#define ENCODE_BUFFERSIZE 2050
1011

1112
//#define ENCODE_BUFFERSIZE 2050
@@ -458,27 +459,46 @@ lprotocol(lua_State *L) {
458459
}
459460

460461
/* global sproto pointer for multi states */
461-
static void * G_sproto = NULL;
462-
static size_t G_sproto_sz = 0;
462+
struct sproto_bin {
463+
void *ptr;
464+
size_t sz;
465+
};
466+
467+
static struct sproto_bin G_sproto[MAX_GLOBALSPROTO];
463468

464469
static int
465470
lsaveproto(lua_State *L) {
466471
size_t sz;
467472
void * buffer = (void *)luaL_checklstring(L,1,&sz);
468-
void * tmp = malloc(sz);
473+
int index = luaL_optinteger(L, 2, 0);
474+
void * tmp;
475+
struct sproto_bin * sbin = &G_sproto[index];
476+
if (index < 0 || index >= MAX_GLOBALSPROTO) {
477+
return luaL_error(L, "Invalid global slot index %d", index);
478+
}
479+
tmp = malloc(sz);
469480
memcpy(tmp, buffer, sz);
470-
if (G_sproto) {
471-
free(G_sproto);
481+
if (sbin->ptr) {
482+
free(sbin->ptr);
472483
}
473-
G_sproto = tmp;
474-
G_sproto_sz = sz;
484+
sbin->ptr = tmp;
485+
sbin->sz = sz;
475486
return 0;
476487
}
477488

478489
static int
479490
lloadproto(lua_State *L) {
480-
lua_pushlightuserdata(L, G_sproto);
481-
lua_pushinteger(L, G_sproto_sz);
491+
int index = luaL_optinteger(L, 1, 0);
492+
struct sproto_bin * sbin = &G_sproto[index];
493+
if (index < 0 || index >= MAX_GLOBALSPROTO) {
494+
return luaL_error(L, "Invalid global slot index %d", index);
495+
}
496+
if (sbin->ptr == NULL) {
497+
return luaL_error(L, "nil sproto at index %d", index);
498+
}
499+
500+
lua_pushlightuserdata(L, sbin->ptr);
501+
lua_pushinteger(L, sbin->sz);
482502
return 2;
483503
}
484504

lualib/sproto.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ function sproto_mt:__gc()
1212
core.deleteproto(self.__cobj)
1313
end
1414

15-
function sproto.new(pbin)
16-
local cobj = assert(core.newproto(pbin))
15+
function sproto.new(...)
16+
local cobj = assert(core.newproto(...))
1717
local self = {
1818
__cobj = cobj,
1919
__tcache = setmetatable( {} , weak_mt ),

lualib/sprotoloader.lua

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local parser = require "sprotoparser"
2+
local core = require "sproto.core"
3+
local sproto = require "sproto"
4+
5+
local loader = {}
6+
7+
function loader.register(filename, index)
8+
local f = assert(io.open(filename), "Can't open sproto file")
9+
local data = f:read "a"
10+
f:close()
11+
local bin = parser.parse(data)
12+
core.saveproto(bin, index)
13+
end
14+
15+
loader.save = core.saveproto
16+
17+
function loader.load(index)
18+
local bin, sz = core.loadproto(index)
19+
return sproto.new(bin,sz)
20+
end
21+
22+
return loader
23+

0 commit comments

Comments
 (0)