2024-01-12 05:03:25 -07:00
|
|
|
local mpack = vim.mpack
|
2023-01-31 15:35:04 -07:00
|
|
|
|
|
|
|
local Response = {}
|
|
|
|
Response.__index = Response
|
|
|
|
|
|
|
|
function Response.new(msgpack_rpc_stream, request_id)
|
|
|
|
return setmetatable({
|
|
|
|
_msgpack_rpc_stream = msgpack_rpc_stream,
|
2023-12-04 15:32:39 -07:00
|
|
|
_request_id = request_id,
|
2023-01-31 15:35:04 -07:00
|
|
|
}, Response)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Response:send(value, is_error)
|
|
|
|
local data = self._msgpack_rpc_stream._session:reply(self._request_id)
|
|
|
|
if is_error then
|
|
|
|
data = data .. self._msgpack_rpc_stream._pack(value)
|
|
|
|
data = data .. self._msgpack_rpc_stream._pack(mpack.NIL)
|
|
|
|
else
|
|
|
|
data = data .. self._msgpack_rpc_stream._pack(mpack.NIL)
|
|
|
|
data = data .. self._msgpack_rpc_stream._pack(value)
|
|
|
|
end
|
|
|
|
self._msgpack_rpc_stream._stream:write(data)
|
|
|
|
end
|
|
|
|
|
2024-01-15 12:49:08 -07:00
|
|
|
--- @class test.MsgpackRpcStream
|
|
|
|
--- @field private _stream test.Stream
|
|
|
|
--- @field private __pack table
|
2023-01-31 15:35:04 -07:00
|
|
|
local MsgpackRpcStream = {}
|
|
|
|
MsgpackRpcStream.__index = MsgpackRpcStream
|
|
|
|
|
|
|
|
function MsgpackRpcStream.new(stream)
|
|
|
|
return setmetatable({
|
|
|
|
_stream = stream,
|
2024-01-16 06:26:21 -07:00
|
|
|
_pack = mpack.Packer(),
|
2023-01-31 15:35:04 -07:00
|
|
|
_session = mpack.Session({
|
|
|
|
unpack = mpack.Unpacker({
|
|
|
|
ext = {
|
2024-01-16 06:26:21 -07:00
|
|
|
-- Buffer
|
2023-12-04 15:32:39 -07:00
|
|
|
[0] = function(_c, s)
|
2024-01-16 06:26:21 -07:00
|
|
|
return mpack.decode(s)
|
2023-12-04 15:32:39 -07:00
|
|
|
end,
|
2024-01-16 06:26:21 -07:00
|
|
|
-- Window
|
2023-12-04 15:32:39 -07:00
|
|
|
[1] = function(_c, s)
|
2024-01-16 06:26:21 -07:00
|
|
|
return mpack.decode(s)
|
2023-12-04 15:32:39 -07:00
|
|
|
end,
|
2024-01-16 06:26:21 -07:00
|
|
|
-- Tabpage
|
2023-12-04 15:32:39 -07:00
|
|
|
[2] = function(_c, s)
|
2024-01-16 06:26:21 -07:00
|
|
|
return mpack.decode(s)
|
2023-12-04 15:32:39 -07:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
}),
|
2023-01-31 15:35:04 -07:00
|
|
|
}),
|
|
|
|
}, MsgpackRpcStream)
|
|
|
|
end
|
|
|
|
|
|
|
|
function MsgpackRpcStream:write(method, args, response_cb)
|
|
|
|
local data
|
|
|
|
if response_cb then
|
|
|
|
assert(type(response_cb) == 'function')
|
|
|
|
data = self._session:request(response_cb)
|
|
|
|
else
|
|
|
|
data = self._session:notify()
|
|
|
|
end
|
|
|
|
|
2023-12-04 15:32:39 -07:00
|
|
|
data = data .. self._pack(method) .. self._pack(args)
|
2023-01-31 15:35:04 -07:00
|
|
|
self._stream:write(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
function MsgpackRpcStream:read_start(request_cb, notification_cb, eof_cb)
|
|
|
|
self._stream:read_start(function(data)
|
|
|
|
if not data then
|
|
|
|
return eof_cb()
|
|
|
|
end
|
|
|
|
local type, id_or_cb, method_or_error, args_or_result
|
|
|
|
local pos = 1
|
|
|
|
local len = #data
|
|
|
|
while pos <= len do
|
2023-12-04 15:32:39 -07:00
|
|
|
type, id_or_cb, method_or_error, args_or_result, pos = self._session:receive(data, pos)
|
2023-01-31 15:35:04 -07:00
|
|
|
if type == 'request' or type == 'notification' then
|
|
|
|
if type == 'request' then
|
2023-12-04 15:32:39 -07:00
|
|
|
request_cb(method_or_error, args_or_result, Response.new(self, id_or_cb))
|
2023-01-31 15:35:04 -07:00
|
|
|
else
|
|
|
|
notification_cb(method_or_error, args_or_result)
|
|
|
|
end
|
|
|
|
elseif type == 'response' then
|
|
|
|
if method_or_error == mpack.NIL then
|
|
|
|
method_or_error = nil
|
|
|
|
else
|
|
|
|
args_or_result = nil
|
|
|
|
end
|
|
|
|
id_or_cb(method_or_error, args_or_result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function MsgpackRpcStream:read_stop()
|
|
|
|
self._stream:read_stop()
|
|
|
|
end
|
|
|
|
|
|
|
|
function MsgpackRpcStream:close(signal)
|
|
|
|
self._stream:close(signal)
|
|
|
|
end
|
|
|
|
|
|
|
|
return MsgpackRpcStream
|