Skip to content

Commit 0367b5c

Browse files
author
Rémy de Sérésin
committed
Support AOT
1 parent a67bd6c commit 0367b5c

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

src/Confluent.Kafka/Impl/LibRdKafka.cs

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#if NET462
3030
using System.ComponentModel;
3131
#endif
32+
#if NET5_0_OR_GREATER
33+
using System.Diagnostics.CodeAnalysis;
34+
#endif
3235

3336

3437
namespace Confluent.Kafka.Impl
@@ -171,7 +174,11 @@ public static string LastError
171174
}
172175
}
173176

174-
static bool SetDelegates(Type nativeMethodsClass)
177+
static bool SetDelegates(
178+
#if NET5_0_OR_GREATER
179+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
180+
#endif
181+
Type nativeMethodsClass)
175182
{
176183
var methods = nativeMethodsClass.GetRuntimeMethods().ToArray();
177184

@@ -662,14 +669,45 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
662669

663670
#endif
664671

665-
private static bool TrySetDelegates(List<Type> nativeMethodCandidateTypes)
672+
private static bool TrySetDelegates(
673+
#if NET5_0_OR_GREATER
674+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
675+
#endif
676+
Type nativeMethodCandidateType)
666677
{
667-
foreach (var t in nativeMethodCandidateTypes)
678+
if (SetDelegates(nativeMethodCandidateType))
668679
{
669-
if (SetDelegates(t))
670-
{
671-
return true;
672-
}
680+
return true;
681+
}
682+
683+
throw new DllNotFoundException("Failed to load the librdkafka native library.");
684+
}
685+
686+
private static bool TrySetDelegates(
687+
#if NET5_0_OR_GREATER
688+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
689+
#endif
690+
Type nativeMethodCandidateType1,
691+
#if NET5_0_OR_GREATER
692+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
693+
#endif
694+
Type nativeMethodCandidateType2,
695+
#if NET5_0_OR_GREATER
696+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
697+
#endif
698+
Type nativeMethodCandidateType3)
699+
{
700+
if (SetDelegates(nativeMethodCandidateType1))
701+
{
702+
return true;
703+
}
704+
if (SetDelegates(nativeMethodCandidateType2))
705+
{
706+
return true;
707+
}
708+
if (SetDelegates(nativeMethodCandidateType3))
709+
{
710+
return true;
673711
}
674712

675713
throw new DllNotFoundException("Failed to load the librdkafka native library.");
@@ -687,7 +725,7 @@ private static void LoadNetStandardDelegates(string userSpecifiedPath)
687725
}
688726
}
689727

690-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
728+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
691729
}
692730

693731
private static void LoadOSXDelegates(string userSpecifiedPath)
@@ -700,7 +738,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
700738
}
701739
}
702740

703-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
741+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
704742
}
705743

706744
private static void LoadLinuxDelegates(string userSpecifiedPath)
@@ -712,7 +750,7 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
712750
throw new InvalidOperationException($"Failed to load librdkafka at location '{userSpecifiedPath}'. dlerror: '{PosixNative.LastError}'.");
713751
}
714752

715-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
753+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
716754
}
717755
else
718756
{
@@ -721,16 +759,15 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
721759
var osName = PlatformApis.GetOSName();
722760
if (osName.Equals("alpine", StringComparison.OrdinalIgnoreCase))
723761
{
724-
delegates.Add(typeof(NativeMethods.NativeMethods_Alpine));
762+
TrySetDelegates(typeof(NativeMethods.NativeMethods_Alpine));
725763
}
726764
else
727765
{
728-
delegates.Add(typeof(NativeMethods.NativeMethods_Centos7));
729-
delegates.Add(typeof(NativeMethods.NativeMethods));
730-
delegates.Add(typeof(NativeMethods.NativeMethods_Centos6));
766+
TrySetDelegates(
767+
typeof(NativeMethods.NativeMethods_Centos7),
768+
typeof(NativeMethods.NativeMethods),
769+
typeof(NativeMethods.NativeMethods_Centos6));
731770
}
732-
733-
TrySetDelegates(delegates);
734771
}
735772
}
736773

src/Confluent.Kafka/Impl/SafeKafkaHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ internal void DeleteConsumerGroupOffsets(String group, IEnumerable<TopicPartitio
18101810
setOption_OperationTimeout(optionsPtr, options.OperationTimeout);
18111811
setOption_completionSource(optionsPtr, completionSourcePtr);
18121812

1813-
if (partitions.Where(tp => tp.Topic == null || tp.Partition == null).Count() > 0)
1813+
if (partitions.Where(tp => tp.Topic == null).Count() > 0)
18141814
{
18151815
throw new ArgumentException("Cannot delete offsets because one or more topics or partitions were specified as null.");
18161816
}

src/Confluent.Kafka/Internal/Util.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using SystemMarshal = System.Runtime.InteropServices.Marshal;
2020
using SystemGCHandle = System.Runtime.InteropServices.GCHandle;
2121
using SystemGCHandleType = System.Runtime.InteropServices.GCHandleType;
22+
#if NET5_0_OR_GREATER
23+
using System.Diagnostics.CodeAnalysis;
24+
#endif
2225

2326

2427
namespace Confluent.Kafka.Internal
@@ -79,7 +82,11 @@ public unsafe static string PtrToStringUTF8(IntPtr strPtr, UIntPtr strLength)
7982
return Encoding.UTF8.GetString((byte*)strPtr.ToPointer(), (int)strLength);
8083
}
8184

82-
public static T PtrToStructure<T>(IntPtr ptr)
85+
public static T PtrToStructure<
86+
#if NET5_0_OR_GREATER
87+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
88+
#endif
89+
T>(IntPtr ptr)
8390
{
8491
return SystemMarshal.PtrToStructure<T>(ptr);
8592
}

0 commit comments

Comments
 (0)