Skip to content
Open
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
21 changes: 21 additions & 0 deletions src/Network/NWEthernetChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#nullable enable

#if MONOMAC
using System.ComponentModel;
using System.Text;
using System.Runtime.CompilerServices;
using CoreFoundation;
Expand Down Expand Up @@ -166,8 +167,28 @@ static void TrampolineStateChangesHandler (IntPtr block, NWEthernetChannelState
}
}

#if !XAMCORE_5_0
[BindingImpl (BindingImplOptions.Optimizable)]
[EditorBrowsable (EditorBrowsableState.Never)]
[Obsolete ("Use the other overload.")]
[OverloadResolutionPriority (-1)]
public void SetStateChangesHandler (Action<NWBrowserState, NWError?> handler)
{
if (handler is null) {
unsafe {
nw_ethernet_channel_set_state_changed_handler (GetCheckedHandle (), null);
}
return;
}

SetStateChangesHandler ((NWEthernetChannelState state, NWError? error) => handler ((NWBrowserState) state, error));
}
#endif

/// <summary>Sets the callback invoked when the Ethernet channel state changes.</summary>
/// <param name="handler">The callback receives the new <see cref="NWEthernetChannelState"/> and an associated <see cref="NWError"/>, if any. Pass <see langword="null"/> to remove the callback.</param>
[BindingImpl (BindingImplOptions.Optimizable)]
public void SetStateChangesHandler (Action<NWEthernetChannelState, NWError?>? handler)
{
unsafe {
if (handler is null) {
Expand Down
1 change: 0 additions & 1 deletion tests/cecil-tests/Documentation.KnownFailures.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22644,7 +22644,6 @@ M:Network.NWEthernetChannel.Cancel
M:Network.NWEthernetChannel.Send(System.ReadOnlySpan{System.Byte},System.UInt16,System.String,System.Action{Network.NWError})
M:Network.NWEthernetChannel.SetQueue(CoreFoundation.DispatchQueue)
M:Network.NWEthernetChannel.SetReceiveHandler(Network.NWEthernetChannelReceiveDelegate)
M:Network.NWEthernetChannel.SetStateChangesHandler(System.Action{Network.NWBrowserState,Network.NWError})
M:Network.NWEthernetChannel.Start
M:Network.NWFramer.CreateMessage
M:Network.NWFramer.CreateOptions``1(Network.NWProtocolDefinition)
Expand Down
69 changes: 69 additions & 0 deletions tests/monotouch-test/Network/NWEthernetChannelTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#if MONOMAC
using System.Threading.Tasks;

using Network;

namespace MonoTouchFixtures.Network {
[TestFixture]
[Preserve (AllMembers = true)]
public class NWEthernetChannelTest {
[TestCase (false)]
[TestCase (true)]
public void StateChangesHandler (bool useLegacyHandler)
{
TestRuntime.AssertXcodeVersion (11, 0);
TestRuntime.IgnoreInCI ("This test requires a wired interface and the custom-protocol entitlement.");

using var monitor = new NWPathMonitor ();
var pathUpdated = new TaskCompletionSource<NWPath> ();
monitor.SetQueue (DispatchQueue.DefaultGlobalQueue);
monitor.SnapshotHandler = currentPath => {
pathUpdated.TrySetResult (currentPath);
};
monitor.Start ();
try {
Assert.That (pathUpdated.Task.Wait (30000), Is.True, "Path update");
} finally {
monitor.Cancel ();
}
var path = pathUpdated.Task.Result;
Assert.That (path, Is.Not.Null, "Current path");

NWInterface? ethernetInterface = null;
path.EnumerateInterfaces (networkInterface => {
if (networkInterface.InterfaceType == NWInterfaceType.Wired)
ethernetInterface = networkInterface;
return true;
});
if (ethernetInterface is null)
Assert.Ignore ("This test requires a wired interface.");

using var channel = new NWEthernetChannel (0x88b5, ethernetInterface);
var stateChanged = new TaskCompletionSource<NWEthernetChannelState> ();
channel.SetQueue (DispatchQueue.DefaultGlobalQueue);
if (useLegacyHandler) {
Action<NWBrowserState, NWError?> legacyHandler = (currentState, error) => {
stateChanged.TrySetResult ((NWEthernetChannelState) currentState);
};
channel.SetStateChangesHandler (legacyHandler);
} else {
channel.SetStateChangesHandler ((currentState, error) => {
stateChanged.TrySetResult (currentState);
});
}
channel.Start ();
try {
Assert.That (stateChanged.Task.Wait (30000), Is.True, "State callback");
Assert.That (stateChanged.Task.Result, Is.AnyOf (
NWEthernetChannelState.Waiting, NWEthernetChannelState.Preparing, NWEthernetChannelState.Ready,
NWEthernetChannelState.Failed, NWEthernetChannelState.Cancelled), "Ethernet channel state");
} finally {
channel.Cancel ();
}
}
}
}
#endif
Loading