From c9aebfcb3eb67ae7b54098615004d9929abfd64f Mon Sep 17 00:00:00 2001 From: Markus Ast Date: Thu, 11 Nov 2021 12:02:02 +0100 Subject: [PATCH] pass throughput limit setting to hook env --- lua/grpc-hook.lua | 3 +++ lua/grpc-mission.lua | 13 ++++++++++--- lua/grpc.lua | 30 +----------------------------- src/hot_reload.rs | 12 ++---------- src/lib.rs | 12 ++---------- src/server.rs | 32 ++++++++++++++++++++++++++------ 6 files changed, 44 insertions(+), 58 deletions(-) diff --git a/lua/grpc-hook.lua b/lua/grpc-hook.lua index 4c682ea0..6588f6ba 100644 --- a/lua/grpc-hook.lua +++ b/lua/grpc-hook.lua @@ -13,6 +13,9 @@ local function load() if not GRPC.dllPath then GRPC.dllPath = lfs.writedir() .. [[Mods\tech\DCS-gRPC\]] end + if GRPC.throughputLimit == nil or GRPC.throughputLimit == 0 or not type(GRPC.throughputLimit) == "number" then + GRPC.throughputLimit = 600 + end -- Let DCS know where to find the DLLs if not string.find(package.cpath, "DCS-gRPC") then diff --git a/lua/grpc-mission.lua b/lua/grpc-mission.lua index 0e8a4a35..bd409f51 100644 --- a/lua/grpc-mission.lua +++ b/lua/grpc-mission.lua @@ -1,4 +1,4 @@ --- Allow manually setting GRPC and path settings. +-- Set default settings. if not _G.GRPC then _G.GRPC = {} end @@ -8,16 +8,23 @@ end if not GRPC.dllPath then GRPC.dllPath = lfs.writedir() .. [[Mods\tech\DCS-gRPC\]] end +if GRPC.throughputLimit == nil or GRPC.throughputLimit == 0 or not type(GRPC.throughputLimit) == "number" then + GRPC.throughputLimit = 600 +end -- Let DCS know where to find the DLLs package.cpath = package.cpath .. GRPC.dllPath .. [[?.dll;]] --- Make paths available to gRPC hook +-- Make settings available to gRPC hook local file, err = io.open(lfs.writedir() .. [[Data\dcs-grpc.lua]], "w") if err then env.error("[GRPC] Error writing config") else - file:write("luaPath = [[" .. GRPC.luaPath .. "]]\ndllPath = [[" .. GRPC.dllPath .. "]]\n") + file:write( + "luaPath = [[" .. GRPC.luaPath .. "]]\n" + .. "dllPath = [[" .. GRPC.dllPath .. "]]\n" + .. "throughputLimit = [[" .. GRPC.throughputLimit .. "]]\n" + ) file:flush() file:close() end diff --git a/lua/grpc.lua b/lua/grpc.lua index 453ae1ae..fca4b44e 100644 --- a/lua/grpc.lua +++ b/lua/grpc.lua @@ -4,34 +4,6 @@ if isMissionEnv then env.info("[GRPC] mission loading ...") end --- --- set default settings --- - -if _G.GRPC == nil then - GRPC = {} -end - -if GRPC.luaPath == nil then - GRPC.luaPath = lfs.writedir()..[[Scripts\DCS-gRPC\]] -end -if GRPC.dllPath == nil then - GRPC.dllPath = lfs.writedir()..[[Mods\Tech\DCS-gRPC\]] -end -if GRPC.evalEnabled == nil then - GRPC.evalEnabled = false -end -if GRPC.host == nil then - GRPC.host = "127.0.0.1" -end -if GRPC.port == nil then - GRPC.port = 50051 -end -GRPC.debug = GRPC.debug == true -if GRPC.throughputLimit == nil or GRPC.throughputLimit == 0 or not type(GRPC.throughputLimit) == "number" then - GRPC.throughputLimit = 600 -end - -- -- load and start RPC -- @@ -288,7 +260,7 @@ else -- hook env frame = 0 local ok, err = pcall(next) if not ok then - log.write("[GRPC]", log.ERROR, "Error retrieving next command: "..tostring(err)) + GRPC.logError("Error retrieving next command: "..tostring(err)) end end end diff --git a/src/hot_reload.rs b/src/hot_reload.rs index cfe218d6..70a46613 100644 --- a/src/hot_reload.rs +++ b/src/hot_reload.rs @@ -12,16 +12,8 @@ use once_cell::sync::Lazy; static LIBRARY: Lazy>> = Lazy::new(|| RwLock::new(None)); -pub fn start(lua: &Lua, config: Value) -> LuaResult<()> { +pub fn start(lua: &Lua, config: Config) -> LuaResult<()> { let lib_path = { - let config: Config = match lua.from_value(config.clone()) { - Ok(event) => event, - Err(err) => { - log::error!("failed to deserialize config: {}", err); - return Ok(()); - } - }; - let mut lib_path = PathBuf::from(&config.dll_path); lib_path.push("dcs_grpc.dll"); lib_path @@ -34,7 +26,7 @@ pub fn start(lua: &Lua, config: Value) -> LuaResult<()> { let mut lib = LIBRARY.write().unwrap(); let lib = lib.get_or_insert(new_lib); - let f: Symbol LuaResult<()>> = unsafe { + let f: Symbol LuaResult<()>> = unsafe { lib.get(b"start") .map_err(|err| mlua::Error::ExternalError(Arc::new(err)))? }; diff --git a/src/lib.rs b/src/lib.rs index 88eabb75..9f615ef6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,27 +68,19 @@ pub fn init(config: &Config) { } #[no_mangle] -pub fn start(lua: &Lua, config: Value) -> LuaResult<()> { +pub fn start(_: &Lua, config: Config) -> LuaResult<()> { { if SERVER.read().unwrap().is_some() { return Ok(()); } } - let config: Config = match lua.from_value(config) { - Ok(event) => event, - Err(err) => { - log::error!("failed to deserialize config: {}", err); - return Ok(()); - } - }; - init(&config); log::info!("Starting ..."); let mut server = - Server::new(config).map_err(|err| mlua::Error::ExternalError(Arc::new(err)))?; + Server::new(&config).map_err(|err| mlua::Error::ExternalError(Arc::new(err)))?; server.run_in_background(); *(SERVER.write().unwrap()) = Some(server); diff --git a/src/server.rs b/src/server.rs index 535559f3..b05b74aa 100644 --- a/src/server.rs +++ b/src/server.rs @@ -26,26 +26,30 @@ pub struct Server { #[derive(Clone)] struct ServerState { addr: SocketAddr, - config: Config, + eval_enabled: bool, ipc_mission: IPC, ipc_hook: IPC<()>, chat: Chat, stats: Stats, } -#[derive(Clone, Deserialize, Serialize)] +#[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Config { pub write_dir: String, pub dll_path: String, + #[serde(default = "default_host")] pub host: String, + #[serde(default = "default_port")] pub port: u16, + #[serde(default)] pub debug: bool, + #[serde(default)] pub eval_enabled: bool, } impl Server { - pub fn new(config: Config) -> Result { + pub fn new(config: &Config) -> Result { let ipc_mission = IPC::new(); let ipc_hook = IPC::new(); let runtime = Runtime::new()?; @@ -55,7 +59,7 @@ impl Server { after_shutdown: None, state: ServerState { addr: format!("{}:{}", config.host, config.port).parse()?, - config, + eval_enabled: config.eval_enabled, ipc_mission, ipc_hook, chat: Chat::default(), @@ -143,7 +147,7 @@ async fn try_run( let ServerState { addr, - config, + eval_enabled, ipc_mission, ipc_hook, chat, @@ -153,7 +157,7 @@ async fn try_run( let mut mission_rpc = MissionRpc::new(ipc_mission, stats.clone(), shutdown_signal.clone()); let mut hook_rpc = HookRpc::new(ipc_hook, chat, stats, shutdown_signal.clone()); - if config.eval_enabled { + if eval_enabled { mission_rpc.enable_eval(); hook_rpc.enable_eval(); } @@ -175,3 +179,19 @@ pub enum StartError { #[error(transparent)] AddrParse(#[from] std::net::AddrParseError), } + +fn default_host() -> String { + String::from("127.0.0.1") +} + +fn default_port() -> u16 { + 50051 +} + +impl<'lua> mlua::FromLua<'lua> for Config { + fn from_lua(lua_value: mlua::Value<'lua>, lua: &'lua mlua::Lua) -> mlua::Result { + use mlua::LuaSerdeExt; + let config: Config = lua.from_value(lua_value)?; + Ok(config) + } +}