Esempio n. 1
0
        /// <summary>
        /// Creates an instance of an implementation of <see cref="IProtocol"/> depending on the provided type.
        /// </summary>
        /// <param name="protocolType">The type of protocol to instantiate.</param>
        /// <returns>A new <see cref="IProtocol"/> implementation instance.</returns>
        public IProtocol CreateForType(OpenTibiaProtocolType protocolType)
        {
            if (!this.protocolInstancesCreated.ContainsKey(protocolType))
            {
                lock (this.protocolCreationLock)
                {
                    if (!this.protocolInstancesCreated.ContainsKey(protocolType))
                    {
                        var handlerSelector = this.handlerSelectorsKnown.FirstOrDefault(handler => handler.ForProtocol == protocolType);

                        if (handlerSelector == null)
                        {
                            throw new NotSupportedException($"There was no {nameof(IHandlerSelector)} registered for protocol type '{protocolType}'.");
                        }

                        IProtocol protocolToAdd = null;

                        protocolToAdd = protocolType switch
                        {
                            OpenTibiaProtocolType.LoginProtocol => new LoginProtocol(this.logger, handlerSelector, this.protocolConfig, this.gameConfig),
                            OpenTibiaProtocolType.GameProtocol => new GameProtocol(this.logger, handlerSelector, this.protocolConfig),
                            OpenTibiaProtocolType.ManagementProtocol => new ManagementProtocol(this.logger, handlerSelector),
                            _ => throw new NotSupportedException($"The protocol type '{protocolType}' is not supported by this factory."),
                        };

                        this.protocolInstancesCreated.Add(protocolType, protocolToAdd);
                    }
                }
            }

            return(this.protocolInstancesCreated[protocolType]);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates an instance of an implementation of <see cref="IProtocol"/> depending on the provided type.
        /// </summary>
        /// <param name="type">The type of protocol to implement.</param>
        /// <param name="handlerFactory">The handler factory to pass down to the protocol instance.</param>
        /// <returns>A new <see cref="IProtocol"/> implementation instance.</returns>
        public static IProtocol CreateForType(OpenTibiaProtocolType type, IHandlerFactory handlerFactory)
        {
            if (type == OpenTibiaProtocolType.LoginProtocol)
            {
                return(new LoginProtocol(handlerFactory));
            }

            if (type == OpenTibiaProtocolType.GameProtocol)
            {
                return(new GameProtocol(handlerFactory));
            }

            return(new ManagementProtocol(handlerFactory));
        }