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

7 months ago
using System.Collections.Concurrent;
using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using log4net;
namespace HybirdFrameworkDriver.Session;
public class SessionMgr
{
private static readonly ILog Log = LogManager.GetLogger(typeof(SessionMgr));
7 months ago
private static readonly ConcurrentDictionary<string, IoSession> Dictionary = new();
7 months ago
7 months ago
private static readonly ConcurrentDictionary<string, ModbusSession> ModbusDictionary = new();
7 months ago
7 months ago
public ConcurrentDictionary<string, object>? BusinessMap { get; set; }
7 months ago
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);
}
7 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);
}
7 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)
{
7 months ago
var session = ChannelUtils.GetSessionBy(channel);
7 months ago
IoSession? outSession;
Dictionary.Remove(session.Key, out outSession);
session.Close();
}
7 months ago
public static void Broadcast(Object buffer)
7 months ago
{
7 months ago
foreach (var session in Dictionary.Values) session.Send(buffer);
7 months ago
}
7 months ago
public static object GetAttr(IoSession session, string key)
7 months ago
{
7 months ago
object? obj;
7 months ago
session.BusinessMap.TryGetValue(key, out obj);
return obj;
}
7 months ago
public static void SetAttr(IoSession session, string key, object obj)
7 months ago
{
session.BusinessMap.TryAdd(key, obj);
}
7 months ago
public static object GetAttrByKey(string? key, string mapKey)
7 months ago
{
if (!string.IsNullOrEmpty(key))
{
7 months ago
Dictionary.TryGetValue(key, out var session);
7 months ago
if (session != null)
{
7 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
7 months ago
7 months ago
if (obj != null) return (int)obj;
7 months ago
}
}
return 0;
}
7 months ago
public static void SetAttrModbus(ModbusSession session, string key, object obj)
7 months ago
{
7 months ago
if (session.BusinessMap == null) session.BusinessMap = new ConcurrentDictionary<string, object>();
7 months ago
session.BusinessMap.TryAdd(key, obj);
}
7 months ago
public static object GetAttrModbus(string key, string mapKey)
7 months ago
{
7 months ago
ModbusDictionary.TryGetValue(key, out var session);
7 months ago
if (session != null)
{
7 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
7 months ago
7 months ago
if (obj != null) return (int)obj;
7 months ago
}
return 0;
}
}