Skip to content

Commit

Permalink
reworked pinvokes to detect os at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
wireless90 committed Jan 10, 2024
1 parent 8f06127 commit e3b38f6
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 98 deletions.
62 changes: 41 additions & 21 deletions IotaSDK.NET/Common/Rust/RustBridgeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ namespace IotaSDK.NET.Common.Rust
{
internal class RustBridgeClient
{
#if WINDOWS
private const string DllName = "iota_sdk.dll";
private static class WindowsNativeMethods
{
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr create_client(IntPtr optionsPtr);

#elif LINUX
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool destroy_client(IntPtr clientPtr);

private const string DllName = "libiota_sdk.so";

#endif
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr);
}

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr create_client(IntPtr optionsPtr);
private static class LinuxNativeMethods
{
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr create_client(IntPtr optionsPtr);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern bool destroy_client(IntPtr clientPtr);
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool destroy_client(IntPtr clientPtr);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr);
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr);
}

public async Task<IntPtr?> CreateClientAsync(string options)
{
Expand All @@ -33,11 +39,16 @@ internal class RustBridgeClient
try
{
optionsPtr = Marshal.StringToHGlobalAnsi(options);
IntPtr client = create_client(optionsPtr);
IntPtr client = IntPtr.Zero;

if (client == IntPtr.Zero)
return (IntPtr?)null;
return (IntPtr?)client;
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();

return client == IntPtr.Zero ? (IntPtr?)null : client;
}
finally
{
Expand All @@ -50,7 +61,12 @@ internal class RustBridgeClient
{
return await Task.Run(() =>
{
return destroy_client(clientPtr);
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();
});
}

Expand All @@ -63,12 +79,16 @@ internal class RustBridgeClient
try
{
methodPtr = Marshal.StringToHGlobalAnsi(method);
IntPtr clientResponse = call_client_method(clientPtr, methodPtr);
IntPtr clientResponse = IntPtr.Zero;

if (clientResponse == IntPtr.Zero)
return null;
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
return Marshal.PtrToStringAnsi(clientResponse);
throw new PlatformNotSupportedException();

return clientResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(clientResponse);
}
finally
{
Expand Down
58 changes: 39 additions & 19 deletions IotaSDK.NET/Common/Rust/RustBridgeCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@ namespace IotaSDK.NET.Common.Rust
{
public class RustBridgeCommon
{
#if WINDOWS
private const string DllName = "iota_sdk.dll";
private static class WindowsNativeMethods
{
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr binding_get_last_error();

#elif LINUX
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool init_logger(IntPtr configPtr);

private const string DllName = "libiota_sdk.so";

#endif
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr call_utils_method(IntPtr configPtr);
}

[DllImport(DllName, 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(DllName, 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 bool init_logger(IntPtr configPtr);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr call_utils_method(IntPtr configPtr);
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr call_utils_method(IntPtr configPtr);
}

public RustBridgeCommon()
{
Expand All @@ -33,7 +39,14 @@ public RustBridgeCommon()
{
return await Task.Run(() =>
{
IntPtr errorResponse = binding_get_last_error();
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();

try
{
return errorResponse == IntPtr.Zero
Expand All @@ -57,7 +70,9 @@ public RustBridgeCommon()
try
{
configPtr = Marshal.StringToHGlobalAnsi(config);
return init_logger(configPtr);
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? WindowsNativeMethods.init_logger(configPtr)
: LinuxNativeMethods.init_logger(configPtr);
}
finally
{
Expand All @@ -75,12 +90,17 @@ public RustBridgeCommon()
try
{
configPtr = Marshal.StringToHGlobalAnsi(config);
IntPtr utilsResponse = call_utils_method(configPtr);

if (utilsResponse == IntPtr.Zero)
return null;
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
return Marshal.PtrToStringAnsi(utilsResponse);
throw new PlatformNotSupportedException();

return utilsResponse == IntPtr.Zero
? null
: Marshal.PtrToStringAnsi(utilsResponse);
}
finally
{
Expand Down
60 changes: 37 additions & 23 deletions IotaSDK.NET/Common/Rust/RustBridgeSecretManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ namespace IotaSDK.NET.Common.Rust
{
internal class RustBridgeSecretManager
{
#if WINDOWS
private const string DllName = "iota_sdk.dll";

#elif LINUX
private static class WindowsNativeMethods
{
[DllImport("iota_sdk.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr create_secret_manager(IntPtr optionsPtr);

private const string DllName = "libiota_sdk.so";

#endif
[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);
}

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr create_secret_manager(IntPtr optionsPtr);
private static class LinuxNativeMethods
{
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr create_secret_manager(IntPtr optionsPtr);

[DllImport(DllName, 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 bool destroy_secret_manager(IntPtr secretManagerPtr);

[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern IntPtr call_secret_manager_method(IntPtr secretManagerPtr, IntPtr methodPtr);
[DllImport("libiota_sdk.so", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr call_secret_manager_method(IntPtr secretManagerPtr, IntPtr methodPtr);
}

public async Task<IntPtr?> CreateSecretManagerAsync(string options)
{
Expand All @@ -34,12 +39,15 @@ internal class RustBridgeSecretManager
try
{
optionsPtr = Marshal.StringToHGlobalAnsi(options);
IntPtr secretManagerResponse = create_secret_manager(optionsPtr);

if (secretManagerResponse == IntPtr.Zero)
return (IntPtr?)null;
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
return (IntPtr?)secretManagerResponse;
throw new PlatformNotSupportedException();

return secretManagerResponse == IntPtr.Zero ? (IntPtr?)null : (IntPtr?)secretManagerResponse;
}
finally
{
Expand All @@ -52,7 +60,9 @@ internal class RustBridgeSecretManager
{
return await Task.Run(() =>
{
return destroy_secret_manager(secretManagerPtr);
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? WindowsNativeMethods.destroy_secret_manager(secretManagerPtr)
: LinuxNativeMethods.destroy_secret_manager(secretManagerPtr);
});
}

Expand All @@ -65,11 +75,15 @@ internal class RustBridgeSecretManager
try
{
methodPtr = Marshal.StringToHGlobalAnsi(method);
IntPtr methodResponse = call_secret_manager_method(secretManagerPtr, methodPtr);
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();

if (methodResponse == IntPtr.Zero)
return null;
return Marshal.PtrToStringAnsi(methodResponse);
return methodResponse == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(methodResponse);
}
finally
{
Expand Down
Loading

0 comments on commit e3b38f6

Please sign in to comment.