From de9d5ae6fcef91f9dc56e637efd84aba6c4eac3a Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Fri, 24 May 2024 10:19:54 +0800 Subject: [PATCH] =?UTF-8?q?MsgCache=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkDriver/Common/MsgCache.cs | 44 ++++++++++++++++++++-- Service/Cloud/Client/CloudClient.cs | 1 - Service/Cloud/Handler/CarAuthResHandler.cs | 2 +- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/HybirdFrameworkDriver/Common/MsgCache.cs b/HybirdFrameworkDriver/Common/MsgCache.cs index 079743c..79dec65 100644 --- a/HybirdFrameworkDriver/Common/MsgCache.cs +++ b/HybirdFrameworkDriver/Common/MsgCache.cs @@ -36,12 +36,48 @@ public class MsgCache { _dictionary.Remove(MsgKey + key, out var value); } - - } public class MsgPair { - public TReq? Req { get; set; } - public TResp? Resp { get; set; } + private readonly ManualResetEvent _lock = new(false); + private TReq? _req; + + public TReq? Req + { + get => _req; + set + { + _req = value; + ClearResp(); + } + } + + private TResp? _resp; + + public void ClearResp() + { + _resp = default; + } + + public void SetResp(TResp? resp) + { + _resp = resp; + _lock.Set(); + _lock.Reset(); + } + + public TResp? GetResp(TimeSpan? timeSpan = default) + { + if (timeSpan != null) + { + _lock.WaitOne(timeSpan.Value); + } + else + { + _lock.WaitOne(); + } + + return _resp; + } } \ No newline at end of file diff --git a/Service/Cloud/Client/CloudClient.cs b/Service/Cloud/Client/CloudClient.cs index 1fb4783..5ddfd4c 100644 --- a/Service/Cloud/Client/CloudClient.cs +++ b/Service/Cloud/Client/CloudClient.cs @@ -347,7 +347,6 @@ public class CloudClient : IMqttClientConnectedHandler, IMqttApplicationMessageR public void SendVehicleCertification(VehicleCertification vehicleCertification) { this.CarAuth.Req = vehicleCertification; - this.CarAuth.Resp = null; this.Publish(vehicleCertification); } diff --git a/Service/Cloud/Handler/CarAuthResHandler.cs b/Service/Cloud/Handler/CarAuthResHandler.cs index 431c485..43401fc 100644 --- a/Service/Cloud/Handler/CarAuthResHandler.cs +++ b/Service/Cloud/Handler/CarAuthResHandler.cs @@ -18,6 +18,6 @@ public class CarAuthResHandler : IBaseHandler public void Handle(string t) { VehicleCertificationResp? resp = JsonConvert.DeserializeObject(t); - CloudClientMgr.CloudClient.CarAuth.Resp = resp; + CloudClientMgr.CloudClient.CarAuth.SetResp(resp); } } \ No newline at end of file