Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/multi-19ba9b227f
Browse files Browse the repository at this point in the history
  • Loading branch information
mregen authored Feb 6, 2025
2 parents 04a5338 + 029b84b commit 6940530
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 143 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.1" />
<PackageReference Include="Mono.Options" Version="6.12.0.148" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.1" />
<PackageReference Include="Mono.Options" Version="6.12.0.148" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Expressions" Version="5.0.0" />
Expand Down
30 changes: 5 additions & 25 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpMessageSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,38 +667,18 @@ private bool ReadNext()
/// </summary>
private delegate void CallbackAction(SocketError error);

/// <summary>
/// Try to connect to endpoint and do callback if connected successfully
/// </summary>
/// <param name="address">Endpoint address</param>
/// <param name="addressFamily">Endpoint address family</param>
/// <param name="port">Endpoint port</param>
/// <param name="callback">Callback that must be executed if the connection would be established</param>
private SocketError BeginConnect(IPAddress address, AddressFamily addressFamily, int port, CallbackAction callback)
{
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
var args = new SocketAsyncEventArgs() {
UserToken = callback,
RemoteEndPoint = new IPEndPoint(address, port),
};
args.Completed += OnSocketConnected;
if (!socket.ConnectAsync(args))
{
// I/O completed synchronously
OnSocketConnected(socket, args);
return args.SocketError;
}
return SocketError.InProgress;
}

/// <summary>
/// Try to connect to endpoint and do callback if connected successfully
/// </summary>
/// <param name="endpoint">The DNS endpoint</param>
/// <param name="callback">Callback that must be executed if the connection would be established</param>
private SocketError BeginConnect(DnsEndPoint endpoint, CallbackAction callback)
{
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp){
NoDelay = true,
LingerState = new LingerOption(true, 5),
};

var args = new SocketAsyncEventArgs() {
UserToken = callback,
RemoteEndPoint = endpoint,
Expand Down
31 changes: 15 additions & 16 deletions Stack/Opc.Ua.Core/Stack/Tcp/TcpTransportListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,24 +574,18 @@ public void Start()
port = Utils.UaTcpDefaultPort;
}

bool bindToSpecifiedAddress = true;
UriHostNameType hostType = Uri.CheckHostName(m_uri.Host);
if (hostType == UriHostNameType.Dns || hostType == UriHostNameType.Unknown || hostType == UriHostNameType.Basic)
{
bindToSpecifiedAddress = false;
}

IPAddress ipAddress = IPAddress.Any;
if (bindToSpecifiedAddress)
{
ipAddress = IPAddress.Parse(m_uri.Host);
}
bool bindToSpecifiedAddress = hostType != UriHostNameType.Dns && hostType != UriHostNameType.Unknown && hostType != UriHostNameType.Basic;
IPAddress ipAddress = bindToSpecifiedAddress ? IPAddress.Parse(m_uri.Host) : IPAddress.Any;

