コード例 #1
0
ファイル: NetworkService.cs プロジェクト: tvvister91/Arena
        private void Init()
        {
            ConnectionState = NetworkConnectionStates.None;
            //UIApplication.SharedApplication.InvokeOnMainThread(() =>
            //{
            try
            {
                System.Diagnostics.Debug.WriteLine($"*** {GetType().Name}.Init - Reachability on Main Thread");

                while (_Reachability == null)
                {
                    Thread.Sleep(10);
                    _Reachability = SetReachability();
                }

                NetworkReachabilityFlags flags;
                if (_Reachability.TryGetFlags(out flags))
                {
                    System.Diagnostics.Debug.WriteLine($"*** {GetType().Name}.Init - Initial reachability: {flags}");
                    var connected = flags.HasFlag(NetworkReachabilityFlags.Reachable);
                    if (connected)
                    {
                        GetNetworkSetup(flags);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"*** {GetType().Name}.Init - Exception: {ex}");
            }

            OnConnectivityChanged?.Invoke(this, new EventArgs());
            //});
        }
コード例 #2
0
ファイル: NetworkService.cs プロジェクト: tvvister91/Arena
 void NetworkInformation_NetworkStatusChanged(object sender)
 {
     System.Diagnostics.Debug.WriteLine("NetworkManager: Network status changed");
     //  get the new status
     GetStatus();
     //  notify
     OnConnectivityChanged?.Invoke(this, new EventArgs());
 }
コード例 #3
0
ファイル: NetworkService.cs プロジェクト: tvvister91/Arena
        private async Task GetState()
        {
            try
            {
                System.Diagnostics.Debug.WriteLine("*** NetworkService - Connectivity changed.");
                _Recieved = DateTime.Now;

                //  allow changes to settle
                await Task.Delay(500);

                var diffs = DateTime.Now.Subtract(_Recieved).TotalMilliseconds;
                System.Diagnostics.Debug.WriteLine(string.Format("*** NetworkService.GetState - Difference = {0}", diffs));
                if (diffs < 500)
                {
                    return;
                }

                System.Diagnostics.Debug.WriteLine("*** NetworkService - Checking connectivity.");
                System.Diagnostics.Debug.WriteLine("*** NetworkService - Getting telephone service.");
                var tel = (TelephonyManager)Application.Context.GetSystemService(Application.TelephonyService);

                NetworkInfo info = _Manager.ActiveNetworkInfo;
                if (info == null)
                {
                    System.Diagnostics.Debug.WriteLine("*** NetworkService - No connectivity.");
                    ConnectionState = NetworkConnectionStates.None;
                    OnConnectivityChanged?.Invoke(this, new EventArgs());
                    return;
                }

                if ((info.Type == ConnectivityType.Ethernet) || (info.Type == ConnectivityType.Wifi))
                {
                    System.Diagnostics.Debug.WriteLine("*** NetworkService - On WIFI.");
                    ConnectionState = NetworkConnectionStates.Wifi;
                }
                else if (info.Type == ConnectivityType.Mobile)
                {
                    System.Diagnostics.Debug.WriteLine("*** NetworkService - On Cellular.");
                    ConnectionState = NetworkConnectionStates.CellularFast;
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("*** NetworkService - Connectivity unknown.");
                    ConnectionState = NetworkConnectionStates.Unknown;
                }

                OnConnectivityChanged?.Invoke(this, new EventArgs());
            }
            catch (Exception ex)
            {
                ConnectionState = NetworkConnectionStates.Unknown;
                System.Diagnostics.Debug.WriteLine("NetworkService - Failed to get network state: {0}", ex.Message);
                OnConnectivityChanged?.Invoke(this, new EventArgs());
            }
        }
コード例 #4
0
        private void ConnectivityChanged(object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e)
        {
            IsConnectedToInternet = e.IsConnected;
            OnConnectivityChanged?.Invoke(IsConnectedToInternet);

            if (IsConnectedToInternet)
            {
                ConnectSockets();
            }
            else
            {
                DisconnectSockets();
            }
        }
コード例 #5
0
        private void ReachablilityCallback(NetworkReachabilityFlags flags)
        {
            var connected = flags.HasFlag(NetworkReachabilityFlags.Reachable);

            if (connected)
            {
                GetNetworkSetup(flags);
            }
            else
            {
                ConnectionState = NetworkConnectionStates.None;
            }
            OnConnectivityChanged?.Invoke(this, new EventArgs());
        }
コード例 #6
0
ファイル: NetworkService.cs プロジェクト: tvvister91/Arena
        private void ReachablilityCallback(NetworkReachabilityFlags flags)
        {
            System.Diagnostics.Debug.WriteLine($"*** {GetType().Name}.ReachablilityCallback - Callback!!");
            var connected = flags.HasFlag(NetworkReachabilityFlags.Reachable);

            if (connected)
            {
                GetNetworkSetup(flags);
            }
            else
            {
                ConnectionState = NetworkConnectionStates.None;
            }
            OnConnectivityChanged?.Invoke(this, new EventArgs());
        }