Skip to content

Commit

Permalink
Merge pull request #42 from mysteriumnetwork/fix-windows-status-listener
Browse files Browse the repository at this point in the history
Fix windows status listener
  • Loading branch information
KristijanMitrik authored Sep 2, 2024
2 parents ee9dca0 + 49d2d05 commit 41f9215
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
9 changes: 8 additions & 1 deletion example/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(BINARY_NAME "wireguard_dart_example")

# Explicitly opt in to modern CMake behaviors to avoid warnings with recent
# versions of CMake.
cmake_policy(SET CMP0063 NEW)
cmake_policy(VERSION 3.14...3.25)

# Define build configuration option.
get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
Expand Down Expand Up @@ -52,6 +52,7 @@ add_subdirectory(${FLUTTER_MANAGED_DIR})
# Application build; see runner/CMakeLists.txt.
add_subdirectory("runner")


# Generated plugin build rules, which manage building the plugins and adding
# them to the application.
include(flutter/generated_plugins.cmake)
Expand Down Expand Up @@ -86,6 +87,12 @@ if(PLUGIN_BUNDLED_LIBRARIES)
COMPONENT Runtime)
endif()

# Copy the native assets provided by the build.dart from all packages.
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/")
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
COMPONENT Runtime)

# Fully re-copy the assets directory on each build to avoid having stale files
# from a previous install.
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")
Expand Down
10 changes: 10 additions & 0 deletions example/windows/runner/flutter_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ bool FlutterWindow::OnCreate() {
}
RegisterPlugins(flutter_controller_->engine());
SetChildContent(flutter_controller_->view()->GetNativeWindow());

flutter_controller_->engine()->SetNextFrameCallback([&]() {
this->Show();
});

// Flutter can complete the first frame before the "show window" callback is
// registered. The following call ensures a frame is pending to ensure the
// window is shown. It is a no-op if the first frame hasn't completed yet.
flutter_controller_->ForceRedraw();

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion example/windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
FlutterWindow window(project);
Win32Window::Point origin(10, 10);
Win32Window::Size size(1280, 720);
if (!window.CreateAndShow(L"wireguard_dart_example", origin, size)) {
if (!window.Create(L"wireguard_dart_example", origin, size)) {
return EXIT_FAILURE;
}
window.SetQuitOnClose(true);
Expand Down
9 changes: 5 additions & 4 deletions example/windows/runner/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
}
int target_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, nullptr, 0, nullptr, nullptr);
-1, nullptr, 0, nullptr, nullptr)
-1; // remove the trailing null character
int input_length = (int)wcslen(utf16_string);
std::string utf8_string;
if (target_length == 0 || target_length > utf8_string.max_size()) {
if (target_length <= 0 || target_length > utf8_string.max_size()) {
return utf8_string;
}
utf8_string.resize(target_length);
int converted_length = ::WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
-1, utf8_string.data(),
target_length, nullptr, nullptr);
input_length, utf8_string.data(), target_length, nullptr, nullptr);
if (converted_length == 0) {
return std::string();
}
Expand Down
30 changes: 17 additions & 13 deletions windows/connection_status_observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <thread>

#include "connection_status.h"

namespace wireguard_dart {

ConnectionStatusObserver::ConnectionStatusObserver() {}
Expand All @@ -17,7 +16,22 @@ void ConnectionStatusObserver::StartObserving(std::wstring service_name) {
if (m_running.load() == true) {
return;
}
watch_thread = std::thread(&ConnectionStatusObserver::StartObservingThreadProc, this, service_name);
if (service_name.size() > 0) {
m_service_name = service_name;
}
SC_HANDLE service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (service_manager == NULL) {
return;
}
SC_HANDLE service = OpenService(service_manager, &m_service_name[0], SERVICE_QUERY_STATUS | SERVICE_INTERROGATE);
if (service != NULL) {
m_running.store(true);
watch_thread = std::thread(&ConnectionStatusObserver::StartObservingThreadProc, this, service_manager, service);
} else {
CloseServiceHandle(service_manager);
m_running.store(false);
return;
}
}

void ConnectionStatusObserver::StopObserving() { m_watch_thread_stop.store(true); }
Expand All @@ -29,17 +43,7 @@ void ConnectionStatusObserver::Shutdown() {
}
}

void ConnectionStatusObserver::StartObservingThreadProc(std::wstring service_name) {
m_running.store(true);
SC_HANDLE service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (service_manager == NULL) {
return;
}
SC_HANDLE service = OpenService(service_manager, &service_name[0], SERVICE_QUERY_STATUS | SERVICE_INTERROGATE);
if (service == NULL) {
CloseServiceHandle(service_manager);
return;
}
void ConnectionStatusObserver::StartObservingThreadProc(SC_HANDLE service_manager, SC_HANDLE service) {
SERVICE_NOTIFY s_notify = {0};
s_notify.dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
s_notify.pfnNotifyCallback = &ServiceNotifyCallback;
Expand Down
3 changes: 2 additions & 1 deletion windows/connection_status_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ class ConnectionStatusObserver : public flutter::StreamHandler<flutter::Encodabl
private:
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> sink_;
PSC_NOTIFICATION_REGISTRATION subscription_;
void StartObservingThreadProc(std::wstring service_name);
void StartObservingThreadProc(SC_HANDLE service_manager, SC_HANDLE service);

void Shutdown();
std::thread watch_thread;
std::atomic_bool m_watch_thread_stop;
std::atomic_bool m_running;
std::wstring m_service_name;
};

} // namespace wireguard_dart
Expand Down
2 changes: 2 additions & 0 deletions windows/wireguard_dart_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@ void WireguardDartPlugin::HandleMethodCall(const flutter::MethodCall<flutter::En
result->Error(std::string(e.what()));
return;
}
this->connection_status_observer_.get()->StartObserving(L"");
try {
tunnel_service->Start();
} catch (std::exception &e) {
result->Error(std::string(e.what()));
return;
}

result->Success();
return;
}
Expand Down

0 comments on commit 41f9215

Please sign in to comment.