Skip to content

Commit

Permalink
Support AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy de Sérésin committed Jan 21, 2024
1 parent a67bd6c commit 0367b5c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
69 changes: 53 additions & 16 deletions src/Confluent.Kafka/Impl/LibRdKafka.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#if NET462
using System.ComponentModel;
#endif
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif


namespace Confluent.Kafka.Impl
Expand Down Expand Up @@ -171,7 +174,11 @@ public static string LastError
}
}

static bool SetDelegates(Type nativeMethodsClass)
static bool SetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodsClass)
{
var methods = nativeMethodsClass.GetRuntimeMethods().ToArray();

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

#endif

private static bool TrySetDelegates(List<Type> nativeMethodCandidateTypes)
private static bool TrySetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType)
{
foreach (var t in nativeMethodCandidateTypes)
if (SetDelegates(nativeMethodCandidateType))
{
if (SetDelegates(t))
{
return true;
}
return true;
}

throw new DllNotFoundException("Failed to load the librdkafka native library.");
}

private static bool TrySetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType1,
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType2,
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType3)
{
if (SetDelegates(nativeMethodCandidateType1))
{
return true;
}
if (SetDelegates(nativeMethodCandidateType2))
{
return true;
}
if (SetDelegates(nativeMethodCandidateType3))
{
return true;
}

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

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}

private static void LoadOSXDelegates(string userSpecifiedPath)
Expand All @@ -700,7 +738,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
}
}

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}

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

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}
else
{
Expand All @@ -721,16 +759,15 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
var osName = PlatformApis.GetOSName();
if (osName.Equals("alpine", StringComparison.OrdinalIgnoreCase))
{
delegates.Add(typeof(NativeMethods.NativeMethods_Alpine));
TrySetDelegates(typeof(NativeMethods.NativeMethods_Alpine));
}
else
{
delegates.Add(typeof(NativeMethods.NativeMethods_Centos7));
delegates.Add(typeof(NativeMethods.NativeMethods));
delegates.Add(typeof(NativeMethods.NativeMethods_Centos6));
TrySetDelegates(
typeof(NativeMethods.NativeMethods_Centos7),
typeof(NativeMethods.NativeMethods),
typeof(NativeMethods.NativeMethods_Centos6));
}

TrySetDelegates(delegates);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Confluent.Kafka/Impl/SafeKafkaHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1810,7 +1810,7 @@ internal void DeleteConsumerGroupOffsets(String group, IEnumerable<TopicPartitio
setOption_OperationTimeout(optionsPtr, options.OperationTimeout);
setOption_completionSource(optionsPtr, completionSourcePtr);

if (partitions.Where(tp => tp.Topic == null || tp.Partition == null).Count() > 0)
if (partitions.Where(tp => tp.Topic == null).Count() > 0)
{
throw new ArgumentException("Cannot delete offsets because one or more topics or partitions were specified as null.");
}
Expand Down
9 changes: 8 additions & 1 deletion src/Confluent.Kafka/Internal/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
using SystemMarshal = System.Runtime.InteropServices.Marshal;
using SystemGCHandle = System.Runtime.InteropServices.GCHandle;
using SystemGCHandleType = System.Runtime.InteropServices.GCHandleType;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif


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

public static T PtrToStructure<T>(IntPtr ptr)
public static T PtrToStructure<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
T>(IntPtr ptr)
{
return SystemMarshal.PtrToStructure<T>(ptr);
}
Expand Down

0 comments on commit 0367b5c

Please sign in to comment.