[windows] update flag on connectivity state changes, CRLF > LF

Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
R4SAS 2020-04-12 06:00:06 +03:00
parent 90914bb2de
commit 152e579f7e
No known key found for this signature in database
GPG Key ID: 66F6C87B98EBCFE2
2 changed files with 162 additions and 135 deletions

View File

@ -1,59 +1,78 @@
#if WINVER != 0x0501 // supported since Vista #if WINVER != 0x0501 // supported since Vista
#include "Win32NetState.h" #include "Win32NetState.h"
#include <windows.h> #include <windows.h>
#include "Log.h" #include "Log.h"
IUnknown *pUnknown = NULL; IUnknown *pUnknown = nullptr;
INetworkListManager *pNetworkListManager = NULL; INetworkListManager *pNetworkListManager = nullptr;
IConnectionPointContainer *pCPContainer = NULL; IConnectionPointContainer *pCPContainer = nullptr;
DWORD Cookie = 0; IConnectionPoint *pConnectPoint = nullptr;
IConnectionPoint *pConnectPoint = NULL; DWORD Cookie = 0;
void SubscribeToEvents() void SubscribeToEvents()
{ {
LogPrint(eLogInfo, "NetState: Trying to subscribe to NetworkListManagerEvents"); LogPrint(eLogInfo, "NetState: Trying to subscribe to NetworkListManagerEvents");
CoInitialize(NULL); CoInitialize(NULL);
HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown); HRESULT Result = CoCreateInstance(CLSID_NetworkListManager, NULL, CLSCTX_ALL, IID_IUnknown, (void **)&pUnknown);
if (SUCCEEDED(Result)) if (SUCCEEDED(Result))
{ {
Result = pUnknown->QueryInterface(IID_INetworkListManager, (void **)&pNetworkListManager); Result = pUnknown->QueryInterface(IID_INetworkListManager, (void **)&pNetworkListManager);
if (SUCCEEDED(Result)) if (SUCCEEDED(Result))
{ {
/* VARIANT_BOOL IsConnect = VARIANT_FALSE; VARIANT_BOOL IsConnect = VARIANT_FALSE;
Result = pNetworkListManager->IsConnectedToInternet(&IsConnect); Result = pNetworkListManager->IsConnectedToInternet(&IsConnect);
if (SUCCEEDED(Result)) if (SUCCEEDED(Result)) {
LogPrint(eLogInfo, "NetState: IsConnect Result:", IsConnect == VARIANT_TRUE ? "TRUE" : "FALSE"); */ i2p::transport::transports.SetOnline (true);
LogPrint(eLogInfo, "NetState: current state: ", IsConnect == VARIANT_TRUE ? "connected" : "disconnected");
Result = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPContainer); }
if (SUCCEEDED(Result))
{ Result = pNetworkListManager->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPContainer);
Result = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint); if (SUCCEEDED(Result))
if(SUCCEEDED(Result)) {
{ Result = pCPContainer->FindConnectionPoint(IID_INetworkListManagerEvents, &pConnectPoint);
CNetworkListManagerEvent *NetEvent = new CNetworkListManagerEvent; if(SUCCEEDED(Result))
Result = pConnectPoint->Advise((IUnknown *)NetEvent, &Cookie); {
if (SUCCEEDED(Result)) CNetworkListManagerEvent *NetEvent = new CNetworkListManagerEvent;
LogPrint(eLogInfo, "NetState: Successfully subscribed to NetworkListManagerEvent messages"); Result = pConnectPoint->Advise((IUnknown *)NetEvent, &Cookie);
} else if (SUCCEEDED(Result))
LogPrint(eLogError, "NetState: Unable to find interface connection point"); LogPrint(eLogInfo, "NetState: Successfully subscribed to NetworkListManagerEvent messages");
} else else
LogPrint(eLogError, "NetState: Unable to query NetworkListManager interface"); LogPrint(eLogError, "NetState: Unable to subscribe to NetworkListManagerEvent messages");
} else } else
LogPrint(eLogError, "NetState: Unable to query global interface"); LogPrint(eLogError, "NetState: Unable to find interface connection point");
} else } else
LogPrint(eLogError, "NetState: Unable to create INetworkListManager interface"); LogPrint(eLogError, "NetState: Unable to query NetworkListManager interface");
} } else
LogPrint(eLogError, "NetState: Unable to query global interface");
void UnSubscribeFromEvents() } else
{ LogPrint(eLogError, "NetState: Unable to create INetworkListManager interface");
// TODO - DETECT EVERY STAGE STATE and call functions depending it !!! }
pConnectPoint->Unadvise(Cookie);
pConnectPoint->Release(); void UnSubscribeFromEvents()
pCPContainer->Release(); {
pNetworkListManager->Release(); try
pUnknown->Release(); {
CoUninitialize(); if (pConnectPoint) {
} pConnectPoint->Unadvise(Cookie);
pConnectPoint->Release();
#endif // WINVER }
if (pCPContainer)
pCPContainer->Release();
if (pNetworkListManager)
pNetworkListManager->Release();
if (pUnknown)
pUnknown->Release();
CoUninitialize();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "NetState: received exception: ", ex.what ());
}
}
#endif // WINVER

