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.
94 lines
2.5 KiB
94 lines
2.5 KiB
using System.Collections.Concurrent;
|
|
using DotNetty.Buffers;
|
|
using DotNetty.Transport.Channels;
|
|
using log4net;
|
|
|
|
namespace HybirdFrameworkDriver.TcpServer;
|
|
|
|
public class SessionMgr
|
|
{
|
|
private static readonly ILog Log = LogManager.GetLogger(typeof(SessionMgr));
|
|
|
|
private static readonly ConcurrentDictionary<string, IoSession> Dictionary =
|
|
new ConcurrentDictionary<string, IoSession>();
|
|
|
|
private static readonly ConcurrentDictionary<string, ModbusSession> ModbusDictionary =
|
|
new ConcurrentDictionary<string, ModbusSession>();
|
|
|
|
public static void RegisterSession(string key, IoSession ioSession)
|
|
{
|
|
ioSession.Key = key;
|
|
IoSession? session;
|
|
Dictionary.Remove(ioSession.Key, out session);
|
|
Dictionary.AddOrUpdate(key, ioSession, (k, oldSession) => ioSession);
|
|
}
|
|
|
|
public static void UnregisterSession(IChannel channel)
|
|
{
|
|
IoSession session = ChannelUtils.GetSessionBy(channel);
|
|
IoSession? outSession;
|
|
Dictionary.Remove(session.Key, out outSession);
|
|
session.Close();
|
|
}
|
|
|
|
public static void Broadcast(IByteBuffer buffer)
|
|
{
|
|
foreach (IoSession session in Dictionary.Values)
|
|
{
|
|
session.Send(buffer);
|
|
}
|
|
}
|
|
|
|
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 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 ModbusSession? session);
|
|
|
|
if (session != null)
|
|
{
|
|
session.BusinessMap.TryGetValue(mapKey, out Object? obj);
|
|
|
|
if (obj != null)
|
|
{
|
|
return (int)obj;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static ModbusSession GetModbusSession(string key)
|
|
{
|
|
if (ModbusDictionary.ContainsKey(key))
|
|
{
|
|
ModbusSession value;
|
|
ModbusDictionary.TryGetValue(key, out value);
|
|
|
|
return value;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
} |