mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43: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 netpack = require "netpack"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
local sproto = require "sproto"
|
local sproto = require "sproto"
|
||||||
|
local sprotoloader = require "sprotoloader"
|
||||||
|
|
||||||
local host
|
local host
|
||||||
local send_request
|
local send_request
|
||||||
@@ -61,9 +62,10 @@ skynet.register_protocol {
|
|||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
function CMD.start(gate, fd, proto)
|
function CMD.start(gate, fd)
|
||||||
host = sproto.new(proto.c2s):host "package"
|
-- slot 1,2 set at main.lua
|
||||||
send_request = host:attach(sproto.new(proto.s2c))
|
host = sprotoloader.load(1):host "package"
|
||||||
|
send_request = host:attach(sprotoloader.load(2))
|
||||||
skynet.fork(function()
|
skynet.fork(function()
|
||||||
while true do
|
while true do
|
||||||
send_package(send_request "heartbeat")
|
send_package(send_request "heartbeat")
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
local sprotoloader = require "sprotoloader"
|
||||||
|
|
||||||
local max_client = 64
|
local max_client = 64
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
print("Server start")
|
print("Server start")
|
||||||
|
skynet.uniqueservice("protoloader")
|
||||||
local console = skynet.newservice("console")
|
local console = skynet.newservice("console")
|
||||||
skynet.newservice("debug_console",8000)
|
skynet.newservice("debug_console",8000)
|
||||||
skynet.newservice("simpledb")
|
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 skynet = require "skynet"
|
||||||
local netpack = require "netpack"
|
local netpack = require "netpack"
|
||||||
local proto = require "proto"
|
|
||||||
|
|
||||||
local CMD = {}
|
local CMD = {}
|
||||||
local SOCKET = {}
|
local SOCKET = {}
|
||||||
@@ -12,7 +9,7 @@ local agent = {}
|
|||||||
function SOCKET.open(fd, addr)
|
function SOCKET.open(fd, addr)
|
||||||
skynet.error("New client from : " .. addr)
|
skynet.error("New client from : " .. addr)
|
||||||
agent[fd] = skynet.newservice("agent")
|
agent[fd] = skynet.newservice("agent")
|
||||||
skynet.call(agent[fd], "lua", "start", gate, fd, proto)
|
skynet.call(agent[fd], "lua", "start", gate, fd)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function close_agent(fd)
|
local function close_agent(fd)
|
||||||
|
|||||||
@@ -9,24 +9,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.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 DEFAULT_CAP 64
|
||||||
#define MAX_NUMBER 1024
|
#define MAX_NUMBER 1024
|
||||||
|
|
||||||
@@ -1100,7 +1082,10 @@ static uint32_t oid_counter;
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
init_oid_header() {
|
init_oid_header() {
|
||||||
init_winsock();
|
if (oid_counter) {
|
||||||
|
// already init
|
||||||
|
return;
|
||||||
|
}
|
||||||
pid_t pid = getpid();
|
pid_t pid = getpid();
|
||||||
uint32_t h = 0;
|
uint32_t h = 0;
|
||||||
char hostname[256];
|
char hostname[256];
|
||||||
@@ -1117,7 +1102,11 @@ init_oid_header() {
|
|||||||
oid_header[3] = pid & 0xff;
|
oid_header[3] = pid & 0xff;
|
||||||
oid_header[4] = (pid >> 8) & 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
|
static inline int
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "lauxlib.h"
|
#include "lauxlib.h"
|
||||||
#include "sproto.h"
|
#include "sproto.h"
|
||||||
|
|
||||||
|
#define MAX_GLOBALSPROTO 16
|
||||||
#define ENCODE_BUFFERSIZE 2050
|
#define ENCODE_BUFFERSIZE 2050
|
||||||
|
|
||||||
//#define ENCODE_BUFFERSIZE 2050
|
//#define ENCODE_BUFFERSIZE 2050
|
||||||
@@ -458,27 +459,46 @@ lprotocol(lua_State *L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* global sproto pointer for multi states */
|
/* global sproto pointer for multi states */
|
||||||
static void * G_sproto = NULL;
|
struct sproto_bin {
|
||||||
static size_t G_sproto_sz = 0;
|
void *ptr;
|
||||||
|
size_t sz;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct sproto_bin G_sproto[MAX_GLOBALSPROTO];
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lsaveproto(lua_State *L) {
|
lsaveproto(lua_State *L) {
|
||||||
size_t sz;
|
size_t sz;
|
||||||
void * buffer = (void *)luaL_checklstring(L,1,&sz);
|
void * buffer = (void *)luaL_checklstring(L,1,&sz);
|
||||||
void * tmp = malloc(sz);
|
int index = luaL_optinteger(L, 2, 0);
|
||||||
memcpy(tmp, buffer, sz);
|
void * tmp;
|
||||||
if (G_sproto) {
|
struct sproto_bin * sbin = &G_sproto[index];
|
||||||
free(G_sproto);
|
if (index < 0 || index >= MAX_GLOBALSPROTO) {
|
||||||
|
return luaL_error(L, "Invalid global slot index %d", index);
|
||||||
}
|
}
|
||||||
G_sproto = tmp;
|
tmp = malloc(sz);
|
||||||
G_sproto_sz = sz;
|
memcpy(tmp, buffer, sz);
|
||||||
|
if (sbin->ptr) {
|
||||||
|
free(sbin->ptr);
|
||||||
|
}
|
||||||
|
sbin->ptr = tmp;
|
||||||
|
sbin->sz = sz;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lloadproto(lua_State *L) {
|
lloadproto(lua_State *L) {
|
||||||
lua_pushlightuserdata(L, G_sproto);
|
int index = luaL_optinteger(L, 1, 0);
|
||||||
lua_pushinteger(L, G_sproto_sz);
|
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;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ function sproto_mt:__gc()
|
|||||||
core.deleteproto(self.__cobj)
|
core.deleteproto(self.__cobj)
|
||||||
end
|
end
|
||||||
|
|
||||||
function sproto.new(pbin)
|
function sproto.new(...)
|
||||||
local cobj = assert(core.newproto(pbin))
|
local cobj = assert(core.newproto(...))
|
||||||
local self = {
|
local self = {
|
||||||
__cobj = cobj,
|
__cobj = cobj,
|
||||||
__tcache = setmetatable( {} , weak_mt ),
|
__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