mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
bugfix: socket read lock/unlock
This commit is contained in:
@@ -61,20 +61,7 @@ skynet.register_protocol {
|
|||||||
dispatch = function (session, addr, msg, sz)
|
dispatch = function (session, addr, msg, sz)
|
||||||
fd, t, sz = skynet.unpack(msg,sz)
|
fd, t, sz = skynet.unpack(msg,sz)
|
||||||
assert(addr == selfaddr, "PTYPE_SYSTEM message must send by self")
|
assert(addr == selfaddr, "PTYPE_SYSTEM message must send by self")
|
||||||
if t == 0 then
|
if t > 0 then -- lock request when t == 0
|
||||||
-- lock fd
|
|
||||||
if READTHREAD[fd] == nil then
|
|
||||||
skynet.ret()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local q = READLOCK[fd]
|
|
||||||
if q == nil then
|
|
||||||
READLOCK[fd] = { session }
|
|
||||||
else
|
|
||||||
table.insert(q, session)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- request bytes or readline
|
-- request bytes or readline
|
||||||
local buf = READBUF[fd]
|
local buf = READBUF[fd]
|
||||||
if buf == true then
|
if buf == true then
|
||||||
@@ -127,8 +114,6 @@ function socket.close(fd)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function socket.read(fd, sz)
|
function socket.read(fd, sz)
|
||||||
assert(coroutine.running() == READTHREAD[fd], "call socket.lock first")
|
|
||||||
|
|
||||||
local str = buffer.pop(READBUF[fd],sz)
|
local str = buffer.pop(READBUF[fd],sz)
|
||||||
if str then
|
if str then
|
||||||
return str
|
return str
|
||||||
@@ -143,8 +128,6 @@ function socket.read(fd, sz)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function socket.readline(fd, sep)
|
function socket.readline(fd, sep)
|
||||||
assert(coroutine.running() == READTHREAD[fd], "call socket.lock first")
|
|
||||||
|
|
||||||
local str = buffer.readline(READBUF[fd],sep)
|
local str = buffer.readline(READBUF[fd],sep)
|
||||||
if str then
|
if str then
|
||||||
return str
|
return str
|
||||||
@@ -163,13 +146,22 @@ function socket.write(fd, msg, sz)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function socket.lock(fd)
|
function socket.lock(fd)
|
||||||
local lt = READTHREAD[fd]
|
local locked = READTHREAD[fd]
|
||||||
local ct = coroutine.running()
|
if locked then
|
||||||
if lt then
|
-- lock fd
|
||||||
assert(lt ~= ct, "already lock")
|
local session = skynet.genid()
|
||||||
skynet.call(selfaddr, "system",fd,0)
|
local q = READLOCK[fd]
|
||||||
|
if q == nil then
|
||||||
|
READLOCK[fd] = { session }
|
||||||
|
else
|
||||||
|
table.insert(q, session)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.redirect(selfaddr , 0, "system", session, skynet.pack(fd,0))
|
||||||
|
coroutine.yield("CALL",session)
|
||||||
|
else
|
||||||
|
READTHREAD[fd] = true
|
||||||
end
|
end
|
||||||
READTHREAD[fd] = ct
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.unlock(fd)
|
function socket.unlock(fd)
|
||||||
@@ -177,10 +169,10 @@ function socket.unlock(fd)
|
|||||||
local q = READLOCK[fd]
|
local q = READLOCK[fd]
|
||||||
if q then
|
if q then
|
||||||
if q[1] then
|
if q[1] then
|
||||||
|
READTHREAD[fd] = true
|
||||||
response(q[1])
|
response(q[1])
|
||||||
end
|
table.remove(q,1)
|
||||||
table.remove(q,1)
|
else
|
||||||
if q[1] == nil then
|
|
||||||
READLOCK[fd] = nil
|
READLOCK[fd] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user