Skip to content

Commit

Permalink
[Network] Make P/Invokes in NWProtocol* have blittable signatures. (#…
Browse files Browse the repository at this point in the history
…20638)

Contributes towards #15684.
  • Loading branch information
rolfbjarne committed May 24, 2024
1 parent b59ff7c commit 0e2fec8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 101 deletions.
5 changes: 2 additions & 3 deletions src/Network/NWProtocolDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ public NWProtocolDefinition (NativeHandle handle, bool owns) : base (handle, own
#endif

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_protocol_definition_is_equal (OS_nw_protocol_definition definition1, OS_nw_protocol_definition definition2);
static extern byte nw_protocol_definition_is_equal (OS_nw_protocol_definition definition1, OS_nw_protocol_definition definition2);

public bool Equals (object other)
{
if (other is null)
return false;
if (!(other is NWProtocolDefinition otherDefinition))
return false;
return nw_protocol_definition_is_equal (GetCheckedHandle (), otherDefinition.Handle);
return nw_protocol_definition_is_equal (GetCheckedHandle (), otherDefinition.Handle) != 0;
}

[DllImport (Constants.NetworkLibrary)]
Expand Down
10 changes: 5 additions & 5 deletions src/Network/NWProtocolIPOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ public void SetHopLimit (nuint hopLimit)
=> nw_ip_options_set_hop_limit (GetCheckedHandle (), (byte) hopLimit);

public void SetUseMinimumMtu (bool useMinimumMtu)
=> nw_ip_options_set_use_minimum_mtu (GetCheckedHandle (), useMinimumMtu);
=> nw_ip_options_set_use_minimum_mtu (GetCheckedHandle (), useMinimumMtu.AsByte ());

public void SetDisableFragmentation (bool disableFragmentation)
=> nw_ip_options_set_disable_fragmentation (GetCheckedHandle (), disableFragmentation);
=> nw_ip_options_set_disable_fragmentation (GetCheckedHandle (), disableFragmentation.AsByte ());

public void SetCalculateReceiveTime (bool shouldCalculateReceiveTime)
=> nw_ip_options_set_calculate_receive_time (GetCheckedHandle (), shouldCalculateReceiveTime);
=> nw_ip_options_set_calculate_receive_time (GetCheckedHandle (), shouldCalculateReceiveTime.AsByte ());

public void SetIPLocalAddressPreference (NWIPLocalAddressPreference localAddressPreference)
=> nw_ip_options_set_local_address_preference (GetCheckedHandle (), localAddressPreference);
Expand All @@ -71,7 +71,7 @@ public void SetIPLocalAddressPreference (NWIPLocalAddressPreference localAddress
[MacCatalyst (15, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
static extern void nw_ip_options_set_disable_multicast_loopback (OS_nw_protocol_options options, [MarshalAs (UnmanagedType.I1)] bool disableMulticastLoopback);
static extern void nw_ip_options_set_disable_multicast_loopback (OS_nw_protocol_options options, byte disableMulticastLoopback);

#if NET
[SupportedOSPlatform ("tvos15.0")]
Expand All @@ -86,6 +86,6 @@ public void SetIPLocalAddressPreference (NWIPLocalAddressPreference localAddress
[MacCatalyst (15, 0)]
#endif
public void DisableMulticastLoopback (bool disable)
=> nw_ip_options_set_disable_multicast_loopback (GetCheckedHandle (), disable);
=> nw_ip_options_set_disable_multicast_loopback (GetCheckedHandle (), disable.AsByte ());
}
}
35 changes: 14 additions & 21 deletions src/Network/NWProtocolMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,24 @@ public NWProtocolMetadata (NativeHandle handle, bool owns) : base (handle, owns)
public NWProtocolDefinition ProtocolDefinition => new NWProtocolDefinition (nw_protocol_metadata_copy_definition (GetCheckedHandle ()), owns: true);

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_ip (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_ip (OS_nw_protocol_metadata metadata);

public bool IsIP => nw_protocol_metadata_is_ip (GetCheckedHandle ());
public bool IsIP => nw_protocol_metadata_is_ip (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_udp (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_udp (OS_nw_protocol_metadata metadata);

public bool IsUdp => nw_protocol_metadata_is_udp (GetCheckedHandle ());
public bool IsUdp => nw_protocol_metadata_is_udp (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_tls (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_tls (OS_nw_protocol_metadata metadata);

public bool IsTls => nw_protocol_metadata_is_tls (GetCheckedHandle ());
public bool IsTls => nw_protocol_metadata_is_tls (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_tcp (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_tcp (OS_nw_protocol_metadata metadata);

public bool IsTcp => nw_protocol_metadata_is_tcp (GetCheckedHandle ());
public bool IsTcp => nw_protocol_metadata_is_tcp (GetCheckedHandle ()) != 0;

#if NET
[SupportedOSPlatform ("tvos15.0")]
Expand All @@ -109,8 +105,7 @@ public NWProtocolMetadata (NativeHandle handle, bool owns) : base (handle, owns)
[MacCatalyst (15, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_protocol_metadata_is_quic (OS_nw_protocol_metadata metadata);
static extern byte nw_protocol_metadata_is_quic (OS_nw_protocol_metadata metadata);

#if NET
[SupportedOSPlatform ("tvos15.0")]
Expand All @@ -124,7 +119,7 @@ public NWProtocolMetadata (NativeHandle handle, bool owns) : base (handle, owns)
[iOS (15, 0)]
[MacCatalyst (15, 0)]
#endif
public bool IsQuic => nw_protocol_metadata_is_quic (GetCheckedHandle ());
public bool IsQuic => nw_protocol_metadata_is_quic (GetCheckedHandle ()) != 0;

[DllImport (Constants.NetworkLibrary)]
internal static extern IntPtr nw_tls_copy_sec_protocol_metadata (IntPtr handle);
Expand Down Expand Up @@ -253,8 +248,7 @@ public uint TcpGetAvailableSendBuffer ()
[iOS (13, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_framer_message (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_framer_message (OS_nw_protocol_metadata metadata);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -265,7 +259,7 @@ public uint TcpGetAvailableSendBuffer ()
[TV (13, 0)]
[iOS (13, 0)]
#endif
public bool IsFramerMessage => nw_protocol_metadata_is_framer_message (GetCheckedHandle ());
public bool IsFramerMessage => nw_protocol_metadata_is_framer_message (GetCheckedHandle ()) != 0;

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -277,8 +271,7 @@ public uint TcpGetAvailableSendBuffer ()
[iOS (13, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
internal static extern bool nw_protocol_metadata_is_ws (OS_nw_protocol_metadata metadata);
internal static extern byte nw_protocol_metadata_is_ws (OS_nw_protocol_metadata metadata);

#if NET
[SupportedOSPlatform ("tvos13.0")]
Expand All @@ -289,6 +282,6 @@ public uint TcpGetAvailableSendBuffer ()
[TV (13, 0)]
[iOS (13, 0)]
#endif
public bool IsWebSocket => nw_protocol_metadata_is_ws (GetCheckedHandle ());
public bool IsWebSocket => nw_protocol_metadata_is_ws (GetCheckedHandle ()) != 0;
}
}
53 changes: 26 additions & 27 deletions src/Network/NWProtocolOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,35 +132,35 @@ public void IPSetHopLimit (byte hopLimit)
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal static extern void nw_ip_options_set_use_minimum_mtu (IntPtr options, [MarshalAs (UnmanagedType.I1)] bool use_minimum_mtu);
internal static extern void nw_ip_options_set_use_minimum_mtu (IntPtr options, byte use_minimum_mtu);

#if !NET
[Obsolete ("Use the 'NWProtocolIPOptions' class instead.")]
public void IPSetUseMinimumMtu (bool useMinimumMtu)
{
nw_ip_options_set_use_minimum_mtu (GetCheckedHandle (), useMinimumMtu);
nw_ip_options_set_use_minimum_mtu (GetCheckedHandle (), useMinimumMtu.AsByte ());
}
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal static extern void nw_ip_options_set_disable_fragmentation (IntPtr options, [MarshalAs (UnmanagedType.I1)] bool disable_fragmentation);
internal static extern void nw_ip_options_set_disable_fragmentation (IntPtr options, byte disable_fragmentation);

#if !NET
[Obsolete ("Use the 'NWProtocolIPOptions' class instead.")]
public void IPSetDisableFragmentation (bool disableFragmentation)
{
nw_ip_options_set_disable_fragmentation (GetCheckedHandle (), disableFragmentation);
nw_ip_options_set_disable_fragmentation (GetCheckedHandle (), disableFragmentation.AsByte ());
}
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal static extern void nw_ip_options_set_calculate_receive_time (IntPtr options, [MarshalAs (UnmanagedType.I1)] bool calculateReceiveTime);
internal static extern void nw_ip_options_set_calculate_receive_time (IntPtr options, byte calculateReceiveTime);

#if !NET
[Obsolete ("Use the 'NWProtocolIPOptions' class instead.")]
public void IPSetCalculateReceiveTime (bool calculateReceiveTime)
{
nw_ip_options_set_calculate_receive_time (GetCheckedHandle (), calculateReceiveTime);
nw_ip_options_set_calculate_receive_time (GetCheckedHandle (), calculateReceiveTime.AsByte ());
}
#endif // !NET

Expand Down Expand Up @@ -190,35 +190,35 @@ public NWIPLocalAddressPreference IPLocalAddressPreference {
//

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_no_delay (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool noDelay);
internal extern static void nw_tcp_options_set_no_delay (IntPtr handle, byte noDelay);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetNoDelay (bool noDelay) => nw_tcp_options_set_no_delay (GetCheckedHandle (), noDelay);
public void TcpSetNoDelay (bool noDelay) => nw_tcp_options_set_no_delay (GetCheckedHandle (), noDelay.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_no_push (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool noPush);
internal extern static void nw_tcp_options_set_no_push (IntPtr handle, byte noPush);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetNoPush (bool noPush) => nw_tcp_options_set_no_push (GetCheckedHandle (), noPush);
public void TcpSetNoPush (bool noPush) => nw_tcp_options_set_no_push (GetCheckedHandle (), noPush.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_no_options (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool noOptions);
internal extern static void nw_tcp_options_set_no_options (IntPtr handle, byte noOptions);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetNoOptions (bool noOptions) => nw_tcp_options_set_no_options (GetCheckedHandle (), noOptions);
public void TcpSetNoOptions (bool noOptions) => nw_tcp_options_set_no_options (GetCheckedHandle (), noOptions.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_enable_keepalive (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool enableKeepAlive);
internal extern static void nw_tcp_options_set_enable_keepalive (IntPtr handle, byte enableKeepAlive);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetEnableKeepAlive (bool enableKeepAlive) => nw_tcp_options_set_enable_keepalive (GetCheckedHandle (), enableKeepAlive);
public void TcpSetEnableKeepAlive (bool enableKeepAlive) => nw_tcp_options_set_enable_keepalive (GetCheckedHandle (), enableKeepAlive.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
Expand Down Expand Up @@ -278,46 +278,46 @@ public NWIPLocalAddressPreference IPLocalAddressPreference {
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_retransmit_fin_drop (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool retransmitFinDrop);
internal extern static void nw_tcp_options_set_retransmit_fin_drop (IntPtr handle, byte retransmitFinDrop);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetRetransmitFinDrop (bool retransmitFinDrop) => nw_tcp_options_set_retransmit_fin_drop (GetCheckedHandle (), retransmitFinDrop);
public void TcpSetRetransmitFinDrop (bool retransmitFinDrop) => nw_tcp_options_set_retransmit_fin_drop (GetCheckedHandle (), retransmitFinDrop.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_disable_ack_stretching (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool disableAckStretching);
internal extern static void nw_tcp_options_set_disable_ack_stretching (IntPtr handle, byte disableAckStretching);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetDisableAckStretching (bool disableAckStretching) => nw_tcp_options_set_disable_ack_stretching (GetCheckedHandle (), disableAckStretching);
public void TcpSetDisableAckStretching (bool disableAckStretching) => nw_tcp_options_set_disable_ack_stretching (GetCheckedHandle (), disableAckStretching.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_enable_fast_open (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool enableFastOpen);
internal extern static void nw_tcp_options_set_enable_fast_open (IntPtr handle, byte enableFastOpen);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetEnableFastOpen (bool enableFastOpen) => nw_tcp_options_set_enable_fast_open (GetCheckedHandle (), enableFastOpen);
public void TcpSetEnableFastOpen (bool enableFastOpen) => nw_tcp_options_set_enable_fast_open (GetCheckedHandle (), enableFastOpen.AsByte ());
#endif // !NET

[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_tcp_options_set_disable_ecn (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool disableEcn);
internal extern static void nw_tcp_options_set_disable_ecn (IntPtr handle, byte disableEcn);

#if !NET
[Obsolete ("Use the 'NWProtocolTcpOptions' class instead.")]
public void TcpSetDisableEcn (bool disableEcn) => nw_tcp_options_set_disable_ecn (GetCheckedHandle (), disableEcn);
public void TcpSetDisableEcn (bool disableEcn) => nw_tcp_options_set_disable_ecn (GetCheckedHandle (), disableEcn.AsByte ());
#endif // !NET

//
// UDP Options
//
[DllImport (Constants.NetworkLibrary)]
internal extern static void nw_udp_options_set_prefer_no_checksum (IntPtr handle, [MarshalAs (UnmanagedType.U1)] bool preferNoChecksums);
internal extern static void nw_udp_options_set_prefer_no_checksum (IntPtr handle, byte preferNoChecksums);

#if !NET
[Obsolete ("Use the 'NWProtocolUdpOptions' class instead.")]
public void UdpSetPreferNoChecksum (bool preferNoChecksums) => nw_udp_options_set_prefer_no_checksum (GetCheckedHandle (), preferNoChecksums);
public void UdpSetPreferNoChecksum (bool preferNoChecksums) => nw_udp_options_set_prefer_no_checksum (GetCheckedHandle (), preferNoChecksums.AsByte ());
#endif // !NET

//
Expand Down Expand Up @@ -345,9 +345,8 @@ public NWIPLocalAddressPreference IPLocalAddressPreference {
[MacCatalyst (15, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_protocol_options_is_quic (IntPtr options);
static extern byte nw_protocol_options_is_quic (IntPtr options);

public bool IsQuic => nw_protocol_options_is_quic (GetCheckedHandle ());
public bool IsQuic => nw_protocol_options_is_quic (GetCheckedHandle ()) != 0;
}
}
18 changes: 8 additions & 10 deletions src/Network/NWProtocolQuicOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ public SecProtocolOptions SecProtocolOptions
=> new SecProtocolOptions (nw_quic_copy_sec_protocol_options (GetCheckedHandle ()), true);

[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_quic_get_stream_is_unidirectional (OS_nw_protocol_options options);
static extern byte nw_quic_get_stream_is_unidirectional (OS_nw_protocol_options options);

[DllImport (Constants.NetworkLibrary)]
static extern void nw_quic_set_stream_is_unidirectional (OS_nw_protocol_options options, [MarshalAs (UnmanagedType.I1)] bool isUnidirectional);
static extern void nw_quic_set_stream_is_unidirectional (OS_nw_protocol_options options, byte isUnidirectional);

public bool StreamIsUnidirectional {
get => nw_quic_get_stream_is_unidirectional (GetCheckedHandle ());
set => nw_quic_set_stream_is_unidirectional (GetCheckedHandle (), value);
get => nw_quic_get_stream_is_unidirectional (GetCheckedHandle ()) != 0;
set => nw_quic_set_stream_is_unidirectional (GetCheckedHandle (), value.AsByte ());
}

[DllImport (Constants.NetworkLibrary)]
Expand Down Expand Up @@ -209,8 +208,7 @@ public ushort DatagramFrameSize {
[Watch (9, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
[return: MarshalAs (UnmanagedType.I1)]
static extern bool nw_quic_get_stream_is_datagram (OS_nw_protocol_options options);
static extern byte nw_quic_get_stream_is_datagram (OS_nw_protocol_options options);

#if NET
[SupportedOSPlatform ("tvos16.0")]
Expand All @@ -224,7 +222,7 @@ public ushort DatagramFrameSize {
[Watch (9, 0)]
#endif
[DllImport (Constants.NetworkLibrary)]
static extern void nw_quic_set_stream_is_datagram (OS_nw_protocol_options options, [MarshalAs (UnmanagedType.I1)] bool is_datagram);
static extern void nw_quic_set_stream_is_datagram (OS_nw_protocol_options options, byte is_datagram);

#if NET
[SupportedOSPlatform ("tvos16.0")]
Expand All @@ -238,8 +236,8 @@ public ushort DatagramFrameSize {
[Watch (9, 0)]
#endif
public bool StreamIsDatagram {
get => nw_quic_get_stream_is_datagram (GetCheckedHandle ());
set => nw_quic_set_stream_is_datagram (GetCheckedHandle (), value);
get => nw_quic_get_stream_is_datagram (GetCheckedHandle ()) != 0;
set => nw_quic_set_stream_is_datagram (GetCheckedHandle (), value.AsByte ());
}

#if NET
Expand Down
Loading

13 comments on commit 0e2fec8

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.