public bool Configure <TPackageInfo>(ServerOptions options, IServiceCollection services = null, IPipelineFilterFactory <TPackageInfo> pipelineFilterFactory = null, Action <IAppSession, TPackageInfo> packageHandler = null)
            where TPackageInfo : class
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options = options;

            if (services == null)
            {
                services = new ServiceCollection();
            }

            // prepare service collections
            _serviceCollection = services.AddOptions(); // activate options

            // build service provider
            _serviceProvider = services.BuildServiceProvider();

            // initialize logger factory
            LoggerFactory = _serviceProvider.GetService <ILoggerFactory>();

            _logger = LoggerFactory.CreateLogger("SuperSocket");

            if (pipelineFilterFactory == null)
            {
                pipelineFilterFactory = _serviceProvider.GetService <IPipelineFilterFactory <TPackageInfo> >();
            }

            if (pipelineFilterFactory == null)
            {
                throw new ArgumentNullException("pipelineFilterFactory");
            }

            var listenerFactory = _serviceProvider.GetService <IListenerFactory>();

            if (listenerFactory == null)
            {
                listenerFactory = new TcpSocketListenerFactory();
            }

            _listeners = new List <IListener>();

            foreach (var l in options.Listeners)
            {
                var listener = listenerFactory.CreateListener <TPackageInfo>(l, pipelineFilterFactory);
                listener.NewClientAccepted += OnNewClientAccept;
                _listeners.Add(listener);
            }

            return(_configured = true);
        }
Example #2
0
        public bool Configure <TPackageInfo>(ServerOptions options, IServiceCollection services = null, IPipelineFilterFactory <TPackageInfo> pipelineFilterFactory = null, Func <IAppSession, TPackageInfo, Task> packageHandler = null)
            where TPackageInfo : class
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options = options;

            if (services == null)
            {
                services = new ServiceCollection();
            }

            // prepare service collections
            _serviceCollection = services.AddOptions(); // activate options

            // build service provider
            _serviceProvider = services.BuildServiceProvider();

            // initialize logger factory
            LoggerFactory = _serviceProvider.GetService <ILoggerFactory>();

            _logger = LoggerFactory.CreateLogger("SuperSocket");

            if (pipelineFilterFactory == null)
            {
                pipelineFilterFactory = _serviceProvider.GetService <IPipelineFilterFactory <TPackageInfo> >();
            }

            if (pipelineFilterFactory == null)
            {
                throw new ArgumentNullException(nameof(pipelineFilterFactory));
            }

            var listenerFactory = _serviceProvider.GetService <IListenerFactory>();

            if (listenerFactory == null)
            {
                listenerFactory = new TcpSocketListenerFactory();
            }

            _listeners = new List <IListener>();

            foreach (var l in options.Listeners)
            {
                var listener = listenerFactory.CreateListener <TPackageInfo>(l, LoggerFactory, pipelineFilterFactory);
                listener.NewClientAccepted += OnNewClientAccept;

                if (packageHandler != null)
                {
                    _sessionInitializer = (s) =>
                    {
                        if (s.Channel is IChannel <TPackageInfo> channel)
                        {
                            channel.PackageReceived += async(ch, p) =>
                            {
                                try
                                {
                                    await packageHandler(s, p);
                                }
                                catch (Exception e)
                                {
                                    OnSessionError(s, e);
                                }
                            };
                        }
                    };
                }

                _listeners.Add(listener);
            }

            return(_configured = true);
        }