initial commit, iOS network native library

This commit is contained in:
tsanie 2023-08-24 14:30:14 +08:00
commit e49ee1551d
15 changed files with 1329 additions and 0 deletions

405
.gitignore vendored Normal file
View File

@ -0,0 +1,405 @@
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/

25
Network.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1706.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Network", "Network\Network.csproj", "{1D93202E-4401-4C5A-BB91-A3D7C657FB7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1D93202E-4401-4C5A-BB91-A3D7C657FB7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D93202E-4401-4C5A-BB91-A3D7C657FB7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D93202E-4401-4C5A-BB91-A3D7C657FB7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D93202E-4401-4C5A-BB91-A3D7C657FB7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C0290D8F-BF20-4987-86C6-81B38710F2DA}
EndGlobalSection
EndGlobal

56
Network/Connectivity.cs Normal file
View 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
View 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
View 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
View 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
View 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);

View 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();
}
}
}

View 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);
}
}

View 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()
{
}
}

View 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;
}
}
}

View 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;
}
}

View 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;
}
}

View 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();
}
}

View 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);
}
}