void WriteContentHeaderFrame(ref CustomBufferWriter <PipeWriter> writer, MessageProperties properties, ulong length) { var payloadSizeHeader = writer.WriteFrameHeader(FrameType.ContentHeader, channelNumber); writer.Commit(); var bytesWrittenBefore = (uint)writer.BytesCommitted; writer.Write(ClassId.Basic); writer.Write(Reserved); writer.Write(Reserved); writer.Write(length); writer.WriteBasicProperties(properties); writer.Commit(); BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - bytesWrittenBefore)); writer.Write(FrameEnd); }
public Task Publish(string exchange, string routingKey, bool mandatory, MessageProperties properties, Memory <byte> body) { ThrowIfClosed(); return(methods.Send_Publish(exchange, routingKey, mandatory, properties, body)); }
public async Task Send_Publish(string exchange, string routingKey, bool mandatory, MessageProperties properties, Memory <byte> bodyBytes) { var buffer = await socket.GetWriteBuffer(); try { WritePayload(); await buffer.FlushAsync(); } finally { socket.ReleaseWriteBuffer(); } void WritePayload() { var writer = new CustomBufferWriter <PipeWriter>(buffer); var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber); writer.Write(Method.Basic.Publish); writer.Write(Reserved); writer.Write(Reserved); writer.WriteShortString(exchange); writer.WriteShortString(routingKey); writer.WriteBits(mandatory); writer.Commit(); BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize)); writer.Write(FrameEnd); var body = bodyBytes.Span; WriteContentHeaderFrame(ref writer, properties, (ulong)body.Length); var framesToWrite = body.Length > 0; while (framesToWrite) { Span <byte> frame; if (body.Length > maxContentBodySize) { frame = body.Slice(0, (int)maxContentBodySize); body = body.Slice((int)maxContentBodySize); } else { frame = body; framesToWrite = false; } WriteContentBodyFrame(ref writer, frame); } writer.Commit(); } }