mirror of
https://github.com/dotMorten/NmeaParser.git
synced 2026-01-01 14:20:33 +01:00
Handle ntrip stream disconnects
This commit is contained in:
parent
8ea3cb8759
commit
2a0488a3ef
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue