1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| #pragma once
#include <netlistmgr.h> #include <atomic>
std::atomic_bool g_bNetWorkOnline(true);
class CNetworkListManagerEvent : public INetworkListManagerEvents { public: CNetworkListManagerEvent(std::function<void(bool)> netChangedHandler) : m_ref(1) , netChangedHandler_(netChangedHandler) {
}
~CNetworkListManagerEvent() {
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) { HRESULT Result = S_OK; if (IsEqualIID(riid, IID_IUnknown)) { *ppvObject = (IUnknown*)this; } else if (IsEqualIID(riid, IID_INetworkListManagerEvents)) { *ppvObject = (INetworkListManagerEvents*)this; } else { Result = E_NOINTERFACE; }
return Result; }
ULONG STDMETHODCALLTYPE AddRef() { return (ULONG)InterlockedIncrement(&m_ref); }
ULONG STDMETHODCALLTYPE Release() { LONG Result = InterlockedDecrement(&m_ref); if (Result == 0) delete this; return (ULONG)Result; }
virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged( NLM_CONNECTIVITY newConnectivity) { if (newConnectivity == NLM_CONNECTIVITY_DISCONNECTED) { SP_DEV_LOGT("internet status changed. connect closed");
netChangedHandler_(false); } else if ((newConnectivity & NLM_CONNECTIVITY_IPV4_INTERNET) || (newConnectivity & NLM_CONNECTIVITY_IPV6_INTERNET)) { SP_DEV_LOGT("internet status changed. connect success");
netChangedHandler_(true); } return S_OK; }
private: LONG m_ref; std::function<void(bool)> netChangedHandler_; };
class CNetworkMonitor { public: CNetworkMonitor() {}; ~CNetworkMonitor() {};
private: static void InitNetworkMonitor() { SP_DEV_LOGT("InitNetworkMonitor");
bool bSuccess = false; CoInitialize(NULL); IUnknown* pUnknown = NULL; HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void**)&pUnknown); if (SUCCEEDED(Result)) { INetworkListManager* pNetworkListManager = NULL; Result = pUnknown->QueryInterface(IID_INetworkListManager, (void**)&pNetworkListManager); if (SUCCEEDED(Result)) { VARIANT_BOOL IsConnect = VARIANT_FALSE; Result = pNetworkListManager->get_IsConnectedToInternet(&IsConnect); if (SUCCEEDED(Result)) { SP_DEV_LOGT("connect to internet:%s", IsConnect == VARIANT_TRUE ? "TRUE" : "FALSE"); }
IConnectionPointContainer* pCPContainer = NULL; Result = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPContainer); if (SUCCEEDED(Result)) { IConnectionPoint* pConnectPoint = NULL; Result = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint); if (SUCCEEDED(Result)) { DWORD Cookie = NULL;
CNetworkMonitor networkMonitor; std::function<void(bool)> monitorFunc = std::bind(&CNetworkMonitor::NetworkStatusChangedCallback, networkMonitor, std::placeholders::_1); CNetworkListManagerEvent* pNetEvent = new(std::nothrow) CNetworkListManagerEvent(monitorFunc); if (pNetEvent) { Result = pConnectPoint->Advise((IUnknown*)pNetEvent, &Cookie); if (SUCCEEDED(Result)) { SP_DEV_LOGT("InitNetworkMonitor success"); bSuccess = true;
MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg);
if (msg.message == WM_QUIT) { break; } }
pConnectPoint->Unadvise(Cookie); pConnectPoint->Release(); } } } pCPContainer->Release(); } pNetworkListManager->Release(); } pUnknown->Release(); } CoUninitialize(); }
void NetworkStatusChangedCallback(bool status) { g_bNetWorkOnline = status; if (status) { if (g_sp_trust_model.get()) { if (g_sp_trust_model->IsOnline()) { SP_LOGI("Sleep wakes up and automatically triggers a heartbeat"); g_sp_trust_model->OnWakeToSendHeartBeat();
SP_LOGI("rebuild trust nc");
if (g_sp_trust_model->Session()->NeedsL3Tunnel() && !g_sp_trust_model->Session()->IsTunnelSvrsEmpty()) { std::thread buildUtunThread([] { g_sp_trust_model->NotifyUpMessage(SP_TRUST_TOPIC_BUILD_UTUN, SPJSONObject()); }); buildUtunThread.detach(); } } else { SP_LOGI("No login, will not rebuild trust nc"); } } else { SP_LOGI("rebuild trust nc - g_sp_trust_model is null"); } } }
public: void StartMonitor() { std::thread monitorThread(&InitNetworkMonitor); monitorThread.detach(); } };
|