From 29057a0fe35a9e23a091594cec624d7aa71c6483 Mon Sep 17 00:00:00 2001
From: Adrian Stevens <adrian.stevens@gmail.com>
Date: Wed, 3 Jul 2024 08:24:01 -0700
Subject: [PATCH] Add listener proc exception catch + minor cleanup + version
 bump

---
 Source/v2/Meadow.Cli/Meadow.CLI.csproj                 |  2 +-
 Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs        |  2 +-
 .../Connections/SerialConnection.ListenerProc.cs       | 10 ++++++----
 Source/v2/Meadow.Hcom/Connections/SerialConnection.cs  |  4 +++-
 Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs     |  2 +-
 5 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/Source/v2/Meadow.Cli/Meadow.CLI.csproj b/Source/v2/Meadow.Cli/Meadow.CLI.csproj
index ceff66e7..efbd5efe 100644
--- a/Source/v2/Meadow.Cli/Meadow.CLI.csproj
+++ b/Source/v2/Meadow.Cli/Meadow.CLI.csproj
@@ -10,7 +10,7 @@
     <Authors>Wilderness Labs, Inc</Authors>
     <Company>Wilderness Labs, Inc</Company>
     <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
-    <PackageVersion>2.0.51.0</PackageVersion>
+    <PackageVersion>2.0.52.0</PackageVersion>
     <Platforms>AnyCPU</Platforms>
     <PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
     <RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
diff --git a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
index a674c67e..da7ee0b3 100644
--- a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
+++ b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
@@ -6,5 +6,5 @@ namespace Meadow.CLI;
 
 public static class Constants
 {
-    public const string CLI_VERSION = "2.0.51.0";
+    public const string CLI_VERSION = "2.0.52.0";
 }
\ No newline at end of file
diff --git a/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs b/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs
index 0f5a8b65..8a17560e 100644
--- a/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs
+++ b/Source/v2/Meadow.Hcom/Connections/SerialConnection.ListenerProc.cs
@@ -1,6 +1,4 @@
-using System.Runtime.InteropServices;
-
-namespace Meadow.Hcom
+namespace Meadow.Hcom
 {
     public partial class SerialConnection
     {
@@ -94,6 +92,11 @@ private async Task ListenerProc()
                             }
                             goto read;
                         }
+                        catch (Exception ex)
+                        {
+                            Debug.WriteLine($"Error reading from port: {ex.Message}");
+                            await Task.Delay(1000);
+                        }
 
                         Debug.WriteLine($"Received {receivedLength} bytes");
 
@@ -315,7 +318,6 @@ private async Task ListenerProc()
                     }
                     catch (IOException ioe)
                     {
-
                         FileException?.Invoke(this, ioe);
                         // attempt to read timed out (i.e. there's just no data)
                         // NOP
diff --git a/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs b/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
index 20a3345f..1d175e75 100755
--- a/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
+++ b/Source/v2/Meadow.Hcom/Connections/SerialConnection.cs
@@ -1284,7 +1284,7 @@ public override async Task StartDebugging(int port, ILogger? logger, Cancellatio
         }
     }
 
-    public override async Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken)
+    public override Task SendDebuggerData(byte[] debuggerData, uint userData, CancellationToken? cancellationToken)
     {
         var command = RequestBuilder.Build<DebuggerDataRequest>(userData);
         command.DebuggerData = debuggerData;
@@ -1292,5 +1292,7 @@ public override async Task SendDebuggerData(byte[] debuggerData, uint userData,
         _lastRequestConcluded = null;
 
         EnqueueRequest(command);
+
+        return Task.CompletedTask;
     }
 }
\ No newline at end of file
diff --git a/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs b/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs
index ce0a097e..1b359759 100644
--- a/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs
+++ b/Source/v2/Meadow.Hcom/Debugging/DebuggingServer.cs
@@ -49,7 +49,7 @@ public async Task StartListening(CancellationToken cancellationToken)
         _listener.Start();
         _logger?.LogInformation($"Listening for Visual Studio to connect");
 
-        // This call will wait for the client to connect, before continuing. We shouldn't need a loop.
+        // This call will wait for the client to connect, before continuing.
         var tcpClient = await _listener.AcceptTcpClientAsync();
         _activeClient = CreateActiveClient(tcpClient);
     }