Skip to content

Commit

Permalink
Use the built-in NativeLibrary API to load native dependencies on sup…
Browse files Browse the repository at this point in the history
…ported platforms
  • Loading branch information
mgnsm committed Mar 20, 2023
1 parent d4ae641 commit 8bf8a13
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
12 changes: 11 additions & 1 deletion Source/Millistream.Streaming/Interop/NativeImplementation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Runtime.InteropServices;

#if NETCOREAPP3_0_OR_GREATER
using nativeLibrary = System.Runtime.InteropServices.NativeLibrary;
#endif
namespace Millistream.Streaming.Interop
{
unsafe internal sealed class NativeImplementation
Expand Down Expand Up @@ -52,21 +54,29 @@ unsafe internal sealed class NativeImplementation

internal NativeImplementation(string libraryPath)
{
#if !NETCOREAPP3_0_OR_GREATER
NativeLibrary nativeLibrary;
#endif
IntPtr lib;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
#if !NETCOREAPP3_0_OR_GREATER
nativeLibrary = new NativeUnixLibrary();
#endif
lib = nativeLibrary.Load(libraryPath ?? "libmdf.so.0");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
#if !NETCOREAPP3_0_OR_GREATER
nativeLibrary = new NativeWindowsLibrary();
#endif
lib = nativeLibrary.Load(libraryPath ?? "libmdf-0.dll");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
#if !NETCOREAPP3_0_OR_GREATER
nativeLibrary = new NativeUnixLibrary();
#endif
lib = nativeLibrary.Load(libraryPath ?? "libmdf.0.dylib");
}
else
Expand Down
15 changes: 0 additions & 15 deletions Source/Millistream.Streaming/Interop/NativeLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System;
using System.Runtime.InteropServices;

namespace Millistream.Streaming.Interop
{
internal abstract class NativeLibrary
{
private static NativeLibrary _nativeLibrary;

public IntPtr Load(string libraryPath)
{
if (string.IsNullOrEmpty(libraryPath))
Expand Down Expand Up @@ -36,18 +33,6 @@ public bool TryGetExport(IntPtr handle, string name, out IntPtr address)
return address != IntPtr.Zero;
}

public static NativeLibrary GetDefault()
{
if (_nativeLibrary != null)
return _nativeLibrary;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return _nativeLibrary = new NativeUnixLibrary();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return _nativeLibrary = new NativeWindowsLibrary();

throw new PlatformNotSupportedException();
}

protected abstract IntPtr DoLoad(string libraryPath);
protected abstract IntPtr DoGetExport(IntPtr handle, string name);
}
Expand Down
6 changes: 2 additions & 4 deletions Source/Millistream.Streaming/Interop/NativeUnixLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ internal sealed class NativeUnixLibrary : NativeLibrary
private const string DllName = "libdl";
private const int RTLD_NOW = 0x002;

#pragma warning disable IDE1006
[DllImport(DllName, ExactSpelling = true)]
public static extern IntPtr dlopen(string filename, int flags);
private static extern IntPtr dlopen(string filename, int flags);

[DllImport(DllName, ExactSpelling = true)]
public static extern IntPtr dlsym(IntPtr handle, string symbol);
#pragma warning restore IDE1006
private static extern IntPtr dlsym(IntPtr handle, string symbol);

protected override IntPtr DoGetExport(IntPtr handle, string name) =>
dlsym(handle, name);
Expand Down
8 changes: 7 additions & 1 deletion Source/Millistream.Streaming/Millistream.Streaming.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard1.4</TargetFrameworks>
<TargetFrameworks>net45;netcoreapp3.0;netstandard1.4</TargetFrameworks>
<Authors>Magnus Montin</Authors>
<Description>An unofficial .NET wrapper for Millistream's low-latency, high-throughput and high-availability C/C++ streaming API that can be used to subscribe to streaming real-time or delayed financial data.</Description>
<PackageProjectUrl>https://github.com/mgnsm/Millistream.NET</PackageProjectUrl>
Expand All @@ -25,6 +25,12 @@
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="4.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.0' ">
<Compile Remove="Interop/NativeLibrary.cs" />
<Compile Remove="Interop/NativeUnixLibrary.cs" />
<Compile Remove="Interop/NativeWindowsLibrary.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net45" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 8bf8a13

Please sign in to comment.