Skip to content

Commit 41f9215

Browse files
Merge pull request #42 from mysteriumnetwork/fix-windows-status-listener
Fix windows status listener
2 parents ee9dca0 + 49d2d05 commit 41f9215

7 files changed

+45
-20
lines changed

example/windows/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(BINARY_NAME "wireguard_dart_example")
88

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

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

55+
5556
# Generated plugin build rules, which manage building the plugins and adding
5657
# them to the application.
5758
include(flutter/generated_plugins.cmake)
@@ -86,6 +87,12 @@ if(PLUGIN_BUNDLED_LIBRARIES)
8687
COMPONENT Runtime)
8788
endif()
8889

90+
# Copy the native assets provided by the build.dart from all packages.
91+
set(NATIVE_ASSETS_DIR "${PROJECT_BUILD_DIR}native_assets/windows/")
92+
install(DIRECTORY "${NATIVE_ASSETS_DIR}"
93+
DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"
94+
COMPONENT Runtime)
95+
8996
# Fully re-copy the assets directory on each build to avoid having stale files
9097
# from a previous install.
9198
set(FLUTTER_ASSET_DIR_NAME "flutter_assets")

example/windows/runner/flutter_window.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ bool FlutterWindow::OnCreate() {
2626
}
2727
RegisterPlugins(flutter_controller_->engine());
2828
SetChildContent(flutter_controller_->view()->GetNativeWindow());
29+
30+
flutter_controller_->engine()->SetNextFrameCallback([&]() {
31+
this->Show();
32+
});
33+
34+
// Flutter can complete the first frame before the "show window" callback is
35+
// registered. The following call ensures a frame is pending to ensure the
36+
// window is shown. It is a no-op if the first frame hasn't completed yet.
37+
flutter_controller_->ForceRedraw();
38+
2939
return true;
3040
}
3141

example/windows/runner/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
2727
FlutterWindow window(project);
2828
Win32Window::Point origin(10, 10);
2929
Win32Window::Size size(1280, 720);
30-
if (!window.CreateAndShow(L"wireguard_dart_example", origin, size)) {
30+
if (!window.Create(L"wireguard_dart_example", origin, size)) {
3131
return EXIT_FAILURE;
3232
}
3333
window.SetQuitOnClose(true);

example/windows/runner/utils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,17 @@ std::string Utf8FromUtf16(const wchar_t* utf16_string) {
4747
}
4848
int target_length = ::WideCharToMultiByte(
4949
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
50-
-1, nullptr, 0, nullptr, nullptr);
50+
-1, nullptr, 0, nullptr, nullptr)
51+
-1; // remove the trailing null character
52+
int input_length = (int)wcslen(utf16_string);
5153
std::string utf8_string;
52-
if (target_length == 0 || target_length > utf8_string.max_size()) {
54+
if (target_length <= 0 || target_length > utf8_string.max_size()) {
5355
return utf8_string;
5456
}
5557
utf8_string.resize(target_length);
5658
int converted_length = ::WideCharToMultiByte(
5759
CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
58-
-1, utf8_string.data(),
59-
target_length, nullptr, nullptr);
60+
input_length, utf8_string.data(), target_length, nullptr, nullptr);
6061
if (converted_length == 0) {
6162
return std::string();
6263
}

windows/connection_status_observer.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <thread>
77

88
#include "connection_status.h"
9-
109
namespace wireguard_dart {
1110

1211
ConnectionStatusObserver::ConnectionStatusObserver() {}
@@ -17,7 +16,22 @@ void ConnectionStatusObserver::StartObserving(std::wstring service_name) {
1716
if (m_running.load() == true) {
1817
return;
1918
}
20-
watch_thread = std::thread(&ConnectionStatusObserver::StartObservingThreadProc, this, service_name);
19+
if (service_name.size() > 0) {
20+
m_service_name = service_name;
21+
}
22+
SC_HANDLE service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
23+
if (service_manager == NULL) {
24+
return;
25+
}
26+
SC_HANDLE service = OpenService(service_manager, &m_service_name[0], SERVICE_QUERY_STATUS | SERVICE_INTERROGATE);
27+
if (service != NULL) {
28+
m_running.store(true);
29+
watch_thread = std::thread(&ConnectionStatusObserver::StartObservingThreadProc, this, service_manager, service);
30+
} else {
31+
CloseServiceHandle(service_manager);
32+
m_running.store(false);
33+
return;
34+
}
2135
}
2236

2337
void ConnectionStatusObserver::StopObserving() { m_watch_thread_stop.store(true); }
@@ -29,17 +43,7 @@ void ConnectionStatusObserver::Shutdown() {
2943
}
3044
}
3145

32-
void ConnectionStatusObserver::StartObservingThreadProc(std::wstring service_name) {
33-
m_running.store(true);
34-
SC_HANDLE service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
35-
if (service_manager == NULL) {
36-
return;
37-
}
38-
SC_HANDLE service = OpenService(service_manager, &service_name[0], SERVICE_QUERY_STATUS | SERVICE_INTERROGATE);
39-
if (service == NULL) {
40-
CloseServiceHandle(service_manager);
41-
return;
42-
}
46+
void ConnectionStatusObserver::StartObservingThreadProc(SC_HANDLE service_manager, SC_HANDLE service) {
4347
SERVICE_NOTIFY s_notify = {0};
4448
s_notify.dwVersion = SERVICE_NOTIFY_STATUS_CHANGE;
4549
s_notify.pfnNotifyCallback = &ServiceNotifyCallback;

windows/connection_status_observer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ class ConnectionStatusObserver : public flutter::StreamHandler<flutter::Encodabl
2727
private:
2828
std::unique_ptr<flutter::EventSink<flutter::EncodableValue>> sink_;
2929
PSC_NOTIFICATION_REGISTRATION subscription_;
30-
void StartObservingThreadProc(std::wstring service_name);
30+
void StartObservingThreadProc(SC_HANDLE service_manager, SC_HANDLE service);
3131

3232
void Shutdown();
3333
std::thread watch_thread;
3434
std::atomic_bool m_watch_thread_stop;
3535
std::atomic_bool m_running;
36+
std::wstring m_service_name;
3637
};
3738

3839
} // namespace wireguard_dart

windows/wireguard_dart_plugin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@ void WireguardDartPlugin::HandleMethodCall(const flutter::MethodCall<flutter::En
143143
result->Error(std::string(e.what()));
144144
return;
145145
}
146+
this->connection_status_observer_.get()->StartObserving(L"");
146147
try {
147148
tunnel_service->Start();
148149
} catch (std::exception &e) {
149150
result->Error(std::string(e.what()));
150151
return;
151152
}
153+
152154
result->Success();
153155
return;
154156
}

0 commit comments

Comments
 (0)