Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/APRSsharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 31 additions & 1 deletion src/KissTnc/TcpTnc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ namespace AprsSharp.KissTnc
/// </summary>
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;

/// <summary>
/// Initializes a new instance of the <see cref="TcpTnc"/> class.
/// </summary>
/// <param name="tcpConnection">A <see cref="ITcpConnection"/> to communicate with the TNC.</param>
/// <param name="tncPort">Por the remote TNC should use to communicate to the radio.</param>
/// <param name="tncPort">Port the remote TNC should use to communicate to the radio. Part of the KISS protocol.</param>
public TcpTnc(ITcpConnection tcpConnection, byte tncPort)
: base(tncPort)
{
tcpConnectionInjected = true;
this.tcpConnection = tcpConnection ?? throw new ArgumentNullException(nameof(tcpConnection));

if (!this.tcpConnection.Connected)
Expand All @@ -28,9 +34,33 @@ public TcpTnc(ITcpConnection tcpConnection, byte tncPort)
this.tcpConnection.AsyncReceive(bytes => DecodeReceivedData(bytes));
}

/// <summary>
/// Initializes a new instance of the <see cref="TcpTnc"/> class.
/// </summary>
/// <param name="address">The address (e.g. IP or domain name) of the TCP server.</param>
/// <param name="tcpPort">The TCP port for connection to the TCP server.</param>
/// <param name="tncPort">The TNC port used for connection to the radio. Part of the KISS protocol.</param>
public TcpTnc(string address, int tcpPort, byte tncPort)
: base(tncPort)
{
tcpConnectionInjected = false;
tcpConnection = new TcpConnection();
tcpConnection.Connect(address, tcpPort);

tcpConnection.AsyncReceive(bytes => DecodeReceivedData(bytes));
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (tcpConnectionInjected == false)
{
tcpConnection.Dispose();
}
}

base.Dispose(disposing);
}

Expand Down