Skip to content
Merged
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
18 changes: 2 additions & 16 deletions host/src/FunctionsNetHost/Native/NativeExports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,10 @@ namespace FunctionsNetHost
public static class NativeExports
{
[UnmanagedCallersOnly(EntryPoint = "get_application_properties")]
public static int GetApplicationProperties(NativeHostData nativeHostData)
public static unsafe int GetApplicationProperties(NativeHostData* nativeHostData)
Comment thread
kshyju marked this conversation as resolved.
{
Logger.LogTrace("NativeExports.GetApplicationProperties method invoked.");

try
{
var nativeHostApplication = NativeHostApplication.Instance;
GCHandle gch = GCHandle.Alloc(nativeHostApplication, GCHandleType.Pinned);
IntPtr pNativeApplication = gch.AddrOfPinnedObject();
nativeHostData.PNativeApplication = pNativeApplication;

return 1;
}
catch (Exception ex)
{
Logger.Log($"Error in NativeExports.GetApplicationProperties: {ex}");
return 0;
}
return 1;
}

[UnmanagedCallersOnly(EntryPoint = "register_callbacks")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<id>Microsoft.Azure.Functions.DotNetIsolatedNativeHost</id>
<title>Microsoft Azure Functions dotnet-isolated native host</title>
<tags>dotnet-isolated azure-functions azure</tags>
<version>1.0.2</version>
<version>1.0.3</version>
<authors>Microsoft</authors>
<owners>Microsoft</owners>
<projectUrl>https://github.com/Azure/azure-functions-dotnet-worker</projectUrl>
Expand Down
14 changes: 0 additions & 14 deletions src/DotNetWorker.Grpc/NativeHostIntegration/NativeHost.cs

This file was deleted.

33 changes: 11 additions & 22 deletions src/DotNetWorker.Grpc/NativeHostIntegration/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,27 @@ static NativeMethods()
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, ImportResolver);
}

public static NativeHost GetNativeHostData()
{
var result = get_application_properties(out var hostData);
if (result == 1)
{
return hostData;
}

throw new InvalidOperationException($"Invalid result returned from get_application_properties: {result}");
}

public static void RegisterCallbacks(NativeSafeHandle nativeApplication,
public static void RegisterCallbacks(
delegate* unmanaged<byte**, int, IntPtr, IntPtr> requestCallback,
IntPtr grpcHandler)
{
_ = register_callbacks(nativeApplication, requestCallback, grpcHandler);
_ = register_callbacks(IntPtr.Zero, requestCallback, grpcHandler);
}

public static void SendStreamingMessage(NativeSafeHandle nativeApplication, StreamingMessage streamingMessage)
public static void SendStreamingMessage(StreamingMessage streamingMessage)
{
byte[] bytes = streamingMessage.ToByteArray();
_ = send_streaming_message(nativeApplication, bytes, bytes.Length);
fixed (byte* ptr = bytes)
{
_ = send_streaming_message(IntPtr.Zero, ptr, bytes.Length);
}
}

[DllImport(NativeWorkerDll, CharSet = CharSet.Auto)]
private static extern int get_application_properties(out NativeHost hostData);

[DllImport(NativeWorkerDll, CharSet = CharSet.Auto)]
private static extern int send_streaming_message(NativeSafeHandle pInProcessApplication, byte[] streamingMessage, int streamingMessageSize);
[DllImport(NativeWorkerDll)]
private static extern int send_streaming_message(IntPtr pInProcessApplication, byte* streamingMessage, int streamingMessageSize);

[DllImport(NativeWorkerDll, CharSet = CharSet.Auto)]
private static extern unsafe int register_callbacks(NativeSafeHandle pInProcessApplication,
[DllImport(NativeWorkerDll)]
private static extern unsafe int register_callbacks(IntPtr pInProcessApplication,
delegate* unmanaged<byte**, int, IntPtr, IntPtr> requestCallback,
IntPtr grpcHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@ internal class NativeWorkerClient : IWorkerClient
private readonly IMessageProcessor _messageProcessor;
private readonly ChannelReader<StreamingMessage> _outputChannelReader;
private readonly ChannelWriter<StreamingMessage> _outputChannelWriter;
private readonly NativeSafeHandle _application;
private GCHandle _gcHandle;

private readonly Channel<StreamingMessage> _inbound = Channel.CreateUnbounded<StreamingMessage>();

public NativeWorkerClient(IMessageProcessor messageProcessor, GrpcHostChannel outputChannel, NativeHost nativeHostData)
public NativeWorkerClient(IMessageProcessor messageProcessor, GrpcHostChannel outputChannel)
{
_messageProcessor = messageProcessor;
_outputChannelReader = outputChannel.Channel.Reader;
_outputChannelWriter = outputChannel.Channel.Writer;
_application = new NativeSafeHandle(nativeHostData.pNativeApplication);
}

public Task StartAsync(CancellationToken cancellationToken)
Expand All @@ -37,7 +35,7 @@ public Task StartAsync(CancellationToken cancellationToken)
public unsafe void Start()
{
_gcHandle = GCHandle.Alloc(this);
NativeMethods.RegisterCallbacks(_application, &HandleRequest, (IntPtr)_gcHandle);
NativeMethods.RegisterCallbacks(&HandleRequest, (IntPtr)_gcHandle);

_ = ProcessInbound();
_ = ProcessOutbound();
Expand All @@ -55,7 +53,7 @@ private async Task ProcessOutbound()
{
await foreach (StreamingMessage msg in _outputChannelReader.ReadAllAsync())
{
NativeMethods.SendStreamingMessage(_application, msg);
NativeMethods.SendStreamingMessage(msg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public NativeWorkerClientFactory(GrpcHostChannel hostChannel)

public IWorkerClient CreateClient(IMessageProcessor messageProcessor)
{
var nativeHostData = NativeMethods.GetNativeHostData();
return new NativeWorkerClient(messageProcessor, _hostChannel, nativeHostData);
return new NativeWorkerClient(messageProcessor, _hostChannel);
}
}
}