Fix
This commit is contained in:
@@ -49,8 +49,9 @@ namespace Modbus.Net
|
||||
/// 发送内部
|
||||
/// </summary>
|
||||
/// <param name="message">发送的信息</param>
|
||||
/// <param name="repeat">是否为重发消息</param>
|
||||
/// <returns>发送信息的定义</returns>
|
||||
protected async Task<MessageWaitingDef> SendMsgInner(byte[] message)
|
||||
protected async Task<MessageWaitingDef> SendMsgInner(byte[] message, bool repeat = false)
|
||||
{
|
||||
IDisposable asyncLock = null;
|
||||
try
|
||||
@@ -73,6 +74,10 @@ namespace Modbus.Net
|
||||
success = messageSendingdef.ReceiveMutex.WaitOne(TimeoutTime);
|
||||
if (success)
|
||||
{
|
||||
if (!repeat && messageSendingdef.ReceiveMessage == null)
|
||||
{
|
||||
return await SendMsgInner(message, true);
|
||||
}
|
||||
return messageSendingdef;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,6 +159,25 @@ namespace Modbus.Net
|
||||
if (length == -1) break;
|
||||
if (length == 0) return null;
|
||||
}
|
||||
if (skipLength > 0)
|
||||
{
|
||||
lock (WaitingMessages)
|
||||
{
|
||||
var def = GetMessageFromWaitingList(null);
|
||||
if (def != null)
|
||||
{
|
||||
lock (WaitingMessages)
|
||||
{
|
||||
if (WaitingMessages.IndexOf(def) >= 0)
|
||||
{
|
||||
WaitingMessages.Remove(def);
|
||||
}
|
||||
}
|
||||
def.ReceiveMutex.Set();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
foreach (var message in duplicatedMessages)
|
||||
{
|
||||
|
||||
@@ -16,12 +16,8 @@ namespace Modbus.Net
|
||||
|
||||
private bool _taskCancel = false;
|
||||
|
||||
private bool _activateSema = false;
|
||||
|
||||
private int _waitingListMaxCount;
|
||||
|
||||
private Semaphore _taskCycleSema;
|
||||
|
||||
/// <summary>
|
||||
/// 间隔时间
|
||||
/// </summary>
|
||||
@@ -31,33 +27,25 @@ namespace Modbus.Net
|
||||
/// 构造器
|
||||
/// </summary>
|
||||
/// <param name="acquireTime">间隔时间</param>
|
||||
/// <param name="activateSema">是否开启信号量</param>
|
||||
/// <param name="lengthCalc">包切分长度函数</param>
|
||||
/// <param name="checkRightFunc">包校验函数</param>
|
||||
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
||||
public FifoController(int acquireTime, bool activateSema = true, Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null)
|
||||
public FifoController(int acquireTime, Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null)
|
||||
: base(lengthCalc, checkRightFunc)
|
||||
{
|
||||
_waitingListMaxCount = int.Parse(waitingListMaxCount != null ? waitingListMaxCount.ToString() : null ?? ConfigurationReader.GetValueDirect("Controller", "WaitingListCount"));
|
||||
_activateSema = activateSema;
|
||||
if (_activateSema)
|
||||
{
|
||||
_taskCycleSema = new Semaphore(0, _waitingListMaxCount);
|
||||
}
|
||||
AcquireTime = acquireTime;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void SendingMessageControlInner()
|
||||
{
|
||||
_taskCycleSema?.WaitOne();
|
||||
while (!_taskCancel)
|
||||
{
|
||||
if (AcquireTime > 0)
|
||||
{
|
||||
Thread.Sleep(AcquireTime);
|
||||
}
|
||||
bool sendSuccess = false;
|
||||
lock (WaitingMessages)
|
||||
{
|
||||
try
|
||||
@@ -68,7 +56,6 @@ namespace Modbus.Net
|
||||
{
|
||||
_currentSendingPos = WaitingMessages.First();
|
||||
_currentSendingPos.SendMutex.Set();
|
||||
sendSuccess = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -76,13 +63,11 @@ namespace Modbus.Net
|
||||
if (WaitingMessages.Count <= 0)
|
||||
{
|
||||
_currentSendingPos = null;
|
||||
sendSuccess = true;
|
||||
}
|
||||
else if (WaitingMessages.IndexOf(_currentSendingPos) == -1)
|
||||
{
|
||||
_currentSendingPos = WaitingMessages.First();
|
||||
_currentSendingPos.SendMutex.Set();
|
||||
sendSuccess = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,7 +75,6 @@ namespace Modbus.Net
|
||||
{
|
||||
logger.LogError(e, "Controller _currentSendingPos disposed");
|
||||
_currentSendingPos = null;
|
||||
sendSuccess = true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -98,23 +82,13 @@ namespace Modbus.Net
|
||||
_taskCancel = true;
|
||||
}
|
||||
}
|
||||
if (sendSuccess)
|
||||
{
|
||||
_taskCycleSema?.WaitOne();
|
||||
}
|
||||
}
|
||||
_taskCycleSema.Dispose();
|
||||
_taskCycleSema = null;
|
||||
Clear();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SendStart()
|
||||
{
|
||||
if (_taskCycleSema == null && _activateSema)
|
||||
{
|
||||
_taskCycleSema = new Semaphore(0, _waitingListMaxCount);
|
||||
}
|
||||
_taskCancel = false;
|
||||
base.SendStart();
|
||||
}
|
||||
@@ -154,10 +128,6 @@ namespace Modbus.Net
|
||||
return false;
|
||||
}
|
||||
var success = base.AddMessageToList(def);
|
||||
if (success)
|
||||
{
|
||||
_taskCycleSema?.Release();
|
||||
}
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,11 @@ namespace Modbus.Net
|
||||
/// </summary>
|
||||
/// <param name="keyMatches">匹配字典,每个Collection代表一个匹配集合,每一个匹配集合中的数字代表需要匹配的位置,最后计算出来的数字是所有位置数字按照集合排序后叠放在一起</param>
|
||||
/// <param name="acquireTime">获取间隔</param>
|
||||
/// <param name="activateSema">是否开启信号量</param>
|
||||
/// <param name="lengthCalc">包长度计算</param>
|
||||
/// <param name="checkRightFunc">包校验函数</param>
|
||||
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
||||
public MatchController(ICollection<(int, int)>[] keyMatches, int acquireTime, bool activateSema = true,
|
||||
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(acquireTime, activateSema, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||
public MatchController(ICollection<(int, int)>[] keyMatches, int acquireTime,
|
||||
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(acquireTime, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||
{
|
||||
KeyMatches = keyMatches;
|
||||
}
|
||||
@@ -52,6 +51,7 @@ namespace Modbus.Net
|
||||
/// <inheritdoc />
|
||||
protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage)
|
||||
{
|
||||
if (receiveMessage == null) return null;
|
||||
var returnKey = GetKeyFromMessage(receiveMessage);
|
||||
MessageWaitingDef ans;
|
||||
lock (WaitingMessages)
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Modbus.Net
|
||||
/// <inheritdoc />
|
||||
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
||||
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(keyMatches,
|
||||
0, false, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||
0, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user