diff --git a/src/SampleApp.WinDesktop/Ntrip/Client.cs b/src/SampleApp.WinDesktop/Ntrip/Client.cs index 9c1b540..830c207 100644 --- a/src/SampleApp.WinDesktop/Ntrip/Client.cs +++ b/src/SampleApp.WinDesktop/Ntrip/Client.cs @@ -11,6 +11,7 @@ // * See the License for the specific language governing permissions and // * limitations under the License. // ****************************************************************************** +#nullable enable using System; using System.Collections.Generic; @@ -103,15 +104,24 @@ namespace NmeaParser.Gnss.Ntrip private async Task ReceiveThread() { byte[] buffer = new byte[65536]; - + sckt.ReceiveTimeout = 1000; while (connected && sckt != null) { - int count = sckt.Receive(buffer); + int count = sckt.Receive(buffer, SocketFlags.None, out SocketError errorCode); if (count > 0) { DataReceived?.Invoke(this, buffer.Take(count).ToArray()); } - await Task.Delay(10); + await Task.Yield(); + if (!sckt.Connected) + { + if (connected) + { + connected = false; + Disconnected?.Invoke(this, EventArgs.Empty); + } + break; + } } sckt?.Shutdown(SocketShutdown.Both); sckt?.Dispose(); @@ -136,5 +146,6 @@ namespace NmeaParser.Gnss.Ntrip } public event EventHandler? DataReceived; + public event EventHandler Disconnected; } } diff --git a/src/SampleApp.WinDesktop/NtripView.xaml.cs b/src/SampleApp.WinDesktop/NtripView.xaml.cs index 9427586..2723c55 100644 --- a/src/SampleApp.WinDesktop/NtripView.xaml.cs +++ b/src/SampleApp.WinDesktop/NtripView.xaml.cs @@ -1,6 +1,7 @@ using NmeaParser.Gnss.Ntrip; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -64,6 +65,12 @@ namespace SampleApp.WinDesktop } counter = 0; client.Connect(stream.Mountpoint); + client.Disconnected += (s, e) => + { + Debug.WriteLine("NTRIP Stream Disconnected. Retrying..."); + // Try and reconnect after a disconnect + client.Connect(stream.Mountpoint); + }; stop = client.CloseAsync; ntripstatus.Text = $"Connected"; }