BITFALL/Assets/BITKit/Unity/Scripts/Network/NetworkRequest.cs

174 lines
5.3 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System;
using System.Text;
using System.Threading;
using System.Net.Http;
using UnityEngine.Events;
using UnityEngine.Networking;
using UnityEngine.UIElements;
namespace BITKit.HttpNet
{
2023-08-27 02:58:19 +08:00
[Obsolete]
2023-06-08 14:09:50 +08:00
public class NetworkRequest : BITBehavior, IAction
{
[SubclassSelector, SerializeReference] public References _url;
2023-08-27 02:58:19 +08:00
[SerializeField] private Provider onException;
[SerializeField] private Provider provider;
[SerializeField] private Provider onPing;
[SerializeField] private IntervalUpdate updater = new();
2023-06-08 14:09:50 +08:00
[SubclassSelector, SerializeReference] public WebProvider webProvider;
2023-08-27 02:58:19 +08:00
[SerializeField] private Counter counter = new();
[SerializeField] private string result;
[SerializeField] private int requestCount;
[SerializeField] private int responsedCount;
[SerializeField] private float deltaTick;
[SerializeField] private double requestTime;
2023-06-08 14:09:50 +08:00
public LimitTimes limit = new(64);
2023-08-27 02:58:19 +08:00
protected readonly System.Timers.Timer timer = new();
private readonly CompletionRate completionRate = new();
private readonly ValidHandle isActive = new();
private float internalTime;
protected CancellationToken cancellationToken;
private void Awake()
2023-06-08 14:09:50 +08:00
{
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
timer.Elapsed += (x, y) => OnUpdate();
isActive.AddListener(Set);
isActive.AddDisableElements(this);
}
2023-08-27 02:58:19 +08:00
private void OnEnable()
2023-06-08 14:09:50 +08:00
{
isActive.AddElement(this);
}
2023-08-27 02:58:19 +08:00
private void Start()
2023-06-08 14:09:50 +08:00
{
isActive.RemoveDisableElements(this);
}
2023-08-27 02:58:19 +08:00
private void OnDisable()
2023-06-08 14:09:50 +08:00
{
isActive.RemoveElement(this);
}
2023-08-27 02:58:19 +08:00
private void OnDestroy()
2023-06-08 14:09:50 +08:00
{
timer.Dispose();
}
2023-08-27 02:58:19 +08:00
private void SetIntervalUpdateActive(bool active)
2023-06-08 14:09:50 +08:00
{
isActive.SetDisableElements(internalTime, active is false);
}
2023-08-27 02:58:19 +08:00
private void Update()
2023-06-08 14:09:50 +08:00
{
deltaTick = completionRate;
}
2023-08-27 02:58:19 +08:00
private void Set(bool active)
2023-06-08 14:09:50 +08:00
{
if (timer.Enabled != active)
{
if (active)
{
2023-08-12 01:43:24 +08:00
var interval = TimeSpan.FromSeconds(updater.Interval).TotalMilliseconds;
2023-06-08 14:09:50 +08:00
timer.Interval = interval;
timer.AutoReset = true;
timer.Start();
}
else
{
timer.Stop();
}
}
}
2023-08-27 02:58:19 +08:00
private void OnUpdate()
2023-06-08 14:09:50 +08:00
{
try
{
2023-08-12 01:43:24 +08:00
if (limit)
2023-06-08 14:09:50 +08:00
{
Excute(true);
}
}
catch (System.Exception e)
{
2023-08-23 01:59:40 +08:00
BIT4Log.Warning(e);
2023-06-08 14:09:50 +08:00
throw;
}
}
2023-08-12 01:43:24 +08:00
public void Execute()
2023-06-08 14:09:50 +08:00
{
Excute(false);
}
public async void Excute(bool release = false)
{
try
{
BITAppForUnity.ThrowIfNotPlaying();
}
catch (AppIsNotPlayingException)
{
}
catch (OperationCanceledException)
{
}
NetworkCore.Add(_url);
requestCount++;
2023-08-12 01:43:24 +08:00
internalTime += BITApp.Time.DeltaTime;
2023-06-08 14:09:50 +08:00
completionRate.Get();
2023-08-12 01:43:24 +08:00
var time = BITApp.Time.TimeAsDouble;
2023-06-08 14:09:50 +08:00
try
{
var result = await webProvider.GetAsync(_url, cancellationToken);
responsedCount++;
provider?.Set(result);
completionRate.Release();
2023-08-12 01:43:24 +08:00
requestTime = BITApp.Time.TimeAsDouble - time;
2023-06-08 14:09:50 +08:00
if (requestTime is not 0)
onPing?.Set(((int)(requestTime * 1000)).ToString());
this.result = result;
}
catch (System.Exception e)
{
onException?.Set(e);
switch (e)
{
case TimeoutException:
case OperationCanceledException:
case System.Net.WebException:
case System.Net.Http.HttpRequestException:
break;
case InvalidOperationException operationException:
Debug.LogWarning(_url.Get());
goto default;
default:
Debug.LogException(e, this);
break;
}
}
if (release)
{
limit.Release();
}
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(NetworkRequest))]
public class Inspector : BITInspector<NetworkRequest>
{
public override VisualElement CreateInspectorGUI()
{
var root = base.CreateInspectorGUI();
2023-08-12 01:43:24 +08:00
var button = new Button(agent.Execute);
2023-06-08 14:09:50 +08:00
button.text = "Request";
root.Add(button);
return root;
}
}
#endif
}