/// <summary>
        /// Inserts a  ChannelHandler before an existing handler of this pipeline.
        //  The name of the handlers is taken from the NameAttribute of the handler class.
        /// </summary>
        /// <typeparam name="TBefore"></typeparam>
        /// <param name="channelPipeline"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static IChannelPipeline AddBefore <TBefore>(this IChannelPipeline channelPipeline, IChannelHandler handler)
        {
            var name     = handler.GetType().GetTypeInfo().GetCustomAttribute <NameAttribute>(false).Name;
            var baseName = typeof(TBefore).GetTypeInfo().GetCustomAttribute <NameAttribute>(false).Name;

            return(channelPipeline.AddBefore(baseName, name, handler));
        }
예제 #2
0
        internal string GenerateName(IChannelHandler handler)
        {
            ConditionalWeakTable <Type, string> cache = NameCaches[Thread.CurrentThread.ManagedThreadId % NameCaches.Length];
            Type   handlerType = handler.GetType();
            string name        = cache.GetValue(handlerType, GenerateName0);

            lock (this.head)
            {
                // It's not very likely for a user to put more than one handler of the same type, but make sure to avoid
                // any name conflicts.  Note that we don't cache the names generated here.
                if (this.nameContextMap.ContainsKey(name))
                {
                    string baseName = name.Substring(0, name.Length - 1); // Strip the trailing '0'.
                    for (int i = 1;; i++)
                    {
                        string newName = baseName + i;
                        if (!this.nameContextMap.ContainsKey(newName))
                        {
                            name = newName;
                            break;
                        }
                    }
                }
            }

            return(name);
        }
        protected static SkipFlags GetSkipPropagationFlags(IChannelHandler handler)
        {
            Tuple <SkipFlags> skipDirection = SkipTable.GetValue(
                handler.GetType(),
                handlerType => Tuple.Create(CalculateSkipPropagationFlags(handlerType)));

            return(skipDirection?.Item1 ?? 0);
        }
예제 #4
0
        protected static int GetSkipPropagationFlags(IChannelHandler handler)
        {
            var skipDirection = SkipTable.GetValue(
                handler.GetType(),
                handlerType => Tuple.Create(CalculateSkipPropagationFlags(handlerType)));

            return(skipDirection == null ? 0 : skipDirection.Item1);
        }
        protected static int GetSkipPropagationFlags(IChannelHandler handler)
        {
            Tuple<int> skipDirection = SkipTable.GetValue(
                handler.GetType(),
                handlerType => Tuple.Create(CalculateSkipPropagationFlags(handlerType)));

            return skipDirection == null ? 0 : skipDirection.Item1;
        }
        protected static PropagationDirections GetSkipPropagationFlags(IChannelHandler handler)
        {
            Tuple <PropagationDirections> skipDirection = SkipTable.GetValue(
                handler.GetType(),
                handlerType => Tuple.Create(CalculateSkipPropagationFlags(handlerType)));

            return(skipDirection == null ? PropagationDirections.None : skipDirection.Item1);
        }
예제 #7
0
        /// <summary>
        /// 指定函数名称的函数是否被标记为Skip(不包含父类)
        /// </summary>
        /// <param name="handlerType"></param>
        /// <param name="methodName"></param>
        /// <param name="paramTypes"></param>
        /// <returns></returns>
        protected virtual bool IsSkippable(IChannelHandler handler, string methodName)
        {
            var handlerType = handler.GetType();

            var attr = handlerType.GetMethod(methodName).GetCustomAttribute <SkipAttribute>(false);

            return(attr != null);
        }
예제 #8
0
 public ServerSocketChannel Hander(IChannelHandler handler)
 {
     if (handler != null)
     {
         if (handler.GetType().IsImplementedRawGeneric(typeof(MessageToByteEncoder <>)))
         {
             handers[HanderType.Encoder] = handler;
         }
         else if (handler.GetType().IsImplementedRawGeneric(typeof(MessageToMessageDecoder <>)))
         {
             handers[HanderType.Decoder] = handler;
         }
         else if (typeof(ChannelHandlerAdapter).IsAssignableFrom(handler.GetType()))
         {
             handers[HanderType.ReadWrite] = handler;
         }
     }
     return(this);
 }
예제 #9
0
        AbstractChannelHandlerContext GetContextOrThrow(IChannelHandler handler)
        {
            var ctx = (AbstractChannelHandlerContext)this.Context(handler);

            if (ctx == null)
            {
                throw new ArgumentException($"Handler of type `{handler.GetType().Name}` could not be found in the pipeline.");
            }

            return(ctx);
        }
예제 #10
0
        private string FilterName(string name, IChannelHandler handler)
        {
            if (string.IsNullOrEmpty(name))
            {
                name = handler.GetType().Name + "$" + Interlocked.Increment(ref _nextRandomName);
            }
            if (!_nameContextMap.ContainsKey(name))
            {
                return(name);
            }

            throw new ArgumentException($"Duplciate handler name: {name}", nameof(name));
        }
예제 #11
0
        string GenerateName(IChannelHandler handler)
        {
            ConditionalWeakTable <Type, string> cache = NameCaches.Value;
            Type   handlerType = handler.GetType();
            string name        = cache.GetValue(handlerType, t => GenerateName0(t));

            // It's not very likely for a user to put more than one handler of the same type, but make sure to avoid
            // any name conflicts.  Note that we don't cache the names generated here.
            if (this.Context0(name) != null)
            {
                string baseName = name.Substring(0, name.Length - 1); // Strip the trailing '0'.
                for (int i = 1;; i++)
                {
                    string newName = baseName + i;
                    if (this.Context0(newName) == null)
                    {
                        name = newName;
                        break;
                    }
                }
            }
            return(name);
        }
        /// <summary>
        /// Appends a ChannelHandler at the last position of this pipeline.
        //  The name of the handler to append is taken from the NameAttribute of the handler class.
        /// </summary>
        /// <param name="channelPipeline"></param>
        /// <param name="name"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static IChannelPipeline AddLast2(this IChannelPipeline channelPipeline, IChannelHandler handler)
        {
            var nameAttribute = handler.GetType().GetTypeInfo().GetCustomAttribute <NameAttribute>(false);

            return(channelPipeline.AddLast(nameAttribute.Name, handler));
        }
예제 #13
0
        AbstractChannelHandlerContext GetContextOrThrow(IChannelHandler handler)
        {
            var ctx = (AbstractChannelHandlerContext)this.Context(handler);

            if (ctx == null)
            {
                throw new ArgumentException(string.Format("Handler of type `{0}` could not be found in the pipeline.", handler.GetType().Name));
            }
            else
            {
                return(ctx);
            }
        }