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)
{
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)
{
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)
{
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)
{
CarServerMgr.CarServer.Connected = true;
CarServerMgr.CarServer.UnLockMsgResp = msg;
CarServerMgr.CarServer.UnLockMsgPair.SetResp(msg);
}
}

@ -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<IBaseHandler, Decoder, Encoder>
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; }
/// <summary>
@ -29,11 +38,6 @@ public class CarServer : TcpServer<IBaseHandler, Decoder, Encoder>
/// </summary>
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<IBaseHandler, Decoder, Encoder>
CarNo = null;
HeartBeatMsg = null;
ElecMsg = null;
LockMsgResp = null;
UnLockMsgResp = null;
SetParamMsgResp = null;
SettleConfirmMsgResp = null;
LockMsgPair.ClearResp();
UnLockMsgPair.ClearResp();
SetParamMsgPair.ClearResp();
SettleConfirmMsgPair.ClearResp();
}
}

@ -35,9 +35,10 @@ public class CarController : ControllerBase
}
/// <summary>
///
///
/// </summary>
/// <returns>发送结果</returns>
/// <param name="carNo">vin码</param>
/// <returns></returns>
[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;
}
/// <summary>
///
/// 解锁
/// </summary>
/// <param name="carNo"></param>
/// <param name="carNo">vin码</param>
/// <returns></returns>
[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;
}
/// <summary>
///
/// 结算
/// </summary>
/// <param name="carNo"></param>
/// <param name="carNo">vin码</param>
/// <returns></returns>
[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;
}
/// <summary>
@ -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;
}
/// <summary>

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

Loading…
Cancel
Save