protected override async Task <IList <Message> > OnPeekAsync(long fromSequenceNumber, int messageCount = 1) { try { AmqpRequestMessage requestMessage = AmqpRequestMessage.CreateRequest( ManagementConstants.Operations.PeekMessageOperation, this.OperationTimeout, null); requestMessage.Map[ManagementConstants.Properties.FromSequenceNumber] = fromSequenceNumber; requestMessage.Map[ManagementConstants.Properties.MessageCount] = messageCount; if (!string.IsNullOrWhiteSpace(this.sessionId)) { requestMessage.Map[ManagementConstants.Properties.SessionId] = this.sessionId; } List <Message> messages = new List <Message>(); AmqpResponseMessage response = await this.ExecuteRequestResponseAsync(requestMessage).ConfigureAwait(false); if (response.StatusCode == AmqpResponseStatusCode.OK) { Message message = null; var messageList = response.GetListValue <AmqpMap>(ManagementConstants.Properties.Messages); foreach (AmqpMap entry in messageList) { var payload = (ArraySegment <byte>)entry[ManagementConstants.Properties.Message]; AmqpMessage amqpMessage = AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(new[] { payload }), true); message = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage); messages.Add(message); } if (message != null) { this.LastPeekedSequenceNumber = message.SystemProperties.SequenceNumber; } return(messages); } else if (response.StatusCode == AmqpResponseStatusCode.NoContent || (response.StatusCode == AmqpResponseStatusCode.NotFound && Equals(AmqpClientConstants.MessageNotFoundError, response.GetResponseErrorCondition()))) { return(messages); } else { throw response.ToMessagingContractException(); } } catch (Exception exception) { throw AmqpExceptionHelper.GetClientException(exception); } }
protected override async Task <IList <Message> > OnReceiveAsync(int maxMessageCount, TimeSpan serverWaitTime) { ReceivingAmqpLink receiveLink = null; try { TimeoutHelper timeoutHelper = new TimeoutHelper(serverWaitTime, true); receiveLink = await this.ReceiveLinkManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); IEnumerable <AmqpMessage> amqpMessages = null; bool hasMessages = await Task.Factory.FromAsync( (c, s) => receiveLink.BeginReceiveRemoteMessages(maxMessageCount, DefaultBatchFlushInterval, timeoutHelper.RemainingTime(), c, s), a => receiveLink.EndReceiveMessages(a, out amqpMessages), this).ConfigureAwait(false); if (receiveLink.TerminalException != null) { throw receiveLink.TerminalException; } if (hasMessages && amqpMessages != null) { IList <Message> brokeredMessages = null; foreach (var amqpMessage in amqpMessages) { if (brokeredMessages == null) { brokeredMessages = new List <Message>(); } if (this.ReceiveMode == ReceiveMode.ReceiveAndDelete) { receiveLink.DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome); } Message message = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage); brokeredMessages.Add(message); } return(brokeredMessages); } return(null); } catch (Exception exception) { throw AmqpExceptionHelper.GetClientException(exception, receiveLink?.GetTrackingId()); } }
protected override async Task <IList <Message> > OnReceiveBySequenceNumberAsync(IEnumerable <long> sequenceNumbers) { List <Message> messages = new List <Message>(); try { AmqpRequestMessage requestMessage = AmqpRequestMessage.CreateRequest(ManagementConstants.Operations.ReceiveBySequenceNumberOperation, this.OperationTimeout, null); requestMessage.Map[ManagementConstants.Properties.SequenceNumbers] = sequenceNumbers.ToArray(); requestMessage.Map[ManagementConstants.Properties.ReceiverSettleMode] = (uint)(this.ReceiveMode == ReceiveMode.ReceiveAndDelete ? 0 : 1); AmqpResponseMessage response = await this.ExecuteRequestResponseAsync(requestMessage).ConfigureAwait(false); if (response.StatusCode == AmqpResponseStatusCode.OK) { IEnumerable <AmqpMap> amqpMapList = response.GetListValue <AmqpMap>(ManagementConstants.Properties.Messages); foreach (AmqpMap entry in amqpMapList) { ArraySegment <byte> payload = (ArraySegment <byte>)entry[ManagementConstants.Properties.Message]; AmqpMessage amqpMessage = AmqpMessage.CreateAmqpStreamMessage(new BufferListStream(new[] { payload }), true); Message message = AmqpMessageConverter.AmqpMessageToSBMessage(amqpMessage); Guid lockToken; if (entry.TryGetValue(ManagementConstants.Properties.LockToken, out lockToken)) { message.SystemProperties.LockTokenGuid = lockToken; this.requestResponseLockedMessages.AddOrUpdate(lockToken, message.SystemProperties.LockedUntilUtc); } messages.Add(message); } } else { throw response.ToMessagingContractException(); } } catch (Exception exception) { throw AmqpExceptionHelper.GetClientException(exception); } return(messages); }