Created
October 11, 2018 14:51
-
-
Save KillerGoldFisch/b1bf41f6424619a0266ba4a511001a98 to your computer and use it in GitHub Desktop.
Server extensions for https://github.com/chkr1011/MQTTnet, exposes the session manager and sessions via reflection. Remember to lock the session dictionary before using it!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MQTTnet.Server; | |
using System; | |
using System.Collections.Generic; | |
using System.Reflection; | |
namespace MQTTnet.Server.Extensions | |
{ | |
public static class MQTTnetExtensions | |
{ | |
private static FieldInfo MqttClientSessionsManager_FI = typeof(MqttServer).GetField("_clientSessionsManager", BindingFlags.Instance | BindingFlags.NonPublic); | |
private static FieldInfo MqttClientSessions_FI = typeof(MqttClientSessionsManager).GetField("_sessions", BindingFlags.Instance | BindingFlags.NonPublic); | |
public static bool WithSession(this IMqttServer self, string sessionId, Action<MqttClientSession> callback) | |
{ | |
var sessManager = self.GetMqttClientSessionsManager(); | |
if (sessManager is null) return false; | |
var sessions = sessManager.GetMqttClientSessions(); | |
if (sessions is null) return false; | |
lock(sessions) { | |
if (!sessions.ContainsKey(sessionId)) return false; | |
callback?.Invoke(sessions[sessionId]); | |
} | |
return true; | |
} | |
public static MqttClientSessionsManager GetMqttClientSessionsManager(this IMqttServer self) | |
{ | |
return MqttClientSessionsManager_FI.GetValue(self) as MqttClientSessionsManager; | |
} | |
public static Dictionary<string, MqttClientSession> GetMqttClientSessions(this MqttClientSessionsManager self) | |
{ | |
return MqttClientSessions_FI.GetValue(self) as Dictionary<string, MqttClientSession>; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment