From a1a3b980e11d6c22feeee9854b4abde625c6cd6e Mon Sep 17 00:00:00 2001 From: wireless90 <12537739+wireless90@users.noreply.github.com> Date: Wed, 10 Jan 2024 21:30:47 +0800 Subject: [PATCH] reworked pinvokes --- IotaSDK.NET/Common/Rust/RustBridgeClient.cs | 57 ++---------- IotaSDK.NET/Common/Rust/RustBridgeCommon.cs | 56 +++--------- .../Common/Rust/RustBridgeSecretManager.cs | 52 ++--------- IotaSDK.NET/Common/Rust/RustBridgeWallet.cs | 86 ++++--------------- 4 files changed, 47 insertions(+), 204 deletions(-) diff --git a/IotaSDK.NET/Common/Rust/RustBridgeClient.cs b/IotaSDK.NET/Common/Rust/RustBridgeClient.cs index 0df996b..093d30e 100644 --- a/IotaSDK.NET/Common/Rust/RustBridgeClient.cs +++ b/IotaSDK.NET/Common/Rust/RustBridgeClient.cs @@ -6,29 +6,14 @@ namespace IotaSDK.NET.Common.Rust { internal class RustBridgeClient { - private static class WindowsNativeMethods - { - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_client(IntPtr optionsPtr); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_client(IntPtr clientPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr create_client(IntPtr optionsPtr); - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern bool destroy_client(IntPtr clientPtr); - private static class LinuxNativeMethods - { - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_client(IntPtr optionsPtr); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_client(IntPtr clientPtr); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr); public async Task CreateClientAsync(string options) { @@ -39,15 +24,7 @@ private static class LinuxNativeMethods try { optionsPtr = Marshal.StringToHGlobalAnsi(options); - IntPtr client = IntPtr.Zero; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - client = WindowsNativeMethods.create_client(optionsPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - client = LinuxNativeMethods.create_client(optionsPtr); - else - throw new PlatformNotSupportedException(); - + IntPtr client = create_client(optionsPtr); return client == IntPtr.Zero ? (IntPtr?)null : client; } finally @@ -59,15 +36,7 @@ private static class LinuxNativeMethods public async Task DestroyClientAsync(IntPtr clientPtr) { - return await Task.Run(() => - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - return WindowsNativeMethods.destroy_client(clientPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - return LinuxNativeMethods.destroy_client(clientPtr); - else - throw new PlatformNotSupportedException(); - }); + return await Task.Run(() => destroy_client(clientPtr)); } public async Task CallClientMethodAsync(IntPtr clientPtr, string method) @@ -79,15 +48,7 @@ private static class LinuxNativeMethods try { methodPtr = Marshal.StringToHGlobalAnsi(method); - IntPtr clientResponse = IntPtr.Zero; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - clientResponse = WindowsNativeMethods.call_client_method(clientPtr, methodPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - clientResponse = LinuxNativeMethods.call_client_method(clientPtr, methodPtr); - else - throw new PlatformNotSupportedException(); - + IntPtr clientResponse = call_client_method(clientPtr, methodPtr); return clientResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(clientResponse); } finally diff --git a/IotaSDK.NET/Common/Rust/RustBridgeCommon.cs b/IotaSDK.NET/Common/Rust/RustBridgeCommon.cs index 21ed8e6..aa2aa7f 100644 --- a/IotaSDK.NET/Common/Rust/RustBridgeCommon.cs +++ b/IotaSDK.NET/Common/Rust/RustBridgeCommon.cs @@ -6,29 +6,14 @@ namespace IotaSDK.NET.Common.Rust { public class RustBridgeCommon { - private static class WindowsNativeMethods - { - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr binding_get_last_error(); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool init_logger(IntPtr configPtr); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_utils_method(IntPtr configPtr); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr binding_get_last_error(); - private static class LinuxNativeMethods - { - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr binding_get_last_error(); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool init_logger(IntPtr configPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern bool init_logger(IntPtr configPtr); - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_utils_method(IntPtr configPtr); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr call_utils_method(IntPtr configPtr); public RustBridgeCommon() { @@ -39,19 +24,10 @@ public RustBridgeCommon() { return await Task.Run(() => { - IntPtr errorResponse; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - errorResponse = WindowsNativeMethods.binding_get_last_error(); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - errorResponse = LinuxNativeMethods.binding_get_last_error(); - else - throw new PlatformNotSupportedException(); - + IntPtr errorResponse = binding_get_last_error(); try { - return errorResponse == IntPtr.Zero - ? null - : Marshal.PtrToStringAnsi(errorResponse); + return errorResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(errorResponse); } finally { @@ -70,9 +46,7 @@ public RustBridgeCommon() try { configPtr = Marshal.StringToHGlobalAnsi(config); - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.init_logger(configPtr) - : LinuxNativeMethods.init_logger(configPtr); + return init_logger(configPtr); } finally { @@ -90,17 +64,9 @@ public RustBridgeCommon() try { configPtr = Marshal.StringToHGlobalAnsi(config); - IntPtr utilsResponse; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - utilsResponse = WindowsNativeMethods.call_utils_method(configPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - utilsResponse = LinuxNativeMethods.call_utils_method(configPtr); - else - throw new PlatformNotSupportedException(); + IntPtr utilsResponse = call_utils_method(configPtr); - return utilsResponse == IntPtr.Zero - ? null - : Marshal.PtrToStringAnsi(utilsResponse); + return utilsResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(utilsResponse); } finally { diff --git a/IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs b/IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs index 9ad7028..b14c415 100644 --- a/IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs +++ b/IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs @@ -6,29 +6,14 @@ namespace IotaSDK.NET.Common.Rust { internal class RustBridgeSecretManager { - private static class WindowsNativeMethods - { - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_secret_manager(IntPtr optionsPtr); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_secret_manager(IntPtr secretManagerPtr); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_secret_manager_method(IntPtr secretManagerPtr, IntPtr methodPtr); - } - - private static class LinuxNativeMethods - { - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_secret_manager(IntPtr optionsPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr create_secret_manager(IntPtr optionsPtr); - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_secret_manager(IntPtr secretManagerPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern bool destroy_secret_manager(IntPtr secretManagerPtr); - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_secret_manager_method(IntPtr secretManagerPtr, IntPtr methodPtr); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr call_secret_manager_method(IntPtr secretManagerPtr, IntPtr methodPtr); public async Task CreateSecretManagerAsync(string options) { @@ -39,14 +24,7 @@ private static class LinuxNativeMethods try { optionsPtr = Marshal.StringToHGlobalAnsi(options); - IntPtr secretManagerResponse; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - secretManagerResponse = WindowsNativeMethods.create_secret_manager(optionsPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - secretManagerResponse = LinuxNativeMethods.create_secret_manager(optionsPtr); - else - throw new PlatformNotSupportedException(); - + IntPtr secretManagerResponse = create_secret_manager(optionsPtr); return secretManagerResponse == IntPtr.Zero ? (IntPtr?)null : (IntPtr?)secretManagerResponse; } finally @@ -58,12 +36,7 @@ private static class LinuxNativeMethods public async Task DestroySecretManagerAsync(IntPtr secretManagerPtr) { - return await Task.Run(() => - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.destroy_secret_manager(secretManagerPtr) - : LinuxNativeMethods.destroy_secret_manager(secretManagerPtr); - }); + return await Task.Run(() => destroy_secret_manager(secretManagerPtr)); } public async Task CallSecretManagerMethodAsync(IntPtr secretManagerPtr, string method) @@ -75,14 +48,7 @@ private static class LinuxNativeMethods try { methodPtr = Marshal.StringToHGlobalAnsi(method); - IntPtr methodResponse; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - methodResponse = WindowsNativeMethods.call_secret_manager_method(secretManagerPtr, methodPtr); - else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - methodResponse = LinuxNativeMethods.call_secret_manager_method(secretManagerPtr, methodPtr); - else - throw new PlatformNotSupportedException(); - + IntPtr methodResponse = call_secret_manager_method(secretManagerPtr, methodPtr); return methodResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(methodResponse); } finally diff --git a/IotaSDK.NET/Common/Rust/RustBridgeWallet.cs b/IotaSDK.NET/Common/Rust/RustBridgeWallet.cs index 752c35f..467c40f 100644 --- a/IotaSDK.NET/Common/Rust/RustBridgeWallet.cs +++ b/IotaSDK.NET/Common/Rust/RustBridgeWallet.cs @@ -7,47 +7,23 @@ namespace IotaSDK.NET.Common.Rust { public class RustBridgeWallet { - private static class WindowsNativeMethods - { - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_wallet(IntPtr optionsPtr); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_wallet(IntPtr wallet); - - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_wallet_method(IntPtr walletPtr, IntPtr methodPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr create_wallet(IntPtr optionsPtr); - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr get_secret_manager_from_wallet(IntPtr walletPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern bool destroy_wallet(IntPtr wallet); - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr get_client_from_wallet(IntPtr walletPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr call_wallet_method(IntPtr walletPtr, IntPtr methodPtr); - [DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool listen_wallet(IntPtr walletPtr, IntPtr eventsPtr, WalletEventHandler handler); - } - - private static class LinuxNativeMethods - { - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr create_wallet(IntPtr optionsPtr); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr get_secret_manager_from_wallet(IntPtr walletPtr); - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool destroy_wallet(IntPtr wallet); + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern IntPtr get_client_from_wallet(IntPtr walletPtr); - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr call_wallet_method(IntPtr walletPtr, IntPtr methodPtr); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr get_secret_manager_from_wallet(IntPtr walletPtr); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern IntPtr get_client_from_wallet(IntPtr walletPtr); - - [DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] - public static extern bool listen_wallet(IntPtr walletPtr, IntPtr eventsPtr, WalletEventHandler handler); - } + [DllImport("iota_sdk", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + private static extern bool listen_wallet(IntPtr walletPtr, IntPtr eventsPtr, WalletEventHandler handler); public delegate void WalletEventHandler(IntPtr eventPtr); @@ -60,10 +36,7 @@ private static class LinuxNativeMethods try { optionsPtr = Marshal.StringToHGlobalAnsi(options); - IntPtr walletPtr = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.create_wallet(optionsPtr) - : LinuxNativeMethods.create_wallet(optionsPtr); - + IntPtr walletPtr = create_wallet(optionsPtr); return walletPtr == IntPtr.Zero ? (IntPtr?)null : (IntPtr?)walletPtr; } finally @@ -75,12 +48,7 @@ private static class LinuxNativeMethods public async Task DestroyWalletAsync(IntPtr wallet) { - return await Task.Run(() => - { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.destroy_wallet(wallet) - : LinuxNativeMethods.destroy_wallet(wallet); - }); + return await Task.Run(() => destroy_wallet(wallet)); } public async Task CallWalletMethodAsync(IntPtr walletPtr, string method) @@ -92,9 +60,7 @@ private static class LinuxNativeMethods try { methodPtr = Marshal.StringToHGlobalAnsi(method); - IntPtr walletResponse = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.call_wallet_method(walletPtr, methodPtr) - : LinuxNativeMethods.call_wallet_method(walletPtr, methodPtr); + IntPtr walletResponse = call_wallet_method(walletPtr, methodPtr); if (walletResponse == IntPtr.Zero) { @@ -116,26 +82,12 @@ private static class LinuxNativeMethods public async Task GetSecretManagerFromWalletAsync(IntPtr walletPtr) { - return await Task.Run(() => - { - IntPtr secretManager = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.get_secret_manager_from_wallet(walletPtr) - : LinuxNativeMethods.get_secret_manager_from_wallet(walletPtr); - - return secretManager == IntPtr.Zero ? (IntPtr?)null : (IntPtr?)secretManager; - }); + return await Task.Run(() => get_secret_manager_from_wallet(walletPtr)); } public async Task GetClientFromWalletAsync(IntPtr walletPtr) { - return await Task.Run(() => - { - IntPtr client = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.get_client_from_wallet(walletPtr) - : LinuxNativeMethods.get_client_from_wallet(walletPtr); - - return client == IntPtr.Zero ? (IntPtr?)null : (IntPtr?)client; - }); + return await Task.Run(() => get_client_from_wallet(walletPtr)); } public async Task ListenWalletAsync(IntPtr walletPtr, string events, WalletEventHandler handler) @@ -147,9 +99,7 @@ private static class LinuxNativeMethods try { eventsPtr = Marshal.StringToHGlobalAnsi(events); - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? WindowsNativeMethods.listen_wallet(walletPtr, eventsPtr, handler) - : LinuxNativeMethods.listen_wallet(walletPtr, eventsPtr, handler); + return listen_wallet(walletPtr, eventsPtr, handler); } finally {