This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathConnectivity.ios.tvos.macos.reachability.cs
194 lines (158 loc) · 6.5 KB
/
Connectivity.ios.tvos.macos.reachability.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
#if __IOS__
using CoreTelephony;
#endif
using CoreFoundation;
using SystemConfiguration;
namespace Xamarin.Essentials
{
enum NetworkStatus
{
NotReachable,
ReachableViaCarrierDataNetwork,
ReachableViaWiFiNetwork
}
static class Reachability
{
internal const string HostName = "www.microsoft.com";
internal static NetworkStatus RemoteHostStatus()
{
using (var remoteHostReachability = new NetworkReachability(HostName))
{
var reachable = remoteHostReachability.TryGetFlags(out var flags);
if (!reachable)
return NetworkStatus.NotReachable;
if (!IsReachableWithoutRequiringConnection(flags))
return NetworkStatus.NotReachable;
#if __IOS__
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
return NetworkStatus.ReachableViaCarrierDataNetwork;
#endif
return NetworkStatus.ReachableViaWiFiNetwork;
}
}
internal static NetworkStatus InternetConnectionStatus()
{
var status = NetworkStatus.NotReachable;
var defaultNetworkAvailable = IsNetworkAvailable(out var flags);
#if __IOS__
// If it's a WWAN connection..
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
status = NetworkStatus.ReachableViaCarrierDataNetwork;
#endif
// If the connection is reachable and no connection is required, then assume it's WiFi
if (defaultNetworkAvailable)
{
status = NetworkStatus.ReachableViaWiFiNetwork;
}
// If the connection is on-demand or on-traffic and no user intervention
// is required, then assume WiFi.
if (((flags & NetworkReachabilityFlags.ConnectionOnDemand) != 0 || (flags & NetworkReachabilityFlags.ConnectionOnTraffic) != 0) &&
(flags & NetworkReachabilityFlags.InterventionRequired) == 0)
{
status = NetworkStatus.ReachableViaWiFiNetwork;
}
return status;
}
internal static IEnumerable<NetworkStatus> GetActiveConnectionType()
{
var status = new List<NetworkStatus>();
var defaultNetworkAvailable = IsNetworkAvailable(out var flags);
#if __IOS__
// If it's a WWAN connection.
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
{
status.Add(NetworkStatus.ReachableViaCarrierDataNetwork);
}
else if (defaultNetworkAvailable)
#else
// If the connection is reachable and no connection is required, then assume it's WiFi
if (defaultNetworkAvailable)
#endif
{
status.Add(NetworkStatus.ReachableViaWiFiNetwork);
}
else if (((flags & NetworkReachabilityFlags.ConnectionOnDemand) != 0 || (flags & NetworkReachabilityFlags.ConnectionOnTraffic) != 0) &&
(flags & NetworkReachabilityFlags.InterventionRequired) == 0)
{
// If the connection is on-demand or on-traffic and no user intervention
// is required, then assume WiFi.
status.Add(NetworkStatus.ReachableViaWiFiNetwork);
}
return status;
}
internal static bool IsNetworkAvailable(out NetworkReachabilityFlags flags)
{
var ip = new IPAddress(0);
using (var defaultRouteReachability = new NetworkReachability(ip))
{
if (!defaultRouteReachability.TryGetFlags(out flags))
return false;
return IsReachableWithoutRequiringConnection(flags);
}
}
internal static bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags)
{
// Is it reachable with the current network configuration?
var isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
// Do we need a connection to reach it?
var noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0;
#if __IOS__
// Since the network stack will automatically try to get the WAN up,
// probe that
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
noConnectionRequired = true;
#endif
return isReachable && noConnectionRequired;
}
}
class ReachabilityListener : IDisposable
{
NetworkReachability defaultRouteReachability;
NetworkReachability remoteHostReachability;
internal ReachabilityListener()
{
var ip = new IPAddress(0);
defaultRouteReachability = new NetworkReachability(ip);
defaultRouteReachability.SetNotification(OnChange);
defaultRouteReachability.Schedule(CFRunLoop.Main, CFRunLoop.ModeDefault);
remoteHostReachability = new NetworkReachability(Reachability.HostName);
// Need to probe before we queue, or we wont get any meaningful values
// this only happens when you create NetworkReachability from a hostname
remoteHostReachability.TryGetFlags(out var flags);
remoteHostReachability.SetNotification(OnChange);
remoteHostReachability.Schedule(CFRunLoop.Main, CFRunLoop.ModeDefault);
#if __IOS__
Connectivity.CellularData.RestrictionDidUpdateNotifier = new Action<CTCellularDataRestrictedState>(OnRestrictedStateChanged);
#endif
}
internal event Action ReachabilityChanged;
void IDisposable.Dispose() => Dispose();
internal void Dispose()
{
defaultRouteReachability?.Dispose();
defaultRouteReachability = null;
remoteHostReachability?.Dispose();
remoteHostReachability = null;
#if __IOS__
Connectivity.CellularData.RestrictionDidUpdateNotifier = null;
#endif
}
#if __IOS__
void OnRestrictedStateChanged(CTCellularDataRestrictedState state)
{
ReachabilityChanged?.Invoke();
}
#endif
async void OnChange(NetworkReachabilityFlags flags)
{
// Add in artifical delay so the connection status has time to change
// else it will return true no matter what.
await Task.Delay(100);
ReachabilityChanged?.Invoke();
}
}
}