You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by aa...@apache.org on 2023/05/11 08:46:18 UTC
[rocketmq-clients] branch master updated: Return valid MAC address in any situation (#515)
This is an automated email from the ASF dual-hosted git repository.
aaronai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-clients.git
The following commit(s) were added to refs/heads/master by this push:
new 25cbf8e2 Return valid MAC address in any situation (#515)
25cbf8e2 is described below
commit 25cbf8e20dc1e93b274fc42f156711431ad436dd
Author: Aaron Ai <ya...@alibaba-inc.com>
AuthorDate: Thu May 11 16:46:13 2023 +0800
Return valid MAC address in any situation (#515)
---
csharp/rocketmq-client-csharp/Utilities.cs | 16 +++++++++++++---
csharp/tests/UtilitiesTest.cs | 7 +++++++
2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/csharp/rocketmq-client-csharp/Utilities.cs b/csharp/rocketmq-client-csharp/Utilities.cs
index ca334b9b..6d74777d 100644
--- a/csharp/rocketmq-client-csharp/Utilities.cs
+++ b/csharp/rocketmq-client-csharp/Utilities.cs
@@ -32,6 +32,9 @@ namespace Org.Apache.Rocketmq
private static long _instanceSequence = 0;
private static readonly int ProcessId = Process.GetCurrentProcess().Id;
private static readonly string HostName = System.Net.Dns.GetHostName();
+ private static readonly byte[] RandomMacAddressBytes =
+ Enumerable.Range(0, 6).Select(_ => (byte)new Random().Next(256)).ToArray();
+
public const int MasterBrokerId = 0;
public static int GetPositiveMod(int k, int n)
@@ -42,9 +45,16 @@ namespace Org.Apache.Rocketmq
public static byte[] GetMacAddress()
{
- return NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(nic =>
- nic.OperationalStatus == OperationalStatus.Up &&
- nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)?.GetPhysicalAddress().GetAddressBytes();
+ var nic = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
+ x => x.OperationalStatus == OperationalStatus.Up &&
+ x.NetworkInterfaceType != NetworkInterfaceType.Loopback) ??
+ NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
+ x => x.OperationalStatus == OperationalStatus.Unknown &&
+ x.NetworkInterfaceType != NetworkInterfaceType.Loopback) ??
+ NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
+ x => x.NetworkInterfaceType != NetworkInterfaceType.Loopback);
+
+ return nic != null ? nic.GetPhysicalAddress().GetAddressBytes() : RandomMacAddressBytes;
}
public static int GetProcessId()
diff --git a/csharp/tests/UtilitiesTest.cs b/csharp/tests/UtilitiesTest.cs
index 5c82fcaa..ed45bcef 100644
--- a/csharp/tests/UtilitiesTest.cs
+++ b/csharp/tests/UtilitiesTest.cs
@@ -46,5 +46,12 @@ namespace tests
var bytes = Encoding.UTF8.GetBytes("foobar");
Assert.AreEqual(Utilities.ComputeSha1Hash(bytes), "8843D7F92416211DE9EBB963FF4CE28125932878");
}
+
+ [TestMethod]
+ public void TestGetMacAddress()
+ {
+ var macAddress = Utilities.GetMacAddress();
+ Assert.IsNotNull(macAddress);
+ }
}
}
\ No newline at end of file