2022-04-21 16:44:46 +00:00
|
|
|
--
|
|
|
|
-- Message Queue
|
|
|
|
--
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local mqueue = {}
|
|
|
|
|
|
|
|
local TYPE = {
|
2022-04-21 16:44:46 +00:00
|
|
|
COMMAND = 0,
|
2022-04-27 16:21:10 +00:00
|
|
|
DATA = 1,
|
|
|
|
PACKET = 2
|
2022-04-21 16:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
mqueue.TYPE = TYPE
|
|
|
|
|
|
|
|
mqueue.new = function ()
|
2022-04-21 16:44:46 +00:00
|
|
|
local queue = {}
|
|
|
|
|
|
|
|
local length = function ()
|
|
|
|
return #queue
|
|
|
|
end
|
|
|
|
|
|
|
|
local empty = function ()
|
|
|
|
return #queue == 0
|
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
|
|
|
|
local ready = function ()
|
|
|
|
return #queue > 0
|
|
|
|
end
|
2022-04-21 16:44:46 +00:00
|
|
|
|
|
|
|
local _push = function (qtype, message)
|
|
|
|
table.insert(queue, { qtype = qtype, message = message })
|
|
|
|
end
|
|
|
|
|
|
|
|
local push_command = function (message)
|
2022-04-26 01:00:50 +00:00
|
|
|
_push(TYPE.COMMAND, message)
|
2022-04-21 16:44:46 +00:00
|
|
|
end
|
2022-04-27 16:21:10 +00:00
|
|
|
|
2022-05-01 19:35:07 +00:00
|
|
|
local push_data = function (key, value)
|
|
|
|
_push(TYPE.DATA, { key = key, val = value })
|
2022-04-27 16:21:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local push_packet = function (message)
|
|
|
|
_push(TYPE.PACKET, message)
|
|
|
|
end
|
|
|
|
|
2022-04-21 16:44:46 +00:00
|
|
|
local pop = function ()
|
|
|
|
if #queue > 0 then
|
2022-04-27 20:24:28 +00:00
|
|
|
return table.remove(queue, 1)
|
2022-04-21 16:44:46 +00:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
length = length,
|
|
|
|
empty = empty,
|
2022-04-27 16:21:10 +00:00
|
|
|
ready = ready,
|
2022-04-21 16:44:46 +00:00
|
|
|
push_packet = push_packet,
|
2022-04-27 16:21:10 +00:00
|
|
|
push_data = push_data,
|
2022-04-21 16:44:46 +00:00
|
|
|
push_command = push_command,
|
|
|
|
pop = pop
|
|
|
|
}
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return mqueue
|