Skip to content

Commit

Permalink
Merge pull request #74 from DCS-gRPC/hook-throughput
Browse files Browse the repository at this point in the history
pass throughput limit setting to hook env
  • Loading branch information
rkusa authored Nov 11, 2021
2 parents 221a599 + c9aebfc commit 3fc5161
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 58 deletions.
3 changes: 3 additions & 0 deletions lua/grpc-hook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions lua/grpc-mission.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Allow manually setting GRPC and path settings.
-- Set default settings.
if not _G.GRPC then
_G.GRPC = {}
end
Expand All @@ -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
Expand Down
30 changes: 1 addition & 29 deletions lua/grpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down Expand Up @@ -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
Expand Down
12 changes: 2 additions & 10 deletions src/hot_reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ use once_cell::sync::Lazy;

static LIBRARY: Lazy<RwLock<Option<Library>>> = 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
Expand All @@ -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<fn(lua: &Lua, config: Value) -> LuaResult<()>> = unsafe {
let f: Symbol<fn(lua: &Lua, config: Config) -> LuaResult<()>> = unsafe {
lib.get(b"start")
.map_err(|err| mlua::Error::ExternalError(Arc::new(err)))?
};
Expand Down
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 26 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,30 @@ pub struct Server {
#[derive(Clone)]
struct ServerState {
addr: SocketAddr,
config: Config,
eval_enabled: bool,
ipc_mission: IPC<StreamEventsResponse>,
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<Self, StartError> {
pub fn new(config: &Config) -> Result<Self, StartError> {
let ipc_mission = IPC::new();
let ipc_hook = IPC::new();
let runtime = Runtime::new()?;
Expand All @@ -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(),
Expand Down Expand Up @@ -143,7 +147,7 @@ async fn try_run(

let ServerState {
addr,
config,
eval_enabled,
ipc_mission,
ipc_hook,
chat,
Expand All @@ -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();
}
Expand All @@ -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<Self> {
use mlua::LuaSerdeExt;
let config: Config = lua.from_value(lua_value)?;
Ok(config)
}
}

0 comments on commit 3fc5161

Please sign in to comment.