initial commit, iOS network native library
This commit is contained in:
56
Network/Connectivity.cs
Normal file
56
Network/Connectivity.cs
Normal file
@ -0,0 +1,56 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public partial class Connectivity
|
||||
{
|
||||
const string hostName = "www.baidu.com";
|
||||
static NetworkStatus currentStatus;
|
||||
static Action<NetworkStatus>? changedInternal;
|
||||
|
||||
public static NetworkStatus Status => PlatformGetStatus();
|
||||
|
||||
public static event Action<NetworkStatus> Changed
|
||||
{
|
||||
add
|
||||
{
|
||||
var running = changedInternal != null;
|
||||
changedInternal += value;
|
||||
if (!running && changedInternal != null)
|
||||
{
|
||||
currentStatus = Status;
|
||||
StartListeners();
|
||||
}
|
||||
}
|
||||
remove
|
||||
{
|
||||
var running = changedInternal != null;
|
||||
changedInternal -= value;
|
||||
if (running && changedInternal == null)
|
||||
{
|
||||
StopListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static partial NetworkStatus PlatformGetStatus();
|
||||
|
||||
private static partial void StartListeners();
|
||||
|
||||
private static partial void StopListeners();
|
||||
|
||||
static void OnConnectivityChanged()
|
||||
{
|
||||
var status = Status;
|
||||
if (currentStatus != status)
|
||||
{
|
||||
currentStatus = status;
|
||||
changedInternal?.Invoke(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum NetworkStatus
|
||||
{
|
||||
NotReachable,
|
||||
ReachableViaCarrierDataNetwork,
|
||||
ReachableViaWiFiNetwork
|
||||
}
|
15
Network/Network.csproj
Normal file
15
Network/Network.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net7.0-ios</TargetFrameworks>
|
||||
<RootNamespace>Blahblah.Library.Network</RootNamespace>
|
||||
<UseMaui>true</UseMaui>
|
||||
<Nullable>enable</Nullable>
|
||||
<SingleProject>true</SingleProject>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
|
||||
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
|
||||
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
68
Network/NetworkHelper.cs
Normal file
68
Network/NetworkHelper.cs
Normal file
@ -0,0 +1,68 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public partial class NetworkHelper
|
||||
{
|
||||
public const string AcceptAll = "*/*";
|
||||
public const string AcceptHttp = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
|
||||
public const string AcceptImage = "image/webp,image/*,*/*;q=0.8";
|
||||
public const string AcceptJpegImage = "image/jpeg,image/*,*/*;q=0.8";
|
||||
public const string AcceptJson = "application/json";
|
||||
|
||||
const string AcceptHeader = "Accept";
|
||||
const string ReferrerHeader = "Referer";
|
||||
|
||||
const int Timeout = 30;
|
||||
const int ImageTimeout = 60;
|
||||
|
||||
public static CancellationTokenSource TimeoutTokenSource => new(Timeout * 1000);
|
||||
public static CancellationTokenSource ImageTimeoutTokenSource => new(ImageTimeout * 1000);
|
||||
|
||||
static string? proxyHost;
|
||||
static int? proxyPort;
|
||||
|
||||
public static void SetProxy(string? host = null, int? port = null)
|
||||
{
|
||||
//bool changed = proxyHost != host || proxyPort != port;
|
||||
|
||||
proxyHost = host;
|
||||
proxyPort = port;
|
||||
|
||||
/*
|
||||
if (changed)
|
||||
{
|
||||
// TODO: background download
|
||||
}
|
||||
//*/
|
||||
}
|
||||
|
||||
public static NetworkHelper CreateSession(int timeout = Timeout, bool useCookie = true, bool waitsConnectivity = true, Dictionary<string, string>? additionalHeaders = null)
|
||||
{
|
||||
var helper = new NetworkHelper(timeout, useCookie, waitsConnectivity, additionalHeaders);
|
||||
return helper;
|
||||
}
|
||||
|
||||
public static NetworkHelper CreateImageSession(int timeout = ImageTimeout, bool useCookie = false, bool waitsConnectivity = false, Dictionary<string, string>? additionalHeaders = null)
|
||||
{
|
||||
return CreateSession(timeout, false, false, additionalHeaders);
|
||||
}
|
||||
|
||||
public string? Accept { get; set; }
|
||||
public string? Referrer { get; set; }
|
||||
|
||||
readonly object sync = new();
|
||||
|
||||
public partial Task<NetworkResult<string>> GetContentAsync(string url, StringHandler? process = null, CancellationToken token = default);
|
||||
}
|
||||
|
||||
public class HttpResponseException : Exception
|
||||
{
|
||||
public int StatusCode { get; }
|
||||
|
||||
public string Url { get; }
|
||||
|
||||
public HttpResponseException(int code, string url) : base($"HTTP response failed with status code {code}, url: {url}")
|
||||
{
|
||||
StatusCode = code;
|
||||
Url = url;
|
||||
}
|
||||
}
|
37
Network/NetworkResult.cs
Normal file
37
Network/NetworkResult.cs
Normal file
@ -0,0 +1,37 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class NetworkResult<T>
|
||||
{
|
||||
public T? Result { get; }
|
||||
|
||||
public Exception? Exception { get; }
|
||||
|
||||
public NetworkResult(T? obj, Exception? exception = null)
|
||||
{
|
||||
Result = obj;
|
||||
Exception = exception;
|
||||
}
|
||||
|
||||
public void ThrowIfException(bool throwIfDefault = false)
|
||||
{
|
||||
if (Exception != null)
|
||||
{
|
||||
throw Exception;
|
||||
}
|
||||
if (throwIfDefault && Equals(Result, default))
|
||||
{
|
||||
throw new Exception($"Network result is {{{default}}}");
|
||||
}
|
||||
}
|
||||
|
||||
public static implicit operator NetworkResult<T>(T? obj)
|
||||
{
|
||||
return new(obj);
|
||||
}
|
||||
|
||||
public static implicit operator NetworkResult<T>(Exception ex)
|
||||
{
|
||||
return new(default, ex);
|
||||
}
|
||||
}
|
||||
|
46
Network/NetworkTask.cs
Normal file
46
Network/NetworkTask.cs
Normal file
@ -0,0 +1,46 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public abstract partial class NetworkTask : IDisposable
|
||||
{
|
||||
public string Url { get; }
|
||||
|
||||
public CancellationToken Token { get; }
|
||||
|
||||
public bool IsCancelled => disposed || Token.IsCancellationRequested == true;
|
||||
|
||||
bool disposed;
|
||||
|
||||
public void SetCancelled()
|
||||
{
|
||||
OnCancelled();
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void SetException(Exception ex)
|
||||
{
|
||||
OnException(ex);
|
||||
Dispose();
|
||||
}
|
||||
|
||||
protected abstract void OnCancelled();
|
||||
|
||||
protected abstract void OnException(Exception ex);
|
||||
|
||||
private partial void DisposingInternal();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!disposed)
|
||||
{
|
||||
disposed = true;
|
||||
|
||||
DisposingInternal();
|
||||
}
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void StepHandler<T>(float progress, T? update);
|
||||
|
||||
public delegate string StringHandler(string original);
|
195
Network/Platforms/iOS/PlatformConnectivity.cs
Normal file
195
Network/Platforms/iOS/PlatformConnectivity.cs
Normal file
@ -0,0 +1,195 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Net;
|
||||
using CoreFoundation;
|
||||
using CoreTelephony;
|
||||
using SystemConfiguration;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
|
||||
partial class Connectivity
|
||||
{
|
||||
static readonly Lazy<CTCellularData> cellularData = new(() => new CTCellularData());
|
||||
|
||||
static ReachabilityListener? listener;
|
||||
|
||||
private static partial NetworkStatus PlatformGetStatus()
|
||||
{
|
||||
var restricted = cellularData.Value.RestrictedState == CTCellularDataRestrictedState.Restricted;
|
||||
|
||||
var internetStatus = InternetConnectionStatus();
|
||||
if (internetStatus == NetworkStatus.ReachableViaCarrierDataNetwork && !restricted)
|
||||
{
|
||||
return NetworkStatus.ReachableViaCarrierDataNetwork;
|
||||
}
|
||||
if (internetStatus == NetworkStatus.ReachableViaWiFiNetwork)
|
||||
{
|
||||
return NetworkStatus.ReachableViaWiFiNetwork;
|
||||
}
|
||||
|
||||
var remoteStatus = RemoteHostStatus();
|
||||
if (remoteStatus == NetworkStatus.ReachableViaCarrierDataNetwork && !restricted)
|
||||
{
|
||||
return NetworkStatus.ReachableViaCarrierDataNetwork;
|
||||
}
|
||||
if (remoteStatus == NetworkStatus.ReachableViaWiFiNetwork)
|
||||
{
|
||||
return NetworkStatus.ReachableViaWiFiNetwork;
|
||||
}
|
||||
|
||||
return NetworkStatus.NotReachable;
|
||||
}
|
||||
|
||||
static NetworkStatus RemoteHostStatus()
|
||||
{
|
||||
using var remote = new NetworkReachability(hostName);
|
||||
|
||||
if (!remote.TryGetFlags(out var flags))
|
||||
{
|
||||
return NetworkStatus.NotReachable;
|
||||
}
|
||||
|
||||
if (!IsReachableWithoutRequiringConnection(flags))
|
||||
{
|
||||
return NetworkStatus.NotReachable;
|
||||
}
|
||||
|
||||
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
|
||||
{
|
||||
return NetworkStatus.ReachableViaCarrierDataNetwork;
|
||||
}
|
||||
|
||||
return NetworkStatus.ReachableViaWiFiNetwork;
|
||||
|
||||
}
|
||||
|
||||
static bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags)
|
||||
{
|
||||
// Is it reachable with the current network configuration?
|
||||
var isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
|
||||
|
||||
// Do we need a connection to reach it?
|
||||
var noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0;
|
||||
|
||||
// Since the network stack will automatically try to get the WAN up,
|
||||
// probe that
|
||||
if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
|
||||
{
|
||||
noConnectionRequired = true;
|
||||
}
|
||||
|
||||
return isReachable && noConnectionRequired;
|
||||
}
|
||||
|
||||
static NetworkStatus InternetConnectionStatus()
|
||||
{
|
||||
var status = NetworkStatus.NotReachable;
|
||||
|
||||
var defaultNetworkAvailable = IsNetworkAvailable(out var flags);
|
||||
|
||||
// If the connection is reachable and no connection is required, then assume it's WiFi
|
||||
if (defaultNetworkAvailable)
|
||||
{
|
||||
status = NetworkStatus.ReachableViaWiFiNetwork;
|
||||
}
|
||||
else if ((flags & NetworkReachabilityFlags.IsWWAN) != 0)
|
||||
{
|
||||
// If it's a WWAN connection..
|
||||
status = NetworkStatus.ReachableViaCarrierDataNetwork;
|
||||
}
|
||||
|
||||
// If the connection is on-demand or on-traffic and no user intervention
|
||||
// is required, then assume WiFi.
|
||||
if (((flags & NetworkReachabilityFlags.ConnectionOnDemand) != 0 || (flags & NetworkReachabilityFlags.ConnectionOnTraffic) != 0) &&
|
||||
(flags & NetworkReachabilityFlags.InterventionRequired) == 0)
|
||||
{
|
||||
status = NetworkStatus.ReachableViaWiFiNetwork;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
private static bool IsNetworkAvailable(out NetworkReachabilityFlags flags)
|
||||
{
|
||||
var ip = new IPAddress(0);
|
||||
using var route = new NetworkReachability(ip);
|
||||
|
||||
if (!route.TryGetFlags(out flags))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return IsReachableWithoutRequiringConnection(flags);
|
||||
}
|
||||
|
||||
private static partial void StartListeners()
|
||||
{
|
||||
StopListeners();
|
||||
|
||||
listener = new ReachabilityListener();
|
||||
listener.ReachabilityChanged += OnConnectivityChanged;
|
||||
}
|
||||
|
||||
private static partial void StopListeners()
|
||||
{
|
||||
if (listener == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
listener.ReachabilityChanged -= OnConnectivityChanged;
|
||||
listener.Dispose();
|
||||
listener = null;
|
||||
}
|
||||
|
||||
class ReachabilityListener : IDisposable
|
||||
{
|
||||
NetworkReachability? routeReachability;
|
||||
NetworkReachability? remoteReachability;
|
||||
|
||||
public event Action? ReachabilityChanged;
|
||||
|
||||
public ReachabilityListener()
|
||||
{
|
||||
var ip = new IPAddress(0);
|
||||
routeReachability = new NetworkReachability(ip);
|
||||
routeReachability.SetNotification(OnChange);
|
||||
routeReachability.Schedule(CFRunLoop.Main, CFRunLoop.ModeDefault);
|
||||
|
||||
remoteReachability = new NetworkReachability(hostName);
|
||||
|
||||
// Need to probe before we queue, or we wont get any meaningful values
|
||||
// this only happens when you create NetworkReachability from a hostname
|
||||
remoteReachability.TryGetFlags(out var flags);
|
||||
|
||||
remoteReachability.SetNotification(OnChange);
|
||||
remoteReachability.Schedule(CFRunLoop.Main, CFRunLoop.ModeDefault);
|
||||
|
||||
cellularData.Value.RestrictionDidUpdateNotifier = new(OnRestrictedStateChanged);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
routeReachability?.Dispose();
|
||||
routeReachability = null;
|
||||
remoteReachability?.Dispose();
|
||||
remoteReachability = null;
|
||||
|
||||
cellularData.Value.RestrictionDidUpdateNotifier = null;
|
||||
}
|
||||
|
||||
void OnRestrictedStateChanged(CTCellularDataRestrictedState state)
|
||||
{
|
||||
ReachabilityChanged?.Invoke();
|
||||
}
|
||||
|
||||
async void OnChange(NetworkReachabilityFlags flags)
|
||||
{
|
||||
// Add in artifical delay so the connection status has time to change
|
||||
// else it will return true no matter what.
|
||||
await Task.Delay(100);
|
||||
|
||||
ReachabilityChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
237
Network/Platforms/iOS/PlatformNetworkHelper.cs
Normal file
237
Network/Platforms/iOS/PlatformNetworkHelper.cs
Normal file
@ -0,0 +1,237 @@
|
||||
using CoreGraphics;
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
partial class NetworkHelper : NSObject, INSUrlSessionDataDelegate
|
||||
{
|
||||
static bool IsResponseSuccess(NSHttpUrlResponse response)
|
||||
{
|
||||
return (int)response.StatusCode is >= 200 and < 300;
|
||||
}
|
||||
|
||||
NSUrlSession? urlSession;
|
||||
|
||||
readonly Dictionary<NSUrlSessionTask, NetworkTask> tasks = new();
|
||||
|
||||
private NetworkHelper(int timeout, bool useCookie, bool waitsConnectivity = true, Dictionary<string, string>? additionalHeaders = null)
|
||||
{
|
||||
var configuration = NSUrlSessionConfiguration.EphemeralSessionConfiguration;
|
||||
configuration.WaitsForConnectivity = waitsConnectivity;
|
||||
configuration.RequestCachePolicy = NSUrlRequestCachePolicy.UseProtocolCachePolicy;
|
||||
configuration.TimeoutIntervalForRequest = timeout;
|
||||
if (additionalHeaders != null)
|
||||
{
|
||||
configuration.HttpAdditionalHeaders = NSDictionary.FromObjectsAndKeys(
|
||||
additionalHeaders.Values.Select(v => FromObject(v)).ToArray(),
|
||||
additionalHeaders.Keys.Select(k => FromObject(k)).ToArray());
|
||||
}
|
||||
if (useCookie)
|
||||
{
|
||||
configuration.HttpCookieStorage = NSHttpCookieStorage.SharedStorage;
|
||||
}
|
||||
else
|
||||
{
|
||||
configuration.HttpShouldSetCookies = false;
|
||||
}
|
||||
if (proxyHost != null && proxyPort != null)
|
||||
{
|
||||
configuration.StrongConnectionProxyDictionary = new ProxyConfigurationDictionary
|
||||
{
|
||||
HttpEnable = true,
|
||||
HttpsProxyHost = proxyHost,
|
||||
HttpsProxyPort = proxyPort,
|
||||
HttpProxyHost = proxyHost,
|
||||
HttpProxyPort = proxyPort
|
||||
};
|
||||
}
|
||||
urlSession = NSUrlSession.FromConfiguration(configuration, this, null);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
lock (sync)
|
||||
{
|
||||
foreach (var task in tasks.Values)
|
||||
{
|
||||
task.Dispose();
|
||||
}
|
||||
}
|
||||
if (urlSession != null)
|
||||
{
|
||||
urlSession.FinishTasksAndInvalidate();
|
||||
urlSession.Dispose();
|
||||
urlSession = null;
|
||||
}
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
public partial Task<NetworkResult<string>> GetContentAsync(string url, StringHandler? process, CancellationToken token)
|
||||
{
|
||||
var uri = NSUrl.FromString(url) ?? throw new ArgumentNullException(nameof(url));
|
||||
var request = new NSMutableUrlRequest(uri)
|
||||
{
|
||||
[AcceptHeader] = Accept ?? AcceptHttp,
|
||||
[ReferrerHeader] = Referrer
|
||||
};
|
||||
|
||||
var task = (urlSession?.CreateDataTask(request)) ?? throw new NullReferenceException("Url request is null");
|
||||
var taskSource = new TaskCompletionSource<NetworkResult<string>>();
|
||||
var stringTask = new StringTask(url, taskSource, token)
|
||||
{
|
||||
Process = process
|
||||
};
|
||||
lock (sync)
|
||||
{
|
||||
tasks.Add(task, stringTask);
|
||||
}
|
||||
task.Resume();
|
||||
return taskSource.Task;
|
||||
}
|
||||
|
||||
public Task<NetworkResult<bool>> GetImageAsync(string url, string filePath, StepHandler<CGImage>? step = null, CancellationToken token = default)
|
||||
{
|
||||
var uri = NSUrl.FromString(url) ?? throw new ArgumentNullException(nameof(url));
|
||||
var request = new NSMutableUrlRequest(uri)
|
||||
{
|
||||
[AcceptHeader] = Accept ?? AcceptImage,
|
||||
[ReferrerHeader] = Referrer
|
||||
};
|
||||
|
||||
var task = (urlSession?.CreateDataTask(request)) ?? throw new NullReferenceException("Url request is null");
|
||||
var taskSource = new TaskCompletionSource<NetworkResult<bool>>();
|
||||
var imageTask = new ImageTask(url, filePath, step != null, taskSource, token)
|
||||
{
|
||||
Step = step
|
||||
};
|
||||
lock (sync)
|
||||
{
|
||||
tasks.Add(task, imageTask);
|
||||
}
|
||||
task.Resume();
|
||||
return taskSource.Task;
|
||||
}
|
||||
|
||||
public Task<NetworkResult<string>> PostAsync(string url, Action<NSMutableUrlRequest>? prepare = null, CancellationToken token = default)
|
||||
{
|
||||
var uri = NSUrl.FromString(url) ?? throw new ArgumentNullException(nameof(url));
|
||||
var request = new NSMutableUrlRequest(uri)
|
||||
{
|
||||
HttpMethod = "POST",
|
||||
[AcceptHeader] = Accept ?? AcceptImage,
|
||||
[ReferrerHeader] = Referrer
|
||||
};
|
||||
prepare?.Invoke(request);
|
||||
|
||||
var task = (urlSession?.CreateDataTask(request)) ?? throw new NullReferenceException("Url request is null");
|
||||
var taskSource = new TaskCompletionSource<NetworkResult<string>>();
|
||||
var stringTask = new StringTask(url, taskSource, token);
|
||||
lock (sync)
|
||||
{
|
||||
tasks.Add(task, stringTask);
|
||||
}
|
||||
task.Resume();
|
||||
return taskSource.Task;
|
||||
}
|
||||
|
||||
bool TryGetTask(NSUrlSessionTask task, out NetworkTask? networkTask)
|
||||
{
|
||||
bool got;
|
||||
lock (sync)
|
||||
{
|
||||
got = tasks.TryGetValue(task, out networkTask);
|
||||
}
|
||||
return got;
|
||||
}
|
||||
|
||||
[Export("URLSession:dataTask:didReceiveResponse:completionHandler:")]
|
||||
public void DidReceiveResponse(NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
|
||||
{
|
||||
if (!TryGetTask(dataTask, out NetworkTask? networkTask) || networkTask == null)
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Cancel);
|
||||
return;
|
||||
}
|
||||
if (networkTask.IsCancelled)
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Cancel);
|
||||
networkTask.SetException(new TaskCanceledException($"Cancelled on response received: {dataTask.CurrentRequest?.Url}", null, networkTask.Token));
|
||||
return;
|
||||
}
|
||||
if (response is NSHttpUrlResponse httpResponse)
|
||||
{
|
||||
if (IsResponseSuccess(httpResponse))
|
||||
{
|
||||
if (networkTask.OnResponsed(httpResponse))
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Allow);
|
||||
}
|
||||
else
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Cancel);
|
||||
networkTask.SetException(new TaskCanceledException($"Cancelled on task responsed: {dataTask.CurrentRequest?.Url}", null, networkTask.Token));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Cancel);
|
||||
networkTask.SetException(new HttpResponseException((int)httpResponse.StatusCode, httpResponse.Url.AbsoluteString ?? networkTask.Url));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
completionHandler(NSUrlSessionResponseDisposition.Cancel);
|
||||
networkTask.SetException(new InvalidDataException($"Response is not instance of <NSHttpUrlResponse> but <{response?.GetType()}>"));
|
||||
}
|
||||
}
|
||||
|
||||
[Export("URLSession:dataTask:didReceiveData:")]
|
||||
public void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
|
||||
{
|
||||
if (!TryGetTask(dataTask, out NetworkTask? networkTask) || networkTask == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (networkTask.IsCancelled)
|
||||
{
|
||||
networkTask.SetException(new TaskCanceledException($"Cancelled on data received: {dataTask.CurrentRequest?.Url}", null, networkTask.Token));
|
||||
return;
|
||||
}
|
||||
if (networkTask.Data == null)
|
||||
{
|
||||
networkTask.SetException(new InvalidDataException("<NSMutableData> is null"));
|
||||
return;
|
||||
}
|
||||
networkTask.Data.AppendData(data);
|
||||
networkTask.OnReceived((int)data.Length);
|
||||
}
|
||||
|
||||
[Export("URLSession:task:didCompleteWithError:")]
|
||||
public void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
|
||||
{
|
||||
if (!TryGetTask(task, out NetworkTask? networkTask) || networkTask == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (networkTask.IsCancelled)
|
||||
{
|
||||
networkTask.SetException(new TaskCanceledException($"Cancelled on completed: {task.CurrentRequest?.Url}", null, networkTask.Token));
|
||||
return;
|
||||
}
|
||||
if (networkTask.Data == null)
|
||||
{
|
||||
networkTask.SetException(new InvalidDataException("<NSMutableData> is null"));
|
||||
return;
|
||||
}
|
||||
if (error != null)
|
||||
{
|
||||
networkTask.SetException(new Exception(error.ToString()));
|
||||
return;
|
||||
}
|
||||
networkTask.SetCompleted(task.Response as NSHttpUrlResponse);
|
||||
}
|
||||
}
|
||||
|
49
Network/Platforms/iOS/PlatformNetworkTask.cs
Normal file
49
Network/Platforms/iOS/PlatformNetworkTask.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
partial class NetworkTask
|
||||
{
|
||||
public NSMutableData? Data => data;
|
||||
|
||||
NSMutableData? data;
|
||||
|
||||
public NetworkTask(string url, CancellationToken token)
|
||||
{
|
||||
Url = url;
|
||||
Token = token;
|
||||
|
||||
data = new NSMutableData();
|
||||
}
|
||||
|
||||
public void SetCompleted(NSHttpUrlResponse? response)
|
||||
{
|
||||
OnCompleted(response);
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public virtual bool OnResponsed(NSHttpUrlResponse response)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void OnReceived(int length)
|
||||
{
|
||||
}
|
||||
|
||||
protected abstract void OnCompleted(NSHttpUrlResponse? response);
|
||||
|
||||
private partial void DisposingInternal()
|
||||
{
|
||||
Disposing();
|
||||
if (data != null)
|
||||
{
|
||||
data.Dispose();
|
||||
data = null;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Disposing()
|
||||
{
|
||||
}
|
||||
}
|
88
Network/Platforms/iOS/Tasks/ContentTask.cs
Normal file
88
Network/Platforms/iOS/Tasks/ContentTask.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public abstract class ContentTask<T, TResult> : NetworkTask
|
||||
{
|
||||
public StepHandler<T>? Step { get; set; }
|
||||
|
||||
protected virtual bool AllowDefaultResult => false;
|
||||
|
||||
protected TaskCompletionSource<NetworkResult<TResult>>? taskSource;
|
||||
|
||||
protected long expectedContentLength;
|
||||
|
||||
protected ContentTask(string url, TaskCompletionSource<NetworkResult<TResult>> source, CancellationToken token) : base(url, token)
|
||||
{
|
||||
taskSource = source ?? throw new ArgumentNullException(nameof(source));
|
||||
}
|
||||
|
||||
protected override void OnCancelled()
|
||||
{
|
||||
taskSource?.TrySetCanceled();
|
||||
}
|
||||
|
||||
protected override void OnException(Exception ex)
|
||||
{
|
||||
taskSource?.TrySetResult(ex);
|
||||
}
|
||||
|
||||
public override bool OnResponsed(NSHttpUrlResponse response)
|
||||
{
|
||||
expectedContentLength = response.ExpectedContentLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual T? Update(float progress)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public override void OnReceived(int length)
|
||||
{
|
||||
if (Step != null)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
Step(0f, default);
|
||||
}
|
||||
else
|
||||
{
|
||||
float progress = (float)Data.Length / expectedContentLength;
|
||||
var update = Update(progress);
|
||||
Step(progress, update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual TResult? Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void OnCompleted(NSHttpUrlResponse? response)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = Completed(response);
|
||||
if (result == null && !AllowDefaultResult)
|
||||
{
|
||||
throw new NullReferenceException("Result is null");
|
||||
}
|
||||
taskSource?.TrySetResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Disposing()
|
||||
{
|
||||
if (taskSource?.Task.IsCanceled == false)
|
||||
{
|
||||
taskSource.TrySetCanceled();
|
||||
taskSource = null;
|
||||
}
|
||||
}
|
||||
}
|
30
Network/Platforms/iOS/Tasks/DownloadTask.cs
Normal file
30
Network/Platforms/iOS/Tasks/DownloadTask.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class DownloadTask<T> : ContentTask<T, bool>
|
||||
{
|
||||
public string FilePath { get; }
|
||||
|
||||
protected override bool AllowDefaultResult => true;
|
||||
|
||||
public DownloadTask(string url, string filePath, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token) : base(url, source, token)
|
||||
{
|
||||
FilePath = filePath;
|
||||
}
|
||||
|
||||
protected override bool Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
throw new NullReferenceException("Data is null");
|
||||
}
|
||||
var result = Data.Save(FilePath, NSDataWritingOptions.Atomic, out NSError? error);
|
||||
if (error != null)
|
||||
{
|
||||
throw new Exception(error?.ToString() ?? "UUnknown error while saving file");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
14
Network/Platforms/iOS/Tasks/FileTask.cs
Normal file
14
Network/Platforms/iOS/Tasks/FileTask.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class FileTask : DownloadTask<float>
|
||||
{
|
||||
public FileTask(string url, string filePath, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token) : base(url, filePath, source, token)
|
||||
{
|
||||
}
|
||||
|
||||
protected override float Update(float progress)
|
||||
{
|
||||
return progress;
|
||||
}
|
||||
}
|
||||
|
38
Network/Platforms/iOS/Tasks/ImageTask.cs
Normal file
38
Network/Platforms/iOS/Tasks/ImageTask.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using CoreGraphics;
|
||||
using ImageIO;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class ImageTask : DownloadTask<CGImage>
|
||||
{
|
||||
CGImageSource? imageSource;
|
||||
|
||||
public ImageTask(string url, string filePath, bool createImage, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token = default) : base(url, filePath, source, token)
|
||||
{
|
||||
if (createImage)
|
||||
{
|
||||
imageSource = CGImageSource.CreateIncremental(null);
|
||||
}
|
||||
}
|
||||
|
||||
protected override CGImage? Update(float progress)
|
||||
{
|
||||
if (imageSource == null || Data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
imageSource.UpdateData(Data, Data.Length >= (uint)expectedContentLength);
|
||||
return imageSource.CreateImage(0, null!);
|
||||
}
|
||||
|
||||
protected override void Disposing()
|
||||
{
|
||||
if (imageSource != null)
|
||||
{
|
||||
imageSource.Dispose();
|
||||
imageSource = null;
|
||||
}
|
||||
base.Disposing();
|
||||
}
|
||||
}
|
||||
|
26
Network/Platforms/iOS/Tasks/StringTask.cs
Normal file
26
Network/Platforms/iOS/Tasks/StringTask.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class StringTask : ContentTask<string, string>
|
||||
{
|
||||
public StringHandler? Process { get; set; }
|
||||
|
||||
public StringTask(string url, TaskCompletionSource<NetworkResult<string>> source, CancellationToken token) : base(url, source, token)
|
||||
{
|
||||
}
|
||||
|
||||
protected override string? Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string s = NSString.FromData(Data, NSStringEncoding.UTF8);
|
||||
if (Process == null)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
return Process(s);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user