/// <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; }
/// <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; }