Skip to content

Commit

Permalink
Bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Burkalev authored and Konstantin Burkalev committed Nov 22, 2018
1 parent ec48032 commit fe53ceb
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 100 deletions.
5 changes: 5 additions & 0 deletions lib/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local storeConfigs = {
local wiolaConf = {
socketTimeout = 100,
maxPayloadLen = 65536,
pingInterval = 1000, -- interval in ms for sending ping frames. set to 0 for disabling
realms = {},
store = "redis",
storeConfig = {
Expand Down Expand Up @@ -103,6 +104,10 @@ function _M.config(config)
wiolaConf.maxPayloadLen = config.maxPayloadLen
end

if config.pingInterval then
wiolaConf.pingInterval = config.pingInterval
end

if config.realms then
wiolaConf.realms = config.realms
end
Expand Down
115 changes: 61 additions & 54 deletions lib/raw-handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ local wiola = require "wiola"
local config = require("wiola.config").config()
local wiola_max_payload_len = WAMP_PAYLOAD_LENGTHS[config.maxPayloadLen] or 65536
local bit = require "bit"
local tcpSocket, wampServer, cliMaxLength, serializer, serializerStr, data, err, ok, cliData
local tcpSocket, wampServer, cliMaxLength, serializer, serializerStr, data, err, ok, cliData, pingCo

tcpSocket, err = ngx.req.socket(true)

if not tcpSocket then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

tcpSocket:settimeout(config.socketTimeout)
tcpSocket:settimeouts(config.socketTimeout, config.socketTimeout, config.socketTimeout)

wampServer, err = wiola:new()
if not wampServer then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

data, err = tcpSocket:receive(4)

if data == nil then
return ngx.exit(444) -- tcpSocket:close()
return ngx.exit(ngx.ERROR)
end

if string.byte(data) ~= 0x7F then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
elseif string.byte(data, 3) ~= 0x0 or string.byte(data, 4) ~= 0x0 then
cliData = string.char(0x7F, bit.bor(bit.lshift(3, 4), 0), 0, 0)
tcpSocket:send(cliData)
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

cliMaxLength = math.pow(2, 9 + bit.rshift(string.byte(data, 2), 4))
Expand All @@ -65,7 +65,7 @@ elseif serializer == 2 then
else
cliData = string.char(0x7F, bit.bor(bit.lshift(1, 4), 0), 0, 0)
tcpSocket:send(cliData)
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

local sessionId, dataType = wampServer:addConnection(ngx.var.connection, serializerStr)
Expand All @@ -74,7 +74,7 @@ cliData = string.char(0x7F, bit.bor(bit.lshift(wiola_max_payload_len, 4), serial
data, err = tcpSocket:send(cliData)

if not data then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

local function removeConnection(_, sessId)
Expand All @@ -95,7 +95,7 @@ end

ok, err = ngx.on_abort(removeConnectionWrapper)
if not ok then
ngx.exit(444)
ngx.exit(ngx.ERROR)
end

local function getLenBytes(len)
Expand All @@ -107,28 +107,33 @@ local function getLenBytes(len)
return string.char(b1, b2, b3)
end

while true do
local hflags, msgType, msgLen
if config.pingInterval > 0 then
local pinger = function (period)
local pingData
coroutine.yield()

hflags = wampServer:getHandlerFlags(sessionId)
if hflags ~= nil then
if hflags.sendLast == true then
cliData = wampServer:getPendingData(sessionId, true)
while true do

data, err = tcpSocket:send(cliData)
pingData = string.char(1) .. getLenBytes(1) .. 'p'
data, err = tcpSocket:send(pingData)

if not data then
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.ERROR)
end
end

if hflags.close == true then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
ngx.sleep(period)
end
end
cliData = wampServer:getPendingData(sessionId)

while cliData ~= ngx.null do
pingCo = ngx.thread.spawn(pinger, config.pingInterval / 1000)
end

while true do
local hflags, msgType, msgLen
hflags = wampServer:getHandlerFlags(sessionId)
cliData = wampServer:getPendingData(sessionId, hflags.sendLast)

if cliData ~= ngx.null and cliData then

msgLen = string.len(cliData)

Expand All @@ -141,50 +146,52 @@ while true do
end

-- TODO Handle exceeded message length situation

cliData = wampServer:getPendingData(sessionId)
end

data, err = tcpSocket:receive(4)

if data == nil then
if hflags.close == true then
if pingCo then
ngx.thread.kill(pingCo)
end
tcpSocket:shutdown("send")
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
ngx.exit(ngx.OK)
end

msgType = bit.band(string.byte(data), 0xff)
msgLen = bit.lshift(string.byte(data, 2), 16) +
bit.lshift(string.byte(data, 3), 8) +
string.byte(data, 4)
data, err = tcpSocket:receive(4)

if msgType == 0 then -- regular WAMP message
if data ~= nil then
msgType = bit.band(string.byte(data), 0xff)
msgLen = bit.lshift(string.byte(data, 2), 16) +
bit.lshift(string.byte(data, 3), 8) +
string.byte(data, 4)

data, err = tcpSocket:receive(msgLen)
if msgType == 0 then -- regular WAMP message

if data == nil then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
end
data, err = tcpSocket:receive(msgLen)

wampServer:receiveData(sessionId, data)
if data == nil then
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.ERROR)
end
wampServer:receiveData(sessionId, data)

elseif msgType == 1 then -- PING
elseif msgType == 1 then -- PING

data, err = tcpSocket:receive(msgLen)
data, err = tcpSocket:receive(msgLen)

if data == nil then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
end
if data == nil then
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.ERROR)
end

cliData = string.char(2) .. msgLen .. data
data, err = tcpSocket:send(cliData)
cliData = string.char(2) .. msgLen .. data
data, err = tcpSocket:send(cliData)

if not data then
if not data then
end
end

-- elseif msgType == 2 then -- PONG
-- TODO Implement server initiated ping

elseif err == 'closed' then
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.ERROR)
end
end
2 changes: 1 addition & 1 deletion lib/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ function _M:getHandlerFlags(regId)

return fl
else
return nil
return {}
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/wiola.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


local _M = {
_VERSION = '0.9.1',
_VERSION = '0.10.0',
}

_M.__index = _M
Expand Down Expand Up @@ -292,7 +292,7 @@ function _M:_publishMetaEvent(part, eventUri, session, ...)
argsL[1].authprovider = parameters[1].authprovider
end
-- TODO Add information about transport
elseif eventUri == 'wamp.session.on_leave' then
--elseif eventUri == 'wamp.session.on_leave' then
-- nothing to add :)
elseif eventUri == 'wamp.subscription.on_create' then
local details = {
Expand Down
76 changes: 36 additions & 40 deletions lib/ws-handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
local wsServer = require "resty.websocket.server"
local wiola = require "wiola"
local config = require("wiola.config").config()
local webSocket, wampServer, ok, err, bytes
local webSocket, wampServer, ok, err, bytes, pingCo

webSocket, err = wsServer:new({
timeout = config.socketTimeout,
max_payload_len = config.maxPayloadLen
})

if not webSocket then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

wampServer, err = wiola:new()
if not wampServer then
return ngx.exit(444)
return ngx.exit(ngx.ERROR)
end

local sessionId, dataType = wampServer:addConnection(ngx.var.connection, ngx.header["Sec-WebSocket-Protocol"])
Expand All @@ -42,35 +42,32 @@ end

ok, err = ngx.on_abort(removeConnectionWrapper)
if not ok then
ngx.exit(444)
ngx.exit(ngx.ERROR)
end

while true do
local cliData, data, typ, hflags

hflags = wampServer:getHandlerFlags(sessionId)
if hflags ~= nil then
if hflags.sendLast == true then
cliData = wampServer:getPendingData(sessionId, true)

if dataType == 'binary' then
bytes, err = webSocket:send_binary(cliData)
else
bytes, err = webSocket:send_text(cliData)
end
if config.pingInterval > 0 then
local pinger = function (period)
coroutine.yield()

while true do
bytes, err = webSocket:send_ping()
if not bytes then
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.ERROR)
end
end

