-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mysteriumnetwork/33-conn-status-win
Windows real time connection status
- Loading branch information
Showing
17 changed files
with
382 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "connection_status.h" | ||
|
||
#include <windows.h> | ||
|
||
#include <string> | ||
|
||
namespace wireguard_dart { | ||
|
||
std::string ConnectionStatusToString(const ConnectionStatus status) { | ||
switch (status) { | ||
case ConnectionStatus::connected: | ||
return "connected"; | ||
case ConnectionStatus::disconnected: | ||
return "disconnected"; | ||
case ConnectionStatus::connecting: | ||
return "connecting"; | ||
case ConnectionStatus::disconnecting: | ||
return "disconnecting"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
|
||
ConnectionStatus ConnectionStatusFromWinSvcState(DWORD dwCurrentState) { | ||
switch (dwCurrentState) { | ||
case SERVICE_RUNNING: | ||
return ConnectionStatus::connected; | ||
case SERVICE_STOPPED: | ||
case SERVICE_PAUSED: | ||
return ConnectionStatus::disconnected; | ||
case SERVICE_START_PENDING: | ||
case SERVICE_CONTINUE_PENDING: | ||
return ConnectionStatus::connecting; | ||
case SERVICE_STOP_PENDING: | ||
case SERVICE_PAUSE_PENDING: | ||
return ConnectionStatus::disconnecting; | ||
default: | ||
return ConnectionStatus::unknown; | ||
} | ||
} | ||
|
||
} // namespace wireguard_dart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef WIREGUARD_DART_CONNECTION_STATUS_H | ||
#define WIREGUARD_DART_CONNECTION_STATUS_H | ||
|
||
#include <windows.h> | ||
|
||
#include <string> | ||
|
||
namespace wireguard_dart { | ||
|
||
enum ConnectionStatus { connected, disconnected, connecting, disconnecting, unknown }; | ||
|
||
std::string ConnectionStatusToString(const ConnectionStatus status); | ||
|
||
ConnectionStatus ConnectionStatusFromWinSvcState(DWORD dwCurrentState); | ||
|
||
} // namespace wireguard_dart | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "connection_status_observer.h" | ||
|
||
#include <winsvc.h> | ||
|
||
#include <iostream> | ||
#include <thread> | ||
|
||
#include "connection_status.h" | ||
|
||
namespace wireguard_dart { | ||
|
||
ConnectionStatusObserver::ConnectionStatusObserver() {} | ||
|
||
ConnectionStatusObserver::~ConnectionStatusObserver() { Shutdown(); } | ||
|
||
void ConnectionStatusObserver::StartObserving(std::wstring service_name) { | ||
if (m_running.load() == true) { | ||
return; | ||
} | ||
watch_thread = std::thread(&ConnectionStatusObserver::StartObservingThreadProc, this, service_name); | ||
} | ||
|
||
void ConnectionStatusObserver::StopObserving() { m_watch_thread_stop.store(true); } | ||
|
||
void ConnectionStatusObserver::Shutdown() { | ||
m_watch_thread_stop.store(true); | ||
if (watch_thread.joinable()) { | ||
watch_thread.join(); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
SERVICE_NOTIFY s_notify = {0}; | ||
s_notify.dwVersion = SERVICE_NOTIFY_STATUS_CHANGE; | ||
s_notify.pfnNotifyCallback = &ServiceNotifyCallback; | ||
s_notify.pContext = static_cast<void*>(this); | ||
while (m_watch_thread_stop.load() == false) { | ||
if (NotifyServiceStatusChange(service, | ||
SERVICE_NOTIFY_RUNNING | SERVICE_NOTIFY_START_PENDING | SERVICE_NOTIFY_STOPPED | | ||
SERVICE_NOTIFY_STOP_PENDING, | ||
&s_notify) == ERROR_SUCCESS) { | ||
::SleepEx(INFINITE, true); | ||
} else { | ||
CloseServiceHandle(service); | ||
CloseServiceHandle(service_manager); | ||
break; | ||
} | ||
} | ||
m_running.store(false); | ||
} | ||
|
||
void CALLBACK ConnectionStatusObserver::ServiceNotifyCallback(void* ptr) { | ||
SERVICE_NOTIFY* serviceNotify = static_cast<SERVICE_NOTIFY*>(ptr); | ||
ConnectionStatusObserver* instance = static_cast<ConnectionStatusObserver*>(serviceNotify->pContext); | ||
|
||
if (!instance || serviceNotify->dwNotificationStatus != ERROR_SUCCESS) { | ||
return; | ||
} | ||
|
||
auto service_status = &serviceNotify->ServiceStatus; | ||
auto status = ConnectionStatusFromWinSvcState(service_status->dwCurrentState); | ||
|
||
if (instance->sink_) { | ||
instance->sink_->Success(flutter::EncodableValue(ConnectionStatusToString(status))); | ||
} | ||
} | ||
|
||
std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> ConnectionStatusObserver::OnListenInternal( | ||
const flutter::EncodableValue* arguments, std::unique_ptr<flutter::EventSink<flutter::EncodableValue>>&& events) { | ||
sink_ = std::move(events); | ||
// sink_->Success(flutter::EncodableValue(ConnectionStatusToString(ConnectionStatus::disconnected))); | ||
return nullptr; | ||
} | ||
|
||
std::unique_ptr<flutter::StreamHandlerError<flutter::EncodableValue>> ConnectionStatusObserver::OnCancelInternal( | ||
const flutter::EncodableValue* arguments) { | ||
if (sink_) { | ||
sink_.reset(); | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
} // namespace wireguard_dart |
Oops, something went wrong.