/// <summary> /// After sending, we check if the write was successful or if it was a fire-and-forget. /// </summary> /// <param name="sendTask">The task of the send operation.</param> /// <param name="tcsResponse"></param> /// <param name="isFireAndForget">True, if we don't expect a response message.</param> /// <param name="channel"></param> private async Task AfterSendAsync(Task sendTask, TaskCompletionSource <Message.Message> tcsResponse, bool isFireAndForget, IClientChannel channel) { // TODO use for UDP connections, too await sendTask; if (sendTask.IsFaulted) { string msg = String.Format("Failed to write channel the request {0} {1}.", tcsResponse.Task.AsyncState, sendTask.Exception); Logger.Warn(msg); tcsResponse.SetException(sendTask.TryGetException()); } if (isFireAndForget) { Logger.Debug("Fire and forget message {0} sent. Close channel {1} now. {0}", tcsResponse.Task.AsyncState, channel); tcsResponse.SetResult(null); // set FF result // close channel now } else { //.NET specific, we wait here for the response // receive response message // processes client-side inbound pipeline await channel.ReceiveMessageAsync(); } channel.Close(); // TODO not needed, receive method closes... }
private async Task ListenMessagesAsync() { while (!_cancellationTokenSource.IsCancellationRequested) { var message = await _clientChannel.ReceiveMessageAsync(_cancellationTokenSource.Token); ProcessMessage(message); } }
static async Task ConsumeMessagesAsync(IClientChannel clientChannel, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var message = await clientChannel.ReceiveMessageAsync(cancellationToken); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("Message with id '{0}' received from '{1}': {2}", message.Id, message.From ?? clientChannel.RemoteNode, message.Content); Console.ResetColor(); } }
private static async Task ReceiveMessagesAsync(IClientChannel channel, CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { var message = await channel.ReceiveMessageAsync(cancellationToken); System.Console.ForegroundColor = ConsoleColor.Red; System.Console.WriteLine(); System.Console.WriteLine("Message received - From: {0} - Id: {1} - Content: {2}", message.From, message.Id, message.Content); System.Console.ResetColor(); } } catch (OperationCanceledException) { return; } }
private static async Task ReceiveMessagesAsync(IClientChannel channel, CancellationToken cancellationToken) { try { while (!cancellationToken.IsCancellationRequested) { var message = await channel.ReceiveMessageAsync (cancellationToken); System.Console.ForegroundColor = ConsoleColor.Red; System.Console.WriteLine (); System.Console.WriteLine ("Message received - From: {0} - Id: {1} - Content: {2}", message.From, message.Id, message.Content); System.Console.ResetColor (); } } catch (OperationCanceledException) { return; } }
/// <summary> /// After sending, we check if the write was successful or if it was a fire-and-forget. /// </summary> /// <param name="sendTask">The task of the send operation.</param> /// <param name="tcsResponse"></param> /// <param name="isFireAndForget">True, if we don't expect a response message.</param> /// <param name="channel"></param> private async Task AfterSendAsync(Task sendTask, TaskCompletionSource<Message.Message> tcsResponse, bool isFireAndForget, IClientChannel channel) { // TODO use for UDP connections, too await sendTask; if (sendTask.IsFaulted) { string msg = String.Format("Failed to write channel the request {0} {1}.", tcsResponse.Task.AsyncState, sendTask.Exception); Logger.Warn(msg); tcsResponse.SetException(sendTask.TryGetException()); } if (isFireAndForget) { Logger.Debug("Fire and forget message {0} sent. Close channel {1} now. {0}", tcsResponse.Task.AsyncState, channel); tcsResponse.SetResult(null); // set FF result // close channel now } else { //.NET specific, we wait here for the response // receive response message // processes client-side inbound pipeline await channel.ReceiveMessageAsync(); } channel.Close(); // TODO not needed, receive method closes... }