jamesmontemagno / Xamarin.Plugins

Cross-platform Native API Access from Shared Code!
MIT License
1.3k stars 380 forks source link

Connectivity.iOS.IsRemoteReachable timeout issue #253

Closed rihadavid closed 8 years ago

rihadavid commented 8 years ago

This is a

Which plugin does this impact:

Version Number of Plugin: 2.1.2 Simulator Tested On: iPhone 4s iOS 9.0 (13A344)

Expected Behavior

When on wifi without internet, calling CrossConnectivity.Current.IsRemoteReachable() with msTimeout set to 8000 should return false after 8000 seconds.

Actual Behavior

When on wifi without internet, line socket.ConnectAsync(socketEventArg); inside CrossConnectivity.Current.IsRemoteReachable() always takes 30 seconds, although it should only start the connection and not wait for anything.

Suggested fix

calling the line through this function solves the issue but I am not sure if it is good practice:

public static async Task RunBlockingFuncOnOtherThread(Action function, int msTimeout)
        {
            var tcs = new TaskCompletionSource<bool>();
            new System.Threading.Thread(() =>
            {
                function.Invoke();
                if (!tcs.Task.IsCompleted)
                    tcs.TrySetResult(true);
            }).Start();

            Task.Run(async () =>
            {
                await Task.Delay(msTimeout);
                if (!tcs.Task.IsCompleted)
                {
                    Console.WriteLine("RunBlockingFuncOnOtherThread() timed out");
                    tcs.TrySetResult(false);
                }
            });

            await tcs.Task;
        }

If you think it's good solution, I can implement it and create pull request.

rihadavid commented 8 years ago

I've just tested that it does not happen on iPhone 4 iOS 7 device. The issue might be related only to the specific situation I created with the wifi - I disabled IPv4 on my wireless network in windows settings so that the Mac simulator gets wifi but no internet. Maybe it's something that can't really happen in real world and maybe it can be ignored. You can try to reproduce it or close the issue if you think it can be ignored :) Just wanted to let you know, don't currently have much time to do some deep investigation and figuring out different ways to create wifi without internet for my simulator.

RetrotecEd commented 7 years ago

Can confirm this issue. If you have a device acting as a hotspot but with no Internet connectivity, the timeout doesn't work. In fact the timeout just adds time. I am trying rihadavid's solution.

Our setup has three devices: Electric fan that pressurizes/depressurizes a room. A digital gauge that controls the fan. A Xamarin app on an iOS device that controls the gauge.

In situations (like a new construction project) where wifi is not available, the gauge acts as a hotspot so the iOS device can connect and control the gauge.

IS this the best solution? (I know this ticket is closed)

Double-Dude commented 7 years ago

Same here +1.