You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.9 KiB

6 months ago
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));
6 months ago
private static readonly ConcurrentDictionary<string, IoSession> Dictionary = new();
6 months ago
6 months ago
private static readonly ConcurrentDictionary<string, ModbusSession> ModbusDictionary = new();
6 months ago
6 months ago
public ConcurrentDictionary<string, object>? BusinessMap { get; set; }
6 months ago
public static IoSession? GetSession(string key)
{
6 months ago
if (Dictionary.ContainsKey(key))
{
IoSession? value;
Dictionary.TryGetValue(key, out value);
return value;
}
6 months ago
6 months ago
return null;
}
6 months ago
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);
}
6 months ago
public static void ChangeSessionKey(IoSession ioSession, string newKey)
{
var oldKey = ioSession.Key;
if (oldKey != null)
{
Dictionary.Remove(oldKey, out IoSession? session);
}
ioSession.Key = newKey;
Dictionary.AddOrUpdate(newKey, ioSession, (k, oldSession) => ioSession);
}
6 months ago
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)
{
6 months ago
var session = ChannelUtils.GetSessionBy(channel);
6 months ago
IoSession? outSession;
Dictionary.Remove(session.Key, out outSession);
session.Close();
}
public static void Broadcast(Object buffer)
6 months ago
{
foreach (var session in Dictionary.Values) session.Send(buffer);
6 months ago
}
6 months ago
public static object GetAttr(IoSession session, string key)
6 months ago
{
6 months ago
object? obj;
6 months ago
session.BusinessMap.TryGetValue(key, out obj);
return obj;
}
6 months ago
public static void SetAttr(IoSession session, string key, object obj)
6 months ago
{
session.BusinessMap.TryAdd(key, obj);
}
6 months ago
public static object GetAttrByKey(string? key, string mapKey)
6 months ago
{
if (!string.IsNullOrEmpty(key))
{
6 months ago
Dictionary.TryGetValue(key, out var session);
6 months ago
if (session != null)
{
6 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
6 months ago
6 months ago
if (obj != null) return (int)obj;
6 months ago
}
}
return 0;
}
6 months ago
public static void SetAttrModbus(ModbusSession session, string key, object obj)
6 months ago
{
6 months ago
if (session.BusinessMap == null) session.BusinessMap = new ConcurrentDictionary<string, object>();
6 months ago
session.BusinessMap.TryAdd(key, obj);
}
6 months ago
public static object GetAttrModbus(string key, string mapKey)
6 months ago
{
6 months ago
ModbusDictionary.TryGetValue(key, out var session);
6 months ago
if (session != null)
{
6 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
6 months ago
6 months ago
if (obj != null) return (int)obj;
6 months ago
}
return 0;
}
}