Skip to content

Commit

Permalink
[Network] Make the remaining P/Invokes have blittable signatures.
Browse files Browse the repository at this point in the history
Contributes towards xamarin#15684.
  • Loading branch information
rolfbjarne committed May 31, 2024
1 parent 5c5a458 commit 87a68aa
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 132 deletions.
9 changes: 4 additions & 5 deletions src/Network/NWContentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,14 @@ public NWContentContext (string contextIdentifier)
public string? Identifier => Marshal.PtrToStringAnsi (nw_content_context_get_identifier (GetCheckedHandle ()));

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
extern static bool nw_content_context_get_is_final (IntPtr handle);
extern static byte nw_content_context_get_is_final (IntPtr handle);

[DllImport (Constants.NetworkLibrary)]
extern static void nw_content_context_set_is_final (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool is_final);
extern static void nw_content_context_set_is_final (IntPtr handle, byte is_final);

public bool IsFinal {
get => nw_content_context_get_is_final (GetCheckedHandle ());
set => nw_content_context_set_is_final (GetCheckedHandle (), value);
get => nw_content_context_get_is_final (GetCheckedHandle ()) != 0;
set => nw_content_context_set_is_final (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
Expand Down
5 changes: 3 additions & 2 deletions src/Network/NWEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static string nw_endpoint_copy_address_string (OS_nw_endpoint endpoint)
[Watch (9, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
static extern unsafe byte* nw_endpoint_get_signature (OS_nw_endpoint endpoint, out nuint out_signature_length);
static extern unsafe byte* nw_endpoint_get_signature (OS_nw_endpoint endpoint, nuint* out_signature_length);

#if NET
[SupportedOSPlatform ("tvos16.0")]
Expand All @@ -227,7 +227,8 @@ static string nw_endpoint_copy_address_string (OS_nw_endpoint endpoint)
public ReadOnlySpan<byte> Signature {
get {
unsafe {
var data = nw_endpoint_get_signature (GetCheckedHandle (), out var length);
nuint length;
var data = nw_endpoint_get_signature (GetCheckedHandle (), &length);
var mValue = new ReadOnlySpan<byte> (data, (int) length);
// we do not know who manages the byte array, so we return a copy, is more expensive but
// safer until we know what is the mem management.
Expand Down
10 changes: 4 additions & 6 deletions src/Network/NWEstablishmentReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ public class NWEstablishmentReport : NativeObject {
internal NWEstablishmentReport (NativeHandle handle, bool owns) : base (handle, owns) { }

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_establishment_report_get_used_proxy (OS_nw_establishment_report report);
static extern byte nw_establishment_report_get_used_proxy (OS_nw_establishment_report report);

public bool UsedProxy => nw_establishment_report_get_used_proxy (GetCheckedHandle ());
public bool UsedProxy => nw_establishment_report_get_used_proxy (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_establishment_report_get_proxy_configured (OS_nw_establishment_report report);
static extern byte nw_establishment_report_get_proxy_configured (OS_nw_establishment_report report);

public bool ProxyConfigured => nw_establishment_report_get_proxy_configured (GetCheckedHandle ());
public bool ProxyConfigured => nw_establishment_report_get_proxy_configured (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
static extern uint nw_establishment_report_get_previous_attempt_count (OS_nw_establishment_report report);
Expand Down
12 changes: 5 additions & 7 deletions src/Network/NWMulticastGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public NWMulticastGroup (NWEndpoint endpoint)
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_group_descriptor_add_endpoint (OS_nw_group_descriptor descriptor, OS_nw_endpoint endpoint);
static extern byte nw_group_descriptor_add_endpoint (OS_nw_group_descriptor descriptor, OS_nw_endpoint endpoint);

public void AddEndpoint (NWEndpoint endpoint)
{
Expand All @@ -53,15 +52,14 @@ public void AddEndpoint (NWEndpoint endpoint)
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_multicast_group_descriptor_get_disable_unicast_traffic (OS_nw_group_descriptor multicast_descriptor);
static extern byte nw_multicast_group_descriptor_get_disable_unicast_traffic (OS_nw_group_descriptor multicast_descriptor);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_multicast_group_descriptor_set_disable_unicast_traffic (OS_nw_group_descriptor multicast_descriptor, [MarshalAs (UnmanagedType.I1)] bool disable_unicast_traffic);
static extern void nw_multicast_group_descriptor_set_disable_unicast_traffic (OS_nw_group_descriptor multicast_descriptor, byte disable_unicast_traffic);

public bool DisabledUnicastTraffic {
get => nw_multicast_group_descriptor_get_disable_unicast_traffic (GetCheckedHandle ());
set => nw_multicast_group_descriptor_set_disable_unicast_traffic (GetCheckedHandle (), value);
get => nw_multicast_group_descriptor_get_disable_unicast_traffic (GetCheckedHandle ()) != 0;
set => nw_multicast_group_descriptor_set_disable_unicast_traffic (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
Expand Down
72 changes: 32 additions & 40 deletions src/Network/NWParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,27 +288,25 @@ public NWMultiPathService MultipathService {
public NWProtocolStack ProtocolStack => new NWProtocolStack (nw_parameters_copy_default_protocol_stack (GetCheckedHandle ()), owns: true);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_local_only (nw_parameters_t parameters, [MarshalAs (UnmanagedType.I1)] bool local_only);
static extern void nw_parameters_set_local_only (nw_parameters_t parameters, byte local_only);

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_local_only (nw_parameters_t parameters);
static extern byte nw_parameters_get_local_only (nw_parameters_t parameters);

public bool LocalOnly {
get => nw_parameters_get_local_only (GetCheckedHandle ());
set => nw_parameters_set_local_only (GetCheckedHandle (), value);
get => nw_parameters_get_local_only (GetCheckedHandle ()) != 0;
set => nw_parameters_set_local_only (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_prefer_no_proxy (nw_parameters_t parameters, [MarshalAs (UnmanagedType.I1)] bool prefer_no_proxy);
static extern void nw_parameters_set_prefer_no_proxy (nw_parameters_t parameters, byte prefer_no_proxy);

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_prefer_no_proxy (nw_parameters_t parameters);
static extern byte nw_parameters_get_prefer_no_proxy (nw_parameters_t parameters);

public bool PreferNoProxy {
get => nw_parameters_get_prefer_no_proxy (GetCheckedHandle ());
set => nw_parameters_set_prefer_no_proxy (GetCheckedHandle (), value);
get => nw_parameters_get_prefer_no_proxy (GetCheckedHandle ()) != 0;
set => nw_parameters_set_prefer_no_proxy (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
Expand Down Expand Up @@ -461,39 +459,36 @@ public void IterateProhibitedInterfaces (Func<NWInterfaceType, bool> callback)
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_prohibit_expensive (IntPtr handle);
static extern byte nw_parameters_get_prohibit_expensive (IntPtr handle);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_prohibit_expensive (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool prohibit_expensive);
static extern void nw_parameters_set_prohibit_expensive (IntPtr handle, byte prohibit_expensive);

public bool ProhibitExpensive {
get => nw_parameters_get_prohibit_expensive (GetCheckedHandle ());
set => nw_parameters_set_prohibit_expensive (GetCheckedHandle (), value);
get => nw_parameters_get_prohibit_expensive (GetCheckedHandle ()) != 0;
set => nw_parameters_set_prohibit_expensive (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_reuse_local_address (IntPtr handle);
static extern byte nw_parameters_get_reuse_local_address (IntPtr handle);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_reuse_local_address (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool reuse_local_address);
static extern void nw_parameters_set_reuse_local_address (IntPtr handle, byte reuse_local_address);

public bool ReuseLocalAddress {
get => nw_parameters_get_reuse_local_address (GetCheckedHandle ());
set => nw_parameters_set_reuse_local_address (GetCheckedHandle (), value);
get => nw_parameters_get_reuse_local_address (GetCheckedHandle ()) != 0;
set => nw_parameters_set_reuse_local_address (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_fast_open_enabled (IntPtr handle);
static extern byte nw_parameters_get_fast_open_enabled (IntPtr handle);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_fast_open_enabled (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool fast_open_enabled);
static extern void nw_parameters_set_fast_open_enabled (IntPtr handle, byte fast_open_enabled);

public bool FastOpenEnabled {
get => nw_parameters_get_fast_open_enabled (GetCheckedHandle ());
set => nw_parameters_set_fast_open_enabled (GetCheckedHandle (), value);
get => nw_parameters_get_fast_open_enabled (GetCheckedHandle ()) != 0;
set => nw_parameters_set_fast_open_enabled (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
Expand Down Expand Up @@ -529,15 +524,14 @@ public NWEndpoint? LocalEndpoint {


[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_include_peer_to_peer (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool includePeerToPeer);
static extern void nw_parameters_set_include_peer_to_peer (IntPtr handle, byte includePeerToPeer);

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_include_peer_to_peer (IntPtr handle);
static extern byte nw_parameters_get_include_peer_to_peer (IntPtr handle);

public bool IncludePeerToPeer {
get => nw_parameters_get_include_peer_to_peer (GetCheckedHandle ());
set => nw_parameters_set_include_peer_to_peer (GetCheckedHandle (), value);
get => nw_parameters_get_include_peer_to_peer (GetCheckedHandle ()) != 0;
set => nw_parameters_set_include_peer_to_peer (GetCheckedHandle (), value.AsByte ());
}

#if NET
Expand All @@ -550,8 +544,7 @@ public bool IncludePeerToPeer {
[iOS (13, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_parameters_get_prohibit_constrained (IntPtr parameters);
static extern byte nw_parameters_get_prohibit_constrained (IntPtr parameters);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -563,7 +556,7 @@ public bool IncludePeerToPeer {
[iOS (13, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_prohibit_constrained (IntPtr parameters, [MarshalAs (UnmanagedType.I1)] bool prohibit_constrained);
static extern void nw_parameters_set_prohibit_constrained (IntPtr parameters, byte prohibit_constrained);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -575,8 +568,8 @@ public bool IncludePeerToPeer {
[iOS (13, 0)]
#endif
public bool ProhibitConstrained {
get => nw_parameters_get_prohibit_constrained (GetCheckedHandle ());
set => nw_parameters_set_prohibit_constrained (GetCheckedHandle (), value);
get => nw_parameters_get_prohibit_constrained (GetCheckedHandle ()) != 0;
set => nw_parameters_set_prohibit_constrained (GetCheckedHandle (), value.AsByte ());
}

#if NET
Expand Down Expand Up @@ -708,9 +701,8 @@ public unsafe static NWParameters CreateQuic (Action<NWProtocolOptions>? configu
[iOS (16, 0)]
[Watch (9, 0)]
#endif
[return: MarshalAs (UnmanagedType.I1)]
[DllImport (Constants.NetworkLibrary)]
static extern bool nw_parameters_requires_dnssec_validation (OS_nw_parameters parameters);
static extern byte nw_parameters_requires_dnssec_validation (OS_nw_parameters parameters);

#if NET
[SupportedOSPlatform ("tvos16.0")]
Expand All @@ -724,7 +716,7 @@ public unsafe static NWParameters CreateQuic (Action<NWProtocolOptions>? configu
[Watch (9, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
static extern void nw_parameters_set_requires_dnssec_validation (OS_nw_parameters parameters, [MarshalAs (UnmanagedType.I1)] bool requires_dnssec_validation);
static extern void nw_parameters_set_requires_dnssec_validation (OS_nw_parameters parameters, byte requires_dnssec_validation);

#if NET
[SupportedOSPlatform ("tvos16.0")]
Expand All @@ -738,8 +730,8 @@ public unsafe static NWParameters CreateQuic (Action<NWProtocolOptions>? configu
[Watch (9, 0)]
#endif
public bool RequiresDnssecValidation {
get => nw_parameters_requires_dnssec_validation (GetCheckedHandle ());
set => nw_parameters_set_requires_dnssec_validation (GetCheckedHandle (), value);
get => nw_parameters_requires_dnssec_validation (GetCheckedHandle ()) != 0;
set => nw_parameters_set_requires_dnssec_validation (GetCheckedHandle (), value.AsByte ());
}
}
}
35 changes: 14 additions & 21 deletions src/Network/NWPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,29 @@ public NWPath (NativeHandle handle, bool owns) : base (handle, owns) { }
public NWPathStatus Status => nw_path_get_status (GetCheckedHandle ());

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_is_expensive (IntPtr handle);
extern static byte nw_path_is_expensive (IntPtr handle);

public bool IsExpensive => nw_path_is_expensive (GetCheckedHandle ());
public bool IsExpensive => nw_path_is_expensive (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_has_ipv4 (IntPtr handle);
extern static byte nw_path_has_ipv4 (IntPtr handle);

public bool HasIPV4 => nw_path_has_ipv4 (GetCheckedHandle ());
public bool HasIPV4 => nw_path_has_ipv4 (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_has_ipv6 (IntPtr handle);
extern static byte nw_path_has_ipv6 (IntPtr handle);

public bool HasIPV6 => nw_path_has_ipv6 (GetCheckedHandle ());
public bool HasIPV6 => nw_path_has_ipv6 (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_has_dns (IntPtr handle);
extern static byte nw_path_has_dns (IntPtr handle);

public bool HasDns => nw_path_has_dns (GetCheckedHandle ());
public bool HasDns => nw_path_has_dns (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_uses_interface_type (IntPtr handle, NWInterfaceType type);
extern static byte nw_path_uses_interface_type (IntPtr handle, NWInterfaceType type);

public bool UsesInterfaceType (NWInterfaceType type) => nw_path_uses_interface_type (GetCheckedHandle (), type);
public bool UsesInterfaceType (NWInterfaceType type) => nw_path_uses_interface_type (GetCheckedHandle (), type) != 0;

[DllImport (Constants.NetworkLibrary)]
extern static IntPtr nw_path_copy_effective_local_endpoint (IntPtr handle);
Expand All @@ -101,15 +96,14 @@ public NWEndpoint? EffectiveRemoteEndpoint {
}

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.U1)]
extern static bool nw_path_is_equal (IntPtr p1, IntPtr p2);
extern static byte nw_path_is_equal (IntPtr p1, IntPtr p2);

public bool EqualsTo (NWPath other)
{
if (other is null)
return false;

return nw_path_is_equal (GetCheckedHandle (), other.Handle);
return nw_path_is_equal (GetCheckedHandle (), other.Handle) != 0;
}

// Returning 'byte' since 'bool' isn't blittable
Expand Down Expand Up @@ -177,8 +171,7 @@ public void EnumerateInterfaces (Func<NWInterface, bool> callback)
[iOS (13, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_path_is_constrained (IntPtr path);
static extern byte nw_path_is_constrained (IntPtr path);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -189,7 +182,7 @@ public void EnumerateInterfaces (Func<NWInterface, bool> callback)
[TV (13, 0)]
[iOS (13, 0)]
#endif
public bool IsConstrained => nw_path_is_constrained (GetCheckedHandle ());
public bool IsConstrained => nw_path_is_constrained (GetCheckedHandle ()) != 0;

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand Down
4 changes: 2 additions & 2 deletions src/Network/NWPrivacyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public void DisableLogging ()
=> nw_privacy_context_disable_logging (GetCheckedHandle ());

[DllImport (Constants.NetworkLibrary)]
static extern void nw_privacy_context_require_encrypted_name_resolution (OS_nw_privacy_context privacyContext, [MarshalAs (UnmanagedType.I1)] bool requireEncryptedNameResolution, OS_nw_resolver_config fallbackResolverConfig);
static extern void nw_privacy_context_require_encrypted_name_resolution (OS_nw_privacy_context privacyContext, byte requireEncryptedNameResolution, OS_nw_resolver_config fallbackResolverConfig);

public void RequireEncryptedNameResolution (bool requireEncryptedNameResolution, NWResolverConfig? fallbackResolverConfig)
=> nw_privacy_context_require_encrypted_name_resolution (GetCheckedHandle (), requireEncryptedNameResolution, fallbackResolverConfig.GetHandle ());
=> nw_privacy_context_require_encrypted_name_resolution (GetCheckedHandle (), requireEncryptedNameResolution.AsByte (), fallbackResolverConfig.GetHandle ());

#if NET
[SupportedOSPlatform ("tvos17.0")]
Expand Down
Loading

0 comments on commit 87a68aa

Please sign in to comment.