local c_channel = grpc.channel("localhost:50051")
local stub = grcp.service_stub(c_channel, "routeguide.RouteGuide")
Call service method
Sync call
Simple RPC: sync_get_feature()
local feature = stub.sync_request("GetFeature", point(409146138, -746188906))
Server-side streaming RPC: sync_list_features()
local sync_reader = stub:sync_request_read("ListFeatures", rect)
whiletruedolocal feature = sync_reader.read_one()
ifnot feature thenbreakendprint("Found feature: "..inspect(feature))
end-- while
Client-side streaming RPC: sync_record_route()
local sync_writer = stub.sync_request_write("RecordRoute")
for i =1, 10dolocal feature = db.get_rand_feature()
local loc =assert(feature.location)
ifnot sync_writer.write(loc) thenbreakend-- ifend-- for-- Recv status and reponse.local summary, error_str, status_code = sync_writer.close()
if summary thenprint_route_sumary(summary)
end
Bidirectional streaming RPC: sync_route_chat()
local sync_rdwr = stub.sync_request_rdwr("RouteChat")
for _, note inipairs(notes) do
sync_rdwr.write(note)
end
sync_rdwr.close_writing()
-- read remainingwhiletruedolocal server_note = sync_rdwr.read_one()
ifnot server_note thenbreakendprint("Got message: "..inspect(server_note))
end-- while
Async call
Simple RPC: get_feature_async()
stub.async_request("GetFeature", point(409146138, -746188906),
function(resp)
print("Get feature: "..inspect(resp))
stub.shutdown() -- to returnend)
stub.run() -- run async requests until stub.shutdown()
stub.set_error_cb(function(error_str, status_code) ... end)
before async_request()
Run the stub
Async calls need run()
stub.run() -- run async requests until stub.shutdown()
stub.shutdown() ends stub.run().
Server-side streaming RPC: list_features_async()
stub.async_request_read("ListFeatures", rect,
function(f)
assert("table"==type(f))
print(string.format("Got feature %s at %f,%f", f.name,
f.location.latitude/kCoordFactor, f.location.longitude/kCoordFactor))
end,
function(error_str, status_code)
assert("number"==type(status_code))
stub.shutdown() -- To break Run().end)
stub.run() -- until stub.shutdown()
Client-side streaming RPC: record_route_async()
local async_writer = stub.async_request_write("RecordRoute")
for i =1, 10dolocal f = db.get_rand_feature()
local loc = f.locationifnot async_writer:write(loc) dobreakend-- Broken stream.end-- for-- Recv reponse and status.
async_writer.close(
function(resp, error_str, status_code)
if resp thenprint_route_summary(resp)
end-- if
stub.shutdown() -- to break run()end)
stub.run() -- until stutdown()
Bidirectional streaming RPC: route_chat_async()
local rdwr = stub.async_request_rdwr("RouteChat",
function(error_str, status_code)
stub.shutdown() -- to break run()end)
for _, note inipairs(notes) do
rdwr.write(note)
end
rdwr.close_writing() -- Optional.
rdwr.read_each(function(server_note)
assert("table"==type(server_note))
print("Got message: "..inspect(server_note))
end)
stub.run() -- until shutdown()
local svr = grpc.server()
svr:add_listening_port("0.0.0.0:50051")
-- Service implementation is a table.local service =require("route_guide_service")
svr:register_service("routeguide.RouteGuide", service)
svr:run()
Implement service
Service is a table with the service functions defined in the proto file.
The function name must be the same as in the proto file.
The function parameters are different for different RPC method types.
Simple RPC: GetFeature()
--- Simple RPC method.-- @tab request-- @tparam Replier replierfunctionM.GetFeature(request, replier)
assert("table"==type(request))
assert("table"==type(replier))
local name =get_feature_name(request)
local response = { name = name, location = request }
replier:reply(response);
end-- GetFeature()
replier can be copied and reply() later.
Server-side streaming RPC: ListFeatures()
--- Server-to-client streaming method.-- @table rectangle-- @tparam Writer writerfunctionM.ListFeatures(rectangle, writer)
assert("table"==type(rectangle))
assert("table"==type(writer))
...for _, f inipairs(db.features) dolocal l = f.locationif l...thenifnot writer.write(f) thenprint("Failed to write.")
breakend-- if not writer.write()end-- if lend-- for _, f
writer.close()
end-- ListFeatures()
Note: If you are using grpc only as client, grpc-tolua
is another option.
I think it is easy to integrate grpc-lua into Unity,
but I have no time to do this. Work is:
Use C version of Lua
Support Lua5.1 and LuaJIT
Build for iOS and Android
Unity uses Lua library compiled as C.
If use lua.exe and lua.lib compiled as C,
C++ objects on stack must be destructed correctly on error.
See LuaIntf error handling.
Building with Lua51 or LuaJIT library should succeed.
LuaPbIntf, which is used to encode and decode protobuf messages,
need to be recompiled with Lua51 or LuaJIT.
请发表评论