Compare commits

...

6 Commits

6 changed files with 211 additions and 188 deletions

View File

@ -130,9 +130,9 @@ namespace AxibugEmuOnline.Client.ClientCore
if (!request.downloadHandler.isDone)
yield break;
if (request.downloadHandler.Err != null)
if (request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
App.log.Error(request.downloadHandler.ErrInfo);
yield break;
}

View File

@ -88,7 +88,7 @@ public static class AxiHttp
public static long index = 0;
static int singlePkgMaxRead = 1024;
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
{
public AxiRespInfo mReqAsync;
public WaitAxiRequest(AxiRespInfo reqAsync)
@ -117,7 +117,22 @@ public static class AxiHttp
{
public bool isDone = false;
public AxiDownLoadMode downloadMode = AxiDownLoadMode.NotDownLoad;
public string Err = null;
public bool bHadErr
{
get
{
return
isDone = true
&&
(
!string.IsNullOrEmpty(ErrInfo)
||
code != 200
);
}
}
public string ErrInfo;
//public string Err = null;
public string host = "";//host主机头
public string url = "";//pathAndQuery
public int port = 80;
@ -231,10 +246,10 @@ public static class AxiHttp
bool foward_302 = true;
string ourErrMsg = "";
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath,ref ourErrMsg))
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{
Log("ParseURI False");
respinfo.Err = ourErrMsg;
respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0;
respinfo.isDone = true;
return;
@ -496,7 +511,7 @@ public static class AxiHttp
}
catch (Exception ex)
{
respinfo.Err = $"ex : {ex.ToString()}";
respinfo.ErrInfo = $"ex : {ex.ToString()}";
}
finally
{
@ -534,7 +549,7 @@ public static class AxiHttp
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{
Log("ParseURI False");
respinfo.Err = ourErrMsg;
respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0;
respinfo.isDone = true;
return;
@ -778,7 +793,7 @@ public static class AxiHttp
}
catch (Exception ex)
{
respinfo.Err = $"ex : {ex.ToString()}";
respinfo.ErrInfo = $"ex : {ex.ToString()}";
}
finally
{
@ -806,9 +821,9 @@ public static class AxiHttp
{
if (i == 0)
{
respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]);
if (respinfo.code != 200 && respinfo.code != 301 && respinfo.code != 302)
respinfo.ErrInfo = "code:" + respinfo.code;
}
else
{
@ -1043,7 +1058,7 @@ public static class AxiHttp
{
strIPRet = GetDnsIP(strAddressRet).ToString();
}
catch(Exception ex)
catch (Exception ex)
{
errMsg = ex.ToString();
return false;
@ -1164,7 +1179,6 @@ public static class AxiHttp
Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}");
tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient);
if (TimeoutObject.WaitOne(timeoutMSec, false))
{
if (IsConnectionSuccessful)

View File

@ -36,7 +36,7 @@ namespace AxibugEmuOnline.Client
}
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
if (request.downloadHandler.Err == null)
if (!request.downloadHandler.bHadErr)
{
Directory.CreateDirectory(path);
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);

View File

@ -4,7 +4,6 @@ using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
namespace AxibugEmuOnline.Client
{
@ -15,27 +14,7 @@ namespace AxibugEmuOnline.Client
public delegate void GetRomListAPI(Action<Resp_GameList> callback, int page, int pageSize = 10);
public delegate void SearchRomListAPI(Action<Resp_GameList> callback, string searchKey, int page, int pageSize = 10);
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '-' || c == '_' || c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || c == ')')
{
sb.Append(c);
}
else
{
sb.Append('%').Append(((int)c).ToString("X2"));
}
}
return sb.ToString();
}
public void GetNesRomList(Action<Resp_GameList> callback, int page, int pageSize = 10)
{
App.StartCoroutine(GetNesRomListFlow(page, pageSize, callback));
@ -47,15 +26,25 @@ namespace AxibugEmuOnline.Client
}
private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<Resp_GameList> callback)
{
//避免特殊字符和个别文字编码问题
byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
// 将UTF-8编码的字节数组转换回字符串此时是UTF-8编码的字符串
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
searchKey = UrlEncode(utf8String);
App.log.Info($"search->{utf8String} ->{searchKey}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
if (!string.IsNullOrEmpty(searchKey))
{
string oldsearch = searchKey;
//searchKey = System.Net.WebUtility.UrlEncode(searchKey);
searchKey = AxiHttp.UrlEncode(searchKey);
App.log.Info($"search->{oldsearch} ->{searchKey}");
//searchKey = HttpUtility.UrlDecode(searchKey);
}
//避免特殊字符和个别文字编码问题
//byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
//byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
//// 将UTF-8编码的字节数组转换回字符串此时是UTF-8编码的字符串
//string utf8String = Encoding.UTF8.GetString(utf8Bytes);
//searchKey = UrlEncode(utf8String);
//App.log.Info($"search->{utf8String} ->{searchKey}");
string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}";
App.log.Info($"GetRomList=>{url}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get(url);
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
{
@ -63,13 +52,16 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
yield return request.SendWebRequest();
@ -80,12 +72,12 @@ namespace AxibugEmuOnline.Client
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}");
string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}";
App.log.Info($"GetRomList=>{url}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get(url);
yield return request.SendWebRequest;
if (!request.downloadHandler.isDone)
{
@ -93,12 +85,16 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
//请求成功
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest();
@ -109,8 +105,6 @@ namespace AxibugEmuOnline.Client
yield break;
}
*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
@ -124,13 +118,17 @@ namespace AxibugEmuOnline.Client
yield break;
}
if (request.downloadHandler.Err != null)
//成功
if (!request.downloadHandler.bHadErr)
{
App.log.Error(request.downloadHandler.Err);
callback.Invoke(null);
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);
yield break;
}
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
yield return request.SendWebRequest();
@ -141,8 +139,6 @@ namespace AxibugEmuOnline.Client
yield break;
}*/
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);
}
enum PlatformType : byte

