/// <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); } }
/// <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); } }
static async Task Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List <Task> tasks = new List <Task>(); for (int j = 0; j < ClientCount; j++) { Task clientTask = Task.Run(async() => { IMessageProcessor messageProcessor = new ClientMessageProcessor(); IMessageSerializer messageSerializer = new JsonMessageSerializer(); SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, Host, Port); await networkConnector.ConnectAsync(CancellationToken.None); await networkConnector.AuthenticateAsClient(CancellationToken.None); networkConnector.Start(); MessageListener <AuthenticateResponse> loginResponseListener = new MessageListener <AuthenticateResponse>(); MessageListener <RegisterResponse> registerMessageListener = new MessageListener <RegisterResponse>(); MessageListener <CreateTrainingRoomResponse> createTrainingRoomResponseListener = new MessageListener <CreateTrainingRoomResponse>(); MessageListener <GetEnabledTrainingRoomsResponse> getEnabledTrainingRoomsResponseListener = new MessageListener <GetEnabledTrainingRoomsResponse>(); MessageListener <StartTrainingSessionResponse> startTrainingSessionResponseListener = new MessageListener <StartTrainingSessionResponse>(); MessageListener <GetOrganismsResponse> getOrganismsResponseListener = new MessageListener <GetOrganismsResponse>(); MessageListener <PostOrganismsScoreResponse> postOrganismsScoreResponseListener = new MessageListener <PostOrganismsScoreResponse>(); loginResponseListener.Subscribe(messageProcessor); createTrainingRoomResponseListener.Subscribe(messageProcessor); registerMessageListener.Subscribe(messageProcessor); getEnabledTrainingRoomsResponseListener.Subscribe(messageProcessor); startTrainingSessionResponseListener.Subscribe(messageProcessor); getOrganismsResponseListener.Subscribe(messageProcessor); postOrganismsScoreResponseListener.Subscribe(messageProcessor); for (int i = 0; i < MessageCount; i++) { Guid name = Guid.NewGuid(); RegisterRequest registerRequest = new RegisterRequest(name.ToString(), "password2", "Name2"); await networkConnector.SendMessageAsync(registerRequest, CancellationToken.None); RegisterResponse registerResponse = await registerMessageListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"RegisterResponse: \n\tSuccess: {registerResponse.Success}, \n\tRequestId: {registerResponse.RequestId}, \n\tResponseId: {registerResponse.Id}, \n\tMessage:{registerResponse.Message}"); AuthenticateRequest loginRequest = new AuthenticateRequest(name.ToString(), "password2", "Name2"); await networkConnector.SendMessageAsync(loginRequest, CancellationToken.None); AuthenticateResponse loginResponse = await loginResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"AuthenticateResponse: \n\tSuccess: {loginResponse.Success}, \n\tAccessToken: {loginResponse.AccessToken}, \n\tRequestId: {loginResponse.RequestId}, \n\tResponseId: {loginResponse.Id}, \n\tMessage:{loginResponse.Message}"); TrainingRoomSettings trainingRoomSettings = new TrainingRoomSettings(50, 2, 1, 1, 1, 0.4, 3, 0.05, 0.03, 0.75, 0.001, 1, 0.8, 0.1, 0.5, 0.25, 0); CreateTrainingRoomRequest createTrainingRoomRequest = new CreateTrainingRoomRequest(loginResponse.UserId, Guid.NewGuid().ToString(), trainingRoomSettings); await networkConnector.SendMessageAsync(createTrainingRoomRequest, CancellationToken.None); CreateTrainingRoomResponse createTrainingRoomResponse = await createTrainingRoomResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"CreateTrainingRoomResponse: \n\tId: {createTrainingRoomResponse.Id}\n\tRequestId: {createTrainingRoomResponse.RequestId}\n\tDateTime: {createTrainingRoomResponse.DateTime}\n\tMessage: {createTrainingRoomResponse.Message}\n\tSuccess: {createTrainingRoomResponse.Success}\n\tTrainingRoomId: {createTrainingRoomResponse.TrainingRoomId}"); StartTrainingSessionRequest startTrainingSessionRequest = new StartTrainingSessionRequest(loginResponse.UserId, createTrainingRoomResponse.TrainingRoomId); await networkConnector.SendMessageAsync(startTrainingSessionRequest, CancellationToken.None); StartTrainingSessionResponse startTrainingSessionResponse = await startTrainingSessionResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"StartTrainingSessionResponse: \n\tId: {startTrainingSessionResponse.Id}\n\tRequestId: {startTrainingSessionResponse.RequestId}\n\tDateTime: {startTrainingSessionResponse.DateTime}\n\tMessage: {startTrainingSessionResponse.Message}\n\tSuccess: {startTrainingSessionResponse.Success}\n\tTrainingSessionId: {startTrainingSessionResponse.TrainingSession.Id}"); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); // await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); //GetEnabledTrainingRoomsRequest getEnabledTrainingRoomsRequest = new GetEnabledTrainingRoomsRequest(); //await networkConnector.SendMessageAsync(getEnabledTrainingRoomsRequest, CancellationToken.None); //GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await getEnabledTrainingRoomsResponseListener.ReceiveMessageAsync(CancellationToken.None); //Console.WriteLine($"GetEnabledTrainingRoomsResponse: \n\tId: {getEnabledTrainingRoomsResponse.Id}\n\tRequestId: {getEnabledTrainingRoomsResponse.RequestId}\n\tDateTime: {getEnabledTrainingRoomsResponse.DateTime}\n\tMessage: {getEnabledTrainingRoomsResponse.Message}\n\tSuccess: {getEnabledTrainingRoomsResponse.Success}"); //foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms) // Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner.Username}"); } loginResponseListener.Unsubscribe(); }); tasks.Add(clientTask); } await Task.WhenAll(tasks).ContinueWith(action => { stopwatch.Stop(); Console.WriteLine($"Messages per second: {TotalMessages / stopwatch.Elapsed.TotalSeconds:N0}"); Console.ReadKey(); }); }