private static async Task SendAndReceiveAwaitWait(Task getNextMessageTask, IClientConnected clientConnected, IMessagePipeline pipeline, CancellationTokenSource cancellationTokenSource) { while (true) { var doneTask = await Task.WhenAny(getNextMessageTask, Task.Delay(TimeSpan.FromSeconds(10), CancellationToken.None)).ConfigureAwait(false); // If connection was dropped while waiting if (clientConnected.IsClientConnected == false) { cancellationTokenSource.Cancel(); return; } if (doneTask == getNextMessageTask) { return; } } }
private async Task SendAndReceiveMessageWait(string messageHandlerName, string inputText, IClientConnected clientConnected, IMessagePipeline pipeline) { var messageHandler = this.messageHandlerContainer.GetMessageHandler(messageHandlerName); if (messageHandler != null) { var factory = messageHandler.GetMessageFactory(); var messageToSend = await factory.CreateMessage(inputText); await this.SendAndReceiveAwait(messageHandlerName, messageToSend, clientConnected, pipeline).ConfigureAwait(false); } }
private Task SendAndReceiveAwait(string messageHandlerName, IMessage messageToSend, IClientConnected clientConnected, IMessagePipeline pipeline) { var messageHandler = this.messageHandlerContainer.GetMessageHandler(messageHandlerName); return(SendAndReceiveAwait(messageHandler, messageToSend, clientConnected, pipeline)); }
private static Task SendAndReceiveAwait(IMessageHandler messageHandler, IMessage messageToSend, IClientConnected clientConnected, IMessagePipeline pipeline) { var cancellationTokenSource = new CancellationTokenSource(); Task getNextMessageTask; if (messageToSend == null) { getNextMessageTask = messageHandler.ReceiveMessageAsync(pipeline, cancellationTokenSource.Token); } else { getNextMessageTask = messageHandler.SendAndReceiveMessageAsync(messageToSend, pipeline, cancellationTokenSource.Token); } if (getNextMessageTask.IsCompleted) { return(Task.CompletedTask); } return(SendAndReceiveAwaitWait(getNextMessageTask, clientConnected, pipeline, cancellationTokenSource)); }
private Task ProcessGetAwait(string messageHandlerName, IClientConnected clientConnected, IMessagePipeline pipeline) { return(this.SendAndReceiveAwait(messageHandlerName, null, clientConnected, pipeline)); }