Skip to content

Commit bf4c042

Browse files
Update 6.4.7
- Fixed issue with Http Client streaming buffer
1 parent e72d8a5 commit bf4c042

File tree

4 files changed

+68
-17
lines changed

4 files changed

+68
-17
lines changed

build/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Reflection;
22

3-
[assembly: AssemblyVersion("6.4.6")]
4-
[assembly: AssemblyFileVersion("6.4.6")]
3+
[assembly: AssemblyVersion("6.4.7")]
4+
[assembly: AssemblyFileVersion("6.4.7")]
55
[assembly: AssemblyCompany("TrakHound Inc.")]
66
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]

libraries/MTConnect.NET-Common/Extensions/ObjectExtensions.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,41 @@ public static byte[] TrimBytes(byte[] inputBytes, byte[] trimBytes)
221221
return null;
222222
}
223223

224+
public static int TrimStartBytes(ref byte[] inputBytes, byte[] trimBytes)
225+
{
226+
if (inputBytes != null && inputBytes.Length > 0 && trimBytes != null && trimBytes.Length > 0)
227+
{
228+
// Look for Trim bytes
229+
int i = 0;
230+
bool found;
231+
232+
while (i < inputBytes.Length)
233+
{
234+
found = false;
235+
for (var k = 0; k < trimBytes.Length; k++)
236+
{
237+
if (inputBytes[i] == trimBytes[k])
238+
{
239+
found = true;
240+
break;
241+
}
242+
}
243+
if (!found) break;
244+
i++;
245+
}
246+
247+
if (i > 0)
248+
{
249+
// Shift Array over past the initial Whitespace bytes
250+
Array.Copy(inputBytes, i, inputBytes, 0, inputBytes.Length - i);
251+
}
252+
253+
return i; // Return number of bytes shifted in array
254+
}
255+
256+
return 0;
257+
}
258+
224259
public static byte[] TrimStartBytes(byte[] inputBytes, byte[] trimBytes)
225260
{
226261
if (inputBytes != null && inputBytes.Length > 0 && trimBytes != null && trimBytes.Length > 0)

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MTConnectHttpClientStream : IDisposable
2424
private const byte LineFeed = 10;
2525
private const byte CarriageReturn = 13;
2626
private const byte Dash = 45;
27-
private static readonly byte[] _trimBytes = new byte[] { 10, 13 };
27+
private static readonly byte[] _trimBytes = new byte[] { LineFeed, CarriageReturn };
2828
private readonly HttpClient _httpClient;
2929

3030
private CancellationTokenSource _stop;
@@ -310,31 +310,33 @@ private static Stream ReadBody(Stream stream, int length)
310310
{
311311
if (stream != null && length > 0)
312312
{
313-
var i = 0;
314-
var size = length;
315-
var isHeader = true;
313+
int i = 0;
314+
int size = length;
315+
bool isHeader = true;
316+
int j;
317+
int k;
316318

317319
// Create a buffer to contain body of the response
318320
// based on the size of the content-length received in the Http Headers
319-
var body = new MemoryStream(size);
321+
var body = new MemoryStream();
322+
323+
// Create a 255 byte buffer
324+
var chunk = new byte[255];
320325

321326
while (i < size)
322327
{
323-
// Create a 512 byte buffer
324-
var chunk = new byte[512];
325-
326328
// Read from the Network stream and store in the chunk buffer
327-
var j = stream.Read(chunk, 0, chunk.Length);
329+
j = stream.Read(chunk, 0, chunk.Length);
328330

329331
// Remove blank lines before header (can cause XML deserialization error if Xml Declaration is not the first line)
330-
if (isHeader) chunk = ObjectExtensions.TrimStartBytes(chunk, _trimBytes);
331-
332-
// Verify bytes read doesn't exceed destination array
333-
// (could be blank lines after document that gets read)
334-
if (j > size - i) j = size - i;
332+
if (isHeader)
333+
{
334+
k = ObjectExtensions.TrimStartBytes(ref chunk, _trimBytes);
335+
j -= k;
336+
}
335337

336338
// Add the chunk bytes to the body buffer
337-
body.Write(chunk, 0, Math.Min(j, chunk.Length));
339+
body.Write(chunk, 0, j);
338340

339341
// Increment the index of the body buffer based on the number of bytes read in this chunk
340342
i += j;

libraries/MTConnect.NET-XML/Formatters/XmlResponseDocumentFormatter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ public FormatReadResult<IDevicesResponseDocument> CreateDevicesResponseDocument(
256256
catch (Exception ex)
257257
{
258258
messages.Add(ex.Message);
259+
260+
var innerException = ex.InnerException;
261+
while (innerException != null)
262+
{
263+
messages.Add(innerException.Message);
264+
innerException = innerException.InnerException;
265+
}
259266
}
260267

261268
return new FormatReadResult<IDevicesResponseDocument>(document, success, messages, warnings, errors);
@@ -310,6 +317,13 @@ public FormatReadResult<IStreamsResponseDocument> CreateStreamsResponseDocument(
310317
catch (Exception ex)
311318
{
312319
messages.Add(ex.Message);
320+
321+
var innerException = ex.InnerException;
322+
while (innerException != null)
323+
{
324+
messages.Add(innerException.Message);
325+
innerException = innerException.InnerException;
326+
}
313327
}
314328

315329
return new FormatReadResult<IStreamsResponseDocument>(document, success, messages, warnings, errors);

0 commit comments

Comments
 (0)