// create IPv4 or IPv6 socket.
try
{
IPEndPoint endpoint = new IPEndPoint(ipAddress, port);
m_listeningSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
m_listeningSocket = new Socket(endpoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) {
NoDelay = true,
LingerState = new LingerOption(true, 5),
};
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.Completed += OnAccept;
args.UserToken = m_listeningSocket;
Expand Down Expand Up @@ -625,12 +619,17 @@ public void Start()
try
{
IPEndPoint endpointIPv6 = new IPEndPoint(IPAddress.IPv6Any, port);
m_listeningSocketIPv6 = new Socket(endpointIPv6.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
m_listeningSocketIPv6 = new Socket(endpointIPv6.AddressFamily, SocketType.Stream, ProtocolType.Tcp) {
NoDelay = true,
LingerState = new LingerOption(true, 5),
};
SocketAsyncEventArgs args = new SocketAsyncEventArgs() {
UserToken = m_listeningSocketIPv6
};
args.Completed += OnAccept;
args.UserToken = m_listeningSocketIPv6;

m_listeningSocketIPv6.Bind(endpointIPv6);
m_listeningSocketIPv6.Listen(Int32.MaxValue);
m_listeningSocketIPv6.Listen(kSocketBacklog);
if (!m_listeningSocketIPv6.AcceptAsync(args))
{
OnAccept(null, args);
Expand Down
133 changes: 62 additions & 71 deletions Stack/Opc.Ua.Core/Types/Constants/Attributes.Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Xml;

Expand All @@ -22,6 +25,11 @@ namespace Opc.Ua
public static partial class Attributes
{
#region Static Helper Functions
/// <summary>
/// Creates a dictionary of browse names for the attributes.
/// </summary>
private static readonly Lazy<ReadOnlyDictionary<uint, string>> AttributeNames = new Lazy<ReadOnlyDictionary<uint, string>>(CreateAttributeNamesDictionary);

/// <summary>
/// Returns true if the attribute id is valid.
/// </summary>
Expand All @@ -35,50 +43,32 @@ public static bool IsValid(uint attributeId)
/// </summary>
public static string GetBrowseName(uint identifier)
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

foreach (FieldInfo field in fields)
if (AttributeNames.Value.TryGetValue(identifier, out var name))
{
if (identifier == (uint)field.GetValue(typeof(Attributes)))
{
return field.Name;
}
return name;
}

return System.String.Empty;
return string.Empty;
}

/// <summary>
/// Returns the browse names for all attributes.
/// </summary>
public static string[] GetBrowseNames()
public static IReadOnlyCollection<string> GetBrowseNames()
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

int ii = 0;

string[] names = new string[fields.Length];

foreach (FieldInfo field in fields)
{
names[ii++] = field.Name;
}

return names;
return AttributeNames.Value.Values;
}

/// <summary>
/// Returns the id for the attribute with the specified browse name.
/// </summary>
public static uint GetIdentifier(string browseName)
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

foreach (FieldInfo field in fields)
foreach (var field in AttributeNames.Value)
{
if (field.Name == browseName)
if (field.Value == browseName)
{
return (uint)field.GetValue(typeof(Attributes));
return field.Key;
}
}

Expand All @@ -88,34 +78,20 @@ public static uint GetIdentifier(string browseName)
/// <summary>
/// Returns the ids for all attributes.
/// </summary>
public static uint[] GetIdentifiers()
public static IReadOnlyCollection<uint> GetIdentifiers()
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

int ii = 0;
uint[] ids = new uint[fields.Length];

foreach (FieldInfo field in fields)
{
ids[ii++] = (uint)field.GetValue(typeof(Attributes));
}

return ids;
return AttributeNames.Value.Keys;
}

/// <summary>
/// Returns the ids for all attributes which are valid for the at least one of the node classes specified by the mask.
/// </summary>
public static UInt32Collection GetIdentifiers(NodeClass nodeClass)
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

UInt32Collection ids = new UInt32Collection(fields.Length);
UInt32Collection ids = new UInt32Collection(AttributeNames.Value.Count);

foreach (FieldInfo field in fields)
foreach (uint id in AttributeNames.Value.Keys)
{
uint id = (uint)field.GetValue(typeof(Attributes));

if (IsValid(nodeClass, id))
{
ids.Add(id);
Expand Down Expand Up @@ -171,33 +147,33 @@ public static NodeId GetDataTypeId(uint attributeId)
{
switch (attributeId)
{
case Value: return DataTypes.BaseDataType;
case DisplayName: return DataTypes.LocalizedText;
case Description: return DataTypes.LocalizedText;
case WriteMask: return DataTypes.UInt32;
case UserWriteMask: return DataTypes.UInt32;
case NodeId: return DataTypes.NodeId;
case NodeClass: return DataTypes.Enumeration;
case BrowseName: return DataTypes.QualifiedName;
case IsAbstract: return DataTypes.Boolean;
case Symmetric: return DataTypes.Boolean;
case InverseName: return DataTypes.LocalizedText;
case ContainsNoLoops: return DataTypes.Boolean;
case EventNotifier: return DataTypes.Byte;
case DataType: return DataTypes.NodeId;
case ValueRank: return DataTypes.Int32;
case AccessLevel: return DataTypes.Byte;
case UserAccessLevel: return DataTypes.Byte;
case MinimumSamplingInterval: return DataTypes.Duration;
case Historizing: return DataTypes.Boolean;
case Executable: return DataTypes.Boolean;
case UserExecutable: return DataTypes.Boolean;
case ArrayDimensions: return DataTypes.UInt32;
case DataTypeDefinition: return DataTypes.Structure;
case RolePermissions: return DataTypes.RolePermissionType;
case UserRolePermissions: return DataTypes.RolePermissionType;
case AccessRestrictions: return DataTypes.UInt16;
case AccessLevelEx: return DataTypes.UInt32;
case Value: return DataTypeIds.BaseDataType;
case DisplayName: return DataTypeIds.LocalizedText;
case Description: return DataTypeIds.LocalizedText;
case WriteMask: return DataTypeIds.UInt32;
case UserWriteMask: return DataTypeIds.UInt32;
case NodeId: return DataTypeIds.NodeId;
case NodeClass: return DataTypeIds.Enumeration;
case BrowseName: return DataTypeIds.QualifiedName;
case IsAbstract: return DataTypeIds.Boolean;
case Symmetric: return DataTypeIds.Boolean;
case InverseName: return DataTypeIds.LocalizedText;
case ContainsNoLoops: return DataTypeIds.Boolean;
case EventNotifier: return DataTypeIds.Byte;
case DataType: return DataTypeIds.NodeId;
case ValueRank: return DataTypeIds.Int32;
case AccessLevel: return DataTypeIds.Byte;
case UserAccessLevel: return DataTypeIds.Byte;
case MinimumSamplingInterval: return DataTypeIds.Duration;
case Historizing: return DataTypeIds.Boolean;
case Executable: return DataTypeIds.Boolean;
case UserExecutable: return DataTypeIds.Boolean;
case ArrayDimensions: return DataTypeIds.UInt32;
case DataTypeDefinition: return DataTypeIds.Structure;
case RolePermissions: return DataTypeIds.RolePermissionType;
case UserRolePermissions: return DataTypeIds.RolePermissionType;
case AccessRestrictions: return DataTypeIds.UInt16;
case AccessLevelEx: return DataTypeIds.UInt32;
}

return null;
Expand Down Expand Up @@ -409,5 +385,20 @@ public static AttributeWriteMask GetMask(uint attributeId)
return 0;
}
#endregion

#region Private Methods
private static ReadOnlyDictionary<uint, string> CreateAttributeNamesDictionary()
{
FieldInfo[] fields = typeof(Attributes).GetFields(BindingFlags.Public | BindingFlags.Static);

var keyValuePairs = new Dictionary<uint, string>();
foreach (FieldInfo field in fields)
{
keyValuePairs.Add((uint)field.GetValue(typeof(Attributes)), field.Name);
}

return new ReadOnlyDictionary<uint, string>(keyValuePairs);
}
#endregion
}
}
Loading

0 comments on commit 6940530

Please sign in to comment.