mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
1. none blocking connect ; 2. socket.readall ; 3.socket.readline will return last line ; 4. lua service report launcher error
This commit is contained in:
@@ -11,6 +11,7 @@ local READREQUEST = {} -- fd:request_size
|
|||||||
local READSESSION = {} -- fd:session
|
local READSESSION = {} -- fd:session
|
||||||
local READLOCK = {} -- fd:queue(session)
|
local READLOCK = {} -- fd:queue(session)
|
||||||
local READTHREAD= {} -- fd:thread
|
local READTHREAD= {} -- fd:thread
|
||||||
|
local CLOSED = {} -- fd:true
|
||||||
|
|
||||||
local selfaddr = skynet.self()
|
local selfaddr = skynet.self()
|
||||||
|
|
||||||
@@ -27,16 +28,17 @@ skynet.register_protocol {
|
|||||||
local qsz = READREQUEST[fd]
|
local qsz = READREQUEST[fd]
|
||||||
local buf = READBUF[fd]
|
local buf = READBUF[fd]
|
||||||
local bsz
|
local bsz
|
||||||
if sz == 0 or buf == true then
|
if sz == 0 then
|
||||||
buf,bsz = true, qsz
|
CLOSED[fd] = true
|
||||||
else
|
else
|
||||||
buf,bsz = buffer.push(buf, msg, sz)
|
buf,bsz = buffer.push(buf, msg, sz)
|
||||||
end
|
|
||||||
READBUF[fd] = buf
|
READBUF[fd] = buf
|
||||||
|
end
|
||||||
local session = READSESSION[fd]
|
local session = READSESSION[fd]
|
||||||
if qsz == nil or session == nil then
|
if qsz == nil or session == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
if sz > 0 then
|
||||||
if type(qsz) == "number" then
|
if type(qsz) == "number" then
|
||||||
if qsz > bsz then
|
if qsz > bsz then
|
||||||
return
|
return
|
||||||
@@ -47,6 +49,7 @@ skynet.register_protocol {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
response(session)
|
response(session)
|
||||||
READSESSION[fd] = nil
|
READSESSION[fd] = nil
|
||||||
@@ -64,7 +67,7 @@ skynet.register_protocol {
|
|||||||
if t > 0 then -- lock request when t == 0
|
if t > 0 then -- lock request when t == 0
|
||||||
-- request bytes or readline
|
-- request bytes or readline
|
||||||
local buf = READBUF[fd]
|
local buf = READBUF[fd]
|
||||||
if buf == true then
|
if CLOSED[fd] then
|
||||||
skynet.ret()
|
skynet.ret()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -75,7 +78,7 @@ skynet.register_protocol {
|
|||||||
skynet.ret()
|
skynet.ret()
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
else
|
elseif t == 2 then
|
||||||
-- sz is sep
|
-- sz is sep
|
||||||
if buffer.readline(buf, sz, true) then -- don't real read
|
if buffer.readline(buf, sz, true) then -- don't real read
|
||||||
skynet.ret()
|
skynet.ret()
|
||||||
@@ -95,7 +98,10 @@ function socket.open(addr, port)
|
|||||||
if r == "" then
|
if r == "" then
|
||||||
error(cmd .. " failed")
|
error(cmd .. " failed")
|
||||||
end
|
end
|
||||||
return tonumber(r)
|
local fd = tonumber(r)
|
||||||
|
READBUF[fd] = true
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
return fd
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.stdin()
|
function socket.stdin()
|
||||||
@@ -103,42 +109,112 @@ function socket.stdin()
|
|||||||
if r == "" then
|
if r == "" then
|
||||||
error("stdin bind failed")
|
error("stdin bind failed")
|
||||||
end
|
end
|
||||||
return tonumber(r)
|
local fd = tonumber(r)
|
||||||
|
READBUF[fd] = true
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
return fd
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.close(fd)
|
function socket.close(fd)
|
||||||
socket.lock(fd)
|
socket.lock(fd)
|
||||||
skynet.call(".socket", "text", "close", fd)
|
skynet.call(".socket", "text", "close", fd)
|
||||||
READBUF[fd] = true
|
READBUF[fd] = nil
|
||||||
READLOCK[fd] = nil
|
READLOCK[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.read(fd, sz)
|
function socket.read(fd, sz)
|
||||||
local str = buffer.pop(READBUF[fd],sz)
|
local buf = assert(READBUF[fd])
|
||||||
|
local str, bytes = buffer.pop(buf,sz)
|
||||||
if str then
|
if str then
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if CLOSED[fd] then
|
||||||
|
READBUF[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
str = buffer.pop(buf, bytes)
|
||||||
|
return nil, str
|
||||||
|
end
|
||||||
|
|
||||||
READREQUEST[fd] = sz
|
READREQUEST[fd] = sz
|
||||||
skynet.call(selfaddr, "system",fd,1,sz) -- singal size 1
|
skynet.call(selfaddr, "system",fd,1,sz) -- singal size 1
|
||||||
READREQUEST[fd] = nil
|
READREQUEST[fd] = nil
|
||||||
|
|
||||||
str = buffer.pop(READBUF[fd],sz)
|
buf = READBUF[fd]
|
||||||
|
|
||||||
|
str, bytes = buffer.pop(buf,sz)
|
||||||
|
if str then
|
||||||
return str
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
if CLOSED[fd] then
|
||||||
|
READBUF[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
str = buffer.pop(buf, bytes)
|
||||||
|
return nil, str
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function socket.readall(fd)
|
||||||
|
local buf = assert(READBUF[fd])
|
||||||
|
if CLOSED[fd] then
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
READBUF[fd] = nil
|
||||||
|
if buf == nil then
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
local _, bytes = buffer.push(buf)
|
||||||
|
local ret = buffer.pop(buf, bytes)
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
READREQUEST[fd] = math.huge
|
||||||
|
skynet.call(selfaddr, "system",fd,3) -- singal readall
|
||||||
|
READREQUEST[fd] = nil
|
||||||
|
assert(CLOSED[fd])
|
||||||
|
buf = READBUF[fd]
|
||||||
|
READBUF[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
if buf == nil then
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
local _, bytes = buffer.push(buf)
|
||||||
|
local ret = buffer.pop(buf, bytes)
|
||||||
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.readline(fd, sep)
|
function socket.readline(fd, sep)
|
||||||
local str = buffer.readline(READBUF[fd],sep)
|
local buf = assert(READBUF[fd])
|
||||||
|
local str = buffer.readline(buf,sep)
|
||||||
if str then
|
if str then
|
||||||
return str
|
return str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if CLOSED[fd] then
|
||||||
|
READBUF[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
local _, bytes = buffer.push(buf)
|
||||||
|
str = buffer.pop(buf, bytes)
|
||||||
|
return nil, str
|
||||||
|
end
|
||||||
|
|
||||||
READREQUEST[fd] = sep
|
READREQUEST[fd] = sep
|
||||||
skynet.call(selfaddr, "system",fd,2,sep) -- singal sep 2
|
skynet.call(selfaddr, "system",fd,2,sep) -- singal sep 2
|
||||||
READREQUEST[fd] = nil
|
READREQUEST[fd] = nil
|
||||||
|
|
||||||
str = buffer.readline(READBUF[fd],sep)
|
buf = READBUF[fd]
|
||||||
|
str = buffer.readline(buf,sep)
|
||||||
|
if str then
|
||||||
return str
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
if CLOSED[fd] then
|
||||||
|
READBUF[fd] = nil
|
||||||
|
CLOSED[fd] = nil
|
||||||
|
local _, bytes = buffer.push(buf)
|
||||||
|
str = buffer.pop(buf, bytes)
|
||||||
|
return nil, str
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.write(fd, msg, sz)
|
function socket.write(fd, msg, sz)
|
||||||
|
|||||||
@@ -117,6 +117,12 @@ _report_error(lua_State *L, struct skynet_context *ctx, const char *filename, in
|
|||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_report_launcher_error(struct skynet_context *ctx) {
|
||||||
|
// sizeof "ERROR" == 5
|
||||||
|
skynet_sendname(ctx, ".launcher", PTYPE_TEXT, 0, "ERROR", 5);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
||||||
lua_State *L = l->L;
|
lua_State *L = l->L;
|
||||||
@@ -144,6 +150,7 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
|||||||
} else {
|
} else {
|
||||||
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
|
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
|
||||||
}
|
}
|
||||||
|
_report_launcher_error(ctx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int n=0;
|
int n=0;
|
||||||
@@ -162,6 +169,7 @@ _init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_report_error(L, ctx, filename, r);
|
_report_error(L, ctx, filename, r);
|
||||||
|
_report_launcher_error(ctx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,9 @@
|
|||||||
#define READ_BUFFER 4000
|
#define READ_BUFFER 4000
|
||||||
|
|
||||||
#define STATUS_INVALID 0
|
#define STATUS_INVALID 0
|
||||||
#define STATUS_HALFCLOSE 1
|
#define STATUS_CONNECT 1
|
||||||
#define STATUS_SUSPEND 2
|
#define STATUS_HALFCLOSE 2
|
||||||
|
#define STATUS_SUSPEND 3
|
||||||
|
|
||||||
struct write_buffer {
|
struct write_buffer {
|
||||||
struct write_buffer * next;
|
struct write_buffer * next;
|
||||||
@@ -66,7 +67,7 @@ _set_nonblocking(int fd)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
new_socket(struct socket_pool *p, int sock, uint32_t addr) {
|
new_socket(struct socket_pool *p, int sock, uint32_t addr, int session, bool connecting) {
|
||||||
int i;
|
int i;
|
||||||
if (p->count >= p->cap) {
|
if (p->count >= p->cap) {
|
||||||
goto _error;
|
goto _error;
|
||||||
@@ -79,13 +80,18 @@ new_socket(struct socket_pool *p, int sock, uint32_t addr) {
|
|||||||
if (event_add(p->fd, sock, s)) {
|
if (event_add(p->fd, sock, s)) {
|
||||||
goto _error;
|
goto _error;
|
||||||
}
|
}
|
||||||
|
if (connecting) {
|
||||||
|
event_write(p->fd, sock, s, true);
|
||||||
|
s->status = STATUS_CONNECT;
|
||||||
|
} else {
|
||||||
s->status = STATUS_SUSPEND;
|
s->status = STATUS_SUSPEND;
|
||||||
_set_nonblocking(sock);
|
}
|
||||||
int keepalive = 1;
|
int keepalive = 1;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive));
|
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepalive , sizeof(keepalive));
|
||||||
s->fd = sock;
|
s->fd = sock;
|
||||||
s->id = id;
|
s->id = id;
|
||||||
s->source = addr;
|
s->source = addr;
|
||||||
|
s->session = session;
|
||||||
p->count++;
|
p->count++;
|
||||||
p->id = id + 1;
|
p->id = id + 1;
|
||||||
if (p->id > MAX_ID) {
|
if (p->id > MAX_ID) {
|
||||||
@@ -101,9 +107,11 @@ _error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
cmd_bind(struct skynet_context * ctx, struct socket_pool *p, int sock, int session, uint32_t source) {
|
cmd_bind(struct skynet_context * ctx, struct socket_pool *p, int sock, int session, uint32_t source, bool connecting) {
|
||||||
char ret[10];
|
if (!connecting) {
|
||||||
int id = new_socket(p, sock, source);
|
_set_nonblocking(sock);
|
||||||
|
}
|
||||||
|
int id = new_socket(p, sock, source, session, connecting);
|
||||||
if (id<0) {
|
if (id<0) {
|
||||||
reply(ctx, source, session, NULL , 0);
|
reply(ctx, source, session, NULL , 0);
|
||||||
return;
|
return;
|
||||||
@@ -111,8 +119,6 @@ cmd_bind(struct skynet_context * ctx, struct socket_pool *p, int sock, int sessi
|
|||||||
if (p->count == 1) {
|
if (p->count == 1) {
|
||||||
skynet_command(ctx, "TIMEOUT", "0");
|
skynet_command(ctx, "TIMEOUT", "0");
|
||||||
}
|
}
|
||||||
int sz = sprintf(ret,"%d",id);
|
|
||||||
reply(ctx, source, session, ret, sz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -142,8 +148,9 @@ cmd_open(struct skynet_context * ctx, struct socket_pool *p, char * cmd, int ses
|
|||||||
if ( sock < 0 ) {
|
if ( sock < 0 ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
_set_nonblocking(sock);
|
||||||
status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
|
status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
|
||||||
if ( status != 0 ) {
|
if ( status != 0 && errno != EINPROGRESS) {
|
||||||
close(sock);
|
close(sock);
|
||||||
sock = -1;
|
sock = -1;
|
||||||
continue;
|
continue;
|
||||||
@@ -157,7 +164,7 @@ cmd_open(struct skynet_context * ctx, struct socket_pool *p, char * cmd, int ses
|
|||||||
goto _failed;
|
goto _failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd_bind(ctx, p, sock, session, source);
|
cmd_bind(ctx, p, sock, session, source, true);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
_failed:
|
_failed:
|
||||||
@@ -202,6 +209,13 @@ cmd_close(struct skynet_context * ctx, struct socket_pool *p, int id, int sessio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_reply_bind(struct skynet_context * ctx, int sock, int session, uint32_t source) {
|
||||||
|
char ret[10];
|
||||||
|
int sz = sprintf(ret,"%d",sock);
|
||||||
|
reply(ctx, source, session, ret, sz);
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id, char * arg, int session, uint32_t source) {
|
_ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id, char * arg, int session, uint32_t source) {
|
||||||
if (strcmp(command, "open")==0) {
|
if (strcmp(command, "open")==0) {
|
||||||
@@ -209,7 +223,8 @@ _ctrl(struct skynet_context * ctx, struct socket_pool *p, char * command, int id
|
|||||||
} else if (strcmp(command, "close")==0) {
|
} else if (strcmp(command, "close")==0) {
|
||||||
cmd_close(ctx, p, id, session, source);
|
cmd_close(ctx, p, id, session, source);
|
||||||
} else if (strcmp(command, "bind")==0) {
|
} else if (strcmp(command, "bind")==0) {
|
||||||
cmd_bind(ctx, p, id, session, source);
|
cmd_bind(ctx, p, id, session, source, false);
|
||||||
|
_reply_bind(ctx, id, session, source);
|
||||||
} else {
|
} else {
|
||||||
skynet_error(ctx, "Unknown command %s", command);
|
skynet_error(ctx, "Unknown command %s", command);
|
||||||
reply(ctx, source, session, NULL, 0);
|
reply(ctx, source, session, NULL, 0);
|
||||||
@@ -316,7 +331,7 @@ try_send(struct skynet_context *ctx, struct socket_pool *p, uint32_t source, con
|
|||||||
skynet_error(ctx, "%x try to write socket %d:%x", source, id, s->source);
|
skynet_error(ctx, "%x try to write socket %d:%x", source, id, s->source);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if (s->status != STATUS_SUSPEND) {
|
if (s->status != STATUS_SUSPEND && s->status != STATUS_CONNECT) {
|
||||||
skynet_error(ctx, "%x write to closed socket %d", source, id);
|
skynet_error(ctx, "%x write to closed socket %d", source, id);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -335,6 +350,7 @@ try_send(struct skynet_context *ctx, struct socket_pool *p, uint32_t source, con
|
|||||||
|
|
||||||
char * ptr = (char *)(msg+1);
|
char * ptr = (char *)(msg+1);
|
||||||
|
|
||||||
|
if (s->status != STATUS_CONNECT) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int wt = write(s->fd, ptr, sz);
|
int wt = write(s->fd, ptr, sz);
|
||||||
if (wt < 0) {
|
if (wt < 0) {
|
||||||
@@ -352,6 +368,7 @@ try_send(struct skynet_context *ctx, struct socket_pool *p, uint32_t source, con
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct write_buffer * buf = malloc(sizeof(*buf));
|
struct write_buffer * buf = malloc(sizeof(*buf));
|
||||||
buf->next = NULL;
|
buf->next = NULL;
|
||||||
@@ -384,6 +401,19 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
|
|||||||
int i;
|
int i;
|
||||||
for (i=0;i<n;i++) {
|
for (i=0;i<n;i++) {
|
||||||
struct event *e = &p->ev[i];
|
struct event *e = &p->ev[i];
|
||||||
|
struct socket *s= e->s;
|
||||||
|
if (s->status == STATUS_CONNECT) {
|
||||||
|
int error;
|
||||||
|
socklen_t len = sizeof(error);
|
||||||
|
int code = getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
||||||
|
if (code < 0 || error) {
|
||||||
|
force_close(s,p);
|
||||||
|
reply(context, s->source, s->session, NULL , 0);
|
||||||
|
} else {
|
||||||
|
_reply_bind(context, s->id, s->session, s->source);
|
||||||
|
s->status = STATUS_SUSPEND;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (e->read) {
|
if (e->read) {
|
||||||
forward(context, e->s, p);
|
forward(context, e->s, p);
|
||||||
}
|
}
|
||||||
@@ -396,6 +426,7 @@ _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
skynet_command(context, "TIMEOUT", "0");
|
skynet_command(context, "TIMEOUT", "0");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,6 +85,14 @@ skynet.dispatch("text" , function(session, address , cmd)
|
|||||||
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
|
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
|
||||||
instance[address] = nil
|
instance[address] = nil
|
||||||
end
|
end
|
||||||
|
elseif cmd == "ERROR" then
|
||||||
|
-- see serivce-src/service_lua.c
|
||||||
|
-- init failed
|
||||||
|
local reply = instance[address]
|
||||||
|
if reply then
|
||||||
|
skynet.redirect(reply.address , 0, "response", reply.session, "")
|
||||||
|
instance[address] = nil
|
||||||
|
end
|
||||||
else
|
else
|
||||||
-- launch request
|
-- launch request
|
||||||
local service, param = string.match(cmd,"([^ ]+) (.*)")
|
local service, param = string.match(cmd,"([^ ]+) (.*)")
|
||||||
|
|||||||
Reference in New Issue
Block a user