private async Task ReceiveAsync(MessageFrameReceiver messageFrameReceiver, CancellationToken cancellationToken) { var task = messageFrameReceiver.ReceiveAsync(cancellationToken); var messageFrame = await task; _messageQueue.Add(messageFrame); MessageFrameReceived(messageFrame); var newTask = ReceiveAsync(messageFrameReceiver, cancellationToken); }
private async Task <IMessage> RequestWithCancellationToken(IMessage message, CancellationToken cancellationToken) { await _semaphoreRequest.WaitAsync(cancellationToken); try { var sendMessageFrame = _messageFrameCreator.CreateFromMessage(message); await _messageFrameWriter.WriteMessageFrameAsync(sendMessageFrame, cancellationToken); var messageFrame = await _messageFrameReceiver.ReceiveAsync(cancellationToken).ConfigureAwait(false); var responseMessage = _messageSerialization.Deserialize(messageFrame.MessageTypeId, messageFrame.RawMessage); return(responseMessage); } finally { _semaphoreRequest.Release(); } }