From c7752a6455adbf1eecf2f12153a92e50a67bd44c Mon Sep 17 00:00:00 2001 From: Cameron Bielstein Date: Mon, 9 Jun 2025 10:51:00 -0700 Subject: [PATCH 1/2] Add a constructor on TcpTnc that doesn't require making a TcpConnection object. --- src/KissTnc/TcpTnc.cs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/KissTnc/TcpTnc.cs b/src/KissTnc/TcpTnc.cs index 5d204b4..f97e2d3 100644 --- a/src/KissTnc/TcpTnc.cs +++ b/src/KissTnc/TcpTnc.cs @@ -8,16 +8,22 @@ namespace AprsSharp.KissTnc /// public sealed class TcpTnc : Tnc { + [System.Diagnostics.CodeAnalysis.SuppressMessage( + "IDisposableAnalyzers.Correctness", + "IDISP008:Don't assign member with injected and created disposables", + Justification = "Used for testing, disposal managed by the tcpConnectionInjected bool.")] private readonly ITcpConnection tcpConnection; + private readonly bool tcpConnectionInjected; /// /// Initializes a new instance of the class. /// /// A to communicate with the TNC. - /// Por the remote TNC should use to communicate to the radio. + /// Port the remote TNC should use to communicate to the radio. Part of the KISS protocol. public TcpTnc(ITcpConnection tcpConnection, byte tncPort) : base(tncPort) { + tcpConnectionInjected = true; this.tcpConnection = tcpConnection ?? throw new ArgumentNullException(nameof(tcpConnection)); if (!this.tcpConnection.Connected) @@ -28,9 +34,33 @@ public TcpTnc(ITcpConnection tcpConnection, byte tncPort) this.tcpConnection.AsyncReceive(bytes => DecodeReceivedData(bytes)); } + /// + /// Initializes a new instance of the class. + /// + /// The address (e.g. IP or domain name) of the TCP server. + /// The TCP port for connection to the TCP server. + /// The TNC port used for connection to the radio. Part of the KISS protocol. + public TcpTnc(string address, int tcpPort, byte tncPort) + : base(tncPort) + { + tcpConnectionInjected = false; + tcpConnection = new TcpConnection(); + tcpConnection.Connect(address, tcpPort); + + tcpConnection.AsyncReceive(bytes => DecodeReceivedData(bytes)); + } + /// protected override void Dispose(bool disposing) { + if (disposing) + { + if (tcpConnectionInjected == false) + { + tcpConnection.Dispose(); + } + } + base.Dispose(disposing); } From d513325cc2f4e690f6d162e771db160024aa9727 Mon Sep 17 00:00:00 2001 From: Cameron Bielstein Date: Sat, 4 Oct 2025 22:16:28 -0700 Subject: [PATCH 2/2] Use new constructor. --- src/APRSsharp/Program.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/APRSsharp/Program.cs b/src/APRSsharp/Program.cs index a844bad..47551b1 100644 --- a/src/APRSsharp/Program.cs +++ b/src/APRSsharp/Program.cs @@ -285,10 +285,7 @@ private static async Task Execute( { Console.WriteLine($"Connecting to KISS TNC via TCP: {server}:{port}"); - using TcpConnection tcp = new TcpConnection(); - tcp.Connect(server, port); - using Tnc tnc = new TcpTnc(tcp, 0); - + using Tnc tnc = new TcpTnc(server, port, 0); RunTncMode(tnc, callsign, displayParseFailures); break;