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