2014-09-18 05:59:55 +00:00
|
|
|
local ffi = require("ffi")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2014-09-18 05:59:55 +00:00
|
|
|
local MessageQueue = require("ui/message/messagequeue")
|
|
|
|
|
2015-04-27 00:49:27 +00:00
|
|
|
local _ = require("ffi/zeromq_h")
|
2014-09-18 05:59:55 +00:00
|
|
|
local zmq = ffi.load("libs/libzmq.so.4")
|
|
|
|
local czmq = ffi.load("libs/libczmq.so.1")
|
2018-06-02 16:10:55 +00:00
|
|
|
local C = ffi.C
|
2014-09-18 05:59:55 +00:00
|
|
|
|
|
|
|
local StreamMessageQueue = MessageQueue:new{
|
|
|
|
host = nil,
|
|
|
|
port = nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
function StreamMessageQueue:start()
|
|
|
|
self.context = czmq.zctx_new();
|
2018-06-02 16:10:55 +00:00
|
|
|
self.socket = czmq.zsocket_new(self.context, C.ZMQ_STREAM)
|
2014-09-18 05:59:55 +00:00
|
|
|
self.poller = czmq.zpoller_new(self.socket, nil)
|
|
|
|
local endpoint = string.format("tcp://%s:%d", self.host, self.port)
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("connect to endpoint", endpoint)
|
2014-09-18 05:59:55 +00:00
|
|
|
local rc = czmq.zsocket_connect(self.socket, endpoint)
|
|
|
|
if rc ~= 0 then
|
|
|
|
error("cannot connect to " .. endpoint)
|
|
|
|
end
|
2019-02-21 08:56:54 +00:00
|
|
|
local id_size = ffi.new("size_t[1]", 256)
|
2014-09-18 05:59:55 +00:00
|
|
|
local buffer = ffi.new("uint8_t[?]", id_size[0])
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo: Check return of zmq_getsockopt()
|
2018-06-02 16:10:55 +00:00
|
|
|
zmq.zmq_getsockopt(self.socket, C.ZMQ_IDENTITY, buffer, id_size)
|
2014-09-18 05:59:55 +00:00
|
|
|
self.id = ffi.string(buffer, id_size[0])
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.dbg("id", #self.id, self.id)
|
2014-09-18 05:59:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function StreamMessageQueue:stop()
|
|
|
|
if self.poller ~= nil then
|
|
|
|
czmq.zpoller_destroy(ffi.new('zpoller_t *[1]', self.poller))
|
|
|
|
end
|
|
|
|
if self.socket ~= nil then
|
|
|
|
czmq.zsocket_destroy(self.context, self.socket)
|
|
|
|
end
|
|
|
|
if self.context ~= nil then
|
|
|
|
czmq.zctx_destroy(ffi.new('zctx_t *[1]', self.context))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function StreamMessageQueue:handleZframe(frame)
|
|
|
|
local size = czmq.zframe_size(frame)
|
|
|
|
local data = nil
|
|
|
|
if size > 0 then
|
|
|
|
local frame_data = czmq.zframe_data(frame)
|
|
|
|
if frame_data ~= nil then
|
|
|
|
data = ffi.string(frame_data, size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
czmq.zframe_destroy(ffi.new('zframe_t *[1]', frame))
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
function StreamMessageQueue:waitEvent()
|
Calibre: Minor QoL fixes (#7528)
* CalibreMetadata: Get rid of the now useless NULL-hunt: here, this was basically looking for `rapidjson.null` to replace them with... `rapidjson.null` :?. IIRC, that's a remnant of a quirk of the previous JSON parser (possibly even the previous, *previous* JSON parser ^^).
* CalibreSearch: Update the actually relevant NULL-hunt to make it explicit: replace JSON NULLs with Lua nils, instead of relying on an implementation detail of Lua-RapidJSON, because that detail just changed data type ;).
* UIManager: Make sure tasks scheduled during the final ZMQ callback are honored. e.g., the Calibre "Disconnect" handler. This happened to mostly work purely by chance before the event loop rework.
* Calibre: Restore a proper receiveCallback handler after receiving a book, in order not to break the "Disconnect" handler's state (and, well, get a working Disconnect handler, period ^^).
* Calibre: Unbreak metadata cache when it's initialized by a search (regression since #7159).
* Calibre: Handle UTC <-> local time conversions when checking the cache's timestamp against the Calibre metadata timestamp.
* Bump base (Unbreak CRe on Android, update RapidJSON)
2021-04-12 00:31:53 +00:00
|
|
|
-- Successive zframes may come in batches of tens or hundreds in some cases.
|
2021-04-13 22:35:20 +00:00
|
|
|
-- Since we buffer each frame's data in a Lua string,
|
|
|
|
-- and then let the caller concatenate those,
|
|
|
|
-- it may consume a significant amount of memory.
|
|
|
|
-- And it's fairly easy to trigger when receiving file data from Calibre.
|
|
|
|
-- So, throttle reception to 256 packages at most in one waitEvent loop,
|
Calibre: Minor QoL fixes (#7528)
* CalibreMetadata: Get rid of the now useless NULL-hunt: here, this was basically looking for `rapidjson.null` to replace them with... `rapidjson.null` :?. IIRC, that's a remnant of a quirk of the previous JSON parser (possibly even the previous, *previous* JSON parser ^^).
* CalibreSearch: Update the actually relevant NULL-hunt to make it explicit: replace JSON NULLs with Lua nils, instead of relying on an implementation detail of Lua-RapidJSON, because that detail just changed data type ;).
* UIManager: Make sure tasks scheduled during the final ZMQ callback are honored. e.g., the Calibre "Disconnect" handler. This happened to mostly work purely by chance before the event loop rework.
* Calibre: Restore a proper receiveCallback handler after receiving a book, in order not to break the "Disconnect" handler's state (and, well, get a working Disconnect handler, period ^^).
* Calibre: Unbreak metadata cache when it's initialized by a search (regression since #7159).
* Calibre: Handle UTC <-> local time conversions when checking the cache's timestamp against the Calibre metadata timestamp.
* Bump base (Unbreak CRe on Android, update RapidJSON)
2021-04-12 00:31:53 +00:00
|
|
|
-- after which we immediately call receiveCallback.
|
2021-04-13 22:35:20 +00:00
|
|
|
local wait_packages = 256
|
|
|
|
-- In a similar spirit, much like LuaSocket,
|
|
|
|
-- we store the data as ropes of strings in an array,
|
|
|
|
-- to be concatenated by the caller.
|
|
|
|
local t = {}
|
2014-09-29 06:22:33 +00:00
|
|
|
while czmq.zpoller_wait(self.poller, 0) ~= nil and wait_packages > 0 do
|
2014-09-18 05:59:55 +00:00
|
|
|
local id_frame = czmq.zframe_recv(self.socket)
|
|
|
|
if id_frame ~= nil then
|
2016-02-16 06:34:28 +00:00
|
|
|
self:handleZframe(id_frame)
|
2014-09-18 05:59:55 +00:00
|
|
|
end
|
|
|
|
local frame = czmq.zframe_recv(self.socket)
|
|
|
|
if frame ~= nil then
|
2021-04-13 22:35:20 +00:00
|
|
|
local data = self:handleZframe(frame)
|
|
|
|
if data then
|
|
|
|
table.insert(t, data)
|
|
|
|
end
|
2014-09-18 05:59:55 +00:00
|
|
|
end
|
2014-09-29 06:22:33 +00:00
|
|
|
wait_packages = wait_packages - 1
|
2014-09-18 05:59:55 +00:00
|
|
|
end
|
2021-04-13 22:35:20 +00:00
|
|
|
if self.receiveCallback and #t ~= 0 then
|
|
|
|
self.receiveCallback(t)
|
2014-09-18 05:59:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function StreamMessageQueue:send(data)
|
|
|
|
local msg = czmq.zmsg_new()
|
|
|
|
czmq.zmsg_addmem(msg, self.id, #self.id)
|
|
|
|
czmq.zmsg_addmem(msg, data, #data)
|
|
|
|
czmq.zmsg_send(ffi.new('zmsg_t *[1]', msg), self.socket)
|
|
|
|
end
|
|
|
|
|
|
|
|
return StreamMessageQueue
|