Skip to content

Commit

Permalink
Add Client and Stream to TcpClient and Client to UdpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Jul 22, 2022
1 parent 0e990ee commit c3c3fc5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Version>2.2.0</Version>
<Version>2.2.1</Version>
<Authors>Chris Pulman</Authors>
<TargetFrameworks>net461;netstandard2.0;net6.0;</TargetFrameworks>
<Description>An Observable Com port extension of System.IO.Ports.SerialPort</Description>
Expand Down
33 changes: 25 additions & 8 deletions SerialPortRx/TcpClientRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// </copyright>

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Reactive;
Expand Down Expand Up @@ -69,10 +70,26 @@ public TcpClientRx()
/// </value>
public int ReadTimeout
{
get => _tcpClient!.GetStream().ReadTimeout;
set => _tcpClient!.GetStream().ReadTimeout = value;
get => Stream.ReadTimeout;
set => Stream.ReadTimeout = value;
}

/// <summary>
/// Gets the underlying System.Net.Sockets.Socket.
/// </summary>
/// <value>
/// The underlying network System.Net.Sockets.Socket.
/// </value>
public Socket Client => _tcpClient!.Client;

/// <summary>
/// Gets the System.Net.Sockets.NetworkStream used to send and receive data.
/// </summary>
/// <value>
/// The stream.
/// </value>
public Stream Stream => _tcpClient!.GetStream();

/// <summary>
/// Gets or sets the write timeout.
/// </summary>
Expand All @@ -81,8 +98,8 @@ public int ReadTimeout
/// </value>
public int WriteTimeout
{
get => _tcpClient!.GetStream().WriteTimeout;
set => _tcpClient!.GetStream().WriteTimeout = value;
get => Stream.WriteTimeout;
set => Stream.WriteTimeout = value;
}

/// <summary>
Expand All @@ -101,7 +118,7 @@ public int WriteTimeout
{
var dis = new CompositeDisposable();
var lastValue = -1;
dis.Add(Observable.While(() => true, Observable.Return(_tcpClient.GetStream().ReadByte())).Retry()
dis.Add(Observable.While(() => true, Observable.Return(Stream.ReadByte())).Retry()
.Subscribe(
d =>
{
Expand Down Expand Up @@ -150,7 +167,7 @@ public Task Open()
/// <param name="offset">The offset.</param>
/// <param name="count">The count.</param>
public void Write(byte[] buffer, int offset, int count) =>
_tcpClient!.GetStream().Write(buffer, offset, count);
Stream.Write(buffer, offset, count);

/// <summary>
/// Reads the specified buffer.
Expand All @@ -162,7 +179,7 @@ public void Write(byte[] buffer, int offset, int count) =>
public async Task<int> ReadAsync(byte[] buffer, int offset, int count)
{
#pragma warning disable CA1835 // Change the 'ReadAsync' method call to use the 'Stream.ReadAsync(Memory<byte>, CancellationToken)' overload.
var read = await _tcpClient!.GetStream().ReadAsync(buffer, offset, count);
var read = await Stream.ReadAsync(buffer, offset, count);
#pragma warning restore CA1835 // Change the 'ReadAsync' method call to use the 'Stream.ReadAsync(Memory<byte>, CancellationToken)' overload.
if (buffer?.Length > 0)
{
Expand All @@ -180,7 +197,7 @@ public async Task<int> ReadAsync(byte[] buffer, int offset, int count)
/// Discards the in buffer.
/// </summary>
public void DiscardInBuffer() =>
_tcpClient!.GetStream().Flush();
Stream.Flush();

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand Down
20 changes: 14 additions & 6 deletions SerialPortRx/UdpClientRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public UdpClientRx()
/// </value>
public int InfiniteTimeout => Timeout.Infinite;

/// <summary>
/// Gets the underlying System.Net.Sockets.Socket.
/// </summary>
/// <value>
/// The underlying network System.Net.Sockets.Socket.
/// </value>
public Socket Client => _udpClient!.Client;

/// <summary>
/// Gets or sets the read timeout.
/// </summary>
Expand All @@ -74,8 +82,8 @@ public UdpClientRx()
/// </value>
public int ReadTimeout
{
get => _udpClient!.Client.ReceiveTimeout;
set => _udpClient!.Client.ReceiveTimeout = value;
get => Client.ReceiveTimeout;
set => Client.ReceiveTimeout = value;
}

/// <summary>
Expand All @@ -86,8 +94,8 @@ public int ReadTimeout
/// </value>
public int WriteTimeout
{
get => _udpClient!.Client.SendTimeout;
set => _udpClient!.Client.SendTimeout = value;
get => Client.SendTimeout;
set => Client.SendTimeout = value;
}

/// <summary>
Expand Down Expand Up @@ -181,7 +189,7 @@ public void Write(byte[] buffer, int offset, int count)
"Argument count cannot be greater than the length of buffer minus offset.");
}

_udpClient?.Client.Send(buffer.Skip(offset).Take(count).ToArray());
Client.Send(buffer.Skip(offset).Take(count).ToArray());
}

/// <summary>
Expand Down Expand Up @@ -231,7 +239,7 @@ public Task<int> ReadAsync(byte[] buffer, int offset, int count)
{
if (_bufferOffset == 0)
{
_bufferOffset = _udpClient!.Client.Receive(_buffer);
_bufferOffset = Client.Receive(_buffer);
}

if (_bufferOffset < count)
Expand Down
2 changes: 1 addition & 1 deletion Version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "2.2.0",
"version": "2.2.1",
"nuGetPackageVersion": {
"semVer": 2.0
},
Expand Down

0 comments on commit c3c3fc5

Please sign in to comment.