Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set TCP defaults for max message size to align with min buffer size #2616

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpMessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@
/// <summary>
/// The default maximum chunk count for Request and Response messages.
/// </summary>
public const int DefaultMaxChunkCount = 32;
public const int DefaultMaxChunkCount = DefaultMaxMessageSize / MinBufferSize;

/// <summary>
/// The default maximum message size.
/// </summary>
/// <remarks>
/// The default is 2MB. Ensure to set this to a value aligned to <see cref="MinBufferSize"/>.
/// This default is for the Tcp transport. <see cref="DefaultEncodingLimits.MaxMessageSize"/> for the generic default.
/// </remarks>
public const int DefaultMaxMessageSize = DefaultMaxChunkCount * DefaultMaxBufferSize;
public const int DefaultMaxMessageSize = MinBufferSize * 256;

/// <summary>
/// The default maximum message size for the discovery channel.
Expand Down Expand Up @@ -301,5 +302,17 @@
/// The certificates that have the key size larger than KeySizeExtraPadding need an extra padding byte in the transport message
/// </summary>
public const int KeySizeExtraPadding = 2048;

/// <summary>
/// Aligns the max message size to the nearest min buffer size.
/// </summary>
/// <remarks>
/// Align user configured maximum message size to avoid rounding errors in other UA implementations.
/// </remarks>
public static int AlignRoundMaxMessageSize(int value)
{
int alignmentMask = MinBufferSize - 1;
return (value + alignmentMask) & ~alignmentMask;

Check warning on line 315 in Stack/Opc.Ua.Core/Stack/Tcp/TcpMessageType.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Stack/Tcp/TcpMessageType.cs#L314-L315

Added lines #L314 - L315 were not covered by tests
romanett marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
4 changes: 2 additions & 2 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@
{
m_inactivityDetectPeriod = configuration.ChannelLifetime / 2;
m_quotas.MaxBufferSize = configuration.MaxBufferSize;
m_quotas.MaxMessageSize = configuration.MaxMessageSize;
m_quotas.MaxMessageSize = TcpMessageLimits.AlignRoundMaxMessageSize(configuration.MaxMessageSize);

Check warning on line 141 in Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs#L141

Added line #L141 was not covered by tests
m_quotas.ChannelLifetime = configuration.ChannelLifetime;
m_quotas.SecurityTokenLifetime = configuration.SecurityTokenLifetime;
messageContext.MaxArrayLength = configuration.MaxArrayLength;
messageContext.MaxByteStringLength = configuration.MaxByteStringLength;
messageContext.MaxMessageSize = configuration.MaxMessageSize;
messageContext.MaxMessageSize = TcpMessageLimits.AlignRoundMaxMessageSize(configuration.MaxMessageSize);

Check warning on line 146 in Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs#L146

Added line #L146 was not covered by tests
messageContext.MaxStringLength = configuration.MaxStringLength;
messageContext.MaxEncodingNestingLevels = configuration.MaxEncodingNestingLevels;
messageContext.MaxDecoderRecoveries = configuration.MaxDecoderRecoveries;
Expand Down
4 changes: 2 additions & 2 deletions Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,13 +423,13 @@
EndpointConfiguration configuration = m_settings.Configuration;
m_quotas = new ChannelQuotas {
MaxBufferSize = configuration.MaxBufferSize,
MaxMessageSize = configuration.MaxMessageSize,
MaxMessageSize = TcpMessageLimits.AlignRoundMaxMessageSize(configuration.MaxMessageSize),

Check warning on line 426 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs#L426

Added line #L426 was not covered by tests
ChannelLifetime = configuration.ChannelLifetime,
SecurityTokenLifetime = configuration.SecurityTokenLifetime,
MessageContext = new ServiceMessageContext() {
MaxArrayLength = configuration.MaxArrayLength,
MaxByteStringLength = configuration.MaxByteStringLength,
MaxMessageSize = configuration.MaxMessageSize,
MaxMessageSize = TcpMessageLimits.AlignRoundMaxMessageSize(configuration.MaxMessageSize),

Check warning on line 432 in Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs

View check run for this annotation

Codecov / codecov/patch

Stack/Opc.Ua.Core/Stack/Tcp/UaSCBinaryTransportChannel.cs#L432

Added line #L432 was not covered by tests
MaxStringLength = configuration.MaxStringLength,
MaxEncodingNestingLevels = configuration.MaxEncodingNestingLevels,
MaxDecoderRecoveries = configuration.MaxDecoderRecoveries,
Expand Down
18 changes: 11 additions & 7 deletions Stack/Opc.Ua.Core/Types/Utils/DefaultEncodingLimits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

using System;
using Opc.Ua.Bindings;

namespace Opc.Ua
{
Expand All @@ -21,32 +22,35 @@ namespace Opc.Ua
public static class DefaultEncodingLimits
{
/// <summary>
/// The maximum length for any string, byte string or xml element.
/// The default maximum length for any string, byte string or xml element.
/// </summary>
public static readonly int MaxStringLength = UInt16.MaxValue;

/// <summary>
/// The maximum length for any array.
/// The default maximum length for any array.
/// </summary>
public static readonly int MaxArrayLength = UInt16.MaxValue;

/// <summary>
/// The maximum length for any ByteString.
/// The default maximum length for any ByteString.
/// </summary>
public static readonly int MaxByteStringLength = UInt16.MaxValue * 16;

/// <summary>
/// The maximum length for any Message.
/// The default maximum length for any Message.
/// </summary>
public static readonly int MaxMessageSize = UInt16.MaxValue * 32;
/// <remarks>
/// Default is 2MB. Set to multiple of MinBufferSize to avoid rounding errors in other UA implementations.
/// </remarks>
public static readonly int MaxMessageSize = TcpMessageLimits.MinBufferSize * 256;

/// <summary>
/// The maximum nesting level accepted while encoding or decoding objects.
/// The default maximum nesting level accepted while encoding or decoding objects.
/// </summary>
public static readonly int MaxEncodingNestingLevels = 200;

/// <summary>
/// The number of times the decoder can recover from an error
/// The default number of times the decoder can recover from an error
/// caused by an encoded ExtensionObject before throwing a decoder error.
/// </summary>
public static readonly int MaxDecoderRecoveries = 0;
Expand Down
Loading