forked from sin365/AxibugEmuOnline
改善网络请求异常捕获
This commit is contained in:
parent
7c2709f335
commit
55a8e02a3a
@ -130,9 +130,9 @@ namespace AxibugEmuOnline.Client.ClientCore
|
|||||||
if (!request.downloadHandler.isDone)
|
if (!request.downloadHandler.isDone)
|
||||||
yield break;
|
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;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ public static class AxiHttp
|
|||||||
public static long index = 0;
|
public static long index = 0;
|
||||||
static int singlePkgMaxRead = 1024;
|
static int singlePkgMaxRead = 1024;
|
||||||
|
|
||||||
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
|
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
|
||||||
{
|
{
|
||||||
public AxiRespInfo mReqAsync;
|
public AxiRespInfo mReqAsync;
|
||||||
public WaitAxiRequest(AxiRespInfo reqAsync)
|
public WaitAxiRequest(AxiRespInfo reqAsync)
|
||||||
@ -117,7 +117,22 @@ public static class AxiHttp
|
|||||||
{
|
{
|
||||||
public bool isDone = false;
|
public bool isDone = false;
|
||||||
public AxiDownLoadMode downloadMode = AxiDownLoadMode.NotDownLoad;
|
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 host = "";//host主机头
|
||||||
public string url = "";//pathAndQuery
|
public string url = "";//pathAndQuery
|
||||||
public int port = 80;
|
public int port = 80;
|
||||||
@ -231,10 +246,10 @@ public static class AxiHttp
|
|||||||
bool foward_302 = true;
|
bool foward_302 = true;
|
||||||
string ourErrMsg = "";
|
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");
|
Log("ParseURI False");
|
||||||
respinfo.Err = ourErrMsg;
|
respinfo.ErrInfo = ourErrMsg;
|
||||||
respinfo.code = 0;
|
respinfo.code = 0;
|
||||||
respinfo.isDone = true;
|
respinfo.isDone = true;
|
||||||
return;
|
return;
|
||||||
@ -496,7 +511,7 @@ public static class AxiHttp
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
respinfo.Err = $"ex : {ex.ToString()}";
|
respinfo.ErrInfo = $"ex : {ex.ToString()}";
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -534,7 +549,7 @@ public static class AxiHttp
|
|||||||
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");
|
Log("ParseURI False");
|
||||||
respinfo.Err = ourErrMsg;
|
respinfo.ErrInfo = ourErrMsg;
|
||||||
respinfo.code = 0;
|
respinfo.code = 0;
|
||||||
respinfo.isDone = true;
|
respinfo.isDone = true;
|
||||||
return;
|
return;
|
||||||
@ -778,7 +793,7 @@ public static class AxiHttp
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
respinfo.Err = $"ex : {ex.ToString()}";
|
respinfo.ErrInfo = $"ex : {ex.ToString()}";
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -806,9 +821,9 @@ public static class AxiHttp
|
|||||||
{
|
{
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]);
|
respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]);
|
||||||
|
if (respinfo.code != 200 && respinfo.code != 301 && respinfo.code != 302)
|
||||||
|
respinfo.ErrInfo = "code:" + respinfo.code;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -979,11 +994,11 @@ public static class AxiHttp
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public static bool ParseURI(string strURI,
|
public static bool ParseURI(string strURI,
|
||||||
ref bool bIsSSL,
|
ref bool bIsSSL,
|
||||||
ref string strHost,
|
ref string strHost,
|
||||||
ref string strIP,
|
ref string strIP,
|
||||||
ref int Port,
|
ref int Port,
|
||||||
ref string strRelativePath,
|
ref string strRelativePath,
|
||||||
ref string errMsg)
|
ref string errMsg)
|
||||||
{
|
{
|
||||||
@ -1043,7 +1058,7 @@ public static class AxiHttp
|
|||||||
{
|
{
|
||||||
strIPRet = GetDnsIP(strAddressRet).ToString();
|
strIPRet = GetDnsIP(strAddressRet).ToString();
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
errMsg = ex.ToString();
|
errMsg = ex.ToString();
|
||||||
return false;
|
return false;
|
||||||
@ -1164,7 +1179,6 @@ public static class AxiHttp
|
|||||||
Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}");
|
Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}");
|
||||||
tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient);
|
tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient);
|
||||||
|
|
||||||
|
|
||||||
if (TimeoutObject.WaitOne(timeoutMSec, false))
|
if (TimeoutObject.WaitOne(timeoutMSec, false))
|
||||||
{
|
{
|
||||||
if (IsConnectionSuccessful)
|
if (IsConnectionSuccessful)
|
||||||
|
@ -36,7 +36,7 @@ namespace AxibugEmuOnline.Client
|
|||||||
}
|
}
|
||||||
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
|
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
|
||||||
|
|
||||||
if (request.downloadHandler.Err == null)
|
if (!request.downloadHandler.bHadErr)
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(path);
|
Directory.CreateDirectory(path);
|
||||||
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);
|
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);
|
||||||
|
@ -54,7 +54,7 @@ namespace AxibugEmuOnline.Client
|
|||||||
App.log.Info($"search->{oldsearch} ->{searchKey}");
|
App.log.Info($"search->{oldsearch} ->{searchKey}");
|
||||||
//searchKey = HttpUtility.UrlDecode(searchKey);
|
//searchKey = HttpUtility.UrlDecode(searchKey);
|
||||||
}
|
}
|
||||||
////避免特殊字符和个别文字编码问题
|
//避免特殊字符和个别文字编码问题
|
||||||
//byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
|
//byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
|
||||||
//byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
|
//byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
|
||||||
//// 将UTF-8编码的字节数组转换回字符串(此时是UTF-8编码的字符串)
|
//// 将UTF-8编码的字节数组转换回字符串(此时是UTF-8编码的字符串)
|
||||||
@ -71,13 +71,16 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.downloadHandler.Err != null)
|
if (!request.downloadHandler.bHadErr)
|
||||||
{
|
{
|
||||||
App.log.Error(request.downloadHandler.Err);
|
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
|
||||||
callback.Invoke(null);
|
callback.Invoke(resp);
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
App.log.Error(request.downloadHandler.ErrInfo);
|
||||||
|
callback.Invoke(null);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
|
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
|
||||||
yield return request.SendWebRequest();
|
yield return request.SendWebRequest();
|
||||||
@ -88,8 +91,6 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
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)
|
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback)
|
||||||
{
|
{
|
||||||
@ -103,12 +104,16 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.downloadHandler.Err != null)
|
//请求成功
|
||||||
|
if (!request.downloadHandler.bHadErr)
|
||||||
{
|
{
|
||||||
App.log.Error(request.downloadHandler.Err);
|
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
|
||||||
callback.Invoke(null);
|
callback.Invoke(resp);
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
App.log.Error(request.downloadHandler.ErrInfo);
|
||||||
|
callback.Invoke(null);
|
||||||
/*
|
/*
|
||||||
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
|
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
|
||||||
yield return request.SendWebRequest();
|
yield return request.SendWebRequest();
|
||||||
@ -119,8 +124,6 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
|
|
||||||
callback.Invoke(resp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
|
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
|
||||||
@ -134,13 +137,17 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.downloadHandler.Err != null)
|
//成功
|
||||||
|
if (!request.downloadHandler.bHadErr)
|
||||||
{
|
{
|
||||||
App.log.Error(request.downloadHandler.Err);
|
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
|
||||||
callback.Invoke(null);
|
callback.Invoke(resp);
|
||||||
yield break;
|
yield break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
App.log.Error(request.downloadHandler.ErrInfo);
|
||||||
|
callback.Invoke(null);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
|
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
|
||||||
yield return request.SendWebRequest();
|
yield return request.SendWebRequest();
|
||||||
@ -151,8 +158,6 @@ namespace AxibugEmuOnline.Client
|
|||||||
yield break;
|
yield break;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
|
|
||||||
callback.Invoke(resp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PlatformType : byte
|
enum PlatformType : byte
|
||||||
|
@ -136,7 +136,7 @@ namespace AxibugEmuOnline.Client
|
|||||||
var request = downloadRequest;
|
var request = downloadRequest;
|
||||||
downloadRequest = null;
|
downloadRequest = null;
|
||||||
|
|
||||||
if (request.downloadHandler.Err == null)
|
if (!request.downloadHandler.bHadErr)
|
||||||
callback(request.downloadHandler.data);
|
callback(request.downloadHandler.data);
|
||||||
else
|
else
|
||||||
callback(null);
|
callback(null);
|
||||||
|
Loading…
Reference in New Issue
Block a user