summaryrefslogtreecommitdiffhomepage
path: root/windows
diff options
context:
space:
mode:
Diffstat (limited to 'windows')
-rw-r--r--windows/winnet/src/winnet/netmonitor.cpp48
-rw-r--r--windows/winnet/src/winnet/netmonitor.h19
-rw-r--r--windows/winnet/src/winnet/winnet.cpp113
-rw-r--r--windows/winnet/src/winnet/winnet.def3
-rw-r--r--windows/winnet/src/winnet/winnet.h48
5 files changed, 144 insertions, 87 deletions
diff --git a/windows/winnet/src/winnet/netmonitor.cpp b/windows/winnet/src/winnet/netmonitor.cpp
index 6ccaf3d64a..4de068f9cd 100644
--- a/windows/winnet/src/winnet/netmonitor.cpp
+++ b/windows/winnet/src/winnet/netmonitor.cpp
@@ -30,16 +30,16 @@ bool ValidInterfaceType(const MIB_IF_ROW2 &iface)
} // anonyomus namespace
NetMonitor::NetMonitor(NetMonitor::Notifier notifier, bool &currentConnectivity)
- : m_notifier(notifier)
- , m_connected(false)
+ : m_connected(false)
+ , m_notifier(notifier)
, m_notificationHandle(nullptr)
{
- createCache();
+ m_cache = CreateCache();
updateConnectivity();
currentConnectivity = m_connected;
- const auto status = NotifyIpInterfaceChange(AF_UNSPEC, callback, this, FALSE, &m_notificationHandle);
+ const auto status = NotifyIpInterfaceChange(AF_UNSPEC, Callback, this, FALSE, &m_notificationHandle);
THROW_UNLESS(NO_ERROR, status, "Register interface change notification");
}
@@ -49,7 +49,14 @@ NetMonitor::~NetMonitor()
CancelMibChangeNotify2(m_notificationHandle);
}
-void NetMonitor::createCache()
+// static
+bool NetMonitor::CheckConnectivity()
+{
+ return CheckConnectivity(CreateCache());
+}
+
+// static
+NetMonitor::Cache NetMonitor::CreateCache()
{
MIB_IF_TABLE2 *table;
@@ -64,13 +71,17 @@ void NetMonitor::createCache()
FreeMibTable(table);
};
+ std::map<uint64_t, CacheEntry> cache;
+
for (ULONG i = 0; i < table->NumEntries; ++i)
{
- addCacheEntry(table->Table[i]);
+ AddCacheEntry(cache, table->Table[i]);
}
+ return cache;
}
-void NetMonitor::addCacheEntry(const MIB_IF_ROW2 &iface)
+// static
+void NetMonitor::AddCacheEntry(Cache &cache, const MIB_IF_ROW2 &iface)
{
CacheEntry e;
@@ -87,27 +98,32 @@ void NetMonitor::addCacheEntry(const MIB_IF_ROW2 &iface)
e.connected = (MediaConnectStateConnected == iface.MediaConnectState);
}
- m_cache.insert(std::make_pair(e.luid, e));
+ cache.insert(std::make_pair(e.luid, e));
}
-void NetMonitor::updateConnectivity()
+// static
+bool NetMonitor::CheckConnectivity(const Cache &cache)
{
- for (const auto cacheEntryIter : m_cache)
+ for (const auto cacheEntryIter : cache)
{
const auto entry = cacheEntryIter.second;
if (entry.valid && entry.connected)
{
- m_connected = true;
- return;
+ return true;
}
}
- m_connected = false;
+ return false;
+}
+
+void NetMonitor::updateConnectivity()
+{
+ m_connected = NetMonitor::CheckConnectivity(m_cache);
}
//static
-void __stdcall NetMonitor::callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType)
+void __stdcall NetMonitor::Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType)
{
auto thiz = reinterpret_cast<NetMonitor *>(context);
@@ -126,7 +142,7 @@ void __stdcall NetMonitor::callback(void *context, MIB_IPINTERFACE_ROW *hint, MI
return;
}
- thiz->addCacheEntry(iface);
+ thiz->AddCacheEntry(thiz->m_cache, iface);
break;
}
@@ -161,7 +177,7 @@ void __stdcall NetMonitor::callback(void *context, MIB_IPINTERFACE_ROW *hint, MI
return;
}
- thiz->addCacheEntry(iface);
+ thiz->AddCacheEntry(thiz->m_cache, iface);
}
else
{
diff --git a/windows/winnet/src/winnet/netmonitor.h b/windows/winnet/src/winnet/netmonitor.h
index b8e418b82d..77e4417b31 100644
--- a/windows/winnet/src/winnet/netmonitor.h
+++ b/windows/winnet/src/winnet/netmonitor.h
@@ -23,11 +23,9 @@ public:
NetMonitor(Notifier notifier, bool &currentConnectivity);
~NetMonitor();
-private:
-
- std::mutex m_processingMutex;
+ static bool CheckConnectivity();
- Notifier m_notifier;
+private:
struct CacheEntry
{
@@ -41,15 +39,20 @@ private:
bool connected;
};
- std::map<uint64_t, CacheEntry> m_cache;
+ using Cache = std::map<uint64_t, CacheEntry>;
+ std::mutex m_processingMutex;
+ Cache m_cache;
bool m_connected;
+ Notifier m_notifier;
HANDLE m_notificationHandle;
- void createCache();
- void addCacheEntry(const MIB_IF_ROW2 &iface);
+ static Cache CreateCache();
+ static void AddCacheEntry(Cache &cache, const MIB_IF_ROW2 &iface);
+ static bool CheckConnectivity(const Cache &cache);
+
void updateConnectivity();
- static void __stdcall callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType);
+ static void __stdcall Callback(void *context, MIB_IPINTERFACE_ROW *hint, MIB_NOTIFICATION_TYPE updateType);
};
diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp
index 9741613457..aa23adbc8d 100644
--- a/windows/winnet/src/winnet/winnet.cpp
+++ b/windows/winnet/src/winnet/winnet.cpp
@@ -21,7 +21,7 @@ WINNET_API
WinNet_EnsureTopMetric(
const wchar_t *deviceAlias,
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
)
{
try
@@ -51,7 +51,7 @@ WINNET_GTII_STATUS
WINNET_API
WinNet_GetTapInterfaceIpv6Status(
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
)
{
try
@@ -92,12 +92,12 @@ WinNet_GetTapInterfaceIpv6Status(
extern "C"
WINNET_LINKAGE
-WINNET_GTIA_STATUS
+bool
WINNET_API
WinNet_GetTapInterfaceAlias(
wchar_t **alias,
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
)
{
try
@@ -109,7 +109,7 @@ WinNet_GetTapInterfaceAlias(
*alias = stringBuffer;
- return WINNET_GTIA_STATUS::SUCCESS;
+ return true;
}
catch (std::exception &err)
{
@@ -118,11 +118,11 @@ WinNet_GetTapInterfaceAlias(
errorSink(err.what(), errorSinkContext);
}
- return WINNET_GTIA_STATUS::FAILURE;
+ return false;
}
catch (...)
{
- return WINNET_GTIA_STATUS::FAILURE;
+ return false;
}
}
@@ -143,17 +143,18 @@ WinNet_ReleaseString(
}
}
-extern "C"
-WINNET_LINKAGE
-WINNET_ACM_STATUS
-WINNET_API
-WinNet_ActivateConnectivityMonitor(
- WinNetConnectivityMonitorCallback callback,
- uint8_t *currentConnectivity,
- WinNetErrorSink errorSink,
- void* errorSinkContext
-)
-{
+extern "C"
+WINNET_LINKAGE
+bool
+WINNET_API
+WinNet_ActivateConnectivityMonitor(
+ WinNetConnectivityMonitorCallback callback,
+ void *callbackContext,
+ bool *currentConnectivity,
+ WinNetErrorSink errorSink,
+ void *errorSinkContext
+)
+{
try
{
if (nullptr != g_NetMonitor)
@@ -161,9 +162,9 @@ WinNet_ActivateConnectivityMonitor(
throw std::runtime_error("Cannot activate connectivity monitor twice");
}
- auto forwarder = [callback](bool connected)
+ auto forwarder = [callback, callbackContext](bool connected)
{
- callback(static_cast<uint8_t>(connected));
+ callback(connected, callbackContext);
};
bool connected = false;
@@ -172,10 +173,55 @@ WinNet_ActivateConnectivityMonitor(
if (nullptr != currentConnectivity)
{
- *currentConnectivity = static_cast<uint8_t>(connected);
+ *currentConnectivity = connected;
+ }
+
+ return true;
+ }
+ catch (std::exception &err)
+ {
+ if (nullptr != errorSink)
+ {
+ errorSink(err.what(), errorSinkContext);
}
- return WINNET_ACM_STATUS::SUCCESS;
+ return false;
+ }
+ catch (...)
+ {
+ return false;
+ }
+}
+
+extern "C"
+WINNET_LINKAGE
+void
+WINNET_API
+WinNet_DeactivateConnectivityMonitor(
+)
+{
+ try
+ {
+ delete g_NetMonitor;
+ g_NetMonitor = nullptr;
+ }
+ catch (...)
+ {
+ }
+}
+
+extern "C"
+WINNET_LINKAGE
+WINNET_CC_STATUS
+WINNET_API
+WinNet_CheckConnectivity(
+ WinNetErrorSink errorSink,
+ void *errorSinkContext
+)
+{
+ try
+ {
+ return (NetMonitor::CheckConnectivity() ? WINNET_CC_STATUS::CONNECTED : WINNET_CC_STATUS::NOT_CONNECTED);
}
catch (std::exception &err)
{
@@ -184,27 +230,10 @@ WinNet_ActivateConnectivityMonitor(
errorSink(err.what(), errorSinkContext);
}
- return WINNET_ACM_STATUS::FAILURE;
+ return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
}
catch (...)
{
- return WINNET_ACM_STATUS::FAILURE;
+ return WINNET_CC_STATUS::CONNECTIVITY_UNKNOWN;
}
-}
-
-extern "C"
-WINNET_LINKAGE
-void
-WINNET_API
-WinNet_DeactivateConnectivityMonitor(
-)
-{
- try
- {
- delete g_NetMonitor;
- g_NetMonitor = nullptr;
- }
- catch (...)
- {
- }
-}
+}
diff --git a/windows/winnet/src/winnet/winnet.def b/windows/winnet/src/winnet/winnet.def
index 267a797538..4c8cbfba9f 100644
--- a/windows/winnet/src/winnet/winnet.def
+++ b/windows/winnet/src/winnet/winnet.def
@@ -4,3 +4,6 @@ EXPORTS
WinNet_GetTapInterfaceIpv6Status
WinNet_GetTapInterfaceAlias
WinNet_ReleaseString
+ WinNet_ActivateConnectivityMonitor
+ WinNet_DeactivateConnectivityMonitor
+ WinNet_CheckConnectivity
diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h
index c3464d4c03..40ed25f567 100644
--- a/windows/winnet/src/winnet/winnet.h
+++ b/windows/winnet/src/winnet/winnet.h
@@ -1,5 +1,6 @@
#pragma once
-#include <cstdint>
+#include <stdint.h>
+#include <stdbool.h>
#ifdef WINNET_EXPORTS
#define WINNET_LINKAGE __declspec(dllexport)
@@ -25,7 +26,7 @@ WINNET_API
WinNet_EnsureTopMetric(
const wchar_t *deviceAlias,
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
);
enum class WINNET_GTII_STATUS : uint32_t
@@ -41,23 +42,17 @@ WINNET_GTII_STATUS
WINNET_API
WinNet_GetTapInterfaceIpv6Status(
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
);
-enum class WINNET_GTIA_STATUS : uint32_t
-{
- SUCCESS = 0,
- FAILURE = 1,
-};
-
extern "C"
WINNET_LINKAGE
-WINNET_GTIA_STATUS
+bool
WINNET_API
WinNet_GetTapInterfaceAlias(
wchar_t **alias,
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
);
//
@@ -72,23 +67,18 @@ WinNet_ReleaseString(
wchar_t *str
);
-typedef void (WINNET_API *WinNetConnectivityMonitorCallback)(uint8_t connected);
-
-enum class WINNET_ACM_STATUS : uint32_t
-{
- SUCCESS = 0,
- FAILURE = 1,
-};
+typedef void (WINNET_API *WinNetConnectivityMonitorCallback)(bool connected, void *context);
extern "C"
WINNET_LINKAGE
-WINNET_ACM_STATUS
+bool
WINNET_API
WinNet_ActivateConnectivityMonitor(
WinNetConnectivityMonitorCallback callback,
- uint8_t *currentConnectivity,
+ void *callbackContext,
+ bool *currentConnectivity,
WinNetErrorSink errorSink,
- void* errorSinkContext
+ void *errorSinkContext
);
extern "C"
@@ -97,3 +87,19 @@ void
WINNET_API
WinNet_DeactivateConnectivityMonitor(
);
+
+enum class WINNET_CC_STATUS : uint32_t
+{
+ NOT_CONNECTED = 0,
+ CONNECTED = 1,
+ CONNECTIVITY_UNKNOWN = 2,
+};
+
+extern "C"
+WINNET_LINKAGE
+WINNET_CC_STATUS
+WINNET_API
+WinNet_CheckConnectivity(
+ WinNetErrorSink errorSink,
+ void *errorSinkContext
+);