Skip to content

Commit

Permalink
Update v6.4.5
Browse files Browse the repository at this point in the history
- Fixed issue with MTConnectHttpClientStream that caused an exception to be thrown when using multiple MTConnectClient connections. Moved the _httpClient variable from being static to be a class member and now requires the MTConnectHttpClientStream class to be disposed.
  • Loading branch information
PatrickRitchie committed Aug 2, 2024
1 parent 48e861a commit 8233cd4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions build/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Reflection;

[assembly: AssemblyVersion("6.4.4")]
[assembly: AssemblyFileVersion("6.4.4")]
[assembly: AssemblyVersion("6.4.5")]
[assembly: AssemblyFileVersion("6.4.5")]
[assembly: AssemblyCompany("TrakHound Inc.")]
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]
36 changes: 19 additions & 17 deletions libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,23 +717,25 @@ private async Task Worker()
if (CurrentOnly) url = CreateCurrentUrl(Authority, Device, Interval, _streamPath);

// Create and Start the Stream
_stream = new MTConnectHttpClientStream(url, DocumentFormat);
_stream.Timeout = Heartbeat * 3;
_stream.ContentEncodings = ContentEncodings;
_stream.ContentType = ContentType;
_stream.Starting += (s, o) => StreamStarting?.Invoke(this, url);
_stream.Started += (s, o) => StreamStarted?.Invoke(this, url);
_stream.Stopping += (s, o) => StreamStopping?.Invoke(this, url);
_stream.Stopped += (s, o) => StreamStopped?.Invoke(this, url);
_stream.DocumentReceived += (s, doc) => ProcessSampleDocument(doc, _stop.Token);
_stream.ErrorReceived += (s, doc) => ProcessSampleError(doc);
_stream.FormatError += (s, r) => FormatError?.Invoke(this, r);
_stream.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex);
_stream.InternalError += (s, ex) => InternalError?.Invoke(this, ex);

// Run Stream (Blocking call)
await _stream.Run(_stop.Token);

using (_stream = new MTConnectHttpClientStream(url, DocumentFormat))
{
_stream.Timeout = Heartbeat * 3;
_stream.ContentEncodings = ContentEncodings;
_stream.ContentType = ContentType;
_stream.Starting += (s, o) => StreamStarting?.Invoke(this, url);
_stream.Started += (s, o) => StreamStarted?.Invoke(this, url);
_stream.Stopping += (s, o) => StreamStopping?.Invoke(this, url);
_stream.Stopped += (s, o) => StreamStopped?.Invoke(this, url);
_stream.DocumentReceived += (s, doc) => ProcessSampleDocument(doc, _stop.Token);
_stream.ErrorReceived += (s, doc) => ProcessSampleError(doc);
_stream.FormatError += (s, r) => FormatError?.Invoke(this, r);
_stream.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex);
_stream.InternalError += (s, ex) => InternalError?.Invoke(this, ex);

// Run Stream (Blocking call)
await _stream.Run(_stop.Token);
}

initialRequest = false;

if (!_stop.Token.IsCancellationRequested)
Expand Down
19 changes: 11 additions & 8 deletions libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,19 @@ namespace MTConnect.Clients
/// <summary>
/// An Http Stream for reading MTConnect Sample or Current streams and returns MTConnectStreamsResponse documents
/// </summary>
public class MTConnectHttpClientStream
public class MTConnectHttpClientStream : IDisposable
{
private const int DefaultTimeout = 300000;
private const byte LineFeed = 10;
private const byte CarriageReturn = 13;
private const byte Dash = 45;
private static readonly HttpClient _httpClient;
private static readonly byte[] _trimBytes = new byte[] { 10, 13 };
private readonly HttpClient _httpClient;

private CancellationTokenSource _stop;
private string _documentFormat = DocumentFormat.XML;


static MTConnectHttpClientStream()
{
_httpClient = new HttpClient();
_httpClient.Timeout = TimeSpan.FromMilliseconds(DefaultTimeout);
}

public MTConnectHttpClientStream(string url, string documentFormat = DocumentFormat.XML)
{
Id = Guid.NewGuid().ToString();
Expand All @@ -45,6 +39,14 @@ public MTConnectHttpClientStream(string url, string documentFormat = DocumentFor
_documentFormat = documentFormat;
ContentEncodings = HttpContentEncodings.DefaultAccept;
ContentType = MimeTypes.Get(documentFormat);

_httpClient = new HttpClient();
_httpClient.Timeout = TimeSpan.FromMilliseconds(DefaultTimeout);
}

public void Dispose()
{
if (_httpClient != null) _httpClient.Dispose();
}


Expand Down Expand Up @@ -158,6 +160,7 @@ public async Task Run(CancellationToken cancellationToken)
httpRequest.RequestUri = new Uri(Url);
httpRequest.Method = HttpMethod.Get;


using (var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, stop.Token))
#if NET5_0_OR_GREATER
using (var stream = await response.Content.ReadAsStreamAsync(stop.Token))
Expand Down

0 comments on commit 8233cd4

Please sign in to comment.