Skip to content

Commit 462659f

Browse files
authored
Merge pull request #258 from IOTA-NET/257-feat-downgrade-to-net-standard-20-to-support-lower-net-framework-versions
Reworked entire pinvokes, Added Linux support
2 parents 9901d7b + 6ff85d6 commit 462659f

File tree

7 files changed

+339
-307
lines changed

7 files changed

+339
-307
lines changed

IotaSDK.NET.Main/Program.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using IotaSDK.NET.Main.Examples.Native_Tokens.SendNativeTokens;
1+
using IotaSDK.NET.Main.Examples.Accounts_and_Addresses.Check_Balance;
2+
using IotaSDK.NET.Main.Examples.Native_Tokens.SendNativeTokens;
23

34
namespace IotaSDK.NET.Main
45
{
@@ -7,7 +8,7 @@ internal class Program
78
static async Task Main(string[] args)
89
{
910
//await CreateWalletAndAccountExample.Run();
10-
//await CheckBalanceExample.Run();
11+
await CheckBalanceExample.Run();
1112
//await GenerateAddressExample.Run();
1213
//await RequestTokensFromFaucetExample.Run();
1314
//await SendBasecoinTransactionExample.Run();
@@ -16,7 +17,7 @@ static async Task Main(string[] args)
1617
//await BurnNftExample.Run();
1718
//await CreateFoundryExample.Run();
1819
//await SendNativeTokensExample.Run();
19-
await MeltNativeTokensExample.Run();
20+
//await MeltNativeTokensExample.Run();
2021
}
2122

2223
}

IotaSDK.NET/Common/Rust/PInvoke.cs

-78
This file was deleted.

IotaSDK.NET/Common/Rust/RustBridgeClient.cs

+56-34
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,75 @@ namespace IotaSDK.NET.Common.Rust
66
{
77
internal class RustBridgeClient
88
{
9-
private const string DllName = "iota_sdk.dll";
9+
#if WINDOWS
10+
private const string DllName = "iota_sdk.dll";
11+
12+
#elif LINUX
13+
14+
private const string DllName = "libiota_sdk.so";
15+
16+
#endif
17+
18+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
19+
private static extern IntPtr create_client(IntPtr optionsPtr);
20+
21+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
22+
private static extern bool destroy_client(IntPtr clientPtr);
23+
24+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
25+
private static extern IntPtr call_client_method(IntPtr clientPtr, IntPtr methodPtr);
1026

1127
public async Task<IntPtr?> CreateClientAsync(string options)
1228
{
13-
IntPtr optionsPtr = IntPtr.Zero;
14-
15-
try
29+
return await Task.Run(() =>
1630
{
17-
optionsPtr = Marshal.StringToHGlobalAnsi(options);
18-
object? client = await PInvoke.DynamicPInvokeBuilderAsync(typeof(IntPtr), DllName, "create_client", new object[] { optionsPtr }, new Type[] { typeof(IntPtr) });
19-
20-
if (client == null || (IntPtr)client == IntPtr.Zero)
21-
return null;
22-
return (IntPtr?)client;
23-
}
24-
finally
25-
{
26-
Marshal.FreeHGlobal(optionsPtr);
27-
}
31+
IntPtr optionsPtr = IntPtr.Zero;
32+
33+
try
34+
{
35+
optionsPtr = Marshal.StringToHGlobalAnsi(options);
36+
IntPtr client = create_client(optionsPtr);
37+
38+
if (client == IntPtr.Zero)
39+
return (IntPtr?)null;
40+
return (IntPtr?)client;
41+
}
42+
finally
43+
{
44+
Marshal.FreeHGlobal(optionsPtr);
45+
}
46+
});
2847
}
2948

3049
public async Task<bool?> DestroyClientAsync(IntPtr clientPtr)
3150
{
32-
object? isDestroyed = await PInvoke.DynamicPInvokeBuilderAsync(typeof(bool), DllName, "destroy_client", new object[] { clientPtr }, new Type[] { typeof(IntPtr) });
33-
34-
return (bool?)isDestroyed;
51+
return await Task.Run(() =>
52+
{
53+
return destroy_client(clientPtr);
54+
});
3555
}
3656

37-
3857
public async Task<string?> CallClientMethodAsync(IntPtr clientPtr, string method)
3958
{
40-
IntPtr methodPtr = IntPtr.Zero;
41-
42-
try
43-
{
44-
methodPtr = Marshal.StringToHGlobalAnsi(method);
45-
object? clientResponse = await PInvoke.DynamicPInvokeBuilderAsync(typeof(IntPtr), DllName, "call_client_method", new object[] { clientPtr, methodPtr }, new Type[] { typeof(IntPtr), typeof(IntPtr) });
46-
47-
if (clientResponse == null || (IntPtr)clientResponse == IntPtr.Zero)
48-
return null;
49-
else
50-
return Marshal.PtrToStringAnsi((IntPtr)clientResponse);
51-
}
52-
finally
59+
return await Task.Run(() =>
5360
{
54-
Marshal.FreeHGlobal(methodPtr);
55-
}
61+
IntPtr methodPtr = IntPtr.Zero;
62+
63+
try
64+
{
65+
methodPtr = Marshal.StringToHGlobalAnsi(method);
66+
IntPtr clientResponse = call_client_method(clientPtr, methodPtr);
67+
68+
if (clientResponse == IntPtr.Zero)
69+
return null;
70+
else
71+
return Marshal.PtrToStringAnsi(clientResponse);
72+
}
73+
finally
74+
{
75+
Marshal.FreeHGlobal(methodPtr);
76+
}
77+
});
5678
}
5779
}
5880
}

