iOS 使用 Xamarin 的多路径 TCP

Multipath TCP for iOS using Xamarin

我在 Xamarin 中有一个多平台应用程序,它出于某些目的连接到互联网(使用 Phone 的蜂窝网络),然后出于其他目的与本地 WIFI 网络(无法访问互联网)通信。我想从用户那里无缝切换网络(不需要用户交互)。

对于我的应用程序的 Android 部分,我使用以下方法成功实现了:​​

NetworkRequest.Builder request = new NetworkRequest.Builder();
request.AddTransportType(TransportType.Cellular);

之后:

ConnectivityManager.BindProcessToNetwork(network)

我在这里提供的完整解释:

但我无法理解如何在应用程序中为 iOS 实现同样的效果。我已阅读以下链接:

https://developer.apple.com/documentation/foundation/urlsessionconfiguration/improving_network_reliability_using_multipath_tcp

https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-walkthroughs/background-transfer-walkthrough

https://developer.apple.com/documentation/foundation/urlsessionconfiguration/multipathservicetype

并为我的应用程序启用了多路径 TCP 授权。

有人可以指导我在正确的方向上 how/where 在整个 iOS 应用程序生命周期中将 NSUrlSession 对象设置为 NSUrlSessionMultipathServiceType.Handover。

我知道我无法提供太多信息,但非常感谢任何正确方向的帮助。

更新: 根据@SushiHangover 的回答添加以下代码后

var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

我在 Xamarin 中收到以下警告:

最终工作更新: 经过多次尝试,我发现由于某种原因,Xamarin 没有正确地将枚举值映射到它的编号。所以我必须手动设置枚举值,现在切换(即多路径 TCP)工作正常。更新代码:

public void GetDataFromInternet(string strURL)
{
    NSUrl url = new NSUrl(strURL);
    NSUrlRequest request = new NSUrlRequest(url);
    NSUrlSession session = null;
    NSUrlSessionConfiguration config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
    //config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;    //for some reason this does not work!!
    config.MultipathServiceType = (NSUrlSessionMultipathServiceType)2;            //but this works!!
    session = NSUrlSession.FromConfiguration(config);

    NSUrlSessionTask task = session.CreateDataTask(request, (data, response, error) => {
        Console.WriteLine(data);
    });
    task.Resume();
}

how/where set the NSUrlSession object to NSUrlSessionMultipathServiceType.Handover in the whole iOS application lifecycle.

您可以通过 DefaultSessionConfiguration:

更改默认的 NSUrl 会话
var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.MultipathServiceType = NSUrlSessionMultipathServiceType.Handover;

注意:假设您希望在应用程序生命周期中尽早执行此操作,即在 AppDelegate.FinishedLaunching 覆盖中。

P.S。这与您在 Android 上所做的不同,并且确实需要您的服务器(更可能是服务器前面的路由器)启用多路径 TCP 以处理之间的自动切换传入路径