尝试TCP纯原生平替HTTP异步下载和接口请求,支持PSVita

This commit is contained in:
sin365 2024-12-16 23:50:00 +08:00
parent 30dd2ee9aa
commit 6c6761c4e3
9 changed files with 1362 additions and 24 deletions

View File

@ -1,4 +1,4 @@
using AxibugEmuOnline.Client.Manager;
using AxibugEmuOnline.Client.Manager;
using AxibugEmuOnline.Client.Network;
using System.Collections;
using System.IO;
@ -116,14 +116,21 @@ namespace AxibugEmuOnline.Client.ClientCore
yield break;
}
UnityWebRequest request = UnityWebRequest.Get($"{App.httpAPI.WebSiteApi}/CheckStandInfo?platform={platform}&version={Application.version}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{App.httpAPI.WebSiteApi}/CheckStandInfo?platform={platform}&version={Application.version}");
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
yield break;
Resp_CheckStandInfo resp = JsonUtility.FromJson<Resp_CheckStandInfo>(request.downloadHandler.text);
/*UnityWebRequest request = UnityWebRequest.Get($"{App.httpAPI.WebSiteApi}/CheckStandInfo?platform={platform}&version={Application.version}");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
yield break;
App.log.Debug($"ApiResp => {request.downloadHandler.text}");
Resp_CheckStandInfo resp = JsonUtility.FromJson<Resp_CheckStandInfo>(request.downloadHandler.text);
Resp_CheckStandInfo resp = JsonUtility.FromJson<Resp_CheckStandInfo>(request.downloadHandler.text);*/
//需要更新
if (resp.needUpdateClient == 1)
{

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 5034475933564b54db148790f0ae2501
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 5a498f74133ebfa4fa238a33a1c4c0c3

View File

@ -0,0 +1,101 @@
using System.Collections;
using UnityEngine;
using static AxiHttp;
public static class AxiHttpProxy
{
public static SendWebRequestProxy Get(string url)
{
return new SendWebRequestProxy(AxiRequestAsync(url));
}
public static SendDownLoadProxy GetDownLoad(string url)
{
return new SendDownLoadProxy(AxiDownloadAsync(url));
}
public class SendWebRequestProxy
{
public WaitAxiRequest SendWebRequest;
public AxiRespInfo downloadHandler => SendWebRequest.mReqAsync;
public SendWebRequestProxy(WaitAxiRequest request)
{
SendWebRequest = request;
}
~SendWebRequestProxy()
{
SendWebRequest = null;
}
}
public class SendDownLoadProxy
{
public AxiRespInfo downloadHandler;
public SendDownLoadProxy(AxiRespInfo re)
{
downloadHandler = re;
}
~SendDownLoadProxy()
{
downloadHandler = null;
}
}
public static void ShowAxiHttpDebugInfo(AxiRespInfo resp)
{
Debug.Log($"");
Debug.Log($"==== request ====");
Debug.Log($"url =>{resp.url}");
Debug.Log($"Raw =>{resp.requestRaw}");
Debug.Log($"code =>{resp.code}");
Debug.Log($"respInfo.bTimeOut =>{resp.bTimeOut}");
Debug.Log($"");
Debug.Log($"==== response ====");
Debug.Log($"==== header ====");
Debug.Log($"header =>{resp.header}");
Debug.Log($"HeadersCount =>{resp.headers.Count}");
foreach (var kv in resp.headers)
Debug.Log($"{kv.Key} => {kv.Value}");
Debug.Log($"");
Debug.Log($"==== body ====");
Debug.Log($"body_text =>{resp.body}");
Debug.Log($"body_text.Length =>{resp.body.Length}");
Debug.Log($"bodyRaw.Length =>{resp.bodyRaw?.Length}");
Debug.Log($"");
Debug.Log($"==== download ====");
Debug.Log($"downloadMode =>{resp.downloadMode}");
Debug.Log($"respInfo.fileName =>{resp.fileName}");
Debug.Log($"respInfo.NeedloadedLenght =>{resp.NeedloadedLenght}");
Debug.Log($"respInfo.loadedLenght =>{resp.loadedLenght}");
//if (resp.downloadMode == AxiDownLoadMode.DownLoadBytes)
//{
// if (resp.bTimeOut)
// {
// Debug.Log($"DownLoad Timeout!");
// return;
// }
// string downloadSavePath;
// if (Application.platform == RuntimePlatform.PSP2)
// {
// downloadSavePath = dataAxibugPath + "/" + resp.fileName;
// }
// else
// {
// downloadSavePath = persistentDataPath + "/" + resp.fileName;
// }
// try
// {
// File.WriteAllBytes(downloadSavePath, resp.bodyRaw);
// Debug.Log($"DownLoad OK");
// }
// catch (Exception ex)
// {
// Debug.Log($"DownLoad Err {ex.ToString()}");
// }
//}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ec8e2e405992b234eb08896da0f85317

View File

@ -18,7 +18,7 @@ namespace AxibugEmuOnline.Client
GetCacheData(url, TextureCacheDirPath, callback);
}
/// <summary> 移除文件缓存 </summary>
/// <summary> 移除文件缓存 </summary>
public void ClearCaches()
{
if (Directory.Exists(CacheDirPath))
@ -27,6 +27,25 @@ namespace AxibugEmuOnline.Client
IEnumerator DownloadFromURL(string url, string path, Action<byte[]> callback)
{
AxiHttpProxy.SendDownLoadProxy request = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{url}");
while (!request.downloadHandler.isDone)
{
yield return null;
//Debug.Log($"下载进度:{respInfo.DownLoadPr} ->{respInfo.loadedLenght}/{respInfo.NeedloadedLenght}");
}
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
if (request.downloadHandler.Err != null)
{
Directory.CreateDirectory(path);
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);
callback.Invoke(request.downloadHandler.data);
}
else
callback.Invoke(null);
/*
var request = UnityWebRequest.Get($"{App.httpAPI.WebHost}/{url}");
yield return request.SendWebRequest();
@ -38,6 +57,7 @@ namespace AxibugEmuOnline.Client
}
else
callback.Invoke(null);
*/
}
private Dictionary<string, object> cachesInMemory = new Dictionary<string, object>();

View File

@ -1,4 +1,4 @@
using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections;
using System.Collections.Generic;
@ -26,6 +26,16 @@ namespace AxibugEmuOnline.Client
}
private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<Resp_GameList> callback)
{
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
{
callback.Invoke(null);
yield break;
}
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
yield return request.SendWebRequest();
@ -33,13 +43,21 @@ namespace AxibugEmuOnline.Client
{
callback.Invoke(null);
yield break;
}
}*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback)
{
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
{
callback.Invoke(null);
yield break;
}
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest();
@ -48,13 +66,23 @@ namespace AxibugEmuOnline.Client
callback.Invoke(null);
yield break;
}
*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
{
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
{
callback.Invoke(null);
yield break;
}
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
yield return request.SendWebRequest();
@ -62,7 +90,7 @@ namespace AxibugEmuOnline.Client
{
callback.Invoke(null);
yield break;
}
}*/
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);

