更改了文件结构
This commit is contained in:
174
Src/Unity/Scripts/Network/NetworkRequest.cs
Normal file
174
Src/Unity/Scripts/Network/NetworkRequest.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
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
|
||||
{
|
||||
[Obsolete]
|
||||
public class NetworkRequest : BITBehavior, IAction
|
||||
{
|
||||
[SubclassSelector, SerializeReference] public References _url;
|
||||
[SerializeField] private Provider onException;
|
||||
[SerializeField] private Provider provider;
|
||||
[SerializeField] private Provider onPing;
|
||||
[SerializeField] private IntervalUpdate updater = new();
|
||||
[SubclassSelector, SerializeReference] public WebProvider webProvider;
|
||||
[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;
|
||||
public LimitTimes limit = new(64);
|
||||
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()
|
||||
{
|
||||
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
timer.Elapsed += (x, y) => OnUpdate();
|
||||
|
||||
isActive.AddListener(Set);
|
||||
isActive.AddDisableElements(this);
|
||||
}
|
||||
private void OnEnable()
|
||||
{
|
||||
isActive.AddElement(this);
|
||||
}
|
||||
private void Start()
|
||||
{
|
||||
isActive.RemoveDisableElements(this);
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
isActive.RemoveElement(this);
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
timer.Dispose();
|
||||
}
|
||||
private void SetIntervalUpdateActive(bool active)
|
||||
{
|
||||
isActive.SetDisableElements(internalTime, active is false);
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
deltaTick = completionRate;
|
||||
}
|
||||
private void Set(bool active)
|
||||
{
|
||||
if (timer.Enabled != active)
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
var interval = TimeSpan.FromSeconds(updater.Interval).TotalMilliseconds;
|
||||
timer.Interval = interval;
|
||||
timer.AutoReset = true;
|
||||
timer.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
timer.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void OnUpdate()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (limit)
|
||||
{
|
||||
Excute(true);
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
BIT4Log.Warning(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public void Execute()
|
||||
{
|
||||
Excute(false);
|
||||
}
|
||||
public async void Excute(bool release = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
}
|
||||
catch (AppIsNotPlayingException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
NetworkCore.Add(_url);
|
||||
requestCount++;
|
||||
internalTime += BITApp.Time.DeltaTime;
|
||||
completionRate.Get();
|
||||
var time = BITApp.Time.TimeAsDouble;
|
||||
try
|
||||
{
|
||||
var result = await webProvider.GetAsync(_url, cancellationToken);
|
||||
responsedCount++;
|
||||
provider?.Set(result);
|
||||
completionRate.Release();
|
||||
requestTime = BITApp.Time.TimeAsDouble - time;
|
||||
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();
|
||||
var button = new Button(agent.Execute);
|
||||
button.text = "Request";
|
||||
root.Add(button);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user