Skip to content

Commit 95a4ba6

Browse files
authored
optimizing message ack (#65)
1 parent 4ec2590 commit 95a4ba6

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

nanoFramework.Azure.Devices.Client/DeviceClient.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -269,21 +269,20 @@ public bool UpdateReportedProperties(TwinCollection reported, CancellationToken
269269
Debug.WriteLine($"update twin: {twin}");
270270
var rid = _mqttc.Publish($"{TwinReportedPropertiesTopic}?$rid={Guid.NewGuid()}", Encoding.UTF8.GetBytes(twin), MqttQoSLevel.AtLeastOnce, false);
271271
ConfirmationStatus conf = new(rid);
272+
_waitForConfirmation.Add(conf);
272273
_ioTHubStatus.Status = Status.TwinUpdated;
273274
_ioTHubStatus.Message = string.Empty;
274275
StatusUpdated?.Invoke(this, new StatusUpdatedEventArgs(_ioTHubStatus));
275276

276277
if (cancellationToken.CanBeCanceled)
277-
{
278-
_waitForConfirmation.Add(conf);
278+
{
279279
while (!conf.Received && !cancellationToken.IsCancellationRequested)
280280
{
281281
cancellationToken.WaitHandle.WaitOne(200, true);
282-
}
283-
284-
_waitForConfirmation.Remove(conf);
282+
}
285283
}
286284

285+
_waitForConfirmation.Remove(conf);
287286
return conf.Received;
288287
}
289288

@@ -323,19 +322,17 @@ public bool SendMessage(string message, CancellationToken cancellationToken = de
323322

324323
var rid = _mqttc.Publish(topic, Encoding.UTF8.GetBytes(message), QosLevel, false);
325324
ConfirmationStatus conf = new(rid);
325+
_waitForConfirmation.Add(conf);
326326

327327
if (cancellationToken.CanBeCanceled)
328-
{
329-
330-
_waitForConfirmation.Add(conf);
328+
{
331329
while (!conf.Received && !cancellationToken.IsCancellationRequested)
332330
{
333331
cancellationToken.WaitHandle.WaitOne(200, true);
334-
}
335-
336-
_waitForConfirmation.Remove(conf);
332+
}
337333
}
338334

335+
_waitForConfirmation.Remove(conf);
339336
return conf.Received;
340337
}
341338

0 commit comments

Comments
 (0)