Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions MqttLib/Core/QoSManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MqttLib.Core.Messages;
Expand Down Expand Up @@ -158,34 +160,35 @@ public void PublishAccepted(ushort messageID, bool accepted)
}
}

private List<MqttMessage> GetResendMessages(DateTime now)
{
lock (_messages)
{
return _messages.Values.Cast<MqttMessage>()
.Where(x => (now - new DateTime(x.Timestamp)).TotalMilliseconds >= _resendInterval)
.ToList(); // Force enumeration so we can release the lock
}
}

private void MessageDaemon()
{
// NOTE: This function should be called in it's own thread

while (_running)
{
var now = DateTime.Now;
// Check if we should re-send some messages
lock (_messages)
foreach (var mess in GetResendMessages(now))
{
DateTime now = DateTime.Now;

foreach (MqttMessage mess in _messages.Values)
mess.Timestamp = now.Ticks;
mess.Duplicate = true;
try
{
TimeSpan ts = now - new DateTime(mess.Timestamp);
if( ts.TotalMilliseconds >= _resendInterval )
{
mess.Timestamp = now.Ticks;
mess.Duplicate = true;
try
{
Log.Write( LogLevel.DEBUG, "Re-Sending - " + mess.MessageID);
_strManager.SendMessage(mess);
}
catch (Exception e) {
Log.Write(LogLevel.ERROR, e.ToString());
// If we fail for some reason, we will try again another time automatically
}
}
Log.Write( LogLevel.DEBUG, "Re-Sending - " + mess.MessageID);
_strManager.SendMessage(mess);
}
catch (Exception e) {
Log.Write(LogLevel.ERROR, e.ToString());
// If we fail for some reason, we will try again another time automatically
}
}
Thread.Sleep(2000);
Expand Down
2 changes: 2 additions & 0 deletions MqttLib/Mqtt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ void qosManager_MessageReceived(object sender, MqttMessageReceivedEventArgs e)
OnPublished(new CompleteArgs(puback.AckID));
break;
case MessageType.PUBCOMP:
var puback2 = (MqttPubcompMessage)e.Message;
OnPublished(new CompleteArgs(puback2.AckID));
break;
case MessageType.PUBLISH:
MqttPublishMessage m = (MqttPublishMessage)e.Message;
Expand Down