From b5972250f9ac44bde5e0f35afb1ff7e0f7388cc7 Mon Sep 17 00:00:00 2001 From: bollhals Date: Thu, 8 Sep 2022 00:05:38 +0200 Subject: [PATCH] change ref to in and use Unsafe.AsRef --- projects/RabbitMQ.Client/client/api/IModel.cs | 4 +- .../client/api/IModelExtensions.cs | 12 +- .../RabbitMQ.Client/client/framing/Model.cs | 121 +++++++----------- .../client/impl/AutorecoveringModel.cs | 8 +- .../client/impl/Connection.Receive.cs | 2 +- .../RabbitMQ.Client/client/impl/Connection.cs | 3 +- .../client/impl/EmptyBasicProperty.cs | 2 +- .../RabbitMQ.Client/client/impl/ISession.cs | 4 +- .../client/impl/MainSession.cs | 4 +- .../RabbitMQ.Client/client/impl/ModelBase.cs | 24 ++-- .../client/impl/SessionBase.cs | 9 +- .../TestApplications/MassPublish/Program.cs | 2 +- .../Unit/APIApproval.Approve.verified.txt | 6 +- projects/Unit/TestBasicPublish.cs | 2 +- projects/Unit/TestConnectionRecovery.cs | 2 +- 15 files changed, 91 insertions(+), 114 deletions(-) diff --git a/projects/RabbitMQ.Client/client/api/IModel.cs b/projects/RabbitMQ.Client/client/api/IModel.cs index 870095593c..2dbafc3ada 100644 --- a/projects/RabbitMQ.Client/client/api/IModel.cs +++ b/projects/RabbitMQ.Client/client/api/IModel.cs @@ -188,7 +188,7 @@ string BasicConsume( /// Routing key must be shorter than 255 bytes. /// /// - void BasicPublish(string exchange, string routingKey, ref TProperties basicProperties, ReadOnlyMemory body = default, bool mandatory = false) + void BasicPublish(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory body = default, bool mandatory = false) where TProperties : IReadOnlyBasicProperties, IAmqpHeader; /// /// Publishes a message. @@ -198,7 +198,7 @@ void BasicPublish(string exchange, string routingKey, ref TProperti /// Routing key must be shorter than 255 bytes. /// /// - void BasicPublish(CachedString exchange, CachedString routingKey, ref TProperties basicProperties, ReadOnlyMemory body = default, bool mandatory = false) + void BasicPublish(CachedString exchange, CachedString routingKey, in TProperties basicProperties, ReadOnlyMemory body = default, bool mandatory = false) where TProperties : IReadOnlyBasicProperties, IAmqpHeader; #nullable disable diff --git a/projects/RabbitMQ.Client/client/api/IModelExtensions.cs b/projects/RabbitMQ.Client/client/api/IModelExtensions.cs index d7df884227..2c7f745964 100644 --- a/projects/RabbitMQ.Client/client/api/IModelExtensions.cs +++ b/projects/RabbitMQ.Client/client/api/IModelExtensions.cs @@ -82,17 +82,17 @@ public static string BasicConsume(this IModel model, string queue, /// /// The publication occurs with mandatory=false and immediate=false. /// - public static void BasicPublish(this IModel model, PublicationAddress addr, ref T basicProperties, ReadOnlyMemory body) + public static void BasicPublish(this IModel model, PublicationAddress addr, in T basicProperties, ReadOnlyMemory body) where T : IReadOnlyBasicProperties, IAmqpHeader { - model.BasicPublish(addr.ExchangeName, addr.RoutingKey, ref basicProperties, body); + model.BasicPublish(addr.ExchangeName, addr.RoutingKey, in basicProperties, body); } public static void BasicPublish(this IModel model, string exchange, string routingKey, ReadOnlyMemory body = default, bool mandatory = false) - => model.BasicPublish(exchange, routingKey, ref EmptyBasicProperty.Empty, body, mandatory); + => model.BasicPublish(exchange, routingKey, in EmptyBasicProperty.Empty, body, mandatory); public static void BasicPublish(this IModel model, CachedString exchange, CachedString routingKey, ReadOnlyMemory body = default, bool mandatory = false) - => model.BasicPublish(exchange, routingKey, ref EmptyBasicProperty.Empty, body, mandatory); + => model.BasicPublish(exchange, routingKey, in EmptyBasicProperty.Empty, body, mandatory); #nullable disable /// @@ -113,7 +113,7 @@ public static void ExchangeBind(this IModel model, string destination, string so } /// - /// (Extension method) Like exchange bind but sets nowait to true. + /// (Extension method) Like exchange bind but sets nowait to true. /// public static void ExchangeBindNoWait(this IModel model, string destination, string source, string routingKey, IDictionary arguments = null) { @@ -130,7 +130,7 @@ public static void ExchangeDeclare(this IModel model, string exchange, string ty } /// - /// (Extension method) Like ExchangeDeclare but sets nowait to true. + /// (Extension method) Like ExchangeDeclare but sets nowait to true. /// public static void ExchangeDeclareNoWait(this IModel model, string exchange, string type, bool durable = false, bool autoDelete = false, IDictionary arguments = null) diff --git a/projects/RabbitMQ.Client/client/framing/Model.cs b/projects/RabbitMQ.Client/client/framing/Model.cs index 964b4cc0c2..e5659441be 100644 --- a/projects/RabbitMQ.Client/client/framing/Model.cs +++ b/projects/RabbitMQ.Client/client/framing/Model.cs @@ -44,56 +44,47 @@ public Model(ConnectionConfig config, ISession session) : base(config, session) public override void ConnectionTuneOk(ushort channelMax, uint frameMax, ushort heartbeat) { - var cmd = new ConnectionTuneOk(channelMax, frameMax, heartbeat); - ModelSend(ref cmd); + ModelSend(new ConnectionTuneOk(channelMax, frameMax, heartbeat)); } public override void _Private_BasicCancel(string consumerTag, bool nowait) { - var cmd = new BasicCancel(consumerTag, nowait); - ModelSend(ref cmd); + ModelSend(new BasicCancel(consumerTag, nowait)); } public override void _Private_BasicConsume(string queue, string consumerTag, bool noLocal, bool autoAck, bool exclusive, bool nowait, IDictionary arguments) { - var cmd = new BasicConsume(queue, consumerTag, noLocal, autoAck, exclusive, nowait, arguments); - ModelSend(ref cmd); + ModelSend(new BasicConsume(queue, consumerTag, noLocal, autoAck, exclusive, nowait, arguments)); } public override void _Private_BasicGet(string queue, bool autoAck) { - var cmd = new BasicGet(queue, autoAck); - ModelSend(ref cmd); + ModelSend(new BasicGet(queue, autoAck)); } public override void _Private_BasicRecover(bool requeue) { - var cmd = new BasicRecover(requeue); - ModelSend(ref cmd); + ModelSend(new BasicRecover(requeue)); } public override void _Private_ChannelClose(ushort replyCode, string replyText, ushort classId, ushort methodId) { - var cmd = new ChannelClose(replyCode, replyText, classId, methodId); - ModelSend(ref cmd); + ModelSend(new ChannelClose(replyCode, replyText, classId, methodId)); } public override void _Private_ChannelCloseOk() { - var cmd = new ChannelCloseOk(); - ModelSend(ref cmd); + ModelSend(new ChannelCloseOk()); } public override void _Private_ChannelFlowOk(bool active) { - var cmd = new ChannelFlowOk(active); - ModelSend(ref cmd); + ModelSend(new ChannelFlowOk(active)); } public override void _Private_ChannelOpen() { - var cmd = new ChannelOpen(); - ModelRpc(ref cmd, ProtocolCommandId.ChannelOpenOk); + ModelRpc(new ChannelOpen(), ProtocolCommandId.ChannelOpenOk); } public override void _Private_ConfirmSelect(bool nowait) @@ -101,198 +92,184 @@ public override void _Private_ConfirmSelect(bool nowait) var method = new ConfirmSelect(nowait); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.ConfirmSelectOk); + ModelRpc(method, ProtocolCommandId.ConfirmSelectOk); } } public override void _Private_ConnectionCloseOk() { - var cmd = new ConnectionCloseOk(); - ModelSend(ref cmd); + ModelSend(new ConnectionCloseOk()); } public override void _Private_ConnectionOpen(string virtualHost) { - var cmd = new ConnectionOpen(virtualHost); - ModelSend(ref cmd); + ModelSend(new ConnectionOpen(virtualHost)); } public override void _Private_ConnectionSecureOk(byte[] response) { - var cmd = new ConnectionSecureOk(response); - ModelSend(ref cmd); + ModelSend(new ConnectionSecureOk(response)); } public override void _Private_ConnectionStartOk(IDictionary clientProperties, string mechanism, byte[] response, string locale) { - var cmd = new ConnectionStartOk(clientProperties, mechanism, response, locale); - ModelSend(ref cmd); + ModelSend(new ConnectionStartOk(clientProperties, mechanism, response, locale)); } public override void _Private_UpdateSecret(byte[] newSecret, string reason) { - var cmd = new ConnectionUpdateSecret(newSecret, reason); - ModelRpc(ref cmd, ProtocolCommandId.ConnectionUpdateSecretOk); + ModelRpc(new ConnectionUpdateSecret(newSecret, reason), ProtocolCommandId.ConnectionUpdateSecretOk); } public override void _Private_ExchangeBind(string destination, string source, string routingKey, bool nowait, IDictionary arguments) { - ExchangeBind method = new ExchangeBind(destination, source, routingKey, nowait, arguments); + var method = new ExchangeBind(destination, source, routingKey, nowait, arguments); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.ExchangeBindOk); + ModelRpc(method, ProtocolCommandId.ExchangeBindOk); } } public override void _Private_ExchangeDeclare(string exchange, string type, bool passive, bool durable, bool autoDelete, bool @internal, bool nowait, IDictionary arguments) { - ExchangeDeclare method = new ExchangeDeclare(exchange, type, passive, durable, autoDelete, @internal, nowait, arguments); + var method = new ExchangeDeclare(exchange, type, passive, durable, autoDelete, @internal, nowait, arguments); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.ExchangeDeclareOk); + ModelRpc(method, ProtocolCommandId.ExchangeDeclareOk); } } public override void _Private_ExchangeDelete(string exchange, bool ifUnused, bool nowait) { - ExchangeDelete method = new ExchangeDelete(exchange, ifUnused, nowait); + var method = new ExchangeDelete(exchange, ifUnused, nowait); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.ExchangeDeleteOk); + ModelRpc(method, ProtocolCommandId.ExchangeDeleteOk); } } public override void _Private_ExchangeUnbind(string destination, string source, string routingKey, bool nowait, IDictionary arguments) { - ExchangeUnbind method = new ExchangeUnbind(destination, source, routingKey, nowait, arguments); + var method = new ExchangeUnbind(destination, source, routingKey, nowait, arguments); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.ExchangeUnbindOk); + ModelRpc(method, ProtocolCommandId.ExchangeUnbindOk); } } public override void _Private_QueueBind(string queue, string exchange, string routingKey, bool nowait, IDictionary arguments) { - QueueBind method = new QueueBind(queue, exchange, routingKey, nowait, arguments); + var method = new QueueBind(queue, exchange, routingKey, nowait, arguments); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelRpc(ref method, ProtocolCommandId.QueueBindOk); + ModelRpc(method, ProtocolCommandId.QueueBindOk); } } public override void _Private_QueueDeclare(string queue, bool passive, bool durable, bool exclusive, bool autoDelete, bool nowait, IDictionary arguments) { - QueueDeclare method = new QueueDeclare(queue, passive, durable, exclusive, autoDelete, nowait, arguments); + var method = new QueueDeclare(queue, passive, durable, exclusive, autoDelete, nowait, arguments); if (nowait) { - ModelSend(ref method); + ModelSend(method); } else { - ModelSend(ref method); + ModelSend(method); } } public override uint _Private_QueueDelete(string queue, bool ifUnused, bool ifEmpty, bool nowait) { - QueueDelete method = new QueueDelete(queue, ifUnused, ifEmpty, nowait); + var method = new QueueDelete(queue, ifUnused, ifEmpty, nowait); if (nowait) { - ModelSend(ref method); + ModelSend(method); return 0xFFFFFFFF; } - return ModelRpc(ref method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount); + return ModelRpc(method, ProtocolCommandId.QueueDeleteOk, memory => new QueueDeleteOk(memory.Span)._messageCount); } public override uint _Private_QueuePurge(string queue, bool nowait) { - QueuePurge method = new QueuePurge(queue, nowait); + var method = new QueuePurge(queue, nowait); if (nowait) { - ModelSend(ref method); + ModelSend(method); return 0xFFFFFFFF; } - return ModelRpc(ref method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount); + return ModelRpc(method, ProtocolCommandId.QueuePurgeOk, memory => new QueuePurgeOk(memory.Span)._messageCount); } public override void BasicAck(ulong deliveryTag, bool multiple) { - var cmd = new BasicAck(deliveryTag, multiple); - ModelSend(ref cmd); + ModelSend(new BasicAck(deliveryTag, multiple)); } public override void BasicNack(ulong deliveryTag, bool multiple, bool requeue) { - var cmd = new BasicNack(deliveryTag, multiple, requeue); - ModelSend(ref cmd); + ModelSend(new BasicNack(deliveryTag, multiple, requeue)); } public override void BasicQos(uint prefetchSize, ushort prefetchCount, bool global) { - var cmd = new BasicQos(prefetchSize, prefetchCount, global); - ModelRpc(ref cmd, ProtocolCommandId.BasicQosOk); + ModelRpc(new BasicQos(prefetchSize, prefetchCount, global), ProtocolCommandId.BasicQosOk); } public override void BasicRecoverAsync(bool requeue) { - var cmd = new BasicRecoverAsync(requeue); - ModelSend(ref cmd); + ModelSend(new BasicRecoverAsync(requeue)); } public override void BasicReject(ulong deliveryTag, bool requeue) { - var cmd = new BasicReject(deliveryTag, requeue); - ModelSend(ref cmd); + ModelSend(new BasicReject(deliveryTag, requeue)); } public override void QueueUnbind(string queue, string exchange, string routingKey, IDictionary arguments) { - var cmd = new QueueUnbind(queue, exchange, routingKey, arguments); - ModelRpc(ref cmd, ProtocolCommandId.QueueUnbindOk); + ModelRpc(new QueueUnbind(queue, exchange, routingKey, arguments), ProtocolCommandId.QueueUnbindOk); } public override void TxCommit() { - var cmd = new TxCommit(); - ModelRpc(ref cmd, ProtocolCommandId.TxCommitOk); + ModelRpc(new TxCommit(), ProtocolCommandId.TxCommitOk); } public override void TxRollback() { - var cmd = new TxRollback(); - ModelRpc(ref cmd, ProtocolCommandId.TxRollbackOk); + ModelRpc(new TxRollback(), ProtocolCommandId.TxRollbackOk); } public override void TxSelect() { - var cmd = new TxSelect(); - ModelRpc(ref cmd, ProtocolCommandId.TxSelectOk); + ModelRpc(new TxSelect(), ProtocolCommandId.TxSelectOk); } protected override bool DispatchAsynchronous(in IncomingCommand cmd) diff --git a/projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs b/projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs index 601d27d469..51f1a9686e 100644 --- a/projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs +++ b/projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs @@ -249,13 +249,13 @@ public BasicGetResult BasicGet(string queue, bool autoAck) public void BasicNack(ulong deliveryTag, bool multiple, bool requeue) => InnerChannel.BasicNack(deliveryTag, multiple, requeue); - public void BasicPublish(string exchange, string routingKey, ref TProperties basicProperties, ReadOnlyMemory body, bool mandatory) + public void BasicPublish(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory body, bool mandatory) where TProperties : IReadOnlyBasicProperties, IAmqpHeader - => InnerChannel.BasicPublish(exchange, routingKey, ref basicProperties, body, mandatory); + => InnerChannel.BasicPublish(exchange, routingKey, in basicProperties, body, mandatory); - public void BasicPublish(CachedString exchange, CachedString routingKey, ref TProperties basicProperties, ReadOnlyMemory body, bool mandatory) + public void BasicPublish(CachedString exchange, CachedString routingKey, in TProperties basicProperties, ReadOnlyMemory body, bool mandatory) where TProperties : IReadOnlyBasicProperties, IAmqpHeader - => InnerChannel.BasicPublish(exchange, routingKey, ref basicProperties, body, mandatory); + => InnerChannel.BasicPublish(exchange, routingKey, in basicProperties, body, mandatory); public void BasicQos(uint prefetchSize, ushort prefetchCount, bool global) { diff --git a/projects/RabbitMQ.Client/client/impl/Connection.Receive.cs b/projects/RabbitMQ.Client/client/impl/Connection.Receive.cs index 5d944952ab..c4fc9e6f81 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.Receive.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.Receive.cs @@ -148,7 +148,7 @@ private void HardProtocolExceptionHandler(HardProtocolException hpe) try { var cmd = new ConnectionClose(hpe.ShutdownReason.ReplyCode, hpe.ShutdownReason.ReplyText, 0, 0); - _session0.Transmit(ref cmd); + _session0.Transmit(in cmd); if (hpe.CanShutdownCleanly) { ClosingLoop(); diff --git a/projects/RabbitMQ.Client/client/impl/Connection.cs b/projects/RabbitMQ.Client/client/impl/Connection.cs index f7036728d8..aa57de53cd 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.cs @@ -284,8 +284,7 @@ internal void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout) // Try to send connection.close wait for CloseOk in the MainLoop if (!_closed) { - var cmd = new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0); - _session0.Transmit(ref cmd); + _session0.Transmit(new ConnectionClose(reason.ReplyCode, reason.ReplyText, 0, 0)); } } catch (AlreadyClosedException) diff --git a/projects/RabbitMQ.Client/client/impl/EmptyBasicProperty.cs b/projects/RabbitMQ.Client/client/impl/EmptyBasicProperty.cs index f1a3459bc3..7868556dce 100644 --- a/projects/RabbitMQ.Client/client/impl/EmptyBasicProperty.cs +++ b/projects/RabbitMQ.Client/client/impl/EmptyBasicProperty.cs @@ -8,7 +8,7 @@ namespace RabbitMQ.Client.client.impl #nullable enable internal readonly struct EmptyBasicProperty : IReadOnlyBasicProperties, IAmqpHeader { - internal static EmptyBasicProperty Empty; + internal static readonly EmptyBasicProperty Empty; ushort IAmqpHeader.ProtocolClassId => ClassConstants.Basic; diff --git a/projects/RabbitMQ.Client/client/impl/ISession.cs b/projects/RabbitMQ.Client/client/impl/ISession.cs index 1ad103fe13..756bf7db2f 100644 --- a/projects/RabbitMQ.Client/client/impl/ISession.cs +++ b/projects/RabbitMQ.Client/client/impl/ISession.cs @@ -72,8 +72,8 @@ internal interface ISession void Close(ShutdownEventArgs reason, bool notify); bool HandleFrame(in InboundFrame frame); void Notify(); - void Transmit(ref T cmd) where T : struct, IOutgoingAmqpMethod; - void Transmit(ref TMethod cmd, ref THeader header, ReadOnlyMemory body) + void Transmit(in T cmd) where T : struct, IOutgoingAmqpMethod; + void Transmit(in TMethod cmd, in THeader header, ReadOnlyMemory body) where TMethod : struct, IOutgoingAmqpMethod where THeader : IAmqpHeader; } diff --git a/projects/RabbitMQ.Client/client/impl/MainSession.cs b/projects/RabbitMQ.Client/client/impl/MainSession.cs index d600a67eec..8b7e19f597 100644 --- a/projects/RabbitMQ.Client/client/impl/MainSession.cs +++ b/projects/RabbitMQ.Client/client/impl/MainSession.cs @@ -99,7 +99,7 @@ public void SetSessionClosing(bool closeServerInitiated) } } - public override void Transmit(ref T cmd) + public override void Transmit(in T cmd) { if (_closing && // Are we closing? cmd.ProtocolCommandId != ProtocolCommandId.ConnectionCloseOk && // is this not a close-ok? @@ -110,7 +110,7 @@ public override void Transmit(ref T cmd) return; } - base.Transmit(ref cmd); + base.Transmit(in cmd); } } } diff --git a/projects/RabbitMQ.Client/client/impl/ModelBase.cs b/projects/RabbitMQ.Client/client/impl/ModelBase.cs index 6913218232..5620ff067c 100644 --- a/projects/RabbitMQ.Client/client/impl/ModelBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ModelBase.cs @@ -333,7 +333,7 @@ private void HandleCommand(in IncomingCommand cmd) } } - protected void ModelRpc(ref TMethod method, ProtocolCommandId returnCommandId) + protected void ModelRpc(in TMethod method, ProtocolCommandId returnCommandId) where TMethod : struct, IOutgoingAmqpMethod { var k = new SimpleBlockingRpcContinuation(); @@ -341,7 +341,7 @@ protected void ModelRpc(ref TMethod method, ProtocolCommandId returnCom lock (_rpcLock) { Enqueue(k); - Session.Transmit(ref method); + Session.Transmit(in method); k.GetReply(ContinuationTimeout, out reply); } @@ -353,7 +353,7 @@ protected void ModelRpc(ref TMethod method, ProtocolCommandId returnCom } } - protected TReturn ModelRpc(ref TMethod method, ProtocolCommandId returnCommandId, Func, TReturn> createFunc) + protected TReturn ModelRpc(in TMethod method, ProtocolCommandId returnCommandId, Func, TReturn> createFunc) where TMethod : struct, IOutgoingAmqpMethod { var k = new SimpleBlockingRpcContinuation(); @@ -362,7 +362,7 @@ protected TReturn ModelRpc(ref TMethod method, ProtocolCommand lock (_rpcLock) { Enqueue(k); - Session.Transmit(ref method); + Session.Transmit(in method); k.GetReply(ContinuationTimeout, out reply); } @@ -378,13 +378,13 @@ protected TReturn ModelRpc(ref TMethod method, ProtocolCommand } [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected void ModelSend(ref T method) where T : struct, IOutgoingAmqpMethod + protected void ModelSend(in T method) where T : struct, IOutgoingAmqpMethod { - Session.Transmit(ref method); + Session.Transmit(in method); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected void ModelSend(ref TMethod method, ref THeader header, ReadOnlyMemory body) + protected void ModelSend(in TMethod method, in THeader header, ReadOnlyMemory body) where TMethod : struct, IOutgoingAmqpMethod where THeader : IAmqpHeader { @@ -392,7 +392,7 @@ protected void ModelSend(ref TMethod method, ref THeader heade { _flowControlBlock.Wait(); } - Session.Transmit(ref method, ref header, body); + Session.Transmit(in method, in header, body); } internal void OnCallbackException(CallbackExceptionEventArgs args) @@ -898,7 +898,7 @@ public BasicGetResult BasicGet(string queue, bool autoAck) public abstract void BasicNack(ulong deliveryTag, bool multiple, bool requeue); - public void BasicPublish(string exchange, string routingKey, ref TProperties basicProperties, ReadOnlyMemory body, bool mandatory) + public void BasicPublish(string exchange, string routingKey, in TProperties basicProperties, ReadOnlyMemory body, bool mandatory) where TProperties : IReadOnlyBasicProperties, IAmqpHeader { if (NextPublishSeqNo > 0) @@ -910,10 +910,10 @@ public void BasicPublish(string exchange, string routingKey, ref TP } var cmd = new BasicPublish(exchange, routingKey, mandatory, default); - ModelSend(ref cmd, ref basicProperties, body); + ModelSend(in cmd, in basicProperties, body); } - public void BasicPublish(CachedString exchange, CachedString routingKey, ref TProperties basicProperties, ReadOnlyMemory body, bool mandatory) + public void BasicPublish(CachedString exchange, CachedString routingKey, in TProperties basicProperties, ReadOnlyMemory body, bool mandatory) where TProperties : IReadOnlyBasicProperties, IAmqpHeader { if (NextPublishSeqNo > 0) @@ -925,7 +925,7 @@ public void BasicPublish(CachedString exchange, CachedString routin } var cmd = new BasicPublishMemory(exchange.Bytes, routingKey.Bytes, mandatory, default); - ModelSend(ref cmd, ref basicProperties, body); + ModelSend(in cmd, in basicProperties, body); } public void UpdateSecret(string newSecret, string reason) diff --git a/projects/RabbitMQ.Client/client/impl/SessionBase.cs b/projects/RabbitMQ.Client/client/impl/SessionBase.cs index c6991f5c70..a53ec3c376 100644 --- a/projects/RabbitMQ.Client/client/impl/SessionBase.cs +++ b/projects/RabbitMQ.Client/client/impl/SessionBase.cs @@ -30,6 +30,7 @@ //--------------------------------------------------------------------------- using System; +using System.Runtime.CompilerServices; using System.Threading; using RabbitMQ.Client.client.framing; using RabbitMQ.Client.Exceptions; @@ -129,17 +130,17 @@ public void Notify() OnSessionShutdown(reason); } - public virtual void Transmit(ref T cmd) where T : struct, IOutgoingAmqpMethod + public virtual void Transmit(in T cmd) where T : struct, IOutgoingAmqpMethod { if (!IsOpen && cmd.ProtocolCommandId != client.framing.ProtocolCommandId.ChannelCloseOk) { ThrowAlreadyClosedException(); } - Connection.Write(Framing.SerializeToFrames(ref cmd, ChannelNumber)); + Connection.Write(Framing.SerializeToFrames(ref Unsafe.AsRef(cmd), ChannelNumber)); } - public void Transmit(ref TMethod cmd, ref THeader header, ReadOnlyMemory body) + public void Transmit(in TMethod cmd, in THeader header, ReadOnlyMemory body) where TMethod : struct, IOutgoingAmqpMethod where THeader : IAmqpHeader { @@ -148,7 +149,7 @@ public void Transmit(ref TMethod cmd, ref THeader header, Read ThrowAlreadyClosedException(); } - Connection.Write(Framing.SerializeToFrames(ref cmd, ref header, body, ChannelNumber, Connection.MaxPayloadSize)); + Connection.Write(Framing.SerializeToFrames(ref Unsafe.AsRef(cmd), ref Unsafe.AsRef(header), body, ChannelNumber, Connection.MaxPayloadSize)); } private void ThrowAlreadyClosedException() diff --git a/projects/TestApplications/MassPublish/Program.cs b/projects/TestApplications/MassPublish/Program.cs index e489aee3ee..c6cd9ab06a 100644 --- a/projects/TestApplications/MassPublish/Program.cs +++ b/projects/TestApplications/MassPublish/Program.cs @@ -48,7 +48,7 @@ public static void Main() { AppId = "testapp", }; - publisher.BasicPublish("test", "myawesome.routing.key", ref properties, payload); + publisher.BasicPublish("test", "myawesome.routing.key", properties, payload); } messagesSent += ItemsPerBatch; await publisher.WaitForConfirmsOrDieAsync().ConfigureAwait(false); diff --git a/projects/Unit/APIApproval.Approve.verified.txt b/projects/Unit/APIApproval.Approve.verified.txt index ed57116c23..7d50851ac9 100644 --- a/projects/Unit/APIApproval.Approve.verified.txt +++ b/projects/Unit/APIApproval.Approve.verified.txt @@ -456,9 +456,9 @@ namespace RabbitMQ.Client string BasicConsume(string queue, bool autoAck, string consumerTag, bool noLocal, bool exclusive, System.Collections.Generic.IDictionary arguments, RabbitMQ.Client.IBasicConsumer consumer); RabbitMQ.Client.BasicGetResult BasicGet(string queue, bool autoAck); void BasicNack(ulong deliveryTag, bool multiple, bool requeue); - void BasicPublish(RabbitMQ.Client.CachedString exchange, RabbitMQ.Client.CachedString routingKey, ref TProperties basicProperties, System.ReadOnlyMemory body = default, bool mandatory = false) + void BasicPublish(RabbitMQ.Client.CachedString exchange, RabbitMQ.Client.CachedString routingKey, in TProperties basicProperties, System.ReadOnlyMemory body = default, bool mandatory = false) where TProperties : RabbitMQ.Client.IReadOnlyBasicProperties, RabbitMQ.Client.IAmqpHeader; - void BasicPublish(string exchange, string routingKey, ref TProperties basicProperties, System.ReadOnlyMemory body = default, bool mandatory = false) + void BasicPublish(string exchange, string routingKey, in TProperties basicProperties, System.ReadOnlyMemory body = default, bool mandatory = false) where TProperties : RabbitMQ.Client.IReadOnlyBasicProperties, RabbitMQ.Client.IAmqpHeader; void BasicQos(uint prefetchSize, ushort prefetchCount, bool global); void BasicRecover(bool requeue); @@ -502,7 +502,7 @@ namespace RabbitMQ.Client public static string BasicConsume(this RabbitMQ.Client.IModel model, RabbitMQ.Client.IBasicConsumer consumer, string queue, bool autoAck = false, string consumerTag = "", bool noLocal = false, bool exclusive = false, System.Collections.Generic.IDictionary arguments = null) { } public static void BasicPublish(this RabbitMQ.Client.IModel model, RabbitMQ.Client.CachedString exchange, RabbitMQ.Client.CachedString routingKey, System.ReadOnlyMemory body = default, bool mandatory = false) { } public static void BasicPublish(this RabbitMQ.Client.IModel model, string exchange, string routingKey, System.ReadOnlyMemory body = default, bool mandatory = false) { } - public static void BasicPublish(this RabbitMQ.Client.IModel model, RabbitMQ.Client.PublicationAddress addr, ref T basicProperties, System.ReadOnlyMemory body) + public static void BasicPublish(this RabbitMQ.Client.IModel model, RabbitMQ.Client.PublicationAddress addr, in T basicProperties, System.ReadOnlyMemory body) where T : RabbitMQ.Client.IReadOnlyBasicProperties, RabbitMQ.Client.IAmqpHeader { } public static void Close(this RabbitMQ.Client.IModel model) { } public static void Close(this RabbitMQ.Client.IModel model, ushort replyCode, string replyText) { } diff --git a/projects/Unit/TestBasicPublish.cs b/projects/Unit/TestBasicPublish.cs index 428c7c8f31..5854aa094d 100644 --- a/projects/Unit/TestBasicPublish.cs +++ b/projects/Unit/TestBasicPublish.cs @@ -32,7 +32,7 @@ public void TestBasicRoundtripArray() }; string tag = m.BasicConsume(q.QueueName, true, consumer); - m.BasicPublish("", q.QueueName, ref bp, sendBody); + m.BasicPublish("", q.QueueName, bp, sendBody); bool waitResFalse = are.WaitOne(5000); m.BasicCancel(tag); diff --git a/projects/Unit/TestConnectionRecovery.cs b/projects/Unit/TestConnectionRecovery.cs index eef7b84e33..62943aac5f 100644 --- a/projects/Unit/TestConnectionRecovery.cs +++ b/projects/Unit/TestConnectionRecovery.cs @@ -877,7 +877,7 @@ public void TestPublishRpcRightAfterReconnect() { try { - _model.BasicPublish(string.Empty, testQueueName, ref properties, _messageBody); + _model.BasicPublish(string.Empty, testQueueName, properties, _messageBody); } catch (Exception e) {