优化ws模块性能(函数local化)

This commit is contained in:
MrZ626
2021-06-19 21:41:58 +08:00
parent e381f66767
commit d96f45c3b3

View File

@@ -15,20 +15,22 @@ local wsThread=[[
local triggerCHN,sendCHN,readCHN,threadName=... local triggerCHN,sendCHN,readCHN,threadName=...
local CHN_demand,CHN_getCount=triggerCHN.demand,triggerCHN.getCount
local CHN_push,CHN_pop=triggerCHN.push,triggerCHN.pop
local SOCK=require"socket".tcp() local SOCK=require"socket".tcp()
local JSON=require"Zframework.json" local JSON=require"Zframework.json"
do--Connect do--Connect
local host=sendCHN:demand() local host=CHN_demand(sendCHN)
local port=sendCHN:demand() local port=CHN_demand(sendCHN)
local path=sendCHN:demand() local path=CHN_demand(sendCHN)
local body=sendCHN:demand() local body=CHN_demand(sendCHN)
local timeout=sendCHN:demand() local timeout=CHN_demand(sendCHN)
SOCK:settimeout(timeout) SOCK:settimeout(timeout)
local res,err=SOCK:connect(host,port) local res,err=SOCK:connect(host,port)
if err then readCHN:push(err)return end if err then CHN_push(readCHN,err)return end
--WebSocket handshake --WebSocket handshake
if not body then body=""end if not body then body=""end
@@ -46,7 +48,7 @@ do--Connect
--First line of HTTP --First line of HTTP
res,err=SOCK:receive("*l") res,err=SOCK:receive("*l")
if not res then readCHN:push(err)return end if not res then CHN_push(readCHN,err)return end
local code,ctLen local code,ctLen
code=res:find(" ") code=res:find(" ")
code=res:sub(code+1,code+3) code=res:sub(code+1,code+3)
@@ -54,7 +56,7 @@ do--Connect
--Get body length from headers and remove headers --Get body length from headers and remove headers
repeat repeat
res,err=SOCK:receive("*l") res,err=SOCK:receive("*l")
if not res then readCHN:push(err)return end if not res then CHN_push(readCHN,err)return end
if not ctLen and res:find("length")then if not ctLen and res:find("length")then
ctLen=tonumber(res:match("%d+")) ctLen=tonumber(res:match("%d+"))
end end
@@ -63,14 +65,14 @@ do--Connect
--Result --Result
if ctLen then if ctLen then
if code=="101"then if code=="101"then
readCHN:push('success') CHN_push(readCHN,'success')
else else
res,err=SOCK:receive(ctLen) res,err=SOCK:receive(ctLen)
if not res then if not res then
readCHN:push(err) CHN_push(readCHN,err)
else else
res=JSON.decode(res) res=JSON.decode(res)
readCHN:push((code or"XXX")..":"..(res and res.reason or"Server Error")) CHN_push(readCHN,(code or"XXX")..":"..(res and res.reason or"Server Error"))
end end
return return
end end
@@ -125,12 +127,12 @@ local lBuffer=""--Long multi-data buffer
local UFF--Un-finished-frame mode local UFF--Un-finished-frame mode
local sBuffer=""--Short multi-frame buffer local sBuffer=""--Short multi-frame buffer
while true do--Running while true do--Running
triggerCHN:demand() CHN_demand(triggerCHN)
--Send --Send
while sendCHN:getCount()>=2 do while CHN_getCount(sendCHN)>=2 do
local op=sendCHN:pop() local op=CHN_pop(sendCHN)
local message=sendCHN:pop() local message=CHN_pop(sendCHN)
_send(op,message) _send(op,message)
end end
@@ -191,27 +193,27 @@ while true do--Running
--React --React
if op==8 then--8=close if op==8 then--8=close
readCHN:push(op) CHN_push(readCHN,op)
SOCK:close() SOCK:close()
if type(res)=='string'then if type(res)=='string'then
readCHN:push(res:sub(3))--Warning: with 2 bytes close code CHN_push(readCHN,res:sub(3))--Warning: with 2 bytes close code
else else
readCHN:push("WS Error") CHN_push(readCHN,"WS Error")
end end
elseif op==0 then--0=continue elseif op==0 then--0=continue
lBuffer=lBuffer..res lBuffer=lBuffer..res
if fin then if fin then
]]..(debugMode:find'M'and""or"--")..[[print("FIN=1 (c") ]]..(debugMode:find'M'and""or"--")..[[print("FIN=1 (c")
readCHN:push(lBuffer) CHN_push(readCHN,lBuffer)
lBuffer="" lBuffer=""
else else
]]..(debugMode:find'M'and""or"--")..[[print("FIN=0 (c") ]]..(debugMode:find'M'and""or"--")..[[print("FIN=0 (c")
end end
else else
readCHN:push(op) CHN_push(readCHN,op)
if fin then if fin then
]]..(debugMode:find'M'and""or"--")..[[print("OP: "..op.."\tFIN=1") ]]..(debugMode:find'M'and""or"--")..[[print("OP: "..op.."\tFIN=1")
readCHN:push(res) CHN_push(readCHN,res)
else else
]]..(debugMode:find'M'and""or"--")..[[print("OP: "..op.."\tFIN=0") ]]..(debugMode:find'M'and""or"--")..[[print("OP: "..op.."\tFIN=0")
sBuffer=res sBuffer=res
@@ -223,6 +225,9 @@ end
]] ]]
local timer=love.timer.getTime local timer=love.timer.getTime
local CHN=love.thread.newChannel()
local CHN_getCount,CHN_push,CHN_pop=CHN.getCount,CHN.push,CHN.pop
local WS={} local WS={}
local wsList=setmetatable({},{ local wsList=setmetatable({},{
__index=function(l,k) __index=function(l,k)
@@ -265,11 +270,11 @@ function WS.connect(name,subPath,body,timeout)
} }
wsList[name]=ws wsList[name]=ws
ws.thread:start(ws.triggerCHN,ws.sendCHN,ws.readCHN,name) ws.thread:start(ws.triggerCHN,ws.sendCHN,ws.readCHN,name)
ws.sendCHN:push(host) CHN_push(ws.sendCHN,host)
ws.sendCHN:push(port) CHN_push(ws.sendCHN,port)
ws.sendCHN:push(path..subPath) CHN_push(ws.sendCHN,path..subPath)
ws.sendCHN:push(body or"") CHN_push(ws.sendCHN,body or"")
ws.sendCHN:push(timeout or 2.6) CHN_push(ws.sendCHN,timeout or 2.6)
end end
function WS.status(name) function WS.status(name)
@@ -311,8 +316,8 @@ local OPname={
function WS.send(name,message,op) function WS.send(name,message,op)
local ws=wsList[name] local ws=wsList[name]
if ws.real and ws.status=='running'then if ws.real and ws.status=='running'then
ws.sendCHN:push(op and OPcode[op]or 2)--2=binary CHN_push(ws.sendCHN,op and OPcode[op]or 2)--2=binary
ws.sendCHN:push(message) CHN_push(ws.sendCHN,message)
ws.lastPingTime=timer() ws.lastPingTime=timer()
ws.sendTimer=1 ws.sendTimer=1
end end
@@ -320,8 +325,8 @@ end
function WS.read(name) function WS.read(name)
local ws=wsList[name] local ws=wsList[name]
if ws.real and ws.readCHN:getCount()>=2 then if ws.real and CHN_getCount(ws.readCHN)>=2 then
local op,message=ws.readCHN:pop(),ws.readCHN:pop() local op,message=CHN_pop(ws.readCHN),CHN_pop(ws.readCHN)
if op==8 then ws.status='dead'end--8=close if op==8 then ws.status='dead'end--8=close
ws.lastPongTime=timer() ws.lastPongTime=timer()
ws.pongTimer=1 ws.pongTimer=1
@@ -332,8 +337,8 @@ end
function WS.close(name) function WS.close(name)
local ws=wsList[name] local ws=wsList[name]
if ws.real then if ws.real then
ws.sendCHN:push(8)--close CHN_push(ws.sendCHN,8)--close
ws.sendCHN:push("") CHN_push(ws.sendCHN,"")
ws.status='dead' ws.status='dead'
end end
end end
@@ -342,11 +347,11 @@ function WS.update(dt)
local time=timer() local time=timer()
for name,ws in next,wsList do for name,ws in next,wsList do
if ws.real then if ws.real then
if ws.triggerCHN:getCount()==0 then if CHN_getCount(ws.triggerCHN)==0 then
ws.triggerCHN:push(0) CHN_push(ws.triggerCHN,0)
end end
if ws.status=='connecting'then if ws.status=='connecting'then
local mes=ws.readCHN:pop() local mes=CHN_pop(ws.readCHN)
if mes then if mes then
if mes=='success'then if mes=='success'then
ws.status='running' ws.status='running'
@@ -360,8 +365,8 @@ function WS.update(dt)
end end
elseif ws.status=='running'then elseif ws.status=='running'then
if time-ws.lastPingTime>ws.pingInterval then if time-ws.lastPingTime>ws.pingInterval then
ws.sendCHN:push(9) CHN_push(ws.sendCHN,9)
ws.sendCHN:push("")--ping CHN_push(ws.sendCHN,"")--ping
ws.lastPingTime=time ws.lastPingTime=time
end end
if time-ws.lastPongTime>6+2*ws.pingInterval then if time-ws.lastPongTime>6+2*ws.pingInterval then