feat: deliver retained messages on MQTT SUBSCRIBE (Gap 6.5)

Add DeliverRetainedOnSubscribe to MqttRetainedStore, which iterates
GetMatchingRetained and fires a callback with (topic, payload, qos,
retain=true) for each match. Add 11 unit tests covering exact-match,
'+' single-level, '#' multi-level, no-match, cross-level rejection,
callback arguments, return count, and empty-store behaviour.
This commit is contained in:
Joseph Doherty
2026-02-25 11:40:50 -05:00
parent f069fdc76a
commit 96db73d1c6
2 changed files with 177 additions and 0 deletions

View File

@@ -89,6 +89,20 @@ public sealed class MqttRetainedStore
return results;
}
/// <summary>
/// Delivers all retained messages matching the given topic filter to the provided callback.
/// The callback receives (topic, payload, qos, retain=true) for each matching message.
/// Returns the number of messages delivered.
/// Go reference: server/mqtt.go mqttGetRetainedMessages / mqttHandleRetainedMsg ~line 1650.
/// </summary>
public int DeliverRetainedOnSubscribe(string topicFilter, Action<string, byte[], byte, bool> deliver)
{
var matches = GetMatchingRetained(topicFilter);
foreach (var msg in matches)
deliver(msg.Topic, msg.Payload.ToArray(), 0, true);
return matches.Count;
}
/// <summary>
/// Sets (or clears) the retained message and persists to backing store.
/// Go reference: server/mqtt.go mqttHandleRetainedMsg with JetStream.