mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-04-23 04:29:36 +08:00
为DataRequest添加RequestID以防止多线程时冲突;修改AsyncAwaiter等待逻辑 (#77)
This commit is contained in:
parent
a155aeb028
commit
30d10d6e22
@ -52,7 +52,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
/// <param name="IsLongRunning"></param>
|
/// <param name="IsLongRunning"></param>
|
||||||
internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false)
|
internal DataRequest(Socket Socket, DataRequestType RequestType, bool IsLongRunning = false)
|
||||||
{
|
{
|
||||||
Worker = new(Socket, RequestType, IsLongRunning);
|
Worker = new(Socket, RequestType, Guid.NewGuid(), IsLongRunning);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -105,7 +105,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SocketRequest(Socket? Socket, DataRequestType RequestType, bool IsLongRunning = false) : SocketHandlerController(Socket)
|
private class SocketRequest(Socket? Socket, DataRequestType RequestType, Guid RequestID, bool IsLongRunning = false) : SocketHandlerController(Socket)
|
||||||
{
|
{
|
||||||
public Hashtable RequestData { get; } = [];
|
public Hashtable RequestData { get; } = [];
|
||||||
public Hashtable ResultData => _ResultData;
|
public Hashtable ResultData => _ResultData;
|
||||||
@ -114,6 +114,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
|
|
||||||
private readonly Socket? Socket = Socket;
|
private readonly Socket? Socket = Socket;
|
||||||
private readonly DataRequestType RequestType = RequestType;
|
private readonly DataRequestType RequestType = RequestType;
|
||||||
|
private readonly Guid RequestID = RequestID;
|
||||||
private readonly bool _IsLongRunning = IsLongRunning;
|
private readonly bool _IsLongRunning = IsLongRunning;
|
||||||
private Hashtable _ResultData = [];
|
private Hashtable _ResultData = [];
|
||||||
private RequestResult _Result = RequestResult.Missing;
|
private RequestResult _Result = RequestResult.Missing;
|
||||||
@ -124,7 +125,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
SetWorking();
|
SetWorking();
|
||||||
if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success)
|
if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success)
|
||||||
{
|
{
|
||||||
WaitForWorkDone();
|
WaitForWorkDone();
|
||||||
}
|
}
|
||||||
@ -143,7 +144,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
SetWorking();
|
SetWorking();
|
||||||
if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestData) == SocketResult.Success)
|
if (Socket?.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData) == SocketResult.Success)
|
||||||
{
|
{
|
||||||
await WaitForWorkDoneAsync();
|
await WaitForWorkDoneAsync();
|
||||||
}
|
}
|
||||||
@ -163,13 +164,14 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
{
|
{
|
||||||
if (SocketObject.SocketType == SocketMessageType.DataRequest)
|
if (SocketObject.SocketType == SocketMessageType.DataRequest)
|
||||||
{
|
{
|
||||||
Work = SocketObject;
|
|
||||||
Working = false;
|
|
||||||
DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
|
DataRequestType type = SocketObject.GetParam<DataRequestType>(0);
|
||||||
if (type == RequestType)
|
Guid id = SocketObject.GetParam<Guid>(1);
|
||||||
|
if (type == RequestType && id == RequestID)
|
||||||
{
|
{
|
||||||
if (!_IsLongRunning) Dispose();
|
if (!_IsLongRunning) Dispose();
|
||||||
_ResultData = SocketObject.GetParam<Hashtable>(1) ?? [];
|
Work = SocketObject;
|
||||||
|
Working = false;
|
||||||
|
_ResultData = SocketObject.GetParam<Hashtable>(2) ?? [];
|
||||||
_Result = RequestResult.Success;
|
_Result = RequestResult.Success;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
/// <param name="RequestType"></param>
|
/// <param name="RequestType"></param>
|
||||||
internal WebDataRequest(HTTPClient Socket, DataRequestType RequestType)
|
internal WebDataRequest(HTTPClient Socket, DataRequestType RequestType)
|
||||||
{
|
{
|
||||||
Worker = new(Socket, RequestType);
|
Worker = new(Socket, RequestType, Guid.NewGuid());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -90,7 +90,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
return GetHashtableJsonObject<T>(Worker.ResultData, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class WebSocketRequest(HTTPClient? Socket, DataRequestType RequestType)
|
private class WebSocketRequest(HTTPClient? Socket, DataRequestType RequestType, Guid RequestID)
|
||||||
{
|
{
|
||||||
public Hashtable RequestData { get; } = [];
|
public Hashtable RequestData { get; } = [];
|
||||||
public Hashtable ResultData => _ResultData;
|
public Hashtable ResultData => _ResultData;
|
||||||
@ -99,6 +99,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
|
|
||||||
private readonly HTTPClient? Socket = Socket;
|
private readonly HTTPClient? Socket = Socket;
|
||||||
private readonly DataRequestType RequestType = RequestType;
|
private readonly DataRequestType RequestType = RequestType;
|
||||||
|
private readonly Guid RequestID = RequestID;
|
||||||
private readonly Hashtable _ResultData = [];
|
private readonly Hashtable _ResultData = [];
|
||||||
private RequestResult _Result = RequestResult.Missing;
|
private RequestResult _Result = RequestResult.Missing;
|
||||||
private string _Error = "";
|
private string _Error = "";
|
||||||
@ -109,7 +110,7 @@ namespace Milimoe.FunGame.Core.Api.Transmittal
|
|||||||
{
|
{
|
||||||
if (Socket != null)
|
if (Socket != null)
|
||||||
{
|
{
|
||||||
await Socket.Send(SocketMessageType.DataRequest, RequestType, RequestData);
|
await Socket.Send(SocketMessageType.DataRequest, RequestType, RequestID, RequestData);
|
||||||
}
|
}
|
||||||
else throw new ConnectFailedException();
|
else throw new ConnectFailedException();
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,16 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual void WaitForWorkDone()
|
protected virtual void WaitForWorkDone()
|
||||||
{
|
{
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
{
|
||||||
|
if (!Working) break;
|
||||||
|
Thread.Sleep(50);
|
||||||
|
}
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (!Working) break;
|
if (!Working) break;
|
||||||
Thread.Sleep(100);
|
Thread.Sleep(20);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,14 +48,16 @@
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected async virtual Task WaitForWorkDoneAsync()
|
protected async virtual Task WaitForWorkDoneAsync()
|
||||||
{
|
{
|
||||||
await Task.Factory.StartNew(() =>
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
while (true)
|
if (!Working) break;
|
||||||
{
|
await Task.Delay(50);
|
||||||
if (!Working) break;
|
}
|
||||||
Thread.Sleep(100);
|
|
||||||
}
|
while (Working)
|
||||||
});
|
{
|
||||||
|
await Task.Delay(20);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user