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; 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); }