private async Task RequestTerminationAsync(CancellationToken cancellation) { using (var resonseTableSlot = new ResponseTableSlot(this)) { // Send termination message await SendTerminateAsync(resonseTableSlot.SeqNum, cancellation); // Wait for terminate ack await Task.WhenAny(resonseTableSlot.Response, cancellation.AsTask()); } }
private async Task <object> SendInternalAsync <TMessage>(TMessage message, CancellationToken cancellation) { ThrowIfDisposed(); await Initialization; // Reserve a new slot in the response table. using (var responseTableSlot = new ResponseTableSlot(this)) { _logger?.LogInformation($"Sending message of type '{typeof(TMessage).FullName}' with seq-num '{responseTableSlot.SeqNum}'."); cancellation.ThrowIfCancellationRequested(); try { // Serialize the message and send it. await SendPayloadAsync(_serializer.Serialize(message, MessageEncoding.Bson), responseTableSlot.SeqNum, 0, MessageType.Message, MessageEncoding.Bson, cancellation); } catch (Exception exc) when(!(exc is ObjectDisposedException)) { _logger?.LogError($"The message with the seq-num '{responseTableSlot.SeqNum}' could not be sent.", exc); throw; } _logger?.LogInformation($"Sent message with seq-num '{responseTableSlot.SeqNum}'. Waiting for response."); try { // Wait for a reponse fromthe remote end-point or cancellation alternatively. await Task.WhenAny(responseTableSlot.Response, cancellation.AsTask()); } catch (MessageHandlerNotFoundException) { _logger?.LogInformation($"The receiver cannot handle the message with seq-num '{responseTableSlot.SeqNum}'. No suitable message handler was found."); throw; } catch (Exception exc) when(!(exc is OperationCanceledException)) { _logger?.LogInformation($"Received response for the message with seq-num '{responseTableSlot.SeqNum}'."); throw; } _logger?.LogInformation($"Received response for the message with seq-num '{responseTableSlot.SeqNum}'."); return(await responseTableSlot.Response); } }