/// <summary>
        /// Creates a new <see cref="LiteServer{TUser}"/> instance with a server configuration
        /// and a service provider.
        /// </summary>
        /// <param name="options">Server configuration options.</param>
        /// <param name="serviceProvider">Service provider to use.</param>
        public LiteServer(LiteServerOptions options, IServiceProvider?serviceProvider)
        {
            if (options is null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options          = options;
            _serviceProvider = serviceProvider ?? new ServiceCollection().BuildServiceProvider();
            _connectedUsers  = new ConcurrentDictionary <Guid, TUser>();
            _socket          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

            if (_serviceProvider is not null)
            {
                _logger = _serviceProvider.GetService <ILogger <LiteServer <TUser> > >();
            }

            _acceptor = new LiteServerAcceptor(_socket);
            _acceptor.OnClientAccepted += OnClientAccepted;
            _acceptor.OnError          += OnAcceptorError;

            _receiver = new LiteServerReceiver(options.PacketProcessor, Options.ReceiveStrategy, Options.ClientBufferSize);
            _receiver.Disconnected += OnDisconnected;
            _receiver.Error        += OnReceiverError;
        }
Exemple #2
0
        /// <summary>
        /// Creates a new <see cref="LiteServer{TUser}"/> instance with a server configuration
        /// and a service provider.
        /// </summary>
        /// <param name="configuration">Server configuration.</param>
        /// <param name="serviceProvider">Service provider to use.</param>
        public LiteServer(LiteServerOptions configuration, IServiceProvider?serviceProvider)
        {
            Options          = configuration;
            _packetProcessor = configuration.PacketProcessor;
            _serviceProvider = serviceProvider !;
            _connectedUsers  = new ConcurrentDictionary <Guid, TUser>();
            _socket          = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);

            if (serviceProvider is not null)
            {
                _logger = serviceProvider.GetService <ILogger <LiteServer <TUser> > >();
            }

            _acceptor = new LiteServerAcceptor(_socket);
            _acceptor.OnClientAccepted += OnClientAccepted;
            _acceptor.OnError          += OnAcceptorError;

            _receiver = new LiteServerReceiver(_packetProcessor, Options.ReceiveStrategy, Options.ClientBufferSize);
            _receiver.Disconnected += OnDisconnected;
            _receiver.Error        += OnReceiverError;
        }
Exemple #3
0
 /// <summary>
 /// Creates a new <see cref="LiteServer{TUser}"/> instance with a server configuration.
 /// </summary>
 /// <param name="configuration">Server configuration</param>
 public LiteServer(LiteServerOptions configuration)
     : this(configuration, null)
 {
 }
 /// <summary>
 /// Creates a new <see cref="LiteServer{TUser}"/> instance with a server configuration.
 /// </summary>
 /// <param name="options">Server configuration options.</param>
 public LiteServer(LiteServerOptions options)
     : this(options, null)
 {
 }