View File

@ -1,8 +1,9 @@
using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.ClientCore;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace AxibugEmuOnline.Client
@ -12,7 +13,8 @@ namespace AxibugEmuOnline.Client
private HttpAPI.Resp_RomInfo webData;
private bool hasLocalFile;
private EnumPlatform platform;
private UnityWebRequest downloadRequest;
//private UnityWebRequest downloadRequest;
private AxiHttpProxy.SendDownLoadProxy downloadRequest;
public bool IsUserRom { get; private set; }
@ -24,9 +26,14 @@ namespace AxibugEmuOnline.Client
/// <summary> 指示该Rom文件是否已下载完毕 </summary>
public bool RomReady => hasLocalFile;
///// <summary> 指示是否正在下载Rom文件 </summary>
//public bool IsDownloading => downloadRequest != null && downloadRequest.result == UnityWebRequest.Result.InProgress;
//public float Progress => IsDownloading ? downloadRequest.downloadProgress : 0;
/// <summary> 指示是否正在下载Rom文件 </summary>
public bool IsDownloading => downloadRequest != null && downloadRequest.result == UnityWebRequest.Result.InProgress;
public float Progress => IsDownloading ? downloadRequest.downloadProgress : 0;
public bool IsDownloading => downloadRequest != null && !downloadRequest.downloadHandler.isDone;
public float Progress => IsDownloading ? downloadRequest.downloadHandler.DownLoadPr : 0;
public EnumPlatform Platform => platform;
/// <summary> 指示该Rom信息是否已填充 </summary>
@ -114,13 +121,19 @@ namespace AxibugEmuOnline.Client
private IEnumerator DownloadRemoteRom(Action<byte[]> callback)
{
downloadRequest = UnityWebRequest.Get($"{App.httpAPI.WebHost}/{webData.url}");
yield return downloadRequest.SendWebRequest();
downloadRequest = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{webData.url}");
while (!downloadRequest.downloadHandler.isDone)
{
yield return null;
Debug.Log($"下载进度:{downloadRequest.downloadHandler.DownLoadPr} ->{downloadRequest.downloadHandler.loadedLenght}/{downloadRequest.downloadHandler.NeedloadedLenght}");
}
AxiHttpProxy.ShowAxiHttpDebugInfo(downloadRequest.downloadHandler);
var request = downloadRequest;
downloadRequest = null;
if (request.result != UnityWebRequest.Result.Success)
if (request.downloadHandler.Err != null)
{
callback(null);
}
@ -128,6 +141,21 @@ namespace AxibugEmuOnline.Client
{
callback(request.downloadHandler.data);
}
//downloadRequest = UnityWebRequest.Get($"{App.httpAPI.WebHost}/{webData.url}");
//yield return downloadRequest.SendWebRequest();
//var request = downloadRequest;
//downloadRequest = null;
//if (request.result != UnityWebRequest.Result.Success)
//{
// callback(null);
//}
//else
//{
// callback(request.downloadHandler.data);
//}
}
public void SetWebData(HttpAPI.Resp_RomInfo resp_RomInfo)