Ejemplo n.º 1
0
        private void SerializeMessage(BaseMessage message, Stream output)
        {
            var crcIndex = (int)output.Position;

            output.Position = crcIndex + 8; //这里预留8保存crc信息

            var messageStart = (int)output.Position;

            Flag flag = Flags.GetMessageFlags(message);

            // flag
            ByteBufHelper.WriteByte((byte)flag, output);

            byte[] buffer = new byte[8];
            // created time
            ByteBufHelper.WriteInt64(message.CreatedTime.Value.ToTime(), buffer, output);

            if (Flags.IsDelayMessage(flag))
            {
                //Schedule Time
                ByteBufHelper.WriteInt64(message.ScheduleReceiveTime.Value.ToTime(), buffer, output);
            }
            else
            {
                // expired time
                ByteBufHelper.WriteInt64(message.ExpiredTime.Value.ToTime(), buffer, output);
            }


            // subject
            ByteBufHelper.WriteString(message.Subject, buffer, output);

            // message id
            ByteBufHelper.WriteString(message.MessageId, buffer, output);

            WriteTags(flag, message, output);

            //记录当前指针位置
            int writerIndex = (int)output.Position;
            int bodyStart   = writerIndex + 4;

            output.Position = bodyStart;
            //指针 预留4个位置开始写 Attrs
            SerializeMap(message.Attrs, buffer, output);

            int end           = (int)output.Position;
            int bodyLen       = end - bodyStart;
            int messageLength = end - messageStart;

            output.Position = writerIndex;
            //记录 body 的长度
            ByteBufHelper.WriteInt32(bodyLen, buffer, output);

            //write crc32
            output.Position = crcIndex;
            ByteBufHelper.WriteInt64(Crc32.GetCRC32(output, messageStart, messageLength), buffer, output);
            //指针重置到尾端
            output.Position = end;
        }
 public void Write(Stream output)
 {
     byte[] buffer = new byte[8];
     ByteBufHelper.WriteString(request.Subject, buffer, output);
     ByteBufHelper.WriteString(request.Group, buffer, output);
     ByteBufHelper.WriteString(request.ConsumerId, buffer, output);
     ByteBufHelper.WriteInt64(request.PullOffsetBegin, buffer, output);
     ByteBufHelper.WriteInt64(request.PullOffsetEnd, buffer, output);
     ByteBufHelper.WriteByte((byte)(request.IsBroadcast ? 1 : 0), output);
 }
Ejemplo n.º 3
0
 void WriteTags(Flag flag, BaseMessage message, Stream output)
 {
     if (Flags.HasTag(flag))
     {
         var    tags   = message.GetTags();
         byte[] buffer = new byte[8];
         ByteBufHelper.WriteByte((byte)tags.Count, output);
         foreach (var item in tags)
         {
             ByteBufHelper.WriteString(item, buffer, output);
         }
     }
 }
Ejemplo n.º 4
0
 public void Write(Stream output)
 {
     byte[] buffer = new byte[8];
     ByteBufHelper.WriteString(_request.Subject, buffer, output);
     ByteBufHelper.WriteString(_request.Group, buffer, output);
     ByteBufHelper.WriteString(_request.ConsumerId, buffer, output);
     ByteBufHelper.WriteInt32(_request.BatchSize, buffer, output);
     ByteBufHelper.WriteInt64(_request.Offset, buffer, output);
     ByteBufHelper.WriteInt64(_request.PullOffsetBegin, buffer, output);
     ByteBufHelper.WriteInt64(_request.PullOffsetEnd, buffer, output);
     ByteBufHelper.WriteInt64(_request.TimeoutMillis, buffer, output);
     ByteBufHelper.WriteByte(_request.IsBroadcast, output);
     WriteTags(_request, buffer, output);
 }
Ejemplo n.º 5
0
        private void WriteTags(PullMessageRequest request, byte[] buffer, Stream output)
        {
            ByteBufHelper.WriteInt16(_request.TagType, buffer, output);
            var tags = request.Tags;

            if (tags == null || tags.Length == 0)
            {
                ByteBufHelper.WriteByte((byte)0, output);
                return;
            }

            ByteBufHelper.WriteByte((byte)tags.Length, output);
            foreach (var tag in _request.Tags)
            {
                ByteBufHelper.WriteString(tag, buffer, output);
            }
        }