Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Aug 8, 2016
1 parent 23c4421 commit 18569e3
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 387 deletions.
3 changes: 3 additions & 0 deletions lualib/gate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ function gate.send(fd, ...)
if sc == nil then
return false
end
if sc.__close then
return false
end
if sc.pack then
d, sz = sc.pack(...)
else
Expand Down
36 changes: 21 additions & 15 deletions lualib/silly/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ local core = {}
local tinsert = table.insert
local tremove = table.remove

local corunning = coroutine.running
local coyield = coroutine.yield
local coresume = coroutine.resume
coroutine.running = nil
coroutine.yield = nil
coroutine.resume = nil

--coroutine pool will be dynamic size
--so use the weaktable
local copool = {}
Expand All @@ -15,19 +22,19 @@ setmetatable(copool, weakmt)
local function cocreate(f)
local co = table.remove(copool)
if co then
coroutine.resume(co, "STARTUP", f)
coresume(co, "STARTUP", f)
return co
end

local function cocall()
while true do
local ret, func = coroutine.yield("EXIT")
local ret, func = coyield("EXIT")
if ret ~= "STARTUP" then
print("create coroutine fail", ret)
print(debug.traceback())
return
end
local ok, err = core.pcall(func, coroutine.yield())
local ok, err = core.pcall(func, coyield())
if ok == false then
print("cocall", err)
print(debug.traceback())
Expand All @@ -36,8 +43,8 @@ local function cocreate(f)
end

co = coroutine.create(cocall)
coroutine.resume(co) --wakeup the new coroutine
coroutine.resume(co, "STARTUP", f) --pass the function handler
coresume(co) --wakeup the new coroutine
coresume(co, "STARTUP", f) --pass the function handler
if #copool > 100 then
print("coroutine pool large than 100", #copool)
end
Expand All @@ -50,7 +57,7 @@ end
core.udpwrite = function(fd, p, sz, addr)
return silly.udpsend(fd, p, sz, addr) == 0
end
core.running = coroutine.running
core.running = corunning
core.quit = silly.quit
core.tostring = silly.tostring
core.genid = silly.genid
Expand Down Expand Up @@ -132,7 +139,7 @@ function dispatch_wakeup()
if not param then
param = {}
end
waityield(co, coroutine.resume(co, "WAKEUP", table.unpack(param)))
waityield(co, coresume(co, "WAKEUP", table.unpack(param)))
end

function core.fork(func)
Expand All @@ -144,12 +151,12 @@ function core.fork(func)
end

function core.wait()
local co = coroutine.running()
local co = corunning()
assert(wakeup_co_status[co] == nil)
assert(sleep_co_session[co] == nil)
assert(wait_co_status[co] == nil)
wait_co_status[co] = "WAIT"
return waitresume(co, coroutine.yield("WAIT"))
return waitresume(co, coyield("WAIT"))
end

function core.wakeup(co, ...)
Expand All @@ -161,11 +168,11 @@ function core.wakeup(co, ...)
end

function core.sleep(ms)
local co = coroutine.running()
local co = corunning()
local session = silly.timeout(ms)
sleep_session_co[session] = co
sleep_co_session[co] = session
waitresume(co, coroutine.yield("SLEEP"))
waitresume(co, coyield("SLEEP"))
end

function core.timeout(ms, func)
Expand All @@ -178,7 +185,7 @@ end

function core.start(func, ...)
local co = cocreate(func)
waityield(co, coroutine.resume(co, ...))
waityield(co, coresume(co, ...))
end


Expand Down Expand Up @@ -248,7 +255,7 @@ local function doconnect(ip, dispatch, bind, dofunc)
return nil
end
assert(socket_connect[fd] == nil)
socket_connect[fd] = coroutine.running()
socket_connect[fd] = corunning()
local ok = core.wait()
socket_connect[fd] = nil
if ok ~= true then
Expand All @@ -260,7 +267,6 @@ local function doconnect(ip, dispatch, bind, dofunc)
end

function core.connect(ip, dispatch, bind)

return doconnect(ip, dispatch, bind, silly.connect)
end

Expand Down Expand Up @@ -348,7 +354,7 @@ local function dispatch(type, fd, message, ...)
--check if the socket has closed
if dispatch then --have ready close
local co = cocreate(dispatch)
waityield(co, coroutine.resume(co, type, fd, message, ...))
waityield(co, coresume(co, type, fd, message, ...))
end
dispatch_wakeup()
end
Expand Down
70 changes: 0 additions & 70 deletions lualib/spacker.lua

This file was deleted.

82 changes: 0 additions & 82 deletions silly-src/silly_server.c

This file was deleted.

23 changes: 0 additions & 23 deletions silly-src/silly_server.h

This file was deleted.

Loading

0 comments on commit 18569e3

Please sign in to comment.