예제 #1
0
파일: Consumer.cs 프로젝트: uliian/equeue
        private IEnumerable<QueueMessage> DecodeMessages(PullRequest pullRequest, byte[] buffer)
        {
            var messages = new List<QueueMessage>();
            if (buffer == null || buffer.Length <= 4)
            {
                return messages;
            }

            try
            {
                var nextOffset = 0;
                var messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
                while (messageLength > 0)
                {
                    var message = new QueueMessage();
                    var messageBytes = new byte[messageLength];
                    Buffer.BlockCopy(buffer, nextOffset, messageBytes, 0, messageLength);
                    nextOffset += messageLength;
                    message.ReadFrom(messageBytes);
                    if (!message.IsValid())
                    {
                        _logger.ErrorFormat("Invalid message, pullRequest: {0}", pullRequest);
                        continue;
                    }
                    messages.Add(message);
                    if (nextOffset >= buffer.Length)
                    {
                        break;
                    }
                    messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(string.Format("Decode pull return message has exception, pullRequest: {0}", pullRequest), ex);
            }

            return messages;
        }
예제 #2
0
 public QueueMessage GetMessage(long position)
 {
     var buffer = GetMessageBuffer(position);
     if (buffer != null)
     {
         var nextOffset = 0;
         var messageLength = MessageUtils.DecodeInt(buffer, nextOffset, out nextOffset);
         if (messageLength > 0)
         {
             var message = new QueueMessage();
             var messageBytes = new byte[messageLength];
             Buffer.BlockCopy(buffer, nextOffset, messageBytes, 0, messageLength);
             message.ReadFrom(messageBytes);
             return message;
         }
     }
     return null;
 }