From 67ee5021bdb1279aa3c0723fabe4d8e6b09b56dc Mon Sep 17 00:00:00 2001 From: Ottatop Date: Fri, 23 Feb 2024 18:10:28 -0600 Subject: [PATCH] Port Lua client fix --- api/lua/pinnacle/grpc/client.lua | 56 ++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/api/lua/pinnacle/grpc/client.lua b/api/lua/pinnacle/grpc/client.lua index b700913..6142d26 100644 --- a/api/lua/pinnacle/grpc/client.lua +++ b/api/lua/pinnacle/grpc/client.lua @@ -136,19 +136,27 @@ function client.server_streaming_request(grpc_request_params, callback) client.loop:wrap(function() for response_body in stream:each_chunk() do - -- Skip the 1-byte compressed flag and the 4-byte message length ---@diagnostic disable-next-line: redefined-local - local response_body = response_body:sub(6) + local response_body = response_body - ---@diagnostic disable-next-line: redefined-local - local success, obj = pcall(pb.decode, response_type, response_body) - if not success then - print(obj) - os.exit(1) + while response_body:len() > 0 do + local msg_len = string.unpack(">I4", response_body:sub(2, 5)) + + -- Skip the 1-byte compressed flag and the 4-byte message length + response_body = response_body:sub(6, 6 + msg_len - 1) + + ---@diagnostic disable-next-line: redefined-local + local success, obj = pcall(pb.decode, response_type, response_body) + if not success then + print(obj) + os.exit(1) + end + + local response = obj + callback(response) + + response_body = response_body:sub(msg_len + 1) end - - local response = obj - callback(response) end local trailers = stream:get_headers() @@ -184,19 +192,27 @@ function client.bidirectional_streaming_request(grpc_request_params, callback) client.loop:wrap(function() for response_body in stream:each_chunk() do - -- Skip the 1-byte compressed flag and the 4-byte message length ---@diagnostic disable-next-line: redefined-local - local response_body = response_body:sub(6) + local response_body = response_body - ---@diagnostic disable-next-line: redefined-local - local success, obj = pcall(pb.decode, response_type, response_body) - if not success then - print(obj) - os.exit(1) + while response_body:len() > 0 do + local msg_len = string.unpack(">I4", response_body:sub(2, 5)) + + -- Skip the 1-byte compressed flag and the 4-byte message length + response_body = response_body:sub(6, 6 + msg_len - 1) + + ---@diagnostic disable-next-line: redefined-local + local success, obj = pcall(pb.decode, response_type, response_body) + if not success then + print(obj) + os.exit(1) + end + + local response = obj + callback(response) + + response_body = response_body:sub(msg_len + 1) end - - local response = obj - callback(response) end local trailers = stream:get_headers()