Beispiel #1
0
        public void Encode(object obj, Stream stream)
        {
            var msg = obj as Datagram;

            if (msg == null)
            {
                return;
            }

            int start       = (int)stream.Position;
            int headerStart = start + RemotingHeader.LengthFieldLen;

            stream.Position = headerStart;

            byte[] buffer = new byte[4];
            EncodeHeader(msg.Header, buffer, stream);
            int headerSize = (int)stream.Position - headerStart;

            msg.WriteBody(stream);
            int end   = (int)stream.Position;
            int total = end - start - RemotingHeader.TotalSizeLen;

            stream.Position = start;


            ByteBufHelper.WriteInt32(total, buffer, stream);

            ByteBufHelper.WriteInt16((short)headerSize, buffer, stream);

            stream.Position = end;
        }
Beispiel #2
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;
        }
Beispiel #3
0
 private void EncodeHeader(RemotingHeader header, byte[] buffer, Stream stream)
 {
     ByteBufHelper.WriteInt32(RemotingHeader.QmqMagicCode, buffer, stream);
     ByteBufHelper.WriteInt16(header.Code, buffer, stream);
     ByteBufHelper.WriteInt16(header.Version, buffer, stream);
     ByteBufHelper.WriteInt32(header.Opaque, buffer, stream);
     ByteBufHelper.WriteInt32(header.Flag, buffer, stream);
     ByteBufHelper.WriteInt16(header.RequestCode, buffer, stream);
 }
 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);
 }
Beispiel #5
0
        /// <summary>
        ///  |total size|header size|magic code| header | body |
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public object Decode(InputStream stream)
        {
            if (!stream.CanRead)
            {
                return(Response.NeedMore);
            }

            long remain = stream.Length;

            if (remain < RemotingHeader.MinHeaderSize + RemotingHeader.LengthFieldLen)
            {
                return(Response.NeedMore);
            }

            stream.MarkReaderIndex();

            //go to magic code field
            stream.Position = stream.Position + RemotingHeader.LengthFieldLen;

            var buffer    = new byte[4];
            var magicCode = ByteBufHelper.ReadInt32(buffer, stream);

            //go to head, length field
            stream.ResetReaderIndex();
            if (RemotingHeader.QmqMagicCode != magicCode)
            {
                throw new IOException("非法数据");
            }

            stream.MarkReaderIndex();
            var total = ByteBufHelper.ReadInt32(buffer, stream);

            //f**k this
            if (remain - RemotingHeader.TotalSizeLen < total)
            {
                stream.ResetReaderIndex();
                return(Response.NeedMore);
            }

            short headerSize = ByteBufHelper.ReadInt16(buffer, stream);
            var   header     = DecodeHeader(buffer, stream);

            int bodyLen = total - headerSize - RemotingHeader.HeaderSizeLen;

            byte[] body = new byte[bodyLen];
            stream.Read(body, 0, bodyLen);

            var msg = new Datagram
            {
                Body   = body,
                Header = header
            };

            return(msg);
        }
Beispiel #6
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);
         }
     }
 }
Beispiel #7
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;
            }
Beispiel #8
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);
 }
Beispiel #9
0
        private RemotingHeader DecodeHeader(byte[] buffer, InputStream stream)
        {
            var header = new RemotingHeader();

            //skip magic code
            ByteBufHelper.ReadInt32(buffer, stream);

            header.Code        = ByteBufHelper.ReadInt16(buffer, stream);
            header.Version     = ByteBufHelper.ReadInt16(buffer, stream);
            header.Opaque      = ByteBufHelper.ReadInt32(buffer, stream);
            header.Flag        = ByteBufHelper.ReadInt32(buffer, stream);
            header.RequestCode = ByteBufHelper.ReadInt16(buffer, stream);
            return(header);
        }
Beispiel #10
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);
            }
        }
Beispiel #11
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);
            }
        }