-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor out the TCP Client based stream reader so alternative implem…
…entations can be used. (#237)
- Loading branch information
1 parent
9af47d9
commit 626dc51
Showing
6 changed files
with
129 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Solutions/Ais.Net.Receiver/Ais/Net/Receiver/Receiver/INmeaStreamReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// <copyright file="INmeaStreamReader.cs" company="Endjin Limited"> | ||
// Copyright (c) Endjin Limited. All rights reserved. | ||
// </copyright> | ||
|
||
namespace Ais.Net.Receiver.Receiver; | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Abstracts network stream reading operations for NMEA messages | ||
/// </summary> | ||
public interface INmeaStreamReader : IAsyncDisposable | ||
{ | ||
/// <summary> | ||
/// Establishes a connection to the specified host and port | ||
/// </summary> | ||
Task ConnectAsync(string host, int port, CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Reads a line of text asynchronously | ||
/// </summary> | ||
Task<string?> ReadLineAsync(CancellationToken cancellationToken); | ||
|
||
/// <summary> | ||
/// Gets whether data is available to be read | ||
/// </summary> | ||
bool DataAvailable { get; } | ||
|
||
/// <summary> | ||
/// Gets whether the connection is established | ||
/// </summary> | ||
bool Connected { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
Solutions/Ais.Net.Receiver/Ais/Net/Receiver/Receiver/TcpClientNmeaStreamReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// <copyright file="TcpClientNmeaStreamReader.cs" company="Endjin Limited"> | ||
// Copyright (c) Endjin Limited. All rights reserved. | ||
// </copyright> | ||
|
||
namespace Ais.Net.Receiver.Receiver; | ||
|
||
using System.IO; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public class TcpClientNmeaStreamReader : INmeaStreamReader | ||
{ | ||
private TcpClient? tcpClient; | ||
private NetworkStream? stream; | ||
private StreamReader? reader; | ||
|
||
public bool DataAvailable => this.stream?.DataAvailable ?? false; | ||
|
||
public bool Connected => this.tcpClient?.Connected ?? false; | ||
|
||
public async Task ConnectAsync(string host, int port, CancellationToken cancellationToken) | ||
{ | ||
this.tcpClient = new TcpClient(); | ||
await this.tcpClient.ConnectAsync(host, port, cancellationToken); | ||
this.stream = this.tcpClient.GetStream(); | ||
this.reader = new StreamReader(this.stream); | ||
} | ||
|
||
public async Task<string?> ReadLineAsync(CancellationToken cancellationToken) | ||
{ | ||
return this.reader is not null | ||
? await this.reader.ReadLineAsync(cancellationToken).ConfigureAwait(false) | ||
: null; | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
this.reader?.Dispose(); | ||
|
||
if (this.stream is not null) | ||
{ | ||
await this.stream.DisposeAsync(); | ||
} | ||
|
||
this.tcpClient?.Dispose(); | ||
} | ||
} |