View File

@ -136,7 +136,7 @@ namespace AxibugEmuOnline.Client
var request = downloadRequest;
downloadRequest = null;
if (request.downloadHandler.Err == null)
if (!request.downloadHandler.bHadErr)
callback(request.downloadHandler.data);
else
callback(null);

View File

@ -7,147 +7,160 @@ using static AxibugEmuOnline.Client.HttpAPI;
namespace AxibugEmuOnline.Client
{
public class RomLib
{
/// <summary> Rom请求,一页的大小 </summary>
private const int PAGE_SIZE = 10;
public class RomLib
{
/// <summary> Rom请求,一页的大小 </summary>
private const int PAGE_SIZE = 10;
/// <summary> 请求指令 </summary>
private HashSet<int> FetchPageCmd = new HashSet<int>();
private RomFile[] nesRomFetchList;
private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>();
private HttpAPI.GetRomListAPI m_romGetFunc;
private HttpAPI.SearchRomListAPI m_romSearchFunc;
private EnumSupportEmuPlatform m_platform;
/// <summary> 请求指令 </summary>
private HashSet<int> FetchPageCmd = new HashSet<int>();
private RomFile[] nesRomFetchList;
private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>();
private HttpAPI.GetRomListAPI m_romGetFunc;
private HttpAPI.SearchRomListAPI m_romSearchFunc;
private EnumSupportEmuPlatform m_platform;
private string lastSearchKey;
public RomLib(EnumSupportEmuPlatform platform)
{
m_platform = platform;
switch (platform)
{
case EnumSupportEmuPlatform.NES:
m_romGetFunc = App.httpAPI.GetNesRomList;
m_romSearchFunc = App.httpAPI.SearchNesRomList;
break;
}
}
public RomLib(EnumSupportEmuPlatform platform)
{
m_platform = platform;
switch (platform)
{
case EnumSupportEmuPlatform.NES:
m_romGetFunc = App.httpAPI.GetNesRomList;
m_romSearchFunc = App.httpAPI.SearchNesRomList;
break;
}
}
public RomFile GetExistRom(string fileName)
{
var res = RomFile.CreateExistRom(m_platform, fileName);
public RomFile GetExistRom(string fileName)
{
var res = RomFile.CreateExistRom(m_platform, fileName);
nesRomFileNameMapper[res.FileName] = res;
nesRomFileNameMapper[res.FileName] = res;
return res;
}
return res;
}
public RomFile GetRomFile(string romFileName)
{
RomFile romFile;
nesRomFileNameMapper.TryGetValue(romFileName, out romFile);
return romFile;
}
public RomFile GetRomFile(string romFileName)
{
RomFile romFile;
nesRomFileNameMapper.TryGetValue(romFileName, out romFile);
return romFile;
}
/// <summary> 清除所有下载的Rom文件 </summary>
public void ClearRomFile()
{
var path = $"{App.PersistentDataPath}/RemoteRoms/{m_platform}";
if (Directory.Exists(path)) Directory.Delete(path, true);
}
/// <summary> 清除所有下载的Rom文件 </summary>
public void ClearRomFile()
{
var path = $"{App.PersistentDataPath}/RemoteRoms/{m_platform}";
if (Directory.Exists(path)) Directory.Delete(path, true);
}
/// <summary> 移除一个已下载的Rom </summary>
public void RemoveOneRomFile(RomFile romFile)
{
if (romFile.RomReady)
File.Delete(romFile.LocalFilePath);
}
/// <summary> 移除一个已下载的Rom </summary>
public void RemoveOneRomFile(RomFile romFile)
{
if (romFile.RomReady)
File.Delete(romFile.LocalFilePath);
}
/// <summary>
/// 获得所有Rom文件
/// </summary>
/// <param name="callback"></param>
public void FetchRomCount(Action<RomFile[]> callback, string searchKey = null)
{
if (string.IsNullOrWhiteSpace(searchKey))
{
m_romGetFunc((romList) =>
{
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
/// <summary>
/// 获得所有Rom文件
/// </summary>
/// <param name="callback"></param>
public void FetchRomCount(Action<RomFile[]> callback, string searchKey = null)
{
lastSearchKey = searchKey;
if (string.IsNullOrWhiteSpace(searchKey))
{
m_romGetFunc((romList) =>
{
//TODO 请求失败对于romList为空时的处理
callback.Invoke(nesRomFetchList);
}, 0, PAGE_SIZE);
}
else
{
m_romSearchFunc((romList) =>
{
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList);
}, searchKey, 0, PAGE_SIZE);
}
}
callback.Invoke(nesRomFetchList);
}, 0, PAGE_SIZE);
}
else
{
m_romSearchFunc((romList) =>
{
//TODO 请求失败对于romList为空时的处理
public void BeginFetchRomInfo(RomFile romFile)
{
if (romFile.InfoReady) return;
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
FetchPageCmd.Add(romFile.Page);
}
callback.Invoke(nesRomFetchList);
}, searchKey, 0, PAGE_SIZE);
}
}
public void ExecuteFetchRomInfo()
{
if (FetchPageCmd.Count == 0) return;
public void BeginFetchRomInfo(RomFile romFile)
{
if (romFile.InfoReady) return;
foreach (var pageNo in FetchPageCmd)
{
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
FetchPageCmd.Clear();
}
FetchPageCmd.Add(romFile.Page);
}
private void SaveRomInfoFromWeb(Resp_GameList resp)
{
for (int i = 0; i < resp.gameList.Count; i++)
{
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
public void ExecuteFetchRomInfo()
{
if (FetchPageCmd.Count == 0) return;
targetRomFile.SetWebData(webData);
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
}
}
foreach (var pageNo in FetchPageCmd)
{
if (!string.IsNullOrEmpty(lastSearchKey))
{
m_romSearchFunc(SaveRomInfoFromWeb, lastSearchKey, pageNo, PAGE_SIZE);
}
else
{
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
}
FetchPageCmd.Clear();
}
public static string CalcHash(byte[] data)
{
return Helper.FileMD5Hash(data);
}
private void SaveRomInfoFromWeb(Resp_GameList resp)
{
for (int i = 0; i < resp.gameList.Count; i++)
{
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
public void AddRomFile(RomFile rom)
{
nesRomFileNameMapper[rom.FileName] = rom;
}
}
targetRomFile.SetWebData(webData);
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
}
}
public static string CalcHash(byte[] data)
{
return Helper.FileMD5Hash(data);
}
public void AddRomFile(RomFile rom)
{
nesRomFileNameMapper[rom.FileName] = rom;
}
}
}