tbox请求改成同步返回

master
smartwyy 6 months ago
parent 80c1fca70d
commit 58728b3861

@ -0,0 +1,79 @@
using System.Collections.Concurrent;
namespace HybirdFrameworkDriver.Common;
public class MsgCache<T, TReq, TResp>
{
private readonly ConcurrentDictionary<string, MsgPair<TReq, TResp>> _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<TReq, TResp>? value)
{
return _dictionary.TryGetValue(key, out value);
}
public void Remove(string key)
{
_dictionary.Remove(key, out var value);
}
public void AddByMsgId(int key, MsgPair<TReq, TResp> value)
{
_dictionary[MsgKey + key] = value;
}
public bool TryGetMsgId(int key, out MsgPair<TReq, TResp>? value)
{
return _dictionary.TryGetValue(MsgKey + key, out value);
}
public void RemoveMsgId(int key)
{
_dictionary.Remove(MsgKey + key, out var value);
}
}
public class MsgPair<TReq, TResp>
{
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;
}
}

@ -21,6 +21,6 @@ public class LockMsgRespHandler : SimpleChannelInboundHandler<LockMsgResp>, IBas
protected override void ChannelRead0(IChannelHandlerContext ctx, LockMsgResp msg) protected override void ChannelRead0(IChannelHandlerContext ctx, LockMsgResp msg)
{ {
CarServerMgr.CarServer.Connected = true; CarServerMgr.CarServer.Connected = true;
CarServerMgr.CarServer.LockMsgResp = msg; CarServerMgr.CarServer.LockMsgPair.SetResp(msg);
} }
} }

@ -21,6 +21,6 @@ public class SetParamMsgRespHandler : SimpleChannelInboundHandler<SetParamMsgRes
protected override void ChannelRead0(IChannelHandlerContext ctx, SetParamMsgResp msg) protected override void ChannelRead0(IChannelHandlerContext ctx, SetParamMsgResp msg)
{ {
CarServerMgr.CarServer.Connected = true; CarServerMgr.CarServer.Connected = true;
CarServerMgr.CarServer.SetParamMsgResp = msg; CarServerMgr.CarServer.SetParamMsgPair.SetResp(msg);
} }
} }

@ -21,6 +21,6 @@ public class SettleConfirmMsgRespHandler : SimpleChannelInboundHandler<SettleCon
protected override void ChannelRead0(IChannelHandlerContext ctx, SettleConfirmMsgResp msg) protected override void ChannelRead0(IChannelHandlerContext ctx, SettleConfirmMsgResp msg)
{ {
CarServerMgr.CarServer.Connected = true; CarServerMgr.CarServer.Connected = true;
CarServerMgr.CarServer.SettleConfirmMsgResp = msg; CarServerMgr.CarServer.SettleConfirmMsgPair.SetResp(msg);
} }
} }

@ -21,7 +21,6 @@ public class UnLockMsgRespHandler : SimpleChannelInboundHandler<UnLockMsgResp>,
protected override void ChannelRead0(IChannelHandlerContext ctx, UnLockMsgResp msg) protected override void ChannelRead0(IChannelHandlerContext ctx, UnLockMsgResp msg)
{ {
CarServerMgr.CarServer.Connected = true; CarServerMgr.CarServer.Connected = true;
CarServerMgr.CarServer.UnLockMsgResp = msg; CarServerMgr.CarServer.UnLockMsgPair.SetResp(msg);
} }
} }

@ -1,9 +1,11 @@
using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.Common;
using HybirdFrameworkDriver.TcpServer; using HybirdFrameworkDriver.TcpServer;
using Service.Car.Codec; using Service.Car.Codec;
using Service.Car.Handler; using Service.Car.Handler;
using Service.Car.Msg.Car.Req; using Service.Car.Msg.Car.Req;
using Service.Car.Msg.Car.Resp; using Service.Car.Msg.Car.Resp;
using Service.Car.Msg.Host.Req;
namespace Service.Car.Server; namespace Service.Car.Server;
@ -17,6 +19,13 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
public string? CarNo { get; set; } public string? CarNo { get; set; }
public MsgPair<LockMsg, LockMsgResp> LockMsgPair { get; set; } = new ();
public MsgPair<UnLockMsg, UnLockMsgResp> UnLockMsgPair { get; set; } = new ();
public MsgPair<SetParamMsg, SetParamMsgResp> SetParamMsgPair { get; set; } = new ();
public MsgPair<SettleConfirmMsg, SettleConfirmMsgResp> SettleConfirmMsgPair { get; set; } = new ();
public HeartBeatMsg? HeartBeatMsg { get; set; } public HeartBeatMsg? HeartBeatMsg { get; set; }
/// <summary> /// <summary>
@ -29,11 +38,6 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
/// </summary> /// </summary>
public ElecMsg? ElecMsg { get; set; } 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() public CarServer() : base()
{ {
this.ChannelInActiveAction = Clean; this.ChannelInActiveAction = Clean;
@ -49,9 +53,9 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
CarNo = null; CarNo = null;
HeartBeatMsg = null; HeartBeatMsg = null;
ElecMsg = null; ElecMsg = null;
LockMsgResp = null; LockMsgPair.ClearResp();
UnLockMsgResp = null; UnLockMsgPair.ClearResp();
SetParamMsgResp = null; SetParamMsgPair.ClearResp();
SettleConfirmMsgResp = null; SettleConfirmMsgPair.ClearResp();
} }
} }

