mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
Compare commits
25 Commits
v1.0.0-alp
...
v1.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8214c53891 | ||
|
|
5e642b15a4 | ||
|
|
ac093c7142 | ||
|
|
01dcc45827 | ||
|
|
d3ac522dc6 | ||
|
|
55d0d57d5c | ||
|
|
1fec2e6063 | ||
|
|
cc4756de35 | ||
|
|
7fb109dbb0 | ||
|
|
7f578be649 | ||
|
|
69946d75c5 | ||
|
|
24f6994b50 | ||
|
|
07dbfd8651 | ||
|
|
ca50a5f518 | ||
|
|
b8c54cbac2 | ||
|
|
3a2c43e9e0 | ||
|
|
205824ab12 | ||
|
|
4ac047b274 | ||
|
|
987a90af8b | ||
|
|
250531c9a7 | ||
|
|
7b471ae27d | ||
|
|
4f93054f3a | ||
|
|
65fad89794 | ||
|
|
286c0f2c23 | ||
|
|
de57064b96 |
@@ -460,7 +460,7 @@ struct lua_Debug {
|
||||
|
||||
/* Add by skynet */
|
||||
|
||||
extern lua_State * skynet_sig_L;
|
||||
LUA_API lua_State * skynet_sig_L;
|
||||
LUA_API void (lua_checksig_)(lua_State *L);
|
||||
#define lua_checksig(L) if (skynet_sig_L) { lua_checksig_(L); }
|
||||
|
||||
|
||||
12
HISTORY.md
12
HISTORY.md
@@ -1,3 +1,15 @@
|
||||
v1.0.0-alpha7 (2015-6-8)
|
||||
-----------
|
||||
* console support launch snax service
|
||||
* Add cluster.snax
|
||||
* Add nodelay in clusterd
|
||||
* Merge sproto bugfix patch
|
||||
* Move some skynet api into skynet.manager
|
||||
* DNS support underscore
|
||||
* Add logservice in config file for user defined log service
|
||||
* skynet.fork returns coroutine
|
||||
* Fix a few of bugs , see the commits log
|
||||
|
||||
v1.0.0-alpha6 (2015-5-18)
|
||||
-----------
|
||||
* bugfix: httpc.get
|
||||
|
||||
@@ -4,6 +4,7 @@ local socket = require "socket"
|
||||
local sproto = require "sproto"
|
||||
local sprotoloader = require "sprotoloader"
|
||||
|
||||
local WATCHDOG
|
||||
local host
|
||||
local send_request
|
||||
|
||||
@@ -26,6 +27,10 @@ function REQUEST:handshake()
|
||||
return { msg = "Welcome to skynet, I will send heartbeat every 5 sec." }
|
||||
end
|
||||
|
||||
function REQUEST:quit()
|
||||
skynet.call(WATCHDOG, "lua", "close", client_fd)
|
||||
end
|
||||
|
||||
local function request(name, args, response)
|
||||
local f = assert(REQUEST[name])
|
||||
local r = f(args)
|
||||
@@ -62,7 +67,10 @@ skynet.register_protocol {
|
||||
end
|
||||
}
|
||||
|
||||
function CMD.start(gate, fd)
|
||||
function CMD.start(conf)
|
||||
local fd = conf.client
|
||||
local gate = conf.gate
|
||||
WATCHDOG = conf.watchdog
|
||||
-- slot 1,2 set at main.lua
|
||||
host = sprotoloader.load(1):host "package"
|
||||
send_request = host:attach(sprotoloader.load(2))
|
||||
@@ -77,6 +85,11 @@ function CMD.start(gate, fd)
|
||||
skynet.call(gate, "lua", "forward", fd)
|
||||
end
|
||||
|
||||
function CMD.disconnect()
|
||||
-- todo: do something before exit
|
||||
skynet.exit()
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(_,_, command, ...)
|
||||
local f = CMD[command]
|
||||
|
||||
@@ -104,7 +104,11 @@ while true do
|
||||
dispatch_package()
|
||||
local cmd = socket.readstdin()
|
||||
if cmd then
|
||||
send_request("get", { what = cmd })
|
||||
if cmd == "quit" then
|
||||
send_request("quit")
|
||||
else
|
||||
send_request("get", { what = cmd })
|
||||
end
|
||||
else
|
||||
socket.usleep(100)
|
||||
end
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
local skynet = require "skynet"
|
||||
local cluster = require "cluster"
|
||||
require "skynet.manager" -- import skynet.name
|
||||
local snax = require "snax"
|
||||
|
||||
skynet.start(function()
|
||||
local sdb = skynet.newservice("simpledb")
|
||||
@@ -11,4 +12,6 @@ skynet.start(function()
|
||||
print(skynet.call(".simpledb", "lua", "GET", "b"))
|
||||
cluster.open "db"
|
||||
cluster.open "db2"
|
||||
-- unique snax service
|
||||
snax.uniqueservice "pingserver"
|
||||
end)
|
||||
|
||||
@@ -6,4 +6,8 @@ skynet.start(function()
|
||||
print(skynet.call(proxy, "lua", "GET", "a"))
|
||||
print(cluster.call("db", ".simpledb", "GET", "a"))
|
||||
print(cluster.call("db2", ".simpledb", "GET", "b"))
|
||||
|
||||
-- test snax service
|
||||
local pingserver = cluster.snax("db", "pingserver")
|
||||
print(pingserver.req.ping "hello")
|
||||
end)
|
||||
|
||||
@@ -6,4 +6,5 @@ bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||
luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||
lualoader = "lualib/loader.lua"
|
||||
cpath = "./cservice/?.so"
|
||||
cluster = "./examples/clustername.lua"
|
||||
cluster = "./examples/clustername.lua"
|
||||
snax = "./test/?.lua"
|
||||
|
||||
@@ -6,4 +6,5 @@ bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||
luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||
lualoader = "lualib/loader.lua"
|
||||
cpath = "./cservice/?.so"
|
||||
cluster = "./examples/clustername.lua"
|
||||
cluster = "./examples/clustername.lua"
|
||||
snax = "./test/?.lua"
|
||||
|
||||
@@ -30,6 +30,8 @@ set 3 {
|
||||
}
|
||||
}
|
||||
|
||||
quit 4 {}
|
||||
|
||||
]]
|
||||
|
||||
proto.s2c = sprotoparser.parse [[
|
||||
|
||||
@@ -9,14 +9,16 @@ 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)
|
||||
skynet.call(agent[fd], "lua", "start", { gate = gate, client = fd, watchdog = skynet.self() })
|
||||
end
|
||||
|
||||
local function close_agent(fd)
|
||||
local a = agent[fd]
|
||||
agent[fd] = nil
|
||||
if a then
|
||||
skynet.kill(a)
|
||||
agent[fd] = nil
|
||||
skynet.call(gate, "lua", "kick", fd)
|
||||
-- disconnect never return
|
||||
skynet.send(a, "lua", "disconnect")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,6 +39,10 @@ function CMD.start(conf)
|
||||
skynet.call(gate, "lua", "open" , conf)
|
||||
end
|
||||
|
||||
function CMD.close(fd)
|
||||
close_agent(fd)
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(session, source, cmd, subcmd, ...)
|
||||
if cmd == "socket" then
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
/* the eight DES S-boxes */
|
||||
|
||||
uint32_t SB1[64] = {
|
||||
static uint32_t SB1[64] = {
|
||||
0x01010400, 0x00000000, 0x00010000, 0x01010404,
|
||||
0x01010004, 0x00010404, 0x00000004, 0x00010000,
|
||||
0x00000400, 0x01010400, 0x01010404, 0x00000400,
|
||||
|
||||
@@ -59,6 +59,7 @@ channel_release(struct channel *c) {
|
||||
free(p);
|
||||
p = next;
|
||||
}
|
||||
free(c);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
@@ -210,6 +210,16 @@ push_more(lua_State *L, int fd, uint8_t *buffer, int size) {
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
close_uncomplete(lua_State *L, int fd) {
|
||||
struct queue *q = lua_touserdata(L,1);
|
||||
struct uncomplete * uc = find_uncomplete(q, fd);
|
||||
if (uc) {
|
||||
skynet_free(uc->pack.buffer);
|
||||
skynet_free(uc);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
filter_data_(lua_State *L, int fd, uint8_t * buffer, int size) {
|
||||
struct queue *q = lua_touserdata(L,1);
|
||||
@@ -343,6 +353,8 @@ lfilter(lua_State *L) {
|
||||
// ignore listen fd connect
|
||||
return 1;
|
||||
case SKYNET_SOCKET_TYPE_CLOSE:
|
||||
// no more data in fd (message->id)
|
||||
close_uncomplete(L, message->id);
|
||||
lua_pushvalue(L, lua_upvalueindex(TYPE_CLOSE));
|
||||
lua_pushinteger(L, message->id);
|
||||
return 3;
|
||||
@@ -353,6 +365,8 @@ lfilter(lua_State *L) {
|
||||
pushstring(L, buffer, size);
|
||||
return 4;
|
||||
case SKYNET_SOCKET_TYPE_ERROR:
|
||||
// no more data in fd (message->id)
|
||||
close_uncomplete(L, message->id);
|
||||
lua_pushvalue(L, lua_upvalueindex(TYPE_ERROR));
|
||||
lua_pushinteger(L, message->id);
|
||||
pushstring(L, buffer, size);
|
||||
|
||||
@@ -190,7 +190,10 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
luaL_addlstring(&b, current->msg + sb->offset, (sz - skip < bytes) ? sz - skip : bytes);
|
||||
int real_sz = sz - skip;
|
||||
if (real_sz > 0) {
|
||||
luaL_addlstring(&b, current->msg + sb->offset, (real_sz < bytes) ? real_sz : bytes);
|
||||
}
|
||||
return_free_node(L,2,sb);
|
||||
sz-=bytes;
|
||||
if (sz==0)
|
||||
|
||||
@@ -608,8 +608,8 @@ encode_default(const struct sproto_arg *args) {
|
||||
static int
|
||||
ldefault(lua_State *L) {
|
||||
int ret;
|
||||
// 32 is enough for dummy buffer, because ldefault encode nothing but the header.
|
||||
char dummy[32];
|
||||
// 64 is always enough for dummy buffer, except the type has many fields ( > 27).
|
||||
char dummy[64];
|
||||
struct sproto_type * st = lua_touserdata(L, 1);
|
||||
if (st == NULL) {
|
||||
return luaL_argerror(L, 1, "Need a sproto_type object");
|
||||
@@ -617,7 +617,18 @@ ldefault(lua_State *L) {
|
||||
lua_newtable(L);
|
||||
ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L);
|
||||
if (ret<0) {
|
||||
return luaL_error(L, "dummy buffer (%d) is too small", (int)sizeof(dummy));
|
||||
// try again
|
||||
int sz = sizeof(dummy) * 2;
|
||||
void * tmp = lua_newuserdata(L, sz);
|
||||
lua_insert(L, -2);
|
||||
for (;;) {
|
||||
ret = sproto_encode(st, tmp, sz, encode_default, L);
|
||||
if (ret >= 0)
|
||||
break;
|
||||
sz *= 2;
|
||||
tmp = lua_newuserdata(L, sz);
|
||||
lua_replace(L, -3);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,15 @@ function cluster.proxy(node, name)
|
||||
return skynet.call(clusterd, "lua", "proxy", node, name)
|
||||
end
|
||||
|
||||
function cluster.snax(node, name, address)
|
||||
local snax = require "snax"
|
||||
if not address then
|
||||
address = cluster.call(node, ".service", "QUERY", "snaxd" , name)
|
||||
end
|
||||
local handle = skynet.call(clusterd, "lua", "proxy", node, address)
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
skynet.init(function()
|
||||
clusterd = skynet.uniqueservice("clusterd")
|
||||
end)
|
||||
|
||||
@@ -88,10 +88,10 @@ local function verify_domain_name(name)
|
||||
if #name > MAX_DOMAIN_LEN then
|
||||
return false
|
||||
end
|
||||
if not name:match("^[%l%d-%.]+$") then
|
||||
if not name:match("^[_%l%d%-%.]+$") then
|
||||
return false
|
||||
end
|
||||
for w in name:gmatch("([%w-]+)%.?") do
|
||||
for w in name:gmatch("([_%w%-]+)%.?") do
|
||||
if #w > MAX_LABEL_LEN then
|
||||
return false
|
||||
end
|
||||
@@ -113,7 +113,7 @@ end
|
||||
|
||||
local function pack_question(name, qtype, qclass)
|
||||
local labels = {}
|
||||
for w in name:gmatch("([%w-]+)%.?") do
|
||||
for w in name:gmatch("([_%w%-]+)%.?") do
|
||||
table.insert(labels, string.pack("s1",w))
|
||||
end
|
||||
table.insert(labels, '\0')
|
||||
@@ -282,7 +282,7 @@ function dns.resolve(name, ipv6)
|
||||
qdcount = 1,
|
||||
}
|
||||
local req = pack_header(question_header) .. pack_question(name, qtype, QCLASS.IN)
|
||||
assert(dns_server, "Call dns.server fist")
|
||||
assert(dns_server, "Call dns.server first")
|
||||
socket.write(dns_server, req)
|
||||
return suspend(question_header.tid, name, qtype)
|
||||
end
|
||||
|
||||
@@ -174,7 +174,7 @@ function suspend(co, result, command, param, size)
|
||||
-- If the package is too large, returns nil. so we should report error back
|
||||
c.send(co_address, skynet.PTYPE_ERROR, co_session, "")
|
||||
end
|
||||
elseif size == nil then
|
||||
elseif size ~= nil then
|
||||
c.trash(param, size)
|
||||
ret = false
|
||||
end
|
||||
@@ -432,7 +432,7 @@ function skynet.dispatch_unknown_request(unknown)
|
||||
end
|
||||
|
||||
local function unknown_response(session, address, msg, sz)
|
||||
skynet.error(string.format("Response message :" , c.tostring(msg,sz)))
|
||||
skynet.error(string.format("Response message : %s" , c.tostring(msg,sz)))
|
||||
error(string.format("Unknown session : %d from %x", session, address))
|
||||
end
|
||||
|
||||
@@ -450,6 +450,7 @@ function skynet.fork(func,...)
|
||||
func(tunpack(args))
|
||||
end)
|
||||
table.insert(fork_queue, co)
|
||||
return co
|
||||
end
|
||||
|
||||
local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
|
||||
@@ -479,7 +480,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
|
||||
session_coroutine_address[co] = source
|
||||
suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...)))
|
||||
else
|
||||
unknown_request(session, source, msg, sz, proto[prototype])
|
||||
unknown_request(session, source, msg, sz, proto[prototype].name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -44,7 +44,7 @@ return function(skynet, source, filename , ...)
|
||||
if proto then
|
||||
for k,v in pairs(proto) do
|
||||
local name, dispatch = v.name, v.dispatch
|
||||
if name and dispatch then
|
||||
if name and dispatch and not p[name] then
|
||||
local pp = {}
|
||||
p[name] = pp
|
||||
getupvaluetable(pp, dispatch, unique)
|
||||
|
||||
@@ -64,7 +64,7 @@ return function (name , G, loader)
|
||||
local pattern
|
||||
|
||||
do
|
||||
local path = skynet.getenv "snax"
|
||||
local path = assert(skynet.getenv "snax" , "please set snax in config file")
|
||||
|
||||
local errlist = {}
|
||||
|
||||
|
||||
@@ -135,7 +135,7 @@ _ctrl(struct gate * g, const void * msg, int sz) {
|
||||
skynet_socket_start(ctx, g->listen_id);
|
||||
return;
|
||||
}
|
||||
if (memcmp(command, "close", i) == 0) {
|
||||
if (memcmp(command, "close", i) == 0) {
|
||||
if (g->listen_id >= 0) {
|
||||
skynet_socket_close(ctx, g->listen_id);
|
||||
g->listen_id = -1;
|
||||
|
||||
@@ -365,6 +365,7 @@ dispatch_name_queue(struct harbor *h, struct keyvalue * node) {
|
||||
while ((m = pop_queue(queue)) != NULL) {
|
||||
m->header.destination |= (handle & HANDLE_MASK);
|
||||
send_remote(context, fd, m->buffer, m->size, &m->header);
|
||||
skynet_free(m->buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,6 +382,7 @@ dispatch_queue(struct harbor *h, int id) {
|
||||
struct harbor_msg * m;
|
||||
while ((m = pop_queue(queue)) != NULL) {
|
||||
send_remote(h->ctx, fd, m->buffer, m->size, &m->header);
|
||||
skynet_free(m->buffer);
|
||||
}
|
||||
release_queue(queue);
|
||||
s->queue = NULL;
|
||||
|
||||
@@ -20,6 +20,7 @@ local function open_channel(t, key)
|
||||
host = host,
|
||||
port = tonumber(port),
|
||||
response = read_response,
|
||||
nodelay = true,
|
||||
}
|
||||
assert(c:connect(true))
|
||||
t[key] = c
|
||||
|
||||
@@ -11,6 +11,7 @@ skynet.register_protocol {
|
||||
}
|
||||
|
||||
local forward_map = {
|
||||
[skynet.PTYPE_SNAX] = skynet.PTYPE_SYSTEM,
|
||||
[skynet.PTYPE_LUA] = skynet.PTYPE_SYSTEM,
|
||||
[skynet.PTYPE_RESPONSE] = skynet.PTYPE_RESPONSE, -- don't free response message
|
||||
}
|
||||
|
||||
@@ -1,13 +1,26 @@
|
||||
local skynet = require "skynet"
|
||||
local snax = require "snax"
|
||||
local socket = require "socket"
|
||||
|
||||
local function split_cmdline(cmdline)
|
||||
local split = {}
|
||||
for i in string.gmatch(cmdline, "%S+") do
|
||||
table.insert(split,i)
|
||||
end
|
||||
return split
|
||||
end
|
||||
|
||||
local function console_main_loop()
|
||||
local stdin = socket.stdin()
|
||||
socket.lock(stdin)
|
||||
while true do
|
||||
local cmdline = socket.readline(stdin, "\n")
|
||||
if cmdline ~= "" then
|
||||
pcall(skynet.newservice,cmdline)
|
||||
local split = split_cmdline(cmdline)
|
||||
local command = split[1]
|
||||
if command == "snax" then
|
||||
pcall(snax.newservice, select(2, table.unpack(split)))
|
||||
elseif cmdline ~= "" then
|
||||
pcall(skynet.newservice, cmdline)
|
||||
end
|
||||
end
|
||||
socket.unlock(stdin)
|
||||
|
||||
@@ -171,7 +171,7 @@ end
|
||||
|
||||
local function adjust_address(address)
|
||||
if address:sub(1,1) ~= ":" then
|
||||
address = tonumber("0x" .. address) | (skynet.harbor(skynet.self()) << 24)
|
||||
address = assert(tonumber("0x" .. address), "Need an address") | (skynet.harbor(skynet.self()) << 24)
|
||||
end
|
||||
return address
|
||||
end
|
||||
|
||||
@@ -54,7 +54,7 @@ end
|
||||
|
||||
skynet.start(function()
|
||||
local init = false
|
||||
skynet.dispatch("snax", function ( session , source , id, ...)
|
||||
local function dispatcher( session , source , id, ...)
|
||||
local method = func[id]
|
||||
|
||||
if method[2] == "system" then
|
||||
@@ -84,5 +84,11 @@ skynet.start(function()
|
||||
assert(init, "Init first")
|
||||
timing(method, ...)
|
||||
end
|
||||
end)
|
||||
end
|
||||
skynet.dispatch("snax", dispatcher)
|
||||
|
||||
-- set lua dispatcher
|
||||
function snax.enablecluster()
|
||||
skynet.dispatch("lua", dispatcher)
|
||||
end
|
||||
end)
|
||||
|
||||
@@ -8,6 +8,7 @@ struct skynet_config {
|
||||
const char * module_path;
|
||||
const char * bootstrap;
|
||||
const char * logger;
|
||||
const char * logservice;
|
||||
};
|
||||
|
||||
#define THREAD_WORKER 0
|
||||
|
||||
@@ -133,6 +133,7 @@ main(int argc, char *argv[]) {
|
||||
config.bootstrap = optstring("bootstrap","snlua bootstrap");
|
||||
config.daemon = optstring("daemon", NULL);
|
||||
config.logger = optstring("logger", NULL);
|
||||
config.logservice = optstring("logservice", "logger");
|
||||
|
||||
lua_close(L);
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ skynet_current_handle(void) {
|
||||
void * handle = pthread_getspecific(G_NODE.handle_key);
|
||||
return (uint32_t)(uintptr_t)handle;
|
||||
} else {
|
||||
uintptr_t v = (uint32_t)(-THREAD_MAIN);
|
||||
uint32_t v = (uint32_t)(-THREAD_MAIN);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,9 +223,9 @@ skynet_start(struct skynet_config * config) {
|
||||
skynet_timer_init();
|
||||
skynet_socket_init();
|
||||
|
||||
struct skynet_context *ctx = skynet_context_new("logger", config->logger);
|
||||
struct skynet_context *ctx = skynet_context_new(config->logservice, config->logger);
|
||||
if (ctx == NULL) {
|
||||
fprintf(stderr, "Can't launch logger service\n");
|
||||
fprintf(stderr, "Can't launch %s service\n", config->logservice);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ end
|
||||
|
||||
function init( ... )
|
||||
print ("ping server start:", ...)
|
||||
snax.enablecluster() -- enable cluster call
|
||||
-- init queue
|
||||
lock = queue()
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user