if hflags.close == true then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
ngx.sleep(period)
end
end
cliData = wampServer:getPendingData(sessionId)

while cliData ~= ngx.null do
pingCo = ngx.thread.spawn(pinger, config.pingInterval / 1000)
end

while true do
local cliData, data, typ, hflags
hflags = wampServer:getHandlerFlags(sessionId)
cliData = wampServer:getPendingData(sessionId, hflags.sendLast)

if cliData ~= ngx.null and cliData then
if dataType == 'binary' then
bytes, err = webSocket:send_binary(cliData)
else
Expand All @@ -79,40 +76,39 @@ while true do

if not bytes then
end
end

cliData = wampServer:getPendingData(sessionId)
if hflags.close == true then
if pingCo then
ngx.thread.kill(pingCo)
end
webSocket:send_close(1000, "Closing connection")
ngx.timer.at(0, removeConnection, sessionId)
ngx.exit(ngx.OK)
end

if webSocket.fatal then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
ngx.exit(ngx.ERROR)
end

data, typ = webSocket:recv_frame()

if not data then

bytes, err = webSocket:send_ping()
if not bytes then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
if typ == "close" then
if pingCo then
ngx.thread.kill(pingCo)
end

elseif typ == "close" then
bytes, err = webSocket:send_close(1000, "Closing connection")
if not bytes then
return
end
webSocket:send_close(1000, "Closing connection")
ngx.timer.at(0, removeConnection, sessionId)
webSocket:send_close()
ngx.exit(ngx.OK)
break

elseif typ == "ping" then

bytes, err = webSocket:send_pong()
if not bytes then
ngx.timer.at(0, removeConnection, sessionId)
return ngx.exit(444)
ngx.exit(ngx.ERROR)
end

-- elseif typ == "pong" then
Expand Down
2 changes: 1 addition & 1 deletion src/wiola/wiola.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
--local getdump = require("debug.vardump").getdump

local _M = {
_VERSION = '0.9.1',
_VERSION = '0.10.0',
}

_M.__index = _M
Expand Down
4 changes: 2 additions & 2 deletions wiola-0.9.1-2.rockspec → wiola-0.10.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package = "Wiola"
version = "0.9.1-2"
version = "0.10.0-1"

source = {
url = "git://github.com/KSDaemon/wiola.git",
tag = "v0.9.1"
tag = "v0.10.0"
}

description = {
Expand Down

0 comments on commit fe53ceb

Please sign in to comment.