Skip to content

Commit

Permalink
Maybe fix that random issue with the connection?
Browse files Browse the repository at this point in the history
  • Loading branch information
IntQuant committed Dec 1, 2024
1 parent 9901132 commit eb71f44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ewext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ fn netmanager_send(lua: LuaState) -> eyre::Result<()> {
Ok(())
}

fn netmanager_flush(_lua: LuaState) -> eyre::Result<()> {
let mut binding = NETMANAGER.lock().unwrap();
let netmanager = binding.as_mut().unwrap();
netmanager.flush()
}

impl LuaFnRet for InitKV {
fn do_return(self, lua: LuaState) -> c_int {
lua.create_table(2, 0);
Expand Down Expand Up @@ -306,6 +312,7 @@ pub unsafe extern "C" fn luaopen_ewext0(lua: *mut lua_State) -> c_int {
add_lua_fn!(netmanager_connect);
add_lua_fn!(netmanager_recv);
add_lua_fn!(netmanager_send);
add_lua_fn!(netmanager_flush);

add_lua_fn!(module_on_world_update);
}
Expand Down
21 changes: 18 additions & 3 deletions ewext/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ impl NetManager {
}

pub(crate) fn switch_to_non_blocking(&mut self) -> eyre::Result<()> {
self.ws.get_mut().set_read_timeout(None)?;
self.ws.get_mut().set_nonblocking(true)?;
let stream_ref = self.ws.get_mut();
stream_ref.set_nonblocking(true)?;
stream_ref.set_read_timeout(Some(Duration::from_millis(1)))?;
// Set write timeout to a somewhat high value just in case.
stream_ref.set_write_timeout(Some(Duration::from_secs(5)))?;
Ok(())
}

pub(crate) fn send(&mut self, msg: &NoitaOutbound) -> eyre::Result<()> {
self.ws
.send(tungstenite::Message::Binary(bitcode::encode(msg)))?;
.write(tungstenite::Message::Binary(bitcode::encode(msg)))?;
Ok(())
}

Expand All @@ -55,6 +58,18 @@ impl NetManager {
}
}
}

pub(crate) fn flush(&mut self) -> eyre::Result<()> {
match self.ws.flush() {
Ok(()) => Ok(()),
Err(tungstenite::Error::Io(err))
if err.kind() == ErrorKind::WouldBlock || err.kind() == ErrorKind::TimedOut =>
{
Ok(())
}
Err(err) => Err(err.into()),
}
}
}

impl Drop for NetManager {
Expand Down
1 change: 1 addition & 0 deletions quant.ew/files/core/net.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function net.update()
end
handle_message(msg)
end
ewext.netmanager_flush()
end

function net.init()
Expand Down

0 comments on commit eb71f44

Please sign in to comment.