wxr
2020-06-16 f6fd8acd7c53c44187e70b4709443318a628f4b5
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
using System;
using System.Net;
using CoreFoundation;
using SystemConfiguration;
 
namespace Shared
{
    internal enum MyNetworkStatus
    {
        NotReachable = 0,
        ReachableViaCarrierDataNetwork = 1,
        ReachableViaWiFiNetwork = 2
    }
 
    internal static class MyReachability
    {
        static readonly string hostName = "www.apple.com";
 
        public static bool IsReachableWithoutRequiringConnection (NetworkReachabilityFlags flags)
        {
            // Is it reachable with the current network configuration?
            bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
 
            // Do we need a connection to reach it?
            bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
                || (flags & NetworkReachabilityFlags.IsWWAN) != 0;
 
            return isReachable && noConnectionRequired;
        }
 
        // Is the host reachable with the current network configuration
        public static bool IsHostReachable (string host)
        {
            if (string.IsNullOrEmpty (host))
                return false;
 
            using (var r = new NetworkReachability (host)) {
                NetworkReachabilityFlags flags;
 
                if (r.TryGetFlags (out flags))
                    return IsReachableWithoutRequiringConnection (flags);
            }
            return false;
        }
 
        //
        // Raised every time there is an interesting reachable event,
        // we do not even pass the info as to what changed, and
        // we lump all three status we probe into one
        //
        public static  EventHandler ReachabilityChanged;
 
        static void OnChange (NetworkReachabilityFlags flags)
        {
            var h = ReachabilityChanged;
            if (h != null)
                h (null, EventArgs.Empty);
        }
 
        //
        // Returns true if it is possible to reach the AdHoc WiFi network
        // and optionally provides extra network reachability flags as the
        // out parameter
        //
        static NetworkReachability adHocWiFiNetworkReachability;
 
        public static bool IsAdHocWiFiNetworkAvailable (out NetworkReachabilityFlags flags)
        {
            if (adHocWiFiNetworkReachability == null) {
                adHocWiFiNetworkReachability = new NetworkReachability (new IPAddress (new byte [] { 115, 29, 191, 136 }));
                adHocWiFiNetworkReachability.SetNotification (OnChange);
                adHocWiFiNetworkReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
 
            return adHocWiFiNetworkReachability.TryGetFlags (out flags) && IsReachableWithoutRequiringConnection (flags);
        }
 
        static NetworkReachability defaultRouteReachability;
 
        static bool IsNetworkAvailable (out NetworkReachabilityFlags flags)
        {
            if (defaultRouteReachability == null) {
                defaultRouteReachability = new NetworkReachability (new IPAddress (new byte [] { 115, 29, 191, 136 }));
                defaultRouteReachability.SetNotification (OnChange);
                defaultRouteReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
            }
            return defaultRouteReachability.TryGetFlags (out flags) && IsReachableWithoutRequiringConnection (flags);
        }
 
        static NetworkReachability remoteHostReachability;
 
        public static MyNetworkStatus RemoteHostStatus ()
        {
            NetworkReachabilityFlags flags;
            bool reachable;
 
            if (remoteHostReachability == null) {
                remoteHostReachability = new NetworkReachability (hostName);
 
                // Need to probe before we queue, or we wont get any meaningful values
                // this only happens when you create NetworkReachability from a hostname
                reachable = remoteHostReachability.TryGetFlags (out flags);
 
                remoteHostReachability.SetNotification (OnChange);
                remoteHostReachability.Schedule (CFRunLoop.Current, CFRunLoop.ModeDefault);
            } else {
                reachable = remoteHostReachability.TryGetFlags (out flags);
            }
 
            if (!reachable)
                return MyNetworkStatus.NotReachable;
 
            if (!IsReachableWithoutRequiringConnection (flags))
                return MyNetworkStatus.NotReachable;
 
            return (flags & NetworkReachabilityFlags.IsWWAN) != 0 ?
                MyNetworkStatus.ReachableViaCarrierDataNetwork : MyNetworkStatus.ReachableViaWiFiNetwork;
        }
 
        public static MyNetworkStatus InternetConnectionStatus ()
        {
            NetworkReachabilityFlags flags;
            bool defaultNetworkAvailable = IsNetworkAvailable (out flags);
            if (defaultNetworkAvailable && ((flags & NetworkReachabilityFlags.IsDirect) != 0))
                return MyNetworkStatus.NotReachable;
            else if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
                return MyNetworkStatus.ReachableViaCarrierDataNetwork;
            else if (flags == 0)
                return MyNetworkStatus.NotReachable;
            return MyNetworkStatus.ReachableViaWiFiNetwork;
        }
 
        public static bool Online ()
        {
            NetworkReachabilityFlags flags;
            bool defaultNetworkAvailable = IsNetworkAvailable (out flags);
            return defaultNetworkAvailable;
        }
 
 
        public static MyNetworkStatus LocalWifiConnectionStatus ()
        {
            NetworkReachabilityFlags flags;
            if (IsAdHocWiFiNetworkAvailable (out flags))
                if ((flags & NetworkReachabilityFlags.IsDirect) != 0)
                    return MyNetworkStatus.ReachableViaWiFiNetwork;
 
            return MyNetworkStatus.NotReachable;
        }
    }
}