From 1c7604cde2e21a6908b5be637796cb7faae303ec Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 29 May 2024 19:20:54 +0200 Subject: [PATCH] [Network] Make P/Invokes in NW*Descriptor and NWWebSocket* have blittable signatures. (#20659) Contributes towards #15684. --- src/Network/NWAdvertiseDescriptor.cs | 9 ++++----- src/Network/NWBrowserDescriptor.cs | 9 ++++----- src/Network/NWWebSocketOptions.cs | 8 ++++---- src/Network/NWWebSocketRequest.cs | 6 ++---- src/Network/NWWebSocketResponse.cs | 5 ++--- tests/cecil-tests/BlittablePInvokes.KnownFailures.cs | 9 --------- 6 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/Network/NWAdvertiseDescriptor.cs b/src/Network/NWAdvertiseDescriptor.cs index ab9ac917e92c..f02db88fbcad 100644 --- a/src/Network/NWAdvertiseDescriptor.cs +++ b/src/Network/NWAdvertiseDescriptor.cs @@ -140,15 +140,14 @@ public void SetTxtRecord (string txt) } [DllImport (Constants.NetworkLibrary)] - static extern void nw_advertise_descriptor_set_no_auto_rename (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool no_auto_rename); + static extern void nw_advertise_descriptor_set_no_auto_rename (IntPtr handle, byte no_auto_rename); [DllImport (Constants.NetworkLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - static extern bool nw_advertise_descriptor_get_no_auto_rename (IntPtr handle); + static extern byte nw_advertise_descriptor_get_no_auto_rename (IntPtr handle); public bool NoAutoRename { - set => nw_advertise_descriptor_set_no_auto_rename (GetCheckedHandle (), value); - get => nw_advertise_descriptor_get_no_auto_rename (GetCheckedHandle ()); + set => nw_advertise_descriptor_set_no_auto_rename (GetCheckedHandle (), value.AsByte ()); + get => nw_advertise_descriptor_get_no_auto_rename (GetCheckedHandle ()) != 0; } #if NET diff --git a/src/Network/NWBrowserDescriptor.cs b/src/Network/NWBrowserDescriptor.cs index 74add4a98e1e..d4fc58de40e7 100644 --- a/src/Network/NWBrowserDescriptor.cs +++ b/src/Network/NWBrowserDescriptor.cs @@ -121,15 +121,14 @@ public static NWBrowserDescriptor CreateBonjourService (string type, string? dom public static NWBrowserDescriptor CreateBonjourService (string type) => CreateBonjourService (type, null); [DllImport (Constants.NetworkLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - static extern bool nw_browse_descriptor_get_include_txt_record (OS_nw_browse_descriptor descriptor); + static extern byte nw_browse_descriptor_get_include_txt_record (OS_nw_browse_descriptor descriptor); [DllImport (Constants.NetworkLibrary)] - static extern void nw_browse_descriptor_set_include_txt_record (OS_nw_browse_descriptor descriptor, [MarshalAs (UnmanagedType.I1)] bool include_txt_record); + static extern void nw_browse_descriptor_set_include_txt_record (OS_nw_browse_descriptor descriptor, byte include_txt_record); public bool IncludeTxtRecord { - get => nw_browse_descriptor_get_include_txt_record (GetCheckedHandle ()); - set => nw_browse_descriptor_set_include_txt_record (GetCheckedHandle (), value); + get => nw_browse_descriptor_get_include_txt_record (GetCheckedHandle ()) != 0; + set => nw_browse_descriptor_set_include_txt_record (GetCheckedHandle (), value.AsByte ()); } [DllImport (Constants.NetworkLibrary, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] diff --git a/src/Network/NWWebSocketOptions.cs b/src/Network/NWWebSocketOptions.cs index c928e4e8a1db..3eedbf2de73d 100644 --- a/src/Network/NWWebSocketOptions.cs +++ b/src/Network/NWWebSocketOptions.cs @@ -72,13 +72,13 @@ public void AddSubprotocol (string subprotocol) } [DllImport (Constants.NetworkLibrary)] - static extern void nw_ws_options_set_auto_reply_ping (OS_nw_protocol_options options, [MarshalAs (UnmanagedType.I1)] bool auto_reply_ping); + static extern void nw_ws_options_set_auto_reply_ping (OS_nw_protocol_options options, byte auto_reply_ping); public bool AutoReplyPing { get { return autoReplyPing; } set { autoReplyPing = value; - nw_ws_options_set_auto_reply_ping (GetCheckedHandle (), value); + nw_ws_options_set_auto_reply_ping (GetCheckedHandle (), value.AsByte ()); } } @@ -94,13 +94,13 @@ public nuint MaximumMessageSize { } [DllImport (Constants.NetworkLibrary)] - static extern void nw_ws_options_set_skip_handshake (OS_nw_protocol_options options, [MarshalAs (UnmanagedType.I1)] bool skip_handshake); + static extern void nw_ws_options_set_skip_handshake (OS_nw_protocol_options options, byte skip_handshake); public bool SkipHandShake { get { return skipHandShake; } set { skipHandShake = value; - nw_ws_options_set_skip_handshake (GetCheckedHandle (), value); + nw_ws_options_set_skip_handshake (GetCheckedHandle (), value.AsByte ()); } } diff --git a/src/Network/NWWebSocketRequest.cs b/src/Network/NWWebSocketRequest.cs index 916c0547facd..22764169ab63 100644 --- a/src/Network/NWWebSocketRequest.cs +++ b/src/Network/NWWebSocketRequest.cs @@ -39,8 +39,7 @@ public class NWWebSocketRequest : NativeObject { internal NWWebSocketRequest (NativeHandle handle, bool owns) : base (handle, owns) { } [DllImport (Constants.NetworkLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - unsafe static extern bool nw_ws_request_enumerate_additional_headers (OS_nw_ws_request request, BlockLiteral* enumerator); + unsafe static extern byte nw_ws_request_enumerate_additional_headers (OS_nw_ws_request request, BlockLiteral* enumerator); #if !NET delegate void nw_ws_request_enumerate_additional_headers_t (IntPtr block, IntPtr header, IntPtr value); @@ -79,8 +78,7 @@ public void EnumerateAdditionalHeaders (Action handler) } [DllImport (Constants.NetworkLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - unsafe static extern bool nw_ws_request_enumerate_subprotocols (OS_nw_ws_request request, BlockLiteral* enumerator); + unsafe static extern byte nw_ws_request_enumerate_subprotocols (OS_nw_ws_request request, BlockLiteral* enumerator); #if !NET delegate void nw_ws_request_enumerate_subprotocols_t (IntPtr block, IntPtr subprotocol); diff --git a/src/Network/NWWebSocketResponse.cs b/src/Network/NWWebSocketResponse.cs index fb23d31be18d..82e40421679b 100644 --- a/src/Network/NWWebSocketResponse.cs +++ b/src/Network/NWWebSocketResponse.cs @@ -68,8 +68,7 @@ static string nw_ws_response_get_selected_subprotocol (OS_nw_ws_response respons public NWWebSocketResponseStatus Status => nw_ws_response_get_status (GetCheckedHandle ()); [DllImport (Constants.NetworkLibrary)] - [return: MarshalAs (UnmanagedType.I1)] - unsafe static extern bool nw_ws_response_enumerate_additional_headers (OS_nw_ws_response response, BlockLiteral* enumerator); + unsafe static extern byte nw_ws_response_enumerate_additional_headers (OS_nw_ws_response response, BlockLiteral* enumerator); #if !NET delegate void nw_ws_response_enumerate_additional_headers_t (IntPtr block, IntPtr header, IntPtr value); @@ -103,7 +102,7 @@ public bool EnumerateAdditionalHeaders (Action handler) using var block = new BlockLiteral (); block.SetupBlockUnsafe (static_EnumerateHeadersHandler, handler); #endif - return nw_ws_response_enumerate_additional_headers (GetCheckedHandle (), &block); + return nw_ws_response_enumerate_additional_headers (GetCheckedHandle (), &block) != 0; } } diff --git a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs index 254941eeaf3b..ddec2a5122f2 100644 --- a/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs +++ b/tests/cecil-tests/BlittablePInvokes.KnownFailures.cs @@ -27,8 +27,6 @@ public partial class BlittablePInvokes { "AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSend(System.IntPtr,System.IntPtr)", "AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper_stret(System.IntPtr,System.IntPtr)", "AVFoundation.AVSampleCursorSyncInfo ObjCRuntime.Messaging::AVSampleCursorSyncInfo_objc_msgSendSuper(System.IntPtr,System.IntPtr)", - "System.Boolean Network.NWAdvertiseDescriptor::nw_advertise_descriptor_get_no_auto_rename(System.IntPtr)", - "System.Boolean Network.NWBrowserDescriptor::nw_browse_descriptor_get_include_txt_record(System.IntPtr)", "System.Boolean Network.NWConnectionGroup::nw_connection_group_reinsert_extracted_connection(System.IntPtr,System.IntPtr)", "System.Boolean Network.NWContentContext::nw_content_context_get_is_final(System.IntPtr)", "System.Boolean Network.NWEstablishmentReport::nw_establishment_report_get_proxy_configured(System.IntPtr)", @@ -54,15 +52,10 @@ public partial class BlittablePInvokes { "System.Boolean Network.NWTxtRecord::nw_txt_record_access_key(System.IntPtr,System.IntPtr,ObjCRuntime.BlockLiteral*)", "System.Boolean Network.NWTxtRecord::nw_txt_record_apply(System.IntPtr,ObjCRuntime.BlockLiteral*)", "System.Boolean Network.NWTxtRecord::nw_txt_record_is_equal(System.IntPtr,System.IntPtr)", - "System.Boolean Network.NWWebSocketRequest::nw_ws_request_enumerate_additional_headers(System.IntPtr,ObjCRuntime.BlockLiteral*)", - "System.Boolean Network.NWWebSocketRequest::nw_ws_request_enumerate_subprotocols(System.IntPtr,ObjCRuntime.BlockLiteral*)", - "System.Boolean Network.NWWebSocketResponse::nw_ws_response_enumerate_additional_headers(System.IntPtr,ObjCRuntime.BlockLiteral*)", "System.Byte* Network.NWEndpoint::nw_endpoint_get_signature(System.IntPtr,System.UIntPtr&)", "System.Int32 AudioUnit.AUGraph::NewAUGraph(System.IntPtr&)", "System.IntPtr ObjCRuntime.Selector::GetHandle(System.String)", "System.IntPtr Security.SecKey::SecKeyCreateEncryptedData(System.IntPtr,System.IntPtr,System.IntPtr,System.IntPtr&)", - "System.Void Network.NWAdvertiseDescriptor::nw_advertise_descriptor_set_no_auto_rename(System.IntPtr,System.Boolean)", - "System.Void Network.NWBrowserDescriptor::nw_browse_descriptor_set_include_txt_record(System.IntPtr,System.Boolean)", "System.Void Network.NWConnection::nw_connection_send(System.IntPtr,System.IntPtr,System.IntPtr,System.Boolean,ObjCRuntime.BlockLiteral*)", "System.Void Network.NWConnectionGroup::nw_connection_group_set_receive_handler(System.IntPtr,System.UInt32,System.Boolean,ObjCRuntime.BlockLiteral*)", "System.Void Network.NWContentContext::nw_content_context_set_is_final(System.IntPtr,System.Boolean)", @@ -76,8 +69,6 @@ public partial class BlittablePInvokes { "System.Void Network.NWParameters::nw_parameters_set_requires_dnssec_validation(System.IntPtr,System.Boolean)", "System.Void Network.NWParameters::nw_parameters_set_reuse_local_address(System.IntPtr,System.Boolean)", "System.Void Network.NWPrivacyContext::nw_privacy_context_require_encrypted_name_resolution(System.IntPtr,System.Boolean,System.IntPtr)", - "System.Void Network.NWWebSocketOptions::nw_ws_options_set_auto_reply_ping(System.IntPtr,System.Boolean)", - "System.Void Network.NWWebSocketOptions::nw_ws_options_set_skip_handshake(System.IntPtr,System.Boolean)", "System.Void ObjCRuntime.Messaging::void_objc_msgSend_GCDualSenseAdaptiveTriggerPositionalAmplitudes_float(System.IntPtr,System.IntPtr,GameController.GCDualSenseAdaptiveTriggerPositionalAmplitudes,System.Single)", "System.Void ObjCRuntime.Messaging::void_objc_msgSend_GCDualSenseAdaptiveTriggerPositionalResistiveStrengths(System.IntPtr,System.IntPtr,GameController.GCDualSenseAdaptiveTriggerPositionalResistiveStrengths)", "System.Void ObjCRuntime.Messaging::void_objc_msgSendSuper_GCDualSenseAdaptiveTriggerPositionalAmplitudes_float(System.IntPtr,System.IntPtr,GameController.GCDualSenseAdaptiveTriggerPositionalAmplitudes,System.Single)",