Example #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);
 }
Example #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);
         }
     }
 }
Example #4
0
            public void Write(Stream output)
            {
                var start = output.Position;

                output.Position += 2;

                short size = 4;

                byte[] lenBuffer = new byte[2];

                output.Write(SubjectLen, 0, 2);
                output.Write(Subject, 0, Subject.Length);
                ByteBufHelper.WriteString(_request.Subject, lenBuffer, output);

                output.Write(ClientTypeCodeLen, 0, 2);
                output.Write(ClientTypeCode, 0, ClientTypeCode.Length);
                ByteBufHelper.WriteString(_request.ClientType.ToString(), lenBuffer, output);

                output.Write(AppCodeLen, 0, 2);
                output.Write(AppCode, 0, AppCode.Length);
                ByteBufHelper.WriteInt16((short)_request.AppCode.Length, lenBuffer, output);
                output.Write(_request.AppCode, 0, _request.AppCode.Length);

                if (!string.IsNullOrEmpty(_request.ClientId))
                {
                    output.Write(ClientIdLen, 0, 2);
                    output.Write(ClientId, 0, ClientId.Length);
                    ByteBufHelper.WriteString(_request.ClientId, lenBuffer, output);
                    size += 1;
                }

                if (!string.IsNullOrEmpty(_request.ConsumerGroup))
                {
                    output.Write(ConsumerGroupLen, 0, 2);
                    output.Write(ConsumerGroup, 0, ConsumerGroup.Length);
                    ByteBufHelper.WriteString(_request.ConsumerGroup, lenBuffer, output);
                    size += 1;
                }

                output.Write(RequestTypeLen, 0, 2);
                output.Write(RequestType, 0, RequestType.Length);
                ByteBufHelper.WriteString(_request.RequestType.ToString(), lenBuffer, output);

                long end = output.Position;

                output.Position = start;
                ByteBufHelper.WriteInt16(size, lenBuffer, output);

                output.Position = end;
            }
Example #5
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);
 }
Example #6
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);
            }
        }
Example #7
0
        private void SerializeMap(Hashtable map, byte[] lenBuffer, Stream output)
        {
            if (map == null || map.Count == 0)
            {
                return;
            }

            foreach (var item in map.Keys)
            {
                if (item == null || map[item] == null)
                {
                    continue;
                }

                ByteBufHelper.WriteString(item, lenBuffer, output);
                ByteBufHelper.WriteString(map[item], lenBuffer, output);
            }
        }