|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using DotNetty.Transport.Channels;
|
|
|
|
|
using log4net;
|
|
|
|
|
|
|
|
|
|
namespace HybirdFrameworkDriver.Session;
|
|
|
|
|
|
|
|
|
|
public class SessionMgr
|
|
|
|
|
{
|
|
|
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(SessionMgr));
|
|
|
|
|
|
|
|
|
|
private static readonly ConcurrentDictionary<string, IoSession> Dictionary = new();
|
|
|
|
|
|
|
|
|
|
private static readonly ConcurrentDictionary<string, ModbusSession> ModbusDictionary = new();
|
|
|
|
|
|
|
|
|
|
public ConcurrentDictionary<string, object>? BusinessMap { get; set; }
|
|
|
|
|
|
|
|
|
|
public static IoSession? GetSession(string key)
|
|
|
|
|
{
|
|
|
|
|
if (Dictionary.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
IoSession? value;
|
|
|
|
|
Dictionary.TryGetValue(key, out value);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<IoSession> GetSessionList()
|
|
|
|
|
{
|
|
|
|
|
return Dictionary.Values.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ModbusSession GetModbusSession(string key)
|
|
|
|
|
{
|
|
|
|
|
if (ModbusDictionary.ContainsKey(key))
|
|
|
|
|
{
|
|
|
|
|
ModbusSession value;
|
|
|
|
|
ModbusDictionary.TryGetValue(key, out value);
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RegisterSession(IChannel channel, IoSession ioSession)
|
|
|
|
|
{
|
|
|
|
|
var oldKey = ioSession.Key;
|
|
|
|
|
if (oldKey != null)
|
|
|
|
|
{
|
|
|
|
|
IoSession? session;
|
|
|
|
|
Dictionary.Remove(oldKey, out session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChannelUtils.AddChannelSession(channel, ioSession);
|
|
|
|
|
ioSession.Key = channel.Id.ToString();
|
|
|
|
|
Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void RegisterModbusSession(string key, ModbusSession ioSession)
|
|
|
|
|
{
|
|
|
|
|
var oldKey = ioSession.Key;
|
|
|
|
|
if (oldKey != null)
|
|
|
|
|
{
|
|
|
|
|
ModbusSession? session;
|
|
|
|
|
ModbusDictionary.Remove(oldKey, out session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ioSession.Key = key;
|
|
|
|
|
ModbusDictionary.AddOrUpdate(key, ioSession, (k, oldSession) => ioSession);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void UnregisterSession(IChannel channel)
|
|
|
|
|
{
|
|
|
|
|
var session = ChannelUtils.GetSessionBy(channel);
|
|
|
|
|
IoSession? outSession;
|
|
|
|
|
Dictionary.Remove(session.Key, out outSession);
|
|
|
|
|
session.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void Broadcast(Object buffer)
|
|
|
|
|
{
|
|
|
|
|
foreach (var session in Dictionary.Values) session.Send(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object GetAttr(IoSession session, string key)
|
|
|
|
|
{
|
|
|
|
|
object? obj;
|
|
|
|
|
session.BusinessMap.TryGetValue(key, out obj);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetAttr(IoSession session, string key, object obj)
|
|
|
|
|
{
|
|
|
|
|
session.BusinessMap.TryAdd(key, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object GetAttrByKey(string? key, string mapKey)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(key))
|
|
|
|
|
{
|
|
|
|
|
Dictionary.TryGetValue(key, out var session);
|
|
|
|
|
|
|
|
|
|
if (session != null)
|
|
|
|
|
{
|
|
|
|
|
session.BusinessMap.TryGetValue(mapKey, out var obj);
|
|
|
|
|
|
|
|
|
|
if (obj != null) return (int)obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetAttrModbus(ModbusSession session, string key, object obj)
|
|
|
|
|
{
|
|
|
|
|
if (session.BusinessMap == null) session.BusinessMap = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
session.BusinessMap.TryAdd(key, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static object GetAttrModbus(string key, string mapKey)
|
|
|
|
|
{
|
|
|
|
|
ModbusDictionary.TryGetValue(key, out var session);
|
|
|
|
|
|
|
|
|
|
if (session != null)
|
|
|
|
|
{
|
|
|
|
|
session.BusinessMap.TryGetValue(mapKey, out var obj);
|
|
|
|
|
|
|
|
|
|
if (obj != null) return (int)obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|