Skip to content

Commit

Permalink
Fix P2P regression on the last commit
Browse files Browse the repository at this point in the history
Reachability will panic if it receives a blank hostname such as the one that a P2P replicator will send
  • Loading branch information
borrrden committed Oct 27, 2018
1 parent fe9c9bd commit 6ce8317
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions src/Couchbase.Lite.Support.Apple/iOS/Support/iOSReachability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal sealed class iOSReachability : IReachability

private NetworkReachability _ref;
private DispatchQueue _queue;
private AtomicBool _started;
private bool _started;

public event EventHandler<NetworkReachabilityChangeEventArgs> StatusChanged;

Expand Down Expand Up @@ -102,42 +102,51 @@ private void NotifyFlagsChanged(NetworkReachabilityFlags flags)

public void Start()
{
if (_started.Set(true))
_queue.DispatchSync(() =>
{
return;
}

if (Url == null)
{
_ref = new NetworkReachability(new IPAddress(0));
}
else
{
_ref = new NetworkReachability(Url.Host);
}

_ref.SetDispatchQueue(_queue);
_ref.SetNotification(ClientCallback);
if (_ref.GetFlags(out var flags) == StatusCode.OK)
{
Log.To.Sync.I(Tag, $"{this}: flags={flags}; starting...");
NotifyFlagsChanged(flags);
}
else
{
Log.To.Sync.I(Tag, $"{this}: starting...");
}
if (_started) {
return;
}

_started = true;

if (String.IsNullOrEmpty(Url?.Host))
{
_ref = new NetworkReachability(new IPAddress(0));
}
else
{
_ref = new NetworkReachability(Url.Host);
}

_ref.SetDispatchQueue(_queue);
_ref.SetNotification(ClientCallback);
if (_ref.GetFlags(out var flags) == StatusCode.OK)
{
Log.To.Sync.I(Tag, $"{this}: flags={flags}; starting...");
NotifyFlagsChanged(flags);
}
else
{
Log.To.Sync.I(Tag, $"{this}: starting...");
}
});
}

public void Stop()
{
if (!_started.Set(false))
_queue.DispatchSync(() =>
{
return;
}

ReachabilityKnown = false;
_ref.SetDispatchQueue(null);
if (!_started) {
return;
}

_started = false;
ReachabilityKnown = false;
_ref?.SetDispatchQueue(null);
_ref?.Dispose();
_ref = null;
});
}

#endregion
Expand Down

0 comments on commit 6ce8317

Please sign in to comment.