private async Task SendMessageFrameAsync(IMessageFrameWriter writer, MessageFrame messageFrame, CancellationToken cancellationToken) { try { await writer.WriteMessageFrameAsync(messageFrame, cancellationToken); } catch { if (_resentMessageFrames.TryAdd(messageFrame)) { Interlocked.Increment(ref _resentMessageFramesCount); } throw; } }
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(); } }