static public int sendMessage(IntPtr state) { luaProtobuf self = (luaProtobuf)LuaObject.checkSelf(state); //消息ID int idMsg = LuaDLL.luaL_checkinteger(state, 2); //消息内容 LuaTypes type = LuaDLL.lua_type(state, 3); if (type != LuaTypes.LUA_TTABLE) { LogSys.LogError("LUA message error! idMsg = " + idMsg.ToString()); return(0); } LuaTable msgTbl = (LuaTable)LuaObject.checkVar(state, 3); //生成消息 luaMessage msg = luaProtoHelper.readLuaMessage(idMsg, msgTbl); //写入流并发出 using (var stream = new System.IO.MemoryStream()) { using (var protoWriter = new luaProtoWriter(stream)) { protoWriter.writeLuaMessage(msg); protoWriter.close(); } network.protobuf.ClientNetwork.Instance.SendLuaMsg(idMsg, stream); } LuaObject.pushValue(state, true); return(1); }
public void receiveMsg(int idMsg, System.IO.MemoryStream stream) { if (!_luaMsgCallbackMap.ContainsKey(idMsg)) { return; } luaMessage msgRev = null; using (luaProtoReader reader = luaProtoReader.createLuaProtoReader(stream)) { if (reader != null) { msgRev = reader.readLuaMessage(idMsg); } } if (msgRev == null) { return; } //将消息回传给lua回调 string luaCallbackFunc = ""; if (_luaMsgCallbackMap.TryGetValue(idMsg, out luaCallbackFunc)) { LuaState state = luaSvrManager.getInstance().luaSvr.luaState; LuaFunction luafunc = state.getFunction(luaCallbackFunc); if (luafunc == null) { return; } int error = LuaObject.pushTry(state.L); //错误处理 LuaDLL.lua_pushinteger(state.L, idMsg); //压入idMsg luaProtoHelper.createLuaTable(state, msgRev); //压入数据表 luafunc.pcall(2, error); LuaDLL.lua_remove(state.L, error); } }