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 Dictionary = new ConcurrentDictionary(); public static void RegisterSession(IChannel channel, IoSession ioSession) { ioSession.Key = channel.Id.ToString(); IoSession? session; Dictionary.Remove(ioSession.Key, out session); ChannelUtils.AddChannelSession(channel, ioSession); Dictionary.AddOrUpdate(channel.Id.ToString(), 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); } } }