-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDesireCache.cs
65 lines (54 loc) · 1.7 KB
/
DesireCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using MQTT.Types;
using MQTT.Commands;
namespace MQTT.Domain
{
internal class DesireCache
{
readonly Dictionary<int, Desire> _desires = new Dictionary<int, Desire>();
internal void AddAndRemoveDuplicates(Desire d)
{
int key = GetKeyFrom(d.Message, d.MessageId);
if (_desires.ContainsKey(key))
{
_desires.Remove(key);
}
_desires.Add(key, d);
}
internal bool TryGetAndRemove(CommandMessage commandMessage, MessageId messageId, out Desire desire)
{
int key = GetKeyFrom(commandMessage, messageId);
if (_desires.TryGetValue(key, out desire))
{
_desires.Remove(key);
return true;
}
return false;
}
private int GetKeyFrom(CommandMessage message, MessageId id)
{
return (((ushort)message) << 16) + id.Value;
}
}
internal class Desire
{
public Desire(CommandMessage msg, MessageId id, Action<MqttCommand> fulfilled)
{
if (fulfilled == null)
{
throw new ArgumentNullException("fulfilled", "The fulfilled action cannot be null");
}
Message = msg;
MessageId = id;
_fulfilled = fulfilled;
}
public CommandMessage Message { get; private set; }
public MessageId MessageId { get; private set; }
private readonly Action<MqttCommand> _fulfilled;
public void Fulfilled(MqttCommand command)
{
_fulfilled(command);
}
}
}