Skip to content

Commit e1d7911

Browse files
Fixed issue with MTConnectHttpClient receiving blank lines before the header. This caused issues with the Xml deserialization and raised the Internal Error event
1 parent ed4e3fb commit e1d7911

2 files changed

Lines changed: 32 additions & 26 deletions

File tree

libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,7 @@ private void ProcessProbeDocument(IDevicesResponseDocument document)
833833
_cachedDataItems.Clear();
834834
}
835835

836-
// Raise ProbeReceived Event
837-
ProbeReceived?.Invoke(this, document);
838-
836+
var outputDevices = new List<IDevice>();
839837
foreach (var device in document.Devices)
840838
{
841839
var outputDevice = ProcessDevice(document.Header, device);
@@ -846,9 +844,15 @@ private void ProcessProbeDocument(IDevicesResponseDocument document)
846844
_devices.Remove(outputDevice.Uuid);
847845
_devices.Add(outputDevice.Uuid, outputDevice);
848846
}
847+
}
849848

849+
foreach (var outputDevice in outputDevices)
850+
{
850851
DeviceReceived?.Invoke(this, outputDevice);
851852
}
853+
854+
// Raise ProbeReceived Event
855+
ProbeReceived?.Invoke(this, document);
852856
}
853857
}
854858

@@ -920,8 +924,7 @@ private void ProcessSampleDocument(IStreamsResponseDocument document, Cancellati
920924
response.Streams = deviceStreams;
921925

922926

923-
SampleReceived?.Invoke(this, response);
924-
927+
var receivedObservations = new List<IObservation>();
925928

926929
// Process Device Streams
927930
foreach (var deviceStream in response.Streams)
@@ -936,12 +939,16 @@ private void ProcessSampleDocument(IStreamsResponseDocument document, Cancellati
936939
var observations = response.GetObservations();
937940
if (!observations.IsNullOrEmpty())
938941
{
939-
foreach (var observation in observations)
940-
{
941-
ObservationReceived?.Invoke(this, observation);
942-
}
942+
receivedObservations.AddRange(observations);
943943
}
944944
}
945+
946+
SampleReceived?.Invoke(this, response);
947+
948+
foreach (var observation in receivedObservations)
949+
{
950+
ObservationReceived?.Invoke(this, observation);
951+
}
945952
}
946953
}
947954
}

libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class MTConnectHttpClientStream
2525
private const byte CarriageReturn = 13;
2626
private const byte Dash = 45;
2727
private static readonly HttpClient _httpClient;
28+
private static readonly byte[] _trimBytes = new byte[] { 10, 13 };
2829

2930
private CancellationTokenSource _stop;
3031
private string _documentFormat = DocumentFormat.XML;
@@ -152,12 +153,13 @@ public async Task Run(CancellationToken cancellationToken)
152153
}
153154
}
154155

155-
// Get the HTTP Stream
156-
#if NET5_0_OR_GREATER
157-
using (var stream = await _httpClient.GetStreamAsync(Url, stop.Token))
158-
#else
159-
using (var stream = await _httpClient.GetStreamAsync(Url))
160-
#endif
156+
157+
var httpRequest = new HttpRequestMessage();
158+
httpRequest.RequestUri = new Uri(Url);
159+
httpRequest.Method = HttpMethod.Get;
160+
161+
using (var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, stop.Token))
162+
using (var stream = response.Content.ReadAsStream())
161163
{
162164
var header = new List<byte>();
163165
var headerActive = false;
@@ -169,7 +171,6 @@ public async Task Run(CancellationToken cancellationToken)
169171
var line = new List<byte>();
170172
var contentLength = 0;
171173
string contentEncoding = null;
172-
var trimBytes = new byte[] { 10, 13 };
173174
string lineStr = null;
174175

175176

@@ -197,9 +198,6 @@ public async Task Run(CancellationToken cancellationToken)
197198

198199
if (cr && lf)
199200
{
200-
// Trim CR and LF bytes from beginning and end
201-
//var lineBytes = ObjectExtensions.TrimBytes(line.ToArray(), trimBytes);
202-
203201
// Get the current line as a UTF-8 string
204202
lineStr = Encoding.UTF8.GetString(line.ToArray());
205203

@@ -246,7 +244,7 @@ public async Task Run(CancellationToken cancellationToken)
246244

247245
// Add byte to Line Buffer
248246
line.Add((byte)b);
249-
247+
250248
prevByte = b;
251249

252250
// Read the next Byte
@@ -307,16 +305,12 @@ private static Stream ReadBody(Stream stream, int length)
307305
{
308306
var i = 0;
309307
var size = length;
308+
var isHeader = true;
310309

311310
// Create a buffer to contain body of the response
312311
// based on the size of the content-length received in the Http Headers
313312
var body = new MemoryStream(size);
314313

315-
// Read New Line characters
316-
// These always appear after the Http Header
317-
stream.ReadByte(); // 13
318-
stream.ReadByte(); // 10
319-
320314
while (i < size)
321315
{
322316
// Create a 512 byte buffer
@@ -325,15 +319,20 @@ private static Stream ReadBody(Stream stream, int length)
325319
// Read from the Network stream and store in the chunk buffer
326320
var j = stream.Read(chunk, 0, chunk.Length);
327321

322+
// Remove blank lines before header (can cause XML deserialization error if Xml Declaration is not the first line)
323+
if (isHeader) chunk = ObjectExtensions.TrimStartBytes(chunk, _trimBytes);
324+
328325
// Verify bytes read doesn't exceed destination array
329326
// (could be blank lines after document that gets read)
330327
if (j > size - i) j = size - i;
331328

332329
// Add the chunk bytes to the body buffer
333-
body.Write(chunk, 0, j);
330+
body.Write(chunk, 0, Math.Min(j, chunk.Length));
334331

335332
// Increment the index of the body buffer based on the number of bytes read in this chunk
336333
i += j;
334+
335+
isHeader = false;
337336
}
338337

339338
return body;

0 commit comments

Comments
 (0)