IotaSDK.NET/Common/Rust/RustBridgeCommon.cs

+63-41
Original file line numberDiff line numberDiff line change
@@ -6,65 +6,87 @@ namespace IotaSDK.NET.Common.Rust
66
{
77
public class RustBridgeCommon
88
{
9-
private const string DllName = "iota_sdk.dll";
9+
#if WINDOWS
10+
private const string DllName = "iota_sdk.dll";
11+
12+
#elif LINUX
13+
14+
private const string DllName = "libiota_sdk.so";
15+
16+
#endif
17+
18+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
19+
private static extern IntPtr binding_get_last_error();
20+
21+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
22+
private static extern bool init_logger(IntPtr configPtr);
23+
24+
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
25+
private static extern IntPtr call_utils_method(IntPtr configPtr);
1026

1127
public RustBridgeCommon()
1228
{
13-
29+
// Constructor code if necessary
1430
}
31+
1532
public async Task<string?> GetLastErrorAsync()
1633
{
17-
object? errorResponse = await PInvoke.DynamicPInvokeBuilderAsync(typeof(IntPtr), DllName, "binding_get_last_error", new object[] { }, new Type[] { });
18-
19-
try
20-
{
21-
return errorResponse == null || (IntPtr)errorResponse == IntPtr.Zero
22-
? null
23-
: Marshal.PtrToStringAnsi((IntPtr)errorResponse);
24-
}
25-
finally
34+
return await Task.Run(() =>
2635
{
27-
if (errorResponse != null)
28-
Marshal.FreeHGlobal((IntPtr)errorResponse!);
29-
}
36+
IntPtr errorResponse = binding_get_last_error();
37+
try
38+
{
39+
return errorResponse == IntPtr.Zero
40+
? null
41+
: Marshal.PtrToStringAnsi(errorResponse);
42+
}
43+
finally
44+
{
45+
if (errorResponse != IntPtr.Zero)
46+
Marshal.FreeHGlobal(errorResponse);
47+
}
48+
});
3049
}
3150

3251
public async Task<bool?> InitLoggerAsync(string config)
3352
{
34-
IntPtr configPtr = IntPtr.Zero;
35-
36-
try
37-
{
38-
configPtr = Marshal.StringToHGlobalAnsi(config);
39-
object? loggerInitResponse = await PInvoke.DynamicPInvokeBuilderAsync(typeof(bool), DllName, "init_logger", new object[] { configPtr }, new Type[] { typeof(IntPtr) });
40-
return (bool?)loggerInitResponse;
41-
}
42-
finally
53+
return await Task.Run(() =>
4354
{
44-
Marshal.FreeHGlobal(configPtr);
45-
}
55+
IntPtr configPtr = IntPtr.Zero;
56+
57+
try
58+
{
59+
configPtr = Marshal.StringToHGlobalAnsi(config);
60+
return init_logger(configPtr);
61+
}
62+
finally
63+
{
64+
Marshal.FreeHGlobal(configPtr);
65+
}
66+
});
4667
}
4768

4869
public async Task<string?> CallUtilsMethodAsync(string config)
4970
{
50-
IntPtr configPtr = IntPtr.Zero;
51-
52-
try
71+
return await Task.Run(() =>
5372
{
54-
configPtr = Marshal.StringToHGlobalAnsi(config);
55-
object? utilsResponse = await PInvoke.DynamicPInvokeBuilderAsync(typeof(IntPtr), DllName, "call_utils_method", new object[] { configPtr }, new Type[] { typeof(IntPtr) });
73+
IntPtr configPtr = IntPtr.Zero;
5674

57-
if (utilsResponse == null || (IntPtr)utilsResponse == IntPtr.Zero)
58-
return null;
59-
else
60-
return Marshal.PtrToStringAnsi((IntPtr)utilsResponse);
61-
}
62-
finally
63-
{
64-
Marshal.FreeHGlobal(configPtr);
65-
}
66-
}
75+
try
76+
{
77+
configPtr = Marshal.StringToHGlobalAnsi(config);
78+
IntPtr utilsResponse = call_utils_method(configPtr);
6779

80+
if (utilsResponse == IntPtr.Zero)
81+
return null;
82+
else
83+
return Marshal.PtrToStringAnsi(utilsResponse);
84+
}
85+
finally
86+
{
87+
Marshal.FreeHGlobal(configPtr);
88+
}
89+
});
90+
}
6891
}
69-
7092
}

0 commit comments

Comments
 (0)