Handle ntrip stream disconnects

This commit is contained in:
Morten Nielsen 2020-07-29 13:24:37 -07:00
parent 8ea3cb8759
commit 2a0488a3ef
2 changed files with 21 additions and 3 deletions

View file

@ -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<byte[]>? DataReceived;
public event EventHandler Disconnected;
}
}

View file

@ -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";
}