From 256840279b5739935edb727154f9dd04d2127793 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Sun, 26 May 2024 17:15:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkDriver/Session/IoSession.cs | 3 +-- HybirdFrameworkDriver/Session/SessionMgr.cs | 16 ++++++++++++++-- .../TcpServer/ServerListenerHandler.cs | 7 +++++++ HybirdFrameworkDriver/TcpServer/TcpServer.cs | 13 ++++++++----- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/HybirdFrameworkDriver/Session/IoSession.cs b/HybirdFrameworkDriver/Session/IoSession.cs index 88a8d88..6496749 100644 --- a/HybirdFrameworkDriver/Session/IoSession.cs +++ b/HybirdFrameworkDriver/Session/IoSession.cs @@ -1,5 +1,4 @@ using System.Collections.Concurrent; -using DotNetty.Buffers; using DotNetty.Transport.Channels; using log4net; @@ -25,7 +24,7 @@ public class IoSession public ConcurrentDictionary BusinessMap { get; } = new(); - public void Send(IByteBuffer buffer) + public void Send(Object buffer) { Channel.WriteAndFlushAsync(buffer); } diff --git a/HybirdFrameworkDriver/Session/SessionMgr.cs b/HybirdFrameworkDriver/Session/SessionMgr.cs index 4ef69dd..89dc4f3 100644 --- a/HybirdFrameworkDriver/Session/SessionMgr.cs +++ b/HybirdFrameworkDriver/Session/SessionMgr.cs @@ -60,6 +60,18 @@ public class SessionMgr Dictionary.AddOrUpdate(channel.Id.ToString(), ioSession, (k, oldSession) => ioSession); } + 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); + } + public static void RegisterModbusSession(string key, ModbusSession ioSession) { @@ -83,9 +95,9 @@ public class SessionMgr } - public static void Broadcast(IByteBuffer buffer, ConcurrentDictionary dictionary) + public static void Broadcast(Object buffer) { - foreach (var session in dictionary.Values) session.Send(buffer); + foreach (var session in Dictionary.Values) session.Send(buffer); } public static object GetAttr(IoSession session, string key) diff --git a/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs b/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs index d5e7597..0b06ba1 100644 --- a/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs +++ b/HybirdFrameworkDriver/TcpServer/ServerListenerHandler.cs @@ -13,6 +13,8 @@ public class ServerListenerHandler : ChannelHandlerAdapter where TH { private static readonly ILog Log = LogManager.GetLogger(typeof(ServerListenerHandler)); + public Action? ChannelInActiveAction { get; set; } + public override void ChannelRegistered(IChannelHandlerContext context) { base.ChannelRegistered(context); @@ -35,6 +37,11 @@ public class ServerListenerHandler : ChannelHandlerAdapter where TH public override void ChannelInactive(IChannelHandlerContext context) { base.ChannelInactive(context); + if (ChannelInActiveAction != null) + { + ChannelInActiveAction(); + } + SessionMgr.UnregisterSession(context.Channel); Log.Info("inactive " + context.Channel); } diff --git a/HybirdFrameworkDriver/TcpServer/TcpServer.cs b/HybirdFrameworkDriver/TcpServer/TcpServer.cs index 0faa7a8..bb5096d 100644 --- a/HybirdFrameworkDriver/TcpServer/TcpServer.cs +++ b/HybirdFrameworkDriver/TcpServer/TcpServer.cs @@ -32,9 +32,12 @@ public class TcpServer : IDisposable where TH : IChannelHandler private int _port = 9000; public LogLevel? LogLevel { get; set; } + + public Action? ChannelInActiveAction { get; set; } public TcpServer() { + if (LogLevel != null) { InternalLoggerFactory.DefaultFactory.AddProvider(new Log4NetProvider()); @@ -49,12 +52,12 @@ public class TcpServer : IDisposable where TH : IChannelHandler .Handler(new LoggingHandler()) .ChildHandler(new ActionChannelInitializer(channel => { - var serverListenerHandler = new ServerListenerHandler(); - var pipeline = channel.Pipeline; - if (LogLevel != null) + var serverListenerHandler = new ServerListenerHandler { - pipeline.AddLast(new LoggingHandler(LogLevel.Value)); - } + ChannelInActiveAction = ChannelInActiveAction + }; + var pipeline = channel.Pipeline; + pipeline.AddLast(new LoggingHandler()); pipeline.AddLast(serverListenerHandler); pipeline.AddLast(new IdleStateHandler(0, 0, 180)); //检测空闲连接 //业务handler ,这里是实际处理业务的Handler