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:
云风
2013-07-22 21:09:38 +08:00
parent 5516eca93a
commit 300300853b
4 changed files with 181 additions and 58 deletions

View File

@@ -11,6 +11,7 @@ local READREQUEST = {} -- fd:request_size
local READSESSION = {} -- fd:session
local READLOCK = {} -- fd:queue(session)
local READTHREAD= {} -- fd:thread
local CLOSED = {} -- fd:true
local selfaddr = skynet.self()
@@ -27,24 +28,26 @@ skynet.register_protocol {
local qsz = READREQUEST[fd]
local buf = READBUF[fd]
local bsz
if sz == 0 or buf == true then
buf,bsz = true, qsz
if sz == 0 then
CLOSED[fd] = true
else
buf,bsz = buffer.push(buf, msg, sz)
READBUF[fd] = buf
end
READBUF[fd] = buf
local session = READSESSION[fd]
if qsz == nil or session == nil then
return
end
if type(qsz) == "number" then
if qsz > bsz then
return
end
else
-- request readline
if buffer.readline(buf, qsz, true) == nil then
return
if sz > 0 then
if type(qsz) == "number" then
if qsz > bsz then
return
end
else
-- request readline
if buffer.readline(buf, qsz, true) == nil then
return
end
end
end
@@ -64,7 +67,7 @@ skynet.register_protocol {
if t > 0 then -- lock request when t == 0
-- request bytes or readline
local buf = READBUF[fd]
if buf == true then
if CLOSED[fd] then
skynet.ret()
return
end
@@ -75,7 +78,7 @@ skynet.register_protocol {
skynet.ret()
return
end
else
elseif t == 2 then
-- sz is sep
if buffer.readline(buf, sz, true) then -- don't real read
skynet.ret()
@@ -95,7 +98,10 @@ function socket.open(addr, port)
if r == "" then
error(cmd .. " failed")
end
return tonumber(r)
local fd = tonumber(r)
READBUF[fd] = true
CLOSED[fd] = nil
return fd
end
function socket.stdin()
@@ -103,42 +109,112 @@ function socket.stdin()
if r == "" then
error("stdin bind failed")
end
return tonumber(r)
local fd = tonumber(r)
READBUF[fd] = true
CLOSED[fd] = nil
return fd
end
function socket.close(fd)
socket.lock(fd)
skynet.call(".socket", "text", "close", fd)
READBUF[fd] = true
READBUF[fd] = nil
READLOCK[fd] = nil
CLOSED[fd] = nil
end
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
return str
end
if CLOSED[fd] then
READBUF[fd] = nil
CLOSED[fd] = nil
str = buffer.pop(buf, bytes)
return nil, str
end
READREQUEST[fd] = sz
skynet.call(selfaddr, "system",fd,1,sz) -- singal size 1
READREQUEST[fd] = nil
str = buffer.pop(READBUF[fd],sz)
return str
buf = READBUF[fd]
str, bytes = buffer.pop(buf,sz)
if str then
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
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
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
READREQUEST[fd] = sep
skynet.call(selfaddr, "system",fd,2,sep) -- singal sep 2
READREQUEST[fd] = nil
str = buffer.readline(READBUF[fd],sep)
return str
buf = READBUF[fd]
str = buffer.readline(buf,sep)
if str then
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
function socket.write(fd, msg, sz)