View File

@ -1,77 +1,85 @@
#ifndef WIN_32_NETSTATE_H__ #ifndef WIN_32_NETSTATE_H__
#define WIN_32_NETSTATE_H__ #define WIN_32_NETSTATE_H__
#if WINVER != 0x0501 // supported since Vista #if WINVER != 0x0501 // supported since Vista
#include <netlistmgr.h> #include <netlistmgr.h>
#include <ocidl.h> #include <ocidl.h>
#include "Log.h" #include "Log.h"
#include "Transports.h" #include "Transports.h"
class CNetworkListManagerEvent : public INetworkListManagerEvents class CNetworkListManagerEvent : public INetworkListManagerEvents
{ {
public: public:
CNetworkListManagerEvent() : m_ref(1) { } CNetworkListManagerEvent() : m_ref(1) { }
~CNetworkListManagerEvent() { } ~CNetworkListManagerEvent() { }
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
{ {
HRESULT Result = S_OK; HRESULT Result = S_OK;
if (IsEqualIID(riid, IID_IUnknown)) { if (IsEqualIID(riid, IID_IUnknown)) {
*ppvObject = (IUnknown *)this; *ppvObject = (IUnknown *)this;
} else if (IsEqualIID(riid ,IID_INetworkListManagerEvents)) { } else if (IsEqualIID(riid ,IID_INetworkListManagerEvents)) {
*ppvObject = (INetworkListManagerEvents *)this; *ppvObject = (INetworkListManagerEvents *)this;
} else { } else {
Result = E_NOINTERFACE; Result = E_NOINTERFACE;
} }
return Result; return Result;
} }
ULONG STDMETHODCALLTYPE AddRef() ULONG STDMETHODCALLTYPE AddRef()
{ {
return (ULONG)InterlockedIncrement(&m_ref); return (ULONG)InterlockedIncrement(&m_ref);
} }
ULONG STDMETHODCALLTYPE Release() ULONG STDMETHODCALLTYPE Release()
{ {
LONG Result = InterlockedDecrement(&m_ref); LONG Result = InterlockedDecrement(&m_ref);
if (Result == 0) if (Result == 0)
delete this; delete this;
return (ULONG)Result; return (ULONG)Result;
} }
virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(NLM_CONNECTIVITY newConnectivity) virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
{ {
if (newConnectivity == NLM_CONNECTIVITY_DISCONNECTED) if (newConnectivity == NLM_CONNECTIVITY_DISCONNECTED) {
LogPrint(eLogInfo, "NetState: disconnected from network"); i2p::transport::transports.SetOnline (false);
LogPrint(eLogInfo, "NetState: disconnected from network");
if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) != 0) }
LogPrint(eLogInfo, "NetState: connected to internet with IPv4 capability");
if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) != 0) {
if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) != 0) i2p::transport::transports.SetOnline (true);
LogPrint(eLogInfo, "NetState: connected to internet with IPv6 capability"); LogPrint(eLogInfo, "NetState: connected to internet with IPv4 capability");
}
if (
(((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) == 0) && if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) != 0) {
(((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) == 0) i2p::transport::transports.SetOnline (true);
) LogPrint(eLogInfo, "NetState: connected to internet with IPv6 capability");
LogPrint(eLogInfo, "NetState: connected without internet access"); }
return S_OK; if (
} (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) == 0) &&
(((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) == 0)
private: ) {
i2p::transport::transports.SetOnline (false);
LONG m_ref; LogPrint(eLogInfo, "NetState: connected without internet access");
}; }
void SubscribeToEvents(); return S_OK;
void UnSubscribeFromEvents(); }
#else // WINVER == 0x0501 private:
void SubscribeToEvents() { } LONG m_ref;
void UnSubscribeFromEvents() { } };
#endif // WINVER void SubscribeToEvents();
void UnSubscribeFromEvents();
#else // WINVER == 0x0501
void SubscribeToEvents() { }
void UnSubscribeFromEvents() { }
#endif // WINVER
#endif #endif