Skip to content

Commit 9310e63

Browse files
authored
Merge pull request #690 from microsoft/heanzu/mac-net-detect
Heanzu/mac net detect
2 parents ead8b4b + 125b901 commit 9310e63

File tree

4 files changed

+44
-52
lines changed

4 files changed

+44
-52
lines changed

lib/CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,11 @@ if(PAL_IMPLEMENTATION STREQUAL "CPP11")
117117
)
118118
endif()
119119

120-
if(BUILD_IOS)
121-
list(APPEND SRCS
120+
list(APPEND SRCS
122121
pal/posix/NetworkInformationImpl.mm
123122
# TODO: this unit below needs to be deprecated and removed
124123
../third_party/Reachability/ODWReachability.m
125-
)
126-
else()
127-
list(APPEND SRCS
128-
pal/posix/NetworkInformationImpl.cpp
129-
)
130-
endif()
124+
)
131125
else()
132126
list(APPEND SRCS
133127
http/HttpClient_Curl.cpp

lib/pal/posix/NetworkInformationImpl.mm

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ virtual NetworkCost GetNetworkCost()
8484

8585
NetworkInformation::~NetworkInformation() noexcept
8686
{
87-
if (@available(iOS 12.0, *))
87+
if (@available(macOS 10.14, iOS 12.0, *))
8888
{
8989
if (m_isNetDetectEnabled)
9090
{
@@ -105,7 +105,36 @@ virtual NetworkCost GetNetworkCost()
105105
{
106106
auto weak_this = std::weak_ptr<NetworkInformation>(shared_from_this());
107107

108-
if (@available(iOS 12.0, *))
108+
m_reach = [ODWReachability reachabilityForInternetConnection];
109+
void (^block)(NSNotification*) = ^(NSNotification*)
110+
{
111+
auto strong_this = weak_this.lock();
112+
if (!strong_this)
113+
{
114+
return;
115+
}
116+
117+
// NetworkCost information is not available until iOS 12.
118+
// Just make the best guess here.
119+
switch (m_reach.currentReachabilityStatus)
120+
{
121+
case NotReachable:
122+
strong_this->UpdateType(NetworkType_Unknown);
123+
strong_this->UpdateCost(NetworkCost_Unknown);
124+
break;
125+
case ReachableViaWiFi:
126+
strong_this->UpdateType(NetworkType_Wifi);
127+
strong_this->UpdateCost(NetworkCost_Unmetered);
128+
break;
129+
case ReachableViaWWAN:
130+
strong_this->UpdateType(NetworkType_WWAN);
131+
strong_this->UpdateCost(NetworkCost_Metered);
132+
break;
133+
}
134+
};
135+
block(nil); // Update the initial status.
136+
137+
if (@available(macOS 10.14, iOS 12.0, *))
109138
{
110139
m_monitor = nw_path_monitor_create();
111140
nw_path_monitor_set_queue(m_monitor, dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0));
@@ -136,7 +165,7 @@ virtual NetworkCost GetNetworkCost()
136165
type = NetworkType_Wired;
137166
}
138167
cost = nw_path_is_expensive(path) ? NetworkCost_Metered : NetworkCost_Unmetered;
139-
if (@available(iOS 13.0, *))
168+
if (@available(macOS 10.15, iOS 13.0, *))
140169
{
141170
if (nw_path_is_constrained(path))
142171
{
@@ -157,46 +186,15 @@ virtual NetworkCost GetNetworkCost()
157186
nw_path_monitor_cancel(m_monitor);
158187
}
159188
}
160-
else
189+
else if (m_isNetDetectEnabled)
161190
{
162-
m_reach = [ODWReachability reachabilityForInternetConnection];
163-
void (^block)(NSNotification*) = ^(NSNotification*)
164-
{
165-
auto strong_this = weak_this.lock();
166-
if (!strong_this)
167-
{
168-
return;
169-
}
170-
171-
// NetworkCost information is not available until iOS 12.
172-
// Just make the best guess here.
173-
switch (m_reach.currentReachabilityStatus)
174-
{
175-
case NotReachable:
176-
strong_this->UpdateType(NetworkType_Unknown);
177-
strong_this->UpdateCost(NetworkCost_Unknown);
178-
break;
179-
case ReachableViaWiFi:
180-
strong_this->UpdateType(NetworkType_Wifi);
181-
strong_this->UpdateCost(NetworkCost_Unmetered);
182-
break;
183-
case ReachableViaWWAN:
184-
strong_this->UpdateType(NetworkType_WWAN);
185-
strong_this->UpdateCost(NetworkCost_Metered);
186-
break;
187-
}
188-
};
189-
block(nil); // Update the initial status.
190-
if (m_isNetDetectEnabled)
191-
{
192-
m_notificationId =
193-
[[NSNotificationCenter defaultCenter]
194-
addObserverForName: kNetworkReachabilityChangedNotification
195-
object: nil
196-
queue: nil
197-
usingBlock: block];
198-
[m_reach startNotifier];
199-
}
191+
m_notificationId =
192+
[[NSNotificationCenter defaultCenter]
193+
addObserverForName: kNetworkReachabilityChangedNotification
194+
object: nil
195+
queue: nil
196+
usingBlock: block];
197+
[m_reach startNotifier];
200198
}
201199
}
202200

tests/functests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ else()
6767
if(BUILD_IOS)
6868
set (PLATFORM_LIBS "${PLATFORM_LIBS} -framework UIKit -framework Network -framework SystemConfiguration")
6969
else()
70-
set (PLATFORM_LIBS "${PLATFORM_LIBS} -framework IOKit")
70+
set (PLATFORM_LIBS "${PLATFORM_LIBS} -framework IOKit -framework Network -framework SystemConfiguration")
7171
endif()
7272
endif()
7373

tests/unittests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ else()
108108
set (PLATFORM_LIBS "")
109109
# Add flags for obtaining system UUID via IOKit
110110
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
111-
set (PLATFORM_LIBS "-framework CoreFoundation -framework IOKit -framework SystemConfiguration -framework Foundation")
111+
set (PLATFORM_LIBS "-framework CoreFoundation -framework IOKit -framework SystemConfiguration -framework Foundation -framework Network")
112112
if(BUILD_IOS)
113113
set (PLATFORM_LIBS "${PLATFORM_LIBS} -framework UIKit")
114114
endif()

0 commit comments

Comments
 (0)