Skip to content

Commit

Permalink
Merge pull request #265 from IOTA-NET/261-bug-net-framework-needs-dll…
Browse files Browse the repository at this point in the history
…-in-output-dir

reworked pinvokes
  • Loading branch information
wireless90 authored Jan 10, 2024
2 parents c31835c + a1a3b98 commit ea82136
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 204 deletions.
57 changes: 9 additions & 48 deletions IotaSDK.NET/Common/Rust/RustBridgeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntPtr?> CreateClientAsync(string options)
{
Expand All @@ -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
Expand All @@ -59,15 +36,7 @@ private static class LinuxNativeMethods

public async Task<bool?> 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<string?> CallClientMethodAsync(IntPtr clientPtr, string method)
Expand All @@ -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
Expand Down
56 changes: 11 additions & 45 deletions IotaSDK.NET/Common/Rust/RustBridgeCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down
52 changes: 9 additions & 43 deletions IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IntPtr?> CreateSecretManagerAsync(string options)
{
Expand All @@ -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
Expand All @@ -58,12 +36,7 @@ private static class LinuxNativeMethods

public async Task<bool?> 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<string?> CallSecretManagerMethodAsync(IntPtr secretManagerPtr, string method)
Expand All @@ -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
Expand Down
Loading

0 comments on commit ea82136

Please sign in to comment.