Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of funciton pointer #89

Merged
merged 3 commits into from
Aug 6, 2024
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
7 changes: 0 additions & 7 deletions native/yaha_native/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,5 @@ fn main() -> Result<(), Box<dyn Error>> {
.generate_csharp_file("../../src/YetAnotherHttpHandler/NativeMethods.Uwp.g.cs")
.unwrap();

csbindgen::Builder::default()
.input_extern_file("src/binding.rs")
.csharp_namespace("Cysharp.Net.Http")
.csharp_dll_name("Cysharp.Net.Http.YetAnotherHttpHandler.Native")
.csharp_class_name("NativeMethodsFuncPtr")
.generate_csharp_file("../../src/YetAnotherHttpHandler/NativeMethodsFuncPtr.g.cs")
.unwrap();
Ok(())
}
25 changes: 7 additions & 18 deletions src/YetAnotherHttpHandler/NativeHttpHandlerCore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#if NET5_0_OR_GREATER
#define USE_FUNCTION_POINTER
#endif

using System;
using System.Buffers;
using System.Collections.Concurrent;
Expand Down Expand Up @@ -31,17 +27,19 @@ internal class NativeHttpHandlerCore : IDisposable
private readonly YahaContextSafeHandle _handle;
private bool _disposed = false;

// NOTE: We need to keep the callback delegates in advance.
// The delegates are kept on the Rust side, so it will crash if they are garbage collected.
private static readonly unsafe NativeMethods.yaha_init_context_on_status_code_and_headers_receive_delegate OnStatusCodeAndHeaderReceiveCallback = OnStatusCodeAndHeaderReceive;
private static readonly unsafe NativeMethods.yaha_init_context_on_receive_delegate OnReceiveCallback = OnReceive;
private static readonly unsafe NativeMethods.yaha_init_context_on_complete_delegate OnCompleteCallback = OnComplete;

public unsafe NativeHttpHandlerCore(NativeClientSettings settings)
{
var runtimeHandle = NativeRuntime.Instance.Acquire(); // NOTE: We need to call Release on finalizer.
var instanceId = Interlocked.Increment(ref _instanceId);

if (YahaEventSource.Log.IsEnabled()) YahaEventSource.Log.Info($"yaha_init_context");
#if USE_FUNCTION_POINTER
var ctx = NativeMethodsFuncPtr.yaha_init_context(runtimeHandle.DangerousGet(), &OnStatusCodeAndHeaderReceive, &OnReceive, &OnComplete);
#else
var ctx = NativeMethods.yaha_init_context(runtimeHandle.DangerousGet(), OnStatusCodeAndHeaderReceive, OnReceive, OnComplete);
#endif
var ctx = NativeMethods.yaha_init_context(runtimeHandle.DangerousGet(), OnStatusCodeAndHeaderReceiveCallback, OnReceiveCallback, OnCompleteCallback);
_handle = new YahaContextSafeHandle(ctx, instanceId);
_handle.SetParent(runtimeHandle);

Expand Down Expand Up @@ -330,9 +328,6 @@ private static unsafe void SetRequestHeaders(HttpHeaders headers, YahaNativeCont
}
}

#if USE_FUNCTION_POINTER
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
#endif
[MonoPInvokeCallback(typeof(NativeMethods.yaha_init_context_on_status_code_and_headers_receive_delegate))]
private static unsafe void OnStatusCodeAndHeaderReceive(int reqSeq, IntPtr state, int statusCode, YahaHttpVersion version)
{
Expand Down Expand Up @@ -385,9 +380,6 @@ private static unsafe void OnStatusCodeAndHeaderReceive(int reqSeq, IntPtr state
requestContext.Response.SetStatusCode(statusCode);
}

#if USE_FUNCTION_POINTER
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
#endif
[MonoPInvokeCallback(typeof(NativeMethods.yaha_init_context_on_receive_delegate))]
private static unsafe void OnReceive(int reqSeq, IntPtr state, UIntPtr length, byte* buf)
{
Expand All @@ -398,9 +390,6 @@ private static unsafe void OnReceive(int reqSeq, IntPtr state, UIntPtr length, b
requestContext.Response.Write(bufSpan);
}

#if USE_FUNCTION_POINTER
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
#endif
[MonoPInvokeCallback(typeof(NativeMethods.yaha_init_context_on_complete_delegate))]
private static unsafe void OnComplete(int reqSeq, IntPtr state, CompletionReason reason, uint h2ErrorCode)
{
Expand Down
155 changes: 0 additions & 155 deletions src/YetAnotherHttpHandler/NativeMethodsFuncPtr.g.cs

This file was deleted.

Loading