using DotNetty.Common.Utilities; using DotNetty.Transport.Channels; namespace HybirdFrameworkDriver.Session; public static class ChannelUtils { private static readonly AttributeKey SessionKey = AttributeKey.ValueOf("session"); /** * 添加新的会话 * @param channel * @param session * @return */ public static void AddChannelSession(IChannel channel, IoSession session) { AddAttr(channel, SessionKey, session); } public static void AddAttr(IChannel channel, AttributeKey key, T t) where T : class { var attribute = channel.GetAttribute(key); attribute.Set(t); } public static T? GetAttr(IChannel channel, AttributeKey key) where T : class { var attribute = channel.GetAttribute(key); return attribute.Get(); } public static IoSession GetSessionBy(IChannel channel) { var sessionAttr = channel.GetAttribute(SessionKey); return sessionAttr.Get(); } public static string GetIp(IChannel channel) { return channel.RemoteAddress.Serialize().ToString(); } }