fix: 限制UDP最大包字节数,修复了字节计算问题
This commit is contained in:
parent
287c416247
commit
ef1a6c8208
|
@ -330,23 +330,23 @@ public class UDPClientPool
|
|||
return new(new Exception("Message bus not working!"));
|
||||
|
||||
// Calculate read times and segments
|
||||
var maxBytesPerRead = 256 * (32 / 8); // 1024 bytes per read
|
||||
var restBytes = dataLength % maxBytesPerRead;
|
||||
var readTimes = restBytes != 0 ?
|
||||
dataLength / maxBytesPerRead + 1 :
|
||||
dataLength / maxBytesPerRead;
|
||||
var max4BytesPerRead = 0x80; // 1024 bytes per read
|
||||
var rest4Bytes = dataLength % max4BytesPerRead;
|
||||
var readTimes = (rest4Bytes != 0) ?
|
||||
(dataLength / max4BytesPerRead + 1) :
|
||||
(dataLength / max4BytesPerRead);
|
||||
|
||||
for (var i = 0; i < readTimes; i++)
|
||||
{
|
||||
// Calculate current segment size
|
||||
var isLastSegment = i == readTimes - 1;
|
||||
var currentSegmentSize = isLastSegment ? restBytes : maxBytesPerRead;
|
||||
var currentSegmentSize = (isLastSegment && rest4Bytes != 0) ? rest4Bytes : max4BytesPerRead;
|
||||
|
||||
// Set burst length (in 32-bit words)
|
||||
opts.BurstLength = (byte)(currentSegmentSize / 4 - 1);
|
||||
opts.BurstLength = (byte)(currentSegmentSize - 1);
|
||||
|
||||
// Update address for current segment
|
||||
opts.Address = devAddr + (uint)(i * maxBytesPerRead);
|
||||
opts.Address = devAddr + (uint)(i * max4BytesPerRead);
|
||||
|
||||
// Send read address package
|
||||
ret = await UDPClientPool.SendAddrPackAsync(endPoint, new SendAddrPackage(opts));
|
||||
|
@ -365,16 +365,16 @@ public class UDPClientPool
|
|||
return new(new Exception($"Data is null at segment {i}, package: {retPackOpts.ToString()}"));
|
||||
|
||||
// Validate received data length
|
||||
if (retPackOpts.Data.Length != currentSegmentSize)
|
||||
return new(new Exception($"Expected {currentSegmentSize} bytes but received {retPackOpts.Data.Length} bytes at segment {i}"));
|
||||
if (retPackOpts.Data.Length != currentSegmentSize * 4)
|
||||
return new(new Exception($"Expected {currentSegmentSize * 4} bytes but received {retPackOpts.Data.Length} bytes at segment {i}"));
|
||||
|
||||
// Add received data to result
|
||||
resultData.AddRange(retPackOpts.Data);
|
||||
}
|
||||
|
||||
// Validate total data length
|
||||
if (resultData.Count != dataLength)
|
||||
return new(new Exception($"Expected total {dataLength} bytes but received {resultData.Count} bytes"));
|
||||
if (resultData.Count != dataLength * 4)
|
||||
return new(new Exception($"Expected total {dataLength * 4} bytes but received {resultData.Count} bytes"));
|
||||
|
||||
return resultData.ToArray();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue