X-Git-Url: https://code.octet-stream.net/netwatcher/blobdiff_plain/244781f32194c2498ef0e3b8225a9ff9791624af..f5cae732095b884b50921e13827ef6d7375c7c3b:/src/watch_win.rs?ds=inline diff --git a/src/watch_win.rs b/src/watch_win.rs index a225f4a..a247916 100644 --- a/src/watch_win.rs +++ b/src/watch_win.rs @@ -23,7 +23,7 @@ struct WatchState { /// The last result that we captured, for diffing prev_list: List, /// User's callback - cb: Box, + cb: Box, } pub(crate) struct WatchHandle { @@ -39,7 +39,7 @@ impl Drop for WatchHandle { } } -pub(crate) fn watch_interfaces( +pub(crate) fn watch_interfaces( callback: F, ) -> Result { let state = Box::pin(Mutex::new(WatchState { @@ -60,17 +60,15 @@ pub(crate) fn watch_interfaces( }; match res { NO_ERROR => { - // Trigger an initial update. - // This is allowed to race with true updates because it - // will always calculate a diff and discard no-ops. - handle_notif(&mut state.lock().unwrap()); + // Trigger an initial update + handle_notif(&mut state.lock().unwrap(), crate::list::list_interfaces()?); // Then return the handle Ok(WatchHandle { hnd, _state: state }) } - ERROR_INVALID_HANDLE => Err(Error::Internal), - ERROR_INVALID_PARAMETER => Err(Error::Internal), - ERROR_NOT_ENOUGH_MEMORY => Err(Error::Internal), - _ => Err(Error::Internal), // TODO: Use FormatMessage and get real error + ERROR_INVALID_HANDLE => Err(Error::InvalidHandle), + ERROR_INVALID_PARAMETER => Err(Error::InvalidParameter), + ERROR_NOT_ENOUGH_MEMORY => Err(Error::NotEnoughMemory), + _ => Err(Error::UnexpectedWindowsResult(res.0)), } } @@ -81,18 +79,19 @@ unsafe extern "system" fn notif( ) { let state_ptr = ctx as *const Mutex; unsafe { - let state_guard = &mut *state_ptr.as_ref() + let state_guard = &mut *state_ptr + .as_ref() .expect("callback ctx should never be null") .lock() .unwrap(); - handle_notif(state_guard); + let Ok(new_list) = crate::list::list_interfaces() else { + return; + }; + handle_notif(state_guard, new_list); } } -fn handle_notif(state: &mut WatchState) { - let Ok(new_list) = crate::list::list_interfaces() else { - return; - }; +fn handle_notif(state: &mut WatchState, new_list: List) { if new_list == state.prev_list { return; }