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.

140 lines
3.6 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));
6 months ago
private static readonly ConcurrentDictionary<string, IoSession> Dictionary = new();
7 months ago
6 months ago
private static readonly ConcurrentDictionary<string, ModbusSession> ModbusDictionary = new();
7 months ago
6 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);
}
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);
7 months ago
IoSession? outSession;
Dictionary.Remove(session.Key, out outSession);
session.Close();
}
public static void Broadcast(IByteBuffer buffer, ConcurrentDictionary<string, IoSession> dictionary)
{
6 months ago
foreach (var session in dictionary.Values) session.Send(buffer);
7 months ago
}
6 months ago
public static object GetAttr(IoSession session, string key)
7 months ago
{
6 months ago
object? obj;
7 months ago
session.BusinessMap.TryGetValue(key, out obj);
return obj;
}
6 months ago
public static void SetAttr(IoSession session, string key, object obj)
7 months ago
{
session.BusinessMap.TryAdd(key, obj);
}
6 months ago
public static object GetAttrByKey(string? key, string mapKey)
7 months ago
{
if (!string.IsNullOrEmpty(key))
{
6 months ago
Dictionary.TryGetValue(key, out var session);
7 months ago
if (session != null)
{
6 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
7 months ago
6 months ago
if (obj != null) return (int)obj;
7 months ago
}
}
return 0;
}
6 months ago
public static void SetAttrModbus(ModbusSession session, string key, object obj)
7 months ago
{
6 months ago
if (session.BusinessMap == null) session.BusinessMap = new ConcurrentDictionary<string, object>();
7 months ago
session.BusinessMap.TryAdd(key, obj);
}
6 months ago
public static object GetAttrModbus(string key, string mapKey)
7 months ago
{
6 months ago
ModbusDictionary.TryGetValue(key, out var session);
7 months ago
if (session != null)
{
6 months ago
session.BusinessMap.TryGetValue(mapKey, out var obj);
7 months ago
6 months ago
if (obj != null) return (int)obj;
7 months ago
}
return 0;
}
}