mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 03:53:09 +00:00
use sproto global slot
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
13
examples/protoloader.lua
Normal file
13
examples/protoloader.lua
Normal file
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -9,24 +9,6 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#include <winsock2.h>
|
||||
|
||||
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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 ),
|
||||
|
||||
23
lualib/sprotoloader.lua
Normal file
23
lualib/sprotoloader.lua
Normal file
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user