Ejemplo n.º 1
0
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing">
        ///     <c>true</c> to release both managed and unmanaged resources;
        ///     <c>false</c> to release only unmanaged resources.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                IEnumerable <IProtocolHandler> handlers;

                try
                {
                    _handlersLock.TryEnterWriteLock(-1);

                    handlers = HandlersByType.Values.ToList();

                    HandlersByType.Clear();
                    HandlersByProtocol.Clear();

                    foreach (var handler in handlers)
                    {
                        handler.Dispose();
                    }
                }
                finally
                {
                    _handlersLock.ExitWriteLock();
                }
            }

            base.Dispose(disposing);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the unsupported protocols.
        /// </summary>
        /// <param name="supportedProtocols">The supported protocols.</param>
        protected virtual void HandleUnsupportedProtocols(IList <ISupportedProtocol> supportedProtocols)
        {
            // remove unsupported handler mappings (excluding Core protocol)
            try
            {
                _handlersLock.TryEnterReadLock(-1);

                HandlersByType
                .Where(x => x.Value.Protocol > 0 && !supportedProtocols.Contains(x.Value.Protocol, x.Value.Role))
                .ToList()
                .ForEach(x =>
                {
                    x.Value.Session = null;
                    HandlersByType.Remove(x.Key);
                    HandlersByProtocol.Remove(x.Value.Protocol);
                });

                // update remaining handler mappings by protocol
                foreach (var handler in HandlersByType.Values.ToArray())
                {
                    if (!HandlersByProtocol.ContainsKey(handler.Protocol))
                    {
                        HandlersByProtocol[handler.Protocol] = handler;
                    }
                }
            }
            finally
            {
                _handlersLock.ExitReadLock();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Determines whether this instance can handle the specified protocol.
        /// </summary>
        /// <typeparam name="T">The protocol handler interface.</typeparam>
        /// <returns>
        ///   <c>true</c> if the specified protocol handler has been registered; otherwise, <c>false</c>.
        /// </returns>
        public bool CanHandle <T>() where T : IProtocolHandler
        {
            try
            {
                _handlersLock.TryEnterReadLock(-1);

                return(HandlersByType.ContainsKey(typeof(T)));
            }
            finally
            {
                _handlersLock.ExitReadLock();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the registered protocol handler for the specified ETP interface.
        /// </summary>
        /// <typeparam name="T">The protocol handler interface.</typeparam>
        /// <returns>The registered protocol handler instance.</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public T Handler <T>() where T : IProtocolHandler
        {
            try
            {
                _handlersLock.TryEnterReadLock(-1);

                IProtocolHandler handler;
                if (HandlersByType.TryGetValue(typeof(T), out handler) && handler is T)
                {
                    return((T)handler);
                }
            }
            finally
            {
                _handlersLock.ExitReadLock();
            }

            Logger.Debug(Log("[{0}] Protocol handler not registered for {1}.", SessionId, typeof(T).FullName));
            throw new NotSupportedException($"Protocol handler not registered for { typeof(T).FullName }.");
        }