@ -35,9 +35,10 @@ public class CarController : ControllerBase
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
/// <returns>发送结果</returns> /// <param name="carNo">vin码</param>
/// <returns></returns>
[HttpGet("/lock/{carNo}")] [HttpGet("/lock/{carNo}")]
public bool Lock(string carNo) public bool Lock(string carNo)
{ {
@ -47,19 +48,20 @@ public class CarController : ControllerBase
return false; return false;
} }
CarServerMgr.CarServer.LockMsgResp = null; var lockMsg = new LockMsg()
SessionMgr.Broadcast(new LockMsg()
{ {
CarNo = carNo CarNo = carNo
}); };
CarServerMgr.CarServer.LockMsgPair.Req = lockMsg;
SessionMgr.Broadcast(lockMsg);
return true; return CarServerMgr.CarServer.LockMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0;
} }
/// <summary> /// <summary>
/// /// 解锁
/// </summary> /// </summary>
/// <param name="carNo"></param> /// <param name="carNo">vin码</param>
/// <returns></returns> /// <returns></returns>
[HttpGet("/unLock/{carNo}")] [HttpGet("/unLock/{carNo}")]
public bool UnLock(string carNo) public bool UnLock(string carNo)
@ -70,19 +72,21 @@ public class CarController : ControllerBase
return false; return false;
} }
CarServerMgr.CarServer.UnLockMsgResp = null; UnLockMsg unLockMsg = new UnLockMsg()
SessionMgr.Broadcast(new UnLockMsg()
{ {
CarNo = carNo CarNo = carNo
}); };
return true; CarServerMgr.CarServer.UnLockMsgPair.Req = unLockMsg;
SessionMgr.Broadcast(unLockMsg);
return CarServerMgr.CarServer.UnLockMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0;
} }
/// <summary> /// <summary>
/// /// 结算
/// </summary> /// </summary>
/// <param name="carNo"></param> /// <param name="carNo">vin码</param>
/// <returns></returns> /// <returns></returns>
[HttpGet("/SettleConfirm/{carNo}")] [HttpGet("/SettleConfirm/{carNo}")]
public bool SettleConfirm(string carNo) public bool SettleConfirm(string carNo)
@ -93,10 +97,11 @@ public class CarController : ControllerBase
return false; return false;
} }
CarServerMgr.CarServer.SettleConfirmMsgResp = null; var settleConfirmMsg = new SettleConfirmMsg() { CarNo = carNo };
SessionMgr.Broadcast(new SettleConfirmMsg() { CarNo = carNo }); CarServerMgr.CarServer.SettleConfirmMsgPair.Req = settleConfirmMsg;
SessionMgr.Broadcast(settleConfirmMsg);
return true; return CarServerMgr.CarServer.SettleConfirmMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result == 0;
} }
/// <summary> /// <summary>
@ -112,7 +117,6 @@ public class CarController : ControllerBase
return false; return false;
} }
CarServerMgr.CarServer.SetParamMsgResp = null;
SetParamMsg setParamMsg = new SetParamMsg() SetParamMsg setParamMsg = new SetParamMsg()
{ {
AccDischargeCount = setParam.AccDischargeCount, AccDischargeCount = setParam.AccDischargeCount,
@ -129,9 +133,10 @@ public class CarController : ControllerBase
LastTimeBalanceKgce = setParam.LastTimeBalanceKgce, LastTimeBalanceKgce = setParam.LastTimeBalanceKgce,
ElectricityToBeSettled = setParam.ElectricityToBeSettled, ElectricityToBeSettled = setParam.ElectricityToBeSettled,
}; };
CarServerMgr.CarServer.SetParamMsgPair.Req = setParamMsg;
SessionMgr.Broadcast(setParamMsg); SessionMgr.Broadcast(setParamMsg);
return true; return CarServerMgr.CarServer.SetParamMsgPair.GetResp(TimeSpan.FromMinutes(1))?.Result ==0;
} }
/// <summary> /// <summary>

@ -13,7 +13,7 @@ public class CarInfoResp
public bool Connected { get; set; } public bool Connected { get; set; }
/// <summary> /// <summary>
/// 车牌号 /// vin码
/// </summary> /// </summary>
public string? CarNo { get; set; } public string? CarNo { get; set; }

Loading…
Cancel
Save