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; }
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; }
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.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); }