From 0a23beaf50008a66951498b3d92ac9676a408f86 Mon Sep 17 00:00:00 2001 From: voltaney Date: Sun, 6 Apr 2025 04:09:54 +0900 Subject: [PATCH 1/2] Rename and enhance NetworkHelper methods - Changed method name from `GetAllLocalIPv4` to `GetAllLocalIPv4ByNIC` in `NetworkHelper.cs` while keeping the default parameter. - Added a new method `GetAllLocalIPv4` to retrieve local IPv4 addresses of the current machine. - Updated `MainViewModel.cs` to convert the result of `GetAllLocalIPv4` to a list for improved IP address handling. --- TouchSenderReceiver/Helpers/NetworkHelper.cs | 22 +++++++++++++++++-- .../ViewModels/MainViewModel.cs | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/TouchSenderReceiver/Helpers/NetworkHelper.cs b/TouchSenderReceiver/Helpers/NetworkHelper.cs index 62acc49..ba19f9d 100644 --- a/TouchSenderReceiver/Helpers/NetworkHelper.cs +++ b/TouchSenderReceiver/Helpers/NetworkHelper.cs @@ -1,4 +1,5 @@ -using System.Net.NetworkInformation; +using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; namespace TouchSenderReceiver.Helpers; @@ -11,7 +12,7 @@ public class NetworkHelper /// /// /// - public static IList GetAllLocalIPv4(NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Ethernet) + public static IList GetAllLocalIPv4ByNIC(NetworkInterfaceType networkInterfaceType = NetworkInterfaceType.Ethernet) { List ipAddrList = []; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) @@ -29,4 +30,21 @@ public static IList GetAllLocalIPv4(NetworkInterfaceType networkInterfac } return ipAddrList; } + + /// + /// Get all local IPv4 addresses of the current machine. + /// + public static IEnumerable GetAllLocalIPv4() + { + string hostname = Dns.GetHostName(); + + IPAddress[] adrList = Dns.GetHostAddresses(hostname); + foreach (IPAddress address in adrList) + { + if (address.AddressFamily == AddressFamily.InterNetwork) + { + yield return address.ToString(); + } + } + } } diff --git a/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs b/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs index 7677cf3..18325ed 100644 --- a/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs +++ b/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs @@ -112,7 +112,7 @@ public MainViewModel(ITouchReceiverSettingsService touchReceiverSettingsService, _serviceOptions = _touchReceiverSettingsService.ServiceOptions; _screenOptions = _touchReceiverSettingsService.ScreenOptions; - var candidateIpAddresses = NetworkHelper.GetAllLocalIPv4(); + var candidateIpAddresses = NetworkHelper.GetAllLocalIPv4().ToList(); IpAddresses = candidateIpAddresses.Count > 0 ? string.Join(" / ", NetworkHelper.GetAllLocalIPv4()) : "Unknown".GetLocalized(); } From 6c96066a83868424f3f3ca595cb7389875e9eda4 Mon Sep 17 00:00:00 2001 From: voltaney Date: Sun, 6 Apr 2025 04:14:55 +0900 Subject: [PATCH 2/2] Optimize IpAddresses assignment logic - Changed the assignment of `IpAddresses` to use the pre-fetched `candidateIpAddresses` list instead of calling `NetworkHelper.GetAllLocalIPv4()` twice. - Improved code efficiency by avoiding redundant method calls. --- TouchSenderTablet.GUI/ViewModels/MainViewModel.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs b/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs index 18325ed..7809c27 100644 --- a/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs +++ b/TouchSenderTablet.GUI/ViewModels/MainViewModel.cs @@ -113,7 +113,7 @@ public MainViewModel(ITouchReceiverSettingsService touchReceiverSettingsService, _screenOptions = _touchReceiverSettingsService.ScreenOptions; var candidateIpAddresses = NetworkHelper.GetAllLocalIPv4().ToList(); - IpAddresses = candidateIpAddresses.Count > 0 ? string.Join(" / ", NetworkHelper.GetAllLocalIPv4()) : "Unknown".GetLocalized(); + IpAddresses = candidateIpAddresses.Count > 0 ? string.Join(" / ", candidateIpAddresses) : "Unknown".GetLocalized(); } private void SetInitialCanvas()