Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/Kestrel.LibraryLoader/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Kestrel.LibraryLoader
{
public abstract class Library
{
public Library()
{
IsWindows = PlatformApis.IsWindows();
if (!IsWindows)
{
IsDarwin = PlatformApis.IsDarwin();
}
}

public bool IsWindows;
public bool IsDarwin;

public Func<string, IntPtr> LoadLibrary;
public Func<IntPtr, bool> FreeLibrary;
public Func<IntPtr, string, IntPtr> GetProcAddress;

public virtual void Load(string dllToLoad)
{
PlatformApis.Apply(this);

var module = LoadLibrary(dllToLoad);
if (module == IntPtr.Zero)
{
throw new InvalidOperationException("Unable to load library.");
}

foreach (var field in GetType().GetTypeInfo().DeclaredFields)
{
var procAddress = GetProcAddress(module, field.Name.TrimStart('_'));
if (procAddress == IntPtr.Zero)
{
continue;
}
var value = Marshal.GetDelegateForFunctionPointer(procAddress, field.FieldType);
field.SetValue(this, value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.AspNet.Server.Kestrel.Networking
namespace Kestrel.LibraryLoader
{
public static class PlatformApis
{
Expand Down Expand Up @@ -48,15 +46,15 @@ public static bool IsDarwin()
return string.Equals(GetUname(), "Darwin", StringComparison.Ordinal);
}

public static void Apply(Libuv libuv)
public static void Apply(Library library)
{
if (libuv.IsWindows)
if (library.IsWindows)
{
WindowsApis.Apply(libuv);
WindowsApis.Apply(library);
}
else
{
LinuxApis.Apply(libuv);
LinuxApis.Apply(library);
}
}

Expand All @@ -71,11 +69,11 @@ public static class WindowsApis
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);

public static void Apply(Libuv libuv)
public static void Apply(Library library)
{
libuv.LoadLibrary = LoadLibrary;
libuv.FreeLibrary = FreeLibrary;
libuv.GetProcAddress = GetProcAddress;
library.LoadLibrary = LoadLibrary;
library.FreeLibrary = FreeLibrary;
library.GetProcAddress = GetProcAddress;
}
}

Expand Down Expand Up @@ -111,11 +109,11 @@ public static IntPtr GetProcAddress(IntPtr hModule, string procedureName)
return errPtr == IntPtr.Zero ? res : IntPtr.Zero;
}

public static void Apply(Libuv libuv)
public static void Apply(Library library)
{
libuv.LoadLibrary = LoadLibrary;
libuv.FreeLibrary = FreeLibrary;
libuv.GetProcAddress = GetProcAddress;
library.LoadLibrary = LoadLibrary;
library.FreeLibrary = FreeLibrary;
library.GetProcAddress = GetProcAddress;
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Kestrel.LibraryLoader/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "1.0.0-*",
"description": "Load libraries between Windows and Unix with delight.",
"compilationOptions": {
"allowUnsafe": true
}
}
21 changes: 3 additions & 18 deletions src/Microsoft.AspNet.Server.Kestrel/Networking/Libuv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,13 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Kestrel.LibraryLoader;

namespace Microsoft.AspNet.Server.Kestrel.Networking
{
public class Libuv
public class Libuv : Library
{
public Libuv()
{
IsWindows = PlatformApis.IsWindows();
if (!IsWindows)
{
IsDarwin = PlatformApis.IsDarwin();
}
}

public bool IsWindows;
public bool IsDarwin;

public Func<string, IntPtr> LoadLibrary;
public Func<IntPtr, bool> FreeLibrary;
public Func<IntPtr, string, IntPtr> GetProcAddress;

public void Load(string dllToLoad)
public override void Load(string dllToLoad)
{
PlatformApis.Apply(this);

Expand Down
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Server.Kestrel/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"version": "1.0.0-*",
"description": "ASP.NET 5 cross platform development web server.",
"dependencies": {
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*",
"Kestrel.LibraryLoader": "1.0.0-*"
},
"frameworks": {
"dnx451": { },
Expand Down