unity grpc for lua using tolua
(Work In Progress)
With grpc-tolua, you can use grpc in Lua in Unity.
It uses tolua to bind Unity C# and Lua.
- only grpc client, no server
local grpctolua = require('grpctolua')
local channel = grpctolua.new_channel('localhost:50051')
local client = grpctolua.new_client(channel, 'routeguide.RouteGuide')
grpctolua.load_descriptor_set_from_file('route_guide.pb')
function TestGetFeature()
coroutine.start(CoGetFeature)
end
function TestListFeatures()
coroutine.start(CoListFeatures)
end
function TestRecordRoute()
coroutine.start(CoRecordRoute)
end
function TestRouteChat()
coroutine.start(CoRouteChat)
end
function CoGetFeature()
local feature = client:await_call('GetFeature', GetPoint(409146138, -746188906))
print('feature: '..DumpTable(feature))
end
function CoListFeatures()
local req = {lo = GetPoint(400000000, -750000000), hi = GetPoint(420000000, -730000000)}
local call = client:call('ListFeatures', req)
call:wait_for_each_response(function(rsp)
print(DumpTable(rsp))
end)
end
function CoRecordRoute()
local call = client:call('RecordRoute')
coroutine.start(function()
local rsp = call:wait_for_response()
print('RecordRoute resonse: '..DumpTable(rsp))
end)
local features = GetFeatures()
for _, f in ipairs(features) do
print('call:await_write(location)...')
call:await_write(f.location)
end
call:await_complete()
end
function CoRouteChat()
local call = client:call('RouteChat')
coroutine.start(function() CoPrintResponses(call) end)
local notes = GetRouteNotes()
for _, n in ipairs(notes) do
call:await_write(n)
end
call:await_complete()
end
function CoPrintResponses(call)
call:wait_for_each_response(function(rsp)
print('RouteChat response: '..DumpTable(rsp))
end)
end
- Copy grpc_unity_package into Assets dir
- Open
Assets\GrpcToLua\Examples\RouteGuide\RouteGuide.unity
- Menu: Lua -> Generate All
- Run a server on port 50051. For example go server
- Play
It can only run on Windows 64 because only Plugins\x86_64\tolua.dll is updated to support lua-protobuf. On other platforms it will be error with: attempt to call field 'load' (a nil value). Please Integrate starwing/lua-protobuf for other platforms.
- Update grpc_unity_package
- Update to the latest tolua
- Copy Assets, Unity5.x, Luajit64, Luajit from tolua
- Copy
coroutine.wait_until(conditionFunc, co)
from https://github.com/woshihuo12/tolua- See commit 441d9f
- Integrate starwing/lua-protobuf
- Update plugins
- only Plugins\x86_64\tolua.dll is lua-protobuf ready
- Update plugins
- Add in
Assets\Editor\Custom\CustomSetting.cs customTypeList
public static BindType[] customTypeList = { ... // GrpcToLua _GT(typeof(Grpc.Core.Channel)), _GT(typeof(Grpc.Core.ChannelCredentials)), _GT(typeof(Grpc.Core.Status)), _GT(typeof(GrpcToLua.AsyncUnaryCall)), _GT(typeof(GrpcToLua.AsyncServerStreamingCall)), _GT(typeof(GrpcToLua.AsyncClientStreamingCall)), _GT(typeof(GrpcToLua.AsyncDuplexStreamingCall)), _GT(typeof(GrpcToLua.Client)), _GT(typeof(GrpcToLua.DescriptorSetLoader)), _GT(typeof(GrpcToLua.InsecureCredentials)), _GT(typeof(System.Runtime.CompilerServices.TaskAwaiter)), _GT(typeof(System.Runtime.CompilerServices.TaskAwaiter<byte[]>)), _GT(typeof(System.Threading.Tasks.Task<byte[]>)), }
- Add in
Assets\ToLua\Editor\ToLuaExport.cs memberFilter
public static List<string> memberFilter = new List<string> { "Task.IsCompletedSuccessfully", ... }
Make sure to add lua search path like this:
lua = new LuaState();
lua.AddSearchPath(Application.dataPath + "/GrpcToLua/Lua");
Make sure pb is opened:
lua.OpenLibs(LuaDLL.luaopen_pb);
Use starwing/lua-protobuf