From 58728b38615ace62364404f79925f46b111231c0 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Wed, 29 May 2024 20:12:37 +0800 Subject: [PATCH] =?UTF-8?q?tbox=E8=AF=B7=E6=B1=82=E6=94=B9=E6=88=90?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkDriver/Common/MsgCache.cs | 79 +++++++++++++++++++ Service/Car/Handler/LockMsgRespHandler.cs | 2 +- Service/Car/Handler/SetParamMsgRespHandler.cs | 2 +- .../Handler/SettleConfirmMsgRespHandler.cs | 2 +- Service/Car/Handler/UnLockMsgRespHandler.cs | 3 +- Service/Car/Server/CarServer.cs | 22 +++--- WebStarter/Controllers/CarController.cs | 43 +++++----- WebStarter/Dto/Resp/CarInfoResp.cs | 2 +- 8 files changed, 121 insertions(+), 34 deletions(-) create mode 100644 HybirdFrameworkDriver/Common/MsgCache.cs diff --git a/HybirdFrameworkDriver/Common/MsgCache.cs b/HybirdFrameworkDriver/Common/MsgCache.cs new file mode 100644 index 0000000..378575a --- /dev/null +++ b/HybirdFrameworkDriver/Common/MsgCache.cs @@ -0,0 +1,79 @@ +using System.Collections.Concurrent; + +namespace HybirdFrameworkDriver.Common; + +public class MsgCache +{ + private readonly ConcurrentDictionary> _dictionary = new(); + private static readonly string MsgKey = "msg_"; + + public void Add(string key, dynamic value) + { + _dictionary[key] = value; + } + + public bool TryGet(string key, out MsgPair? value) + { + return _dictionary.TryGetValue(key, out value); + } + + public void Remove(string key) + { + _dictionary.Remove(key, out var value); + } + + public void AddByMsgId(int key, MsgPair value) + { + _dictionary[MsgKey + key] = value; + } + + public bool TryGetMsgId(int key, out MsgPair? value) + { + return _dictionary.TryGetValue(MsgKey + key, out value); + } + + public void RemoveMsgId(int key) + { + _dictionary.Remove(MsgKey + key, out var value); + } +} + +public class MsgPair +{ + 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); + } + + return _resp; + } +} \ No newline at end of file diff --git a/Service/Car/Handler/LockMsgRespHandler.cs b/Service/Car/Handler/LockMsgRespHandler.cs index 7767cfe..61448dd 100644 --- a/Service/Car/Handler/LockMsgRespHandler.cs +++ b/Service/Car/Handler/LockMsgRespHandler.cs @@ -21,6 +21,6 @@ public class LockMsgRespHandler : SimpleChannelInboundHandler, IBas protected override void ChannelRead0(IChannelHandlerContext ctx, LockMsgResp msg) { CarServerMgr.CarServer.Connected = true; - CarServerMgr.CarServer.LockMsgResp = msg; + CarServerMgr.CarServer.LockMsgPair.SetResp(msg); } } \ No newline at end of file diff --git a/Service/Car/Handler/SetParamMsgRespHandler.cs b/Service/Car/Handler/SetParamMsgRespHandler.cs index ce38623..6b0ad8a 100644 --- a/Service/Car/Handler/SetParamMsgRespHandler.cs +++ b/Service/Car/Handler/SetParamMsgRespHandler.cs @@ -21,6 +21,6 @@ public class SetParamMsgRespHandler : SimpleChannelInboundHandler, protected override void ChannelRead0(IChannelHandlerContext ctx, UnLockMsgResp msg) { CarServerMgr.CarServer.Connected = true; - CarServerMgr.CarServer.UnLockMsgResp = msg; + CarServerMgr.CarServer.UnLockMsgPair.SetResp(msg); } - } \ No newline at end of file diff --git a/Service/Car/Server/CarServer.cs b/Service/Car/Server/CarServer.cs index 684b31a..dc59c83 100644 --- a/Service/Car/Server/CarServer.cs +++ b/Service/Car/Server/CarServer.cs @@ -1,9 +1,11 @@ using HybirdFrameworkCore.Autofac.Attribute; +using HybirdFrameworkDriver.Common; using HybirdFrameworkDriver.TcpServer; using Service.Car.Codec; using Service.Car.Handler; using Service.Car.Msg.Car.Req; using Service.Car.Msg.Car.Resp; +using Service.Car.Msg.Host.Req; namespace Service.Car.Server; @@ -17,6 +19,13 @@ public class CarServer : TcpServer public string? CarNo { get; set; } + public MsgPair LockMsgPair { get; set; } = new (); + + public MsgPair UnLockMsgPair { get; set; } = new (); + + public MsgPair SetParamMsgPair { get; set; } = new (); + + public MsgPair SettleConfirmMsgPair { get; set; } = new (); public HeartBeatMsg? HeartBeatMsg { get; set; } /// @@ -29,11 +38,6 @@ public class CarServer : TcpServer /// public ElecMsg? ElecMsg { get; set; } - public LockMsgResp? LockMsgResp { get; set; } - public UnLockMsgResp? UnLockMsgResp { get; set; } - public SetParamMsgResp? SetParamMsgResp { get; set; } - public SettleConfirmMsgResp? SettleConfirmMsgResp { get; set; } - public CarServer() : base() { this.ChannelInActiveAction = Clean; @@ -49,9 +53,9 @@ public class CarServer : TcpServer CarNo = null; HeartBeatMsg = null; ElecMsg = null; - LockMsgResp = null; - UnLockMsgResp = null; - SetParamMsgResp = null; - SettleConfirmMsgResp = null; + LockMsgPair.ClearResp(); + UnLockMsgPair.ClearResp(); + SetParamMsgPair.ClearResp(); + SettleConfirmMsgPair.ClearResp(); } } \ No newline at end of file diff --git a/WebStarter/Controllers/CarController.cs b/WebStarter/Controllers/CarController.cs index c67712d..59c30e9 100644 --- a/WebStarter/Controllers/CarController.cs +++ b/WebStarter/Controllers/CarController.cs @@ -35,9 +35,10 @@ public class CarController : ControllerBase } /// - /// 锁车 + /// 加锁 /// - /// 发送结果 + /// vin码 + /// [HttpGet("/lock/{carNo}")] public bool Lock(string carNo) { @@ -47,19 +48,20 @@ public class CarController : ControllerBase return false; } - CarServerMgr.CarServer.LockMsgResp = null; - SessionMgr.Broadcast(new LockMsg() + var lockMsg = new LockMsg() { CarNo = carNo - }); + }; + CarServerMgr.CarServer.LockMsgPair.Req = lockMsg; + SessionMgr.Broadcast(lockMsg); - return true; + return CarServerMgr.CarServer.LockMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0; } /// - /// + /// 解锁 /// - /// + /// vin码 /// [HttpGet("/unLock/{carNo}")] public bool UnLock(string carNo) @@ -70,19 +72,21 @@ public class CarController : ControllerBase return false; } - CarServerMgr.CarServer.UnLockMsgResp = null; - SessionMgr.Broadcast(new UnLockMsg() + UnLockMsg unLockMsg = new UnLockMsg() { CarNo = carNo - }); + }; - return true; + CarServerMgr.CarServer.UnLockMsgPair.Req = unLockMsg; + SessionMgr.Broadcast(unLockMsg); + + return CarServerMgr.CarServer.UnLockMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0; } /// - /// + /// 结算 /// - /// + /// vin码 /// [HttpGet("/SettleConfirm/{carNo}")] public bool SettleConfirm(string carNo) @@ -93,10 +97,11 @@ public class CarController : ControllerBase return false; } - CarServerMgr.CarServer.SettleConfirmMsgResp = null; - SessionMgr.Broadcast(new SettleConfirmMsg() { CarNo = carNo }); + var settleConfirmMsg = new SettleConfirmMsg() { CarNo = carNo }; + CarServerMgr.CarServer.SettleConfirmMsgPair.Req = settleConfirmMsg; + SessionMgr.Broadcast(settleConfirmMsg); - return true; + return CarServerMgr.CarServer.SettleConfirmMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0; } /// @@ -112,7 +117,6 @@ public class CarController : ControllerBase return false; } - CarServerMgr.CarServer.SetParamMsgResp = null; SetParamMsg setParamMsg = new SetParamMsg() { AccDischargeCount = setParam.AccDischargeCount, @@ -129,9 +133,10 @@ public class CarController : ControllerBase LastTimeBalanceKgce = setParam.LastTimeBalanceKgce, ElectricityToBeSettled = setParam.ElectricityToBeSettled, }; + CarServerMgr.CarServer.SetParamMsgPair.Req = setParamMsg; SessionMgr.Broadcast(setParamMsg); - return true; + return CarServerMgr.CarServer.SetParamMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result ==0; } /// diff --git a/WebStarter/Dto/Resp/CarInfoResp.cs b/WebStarter/Dto/Resp/CarInfoResp.cs index 40e04c5..b92b8d9 100644 --- a/WebStarter/Dto/Resp/CarInfoResp.cs +++ b/WebStarter/Dto/Resp/CarInfoResp.cs @@ -13,7 +13,7 @@ public class CarInfoResp public bool Connected { get; set; } /// - /// 车牌号 + /// vin码 /// public string? CarNo { get; set; }