大型重构websocket模块
This commit is contained in:
@@ -49,7 +49,7 @@ function WS.connect(name,subPath,body,timeout)
|
|||||||
readCHN=love.thread.newChannel(),
|
readCHN=love.thread.newChannel(),
|
||||||
lastPingTime=0,
|
lastPingTime=0,
|
||||||
lastPongTime=timer(),
|
lastPongTime=timer(),
|
||||||
pingInterval=12,
|
pingInterval=6,
|
||||||
status='connecting',--'connecting', 'running', 'dead'
|
status='connecting',--'connecting', 'running', 'dead'
|
||||||
sendTimer=0,
|
sendTimer=0,
|
||||||
alertTimer=0,
|
alertTimer=0,
|
||||||
@@ -119,7 +119,11 @@ function WS.read(name)
|
|||||||
local ws=wsList[name]
|
local ws=wsList[name]
|
||||||
if ws.real and ws.status~='connecting'and CHN_getCount(ws.readCHN)>=2 then
|
if ws.real and ws.status~='connecting'and CHN_getCount(ws.readCHN)>=2 then
|
||||||
local op,message=CHN_pop(ws.readCHN),CHN_pop(ws.readCHN)
|
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--8=close
|
||||||
|
ws.status='dead'
|
||||||
|
elseif op==9 then--9=ping
|
||||||
|
WS.send(name,message or"",'pong')
|
||||||
|
end
|
||||||
ws.lastPongTime=timer()
|
ws.lastPongTime=timer()
|
||||||
ws.pongTimer=1
|
ws.pongTimer=1
|
||||||
return message,OPname[op]or op
|
return message,OPname[op]or op
|
||||||
@@ -139,35 +143,41 @@ 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 CHN_getCount(ws.triggerCHN)==0 then
|
if ws.thread:isRunning()then
|
||||||
CHN_push(ws.triggerCHN,0)
|
if CHN_getCount(ws.triggerCHN)==0 then
|
||||||
end
|
CHN_push(ws.triggerCHN,0)
|
||||||
if ws.status=='connecting'then
|
end
|
||||||
local mes=CHN_pop(ws.readCHN)
|
if ws.status=='connecting'then
|
||||||
if mes then
|
local mes=CHN_pop(ws.readCHN)
|
||||||
if mes=='success'then
|
if mes then
|
||||||
ws.status='running'
|
if mes=='success'then
|
||||||
|
ws.status='running'
|
||||||
|
ws.lastPingTime=time
|
||||||
|
ws.lastPongTime=time
|
||||||
|
ws.pongTimer=1
|
||||||
|
else
|
||||||
|
ws.status='dead'
|
||||||
|
MES.new('warn',text.wsFailed..": "..(mes=="timeout"and text.netTimeout or mes))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
elseif ws.status=='running'then
|
||||||
|
if time-ws.lastPingTime>ws.pingInterval then
|
||||||
|
CHN_push(ws.sendCHN,9)
|
||||||
|
CHN_push(ws.sendCHN,"")--ping
|
||||||
ws.lastPingTime=time
|
ws.lastPingTime=time
|
||||||
ws.lastPongTime=time
|
end
|
||||||
ws.pongTimer=1
|
if time-ws.lastPongTime>6+2*ws.pingInterval then
|
||||||
else
|
WS.close(name)
|
||||||
ws.status='dead'
|
|
||||||
MES.new('warn',text.wsFailed..": "..(mes=="timeout"and text.netTimeout or mes))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif ws.status=='running'then
|
if ws.sendTimer>0 then ws.sendTimer=ws.sendTimer-dt end
|
||||||
if time-ws.lastPingTime>ws.pingInterval then
|
if ws.pongTimer>0 then ws.pongTimer=ws.pongTimer-dt end
|
||||||
CHN_push(ws.sendCHN,9)
|
if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end
|
||||||
CHN_push(ws.sendCHN,"")--ping
|
else
|
||||||
ws.lastPingTime=time
|
ws.status='dead'
|
||||||
end
|
ws.real=false
|
||||||
if time-ws.lastPongTime>6+2*ws.pingInterval then
|
MES.new('warn',text.wsClose.."线程错误 Thread error")
|
||||||
WS.close(name)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if ws.sendTimer>0 then ws.sendTimer=ws.sendTimer-dt end
|
|
||||||
if ws.pongTimer>0 then ws.pongTimer=ws.pongTimer-dt end
|
|
||||||
if ws.alertTimer>0 then ws.alertTimer=ws.alertTimer-dt end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -64,139 +64,133 @@ do--Connect
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local byte=string.byte
|
local YIELD=coroutine.yield
|
||||||
local band,shl=bit.band,bit.lshift
|
local byte,char=string.byte,string.char
|
||||||
|
local band,bor,bxor=bit.band,bit.bor,bit.bxor
|
||||||
|
local shl,shr=bit.lshift,bit.rshift
|
||||||
|
|
||||||
local _send do
|
local mask_key={1,14,5,14}
|
||||||
local char=string.char
|
local mask_str=char(unpack(mask_key))
|
||||||
local bor,bxor=bit.bor,bit.bxor
|
local function _send(op,message)
|
||||||
local shr=bit.rshift
|
--Message type
|
||||||
|
SOCK:send(char(bor(op,0x80)))
|
||||||
|
|
||||||
local mask_key={1,14,5,14}
|
if message then
|
||||||
local mask_str=char(unpack(mask_key))
|
--Length
|
||||||
|
local length=#message
|
||||||
function _send(op,message)
|
if length>65535 then
|
||||||
--Message type
|
SOCK:send(char(bor(127,0x80),0,0,0,0,band(shr(length,24),0xff),band(shr(length,16),0xff),band(shr(length,8),0xff),band(length,0xff)))
|
||||||
SOCK:send(char(bor(0x80,op)))
|
elseif length>125 then
|
||||||
|
SOCK:send(char(bor(126,0x80),band(shr(length,8),0xff),band(length,0xff)))
|
||||||
if message then
|
|
||||||
--Length
|
|
||||||
local length=#message
|
|
||||||
if length>65535 then
|
|
||||||
SOCK:send(char(bor(127,0x80),0,0,0,0,band(shr(length,24),0xff),band(shr(length,16),0xff),band(shr(length,8),0xff),band(length,0xff)))
|
|
||||||
elseif length>125 then
|
|
||||||
SOCK:send(char(bor(126,0x80),band(shr(length,8),0xff),band(length,0xff)))
|
|
||||||
else
|
|
||||||
SOCK:send(char(bor(length,0x80)))
|
|
||||||
end
|
|
||||||
SOCK:send(mask_str)
|
|
||||||
local msgbyte={byte(message,1,length)}
|
|
||||||
for i=1,length do
|
|
||||||
msgbyte[i]=bxor(msgbyte[i],mask_key[(i-1)%4+1])
|
|
||||||
end
|
|
||||||
return SOCK:send(char(unpack(msgbyte)))
|
|
||||||
else
|
else
|
||||||
SOCK:send('\128'..mask_str)
|
SOCK:send(char(bor(length,0x80)))
|
||||||
return 0
|
|
||||||
end
|
end
|
||||||
|
local msgbyte={byte(message,1,length)}
|
||||||
|
for i=1,length do
|
||||||
|
msgbyte[i]=bxor(msgbyte[i],mask_key[(i-1)%4+1])
|
||||||
|
end
|
||||||
|
return SOCK:send(mask_str..char(unpack(msgbyte)))
|
||||||
|
else
|
||||||
|
SOCK:send('\128'..mask_str)
|
||||||
|
return 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
local sendThread=coroutine.wrap(function()
|
||||||
local res,err
|
|
||||||
local op,fin
|
|
||||||
local length
|
|
||||||
local lBuffer=""--Long multi-data buffer
|
|
||||||
local UFF--Un-finished-frame mode
|
|
||||||
local sBuffer=""--Short multi-frame buffer
|
|
||||||
while true do--Running
|
|
||||||
CHN_demand(triggerCHN)
|
|
||||||
|
|
||||||
--Send
|
|
||||||
while CHN_getCount(sendCHN)>=2 do
|
|
||||||
op=CHN_pop(sendCHN)
|
|
||||||
local message=CHN_pop(sendCHN)
|
|
||||||
_send(op,message)
|
|
||||||
end
|
|
||||||
|
|
||||||
--Read
|
|
||||||
while true do
|
while true do
|
||||||
if not UFF then--UNF process
|
while CHN_getCount(sendCHN)>=2 do
|
||||||
--Byte 0-1
|
_send(CHN_pop(sendCHN),CHN_pop(sendCHN))
|
||||||
res,err=SOCK:receive(2)
|
|
||||||
if err then break end
|
|
||||||
|
|
||||||
op=band(byte(res,1),0x0f)
|
|
||||||
fin=band(byte(res,1),0x80)==0x80
|
|
||||||
|
|
||||||
--Calculating data length
|
|
||||||
length=band(byte(res,2),0x7f)
|
|
||||||
if length==126 then
|
|
||||||
res,err=SOCK:receive(2)
|
|
||||||
if res then
|
|
||||||
length=shl(byte(res,1),8)+byte(res,2)--!!!!!
|
|
||||||
else
|
|
||||||
CHN_push(readCHN,8)--close
|
|
||||||
CHN_push(readCHN,'{"reason":"'..(err or"error_01")..'"}')
|
|
||||||
end
|
|
||||||
elseif length==127 then
|
|
||||||
local b={byte(SOCK:receive(8),1,8)}
|
|
||||||
length=shl(b[5],24)+shl(b[6],16)+shl(b[7],8)+b[8]
|
|
||||||
end
|
|
||||||
|
|
||||||
if length>0 then
|
|
||||||
--Receive data
|
|
||||||
local s,_,p=SOCK:receive(length)
|
|
||||||
if s then
|
|
||||||
res=s
|
|
||||||
elseif p then--UNF head
|
|
||||||
UFF=true
|
|
||||||
sBuffer=sBuffer..p
|
|
||||||
length=length-#p
|
|
||||||
break
|
|
||||||
end
|
|
||||||
else
|
|
||||||
res=""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local s,_,p=SOCK:receive(length)
|
|
||||||
if s then
|
|
||||||
sBuffer=sBuffer..s
|
|
||||||
length=length-#s
|
|
||||||
elseif p then
|
|
||||||
sBuffer=sBuffer..p
|
|
||||||
length=length-#p
|
|
||||||
end
|
|
||||||
if length==0 then
|
|
||||||
res,sBuffer=sBuffer,""
|
|
||||||
UFF=false
|
|
||||||
else
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
YIELD()
|
||||||
|
end
|
||||||
|
error("break")
|
||||||
|
end)
|
||||||
|
|
||||||
|
local function _receive(sock,len)
|
||||||
|
local buffer=""
|
||||||
|
while true do
|
||||||
|
local r,e,p=sock:receive(len)
|
||||||
|
if r then
|
||||||
|
buffer=buffer..r
|
||||||
|
len=len-#r
|
||||||
|
elseif p then
|
||||||
|
buffer=buffer..p
|
||||||
|
len=len-#p
|
||||||
|
elseif e then
|
||||||
|
return nil,e
|
||||||
|
end
|
||||||
|
if len==0 then
|
||||||
|
return buffer
|
||||||
|
end
|
||||||
|
YIELD()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local readThread=coroutine.wrap(function()
|
||||||
|
local res,err
|
||||||
|
local op,fin
|
||||||
|
local lBuffer=""--Long multi-pack buffer
|
||||||
|
while true do
|
||||||
|
--Byte 0-1
|
||||||
|
res,err=_receive(SOCK,2)
|
||||||
|
assert(not err,err)
|
||||||
|
|
||||||
|
op=band(byte(res,1),0x0f)
|
||||||
|
fin=band(byte(res,1),0x80)==0x80
|
||||||
|
|
||||||
|
--Calculating data length
|
||||||
|
local length=band(byte(res,2),0x7f)
|
||||||
|
if length==126 then
|
||||||
|
res,err=_receive(SOCK,2)
|
||||||
|
assert(not err,err)
|
||||||
|
length=shl(byte(res,1),8)+byte(res,2)
|
||||||
|
elseif length==127 then
|
||||||
|
local lenData
|
||||||
|
lenData,err=_receive(SOCK,8)
|
||||||
|
assert(not err,err)
|
||||||
|
local _,_,_,_,_5,_6,_7,_8=byte(lenData,1,8)
|
||||||
|
length=shl(_5,24)+shl(_6,16)+shl(_7,8)+_8
|
||||||
|
end
|
||||||
|
res,err=_receive(SOCK,length)
|
||||||
|
assert(not err,err)
|
||||||
|
|
||||||
--React
|
--React
|
||||||
if op==8 then--8=close
|
if op==8 then--8=close
|
||||||
CHN_push(readCHN,op)
|
CHN_push(readCHN,8)--close
|
||||||
SOCK:close()
|
|
||||||
if type(res)=='string'then
|
if type(res)=='string'then
|
||||||
CHN_push(readCHN,res:sub(3))--Warning: with 2 bytes close code
|
CHN_push(readCHN,res:sub(3))--Warning: 2 bytes close code at start so :sub(3)
|
||||||
else
|
else
|
||||||
CHN_push(readCHN,"WS Error")
|
CHN_push(readCHN,"WS closed")
|
||||||
end
|
end
|
||||||
|
return
|
||||||
elseif op==0 then--0=continue
|
elseif op==0 then--0=continue
|
||||||
lBuffer=lBuffer..res
|
lBuffer=lBuffer..res
|
||||||
if fin then
|
if fin then
|
||||||
CHN_push(readCHN,lBuffer)
|
CHN_push(readCHN,lBuffer)
|
||||||
lBuffer=""
|
lBuffer=""
|
||||||
else
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
CHN_push(readCHN,op)
|
CHN_push(readCHN,op)
|
||||||
if fin then
|
if fin then
|
||||||
CHN_push(readCHN,res)
|
CHN_push(readCHN,res)
|
||||||
|
lBuffer=""
|
||||||
else
|
else
|
||||||
sBuffer=res
|
lBuffer=res
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
YIELD()
|
||||||
end
|
end
|
||||||
end
|
error("break")
|
||||||
|
end)
|
||||||
|
|
||||||
|
local success,err
|
||||||
|
|
||||||
|
while true do--Running
|
||||||
|
CHN_demand(triggerCHN)
|
||||||
|
success,err=pcall(sendThread)
|
||||||
|
if not success or err then break end
|
||||||
|
success,err=pcall(readThread)
|
||||||
|
if not success or err then break end
|
||||||
|
end
|
||||||
|
|
||||||
|
SOCK:close()
|
||||||
|
CHN_push(readCHN,8)--close
|
||||||
|
CHN_push(readCHN,err or"Disconnected")
|
||||||
@@ -63,11 +63,6 @@ function NET.getlock(name)
|
|||||||
return TIME()<locks[name]
|
return TIME()<locks[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
--Pong back
|
|
||||||
local function _pong(wsName,message)
|
|
||||||
WS.send(wsName,message or"",'pong')
|
|
||||||
end
|
|
||||||
|
|
||||||
--Parse json message
|
--Parse json message
|
||||||
local function _parse(res)
|
local function _parse(res)
|
||||||
res=JSON.decode(res)
|
res=JSON.decode(res)
|
||||||
@@ -86,7 +81,7 @@ local function _closeMessage(message)
|
|||||||
if mes then
|
if mes then
|
||||||
MES.new('info',("%s %s|%s"):format(text.wsClose,mes.type or"",mes.reason or""))
|
MES.new('info',("%s %s|%s"):format(text.wsClose,mes.type or"",mes.reason or""))
|
||||||
else
|
else
|
||||||
MES.new('info',text.wsClose)
|
MES.new('info',("%s %s"):format(text.wsClose,message))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -382,7 +377,6 @@ function NET.updateWS_app()
|
|||||||
local message,op=WS.read('app')
|
local message,op=WS.read('app')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('app',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
@@ -404,7 +398,7 @@ function NET.updateWS_app()
|
|||||||
end
|
end
|
||||||
MES.new('broadcast',res.notice,5)
|
MES.new('broadcast',res.notice,5)
|
||||||
NET.tryLogin(true)
|
NET.tryLogin(true)
|
||||||
TASK.new(NET.freshPlayerCount)
|
-- TASK.new(NET.freshPlayerCount)
|
||||||
elseif res.action==0 then--Broadcast
|
elseif res.action==0 then--Broadcast
|
||||||
MES.new('broadcast',res.data.message,5)
|
MES.new('broadcast',res.data.message,5)
|
||||||
elseif res.action==1 then--Get notice
|
elseif res.action==1 then--Get notice
|
||||||
@@ -438,7 +432,6 @@ function NET.updateWS_user()
|
|||||||
local message,op=WS.read('user')
|
local message,op=WS.read('user')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('user',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
@@ -485,7 +478,6 @@ function NET.updateWS_play()
|
|||||||
local message,op=WS.read('play')
|
local message,op=WS.read('play')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('play',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
@@ -610,7 +602,6 @@ function NET.updateWS_stream()
|
|||||||
local message,op=WS.read('stream')
|
local message,op=WS.read('stream')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('stream',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
@@ -681,7 +672,6 @@ function NET.updateWS_chat()
|
|||||||
local message,op=WS.read('chat')
|
local message,op=WS.read('chat')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('chat',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
@@ -703,7 +693,6 @@ function NET.updateWS_manage()
|
|||||||
local message,op=WS.read('manage')
|
local message,op=WS.read('manage')
|
||||||
if message then
|
if message then
|
||||||
if op=='ping'then
|
if op=='ping'then
|
||||||
_pong('manage',message)
|
|
||||||
elseif op=='pong'then
|
elseif op=='pong'then
|
||||||
elseif op=='close'then
|
elseif op=='close'then
|
||||||
_closeMessage(message)
|
_closeMessage(message)
|
||||||
|
|||||||
Reference in New Issue
Block a user