Пример #1
0
        /// <summary>
        /// Runs the <see cref="Program"/> asynchronously.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Returns an awaitable <see cref="Task"/>.</returns>
        private async Task RunAsync(CancellationToken cancellationToken)
        {
            IConfiguration configuration;

            try
            {
                configuration = ConfigurationLoader.GetConfiguration("appSettings");
            }
            catch (Exception)
            {
                Console.WriteLine("Please check if you have a valid appSettings.json!");
                CancellationTokenSource.Cancel();
                return;
            }
            Startup startup = new Startup(CancellationTokenSource);

            Console.WriteLine("Initializing...");
            await startup.InitializeAsync(configuration, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            Console.WriteLine("Finished initializing!\n");

            Interlocked.Exchange(ref _canReadConsole, 1);
            Interlocked.Exchange(ref _canExit, 1);

            _genericServiceProvider = startup.GetGenericServiceProvider();
            GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await _genericServiceProvider.GetService <ITrainingRoomService>()
                                                                              .GetEnabledTrainingRoomsAsync(new GetEnabledTrainingRoomsRequest());

            foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms)
            {
                Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner.Username}");
            }

            ServerConfiguration serverConfiguration = _genericServiceProvider.GetService <IOptions <ServerConfiguration> >().Value;
            TcpListener         tcpListener         = new TcpListener(IPAddress.Any, serverConfiguration.Port);

            tcpListener.Start();
            Console.WriteLine($"Started listening for clients on port: {serverConfiguration.Port}.");
            while (!cancellationToken.IsCancellationRequested)
            {
                TcpClient tcpClient = await tcpListener.AcceptTcpClientAsync();

                Console.WriteLine($"Accepted a new connection: \n\tLocalEndPoint: {tcpClient.Client.LocalEndPoint}\n\tRemoteEndPoint: {tcpClient.Client.RemoteEndPoint}");
                _ = Task.Run(async() =>
                {
                    IMessageProcessor messageProcessor      = new ServerMessageProcessor(_genericServiceProvider.GetService <MessageToServiceMapper>());
                    IMessageSerializer messageSerializer    = new JsonMessageSerializer();
                    SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, tcpClient);
                    await networkConnector.AuthenticateAsServer(serverConfiguration.Certificate, CancellationToken.None);
                    networkConnector.Start();
                }, cancellationToken);
            }
        }
Пример #2
0
        /// <inheritdoc cref="IClientEndPoint.StartAsync(CancellationToken, IMessageProcessor, IMessageSerializer)"/>
        public async Task StartAsync(CancellationToken cancellationToken, IMessageProcessor messageProcessor, IMessageSerializer messageSerializer)
        {
            _tcpListener.Start();
            Console.WriteLine($"Started listening for clients on port: {_serverConfiguration.ClientPort}.");
            while (!cancellationToken.IsCancellationRequested)
            {
                TcpClient tcpClient = await _tcpListener.AcceptTcpClientAsync();

                Console.WriteLine($"CLIENT | New connection: {tcpClient.Client.RemoteEndPoint}");
                _ = Task.Run(async() =>
                {
                    SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, tcpClient);
                    await networkConnector.AuthenticateAsServer(_serverConfiguration.Certificate, CancellationToken.None);
                    networkConnector.Start();
                }, cancellationToken);
            }
        }