diff --git a/projects/RabbitMQ.Client/client/api/ConnectionFactory.cs b/projects/RabbitMQ.Client/client/api/ConnectionFactory.cs index 5690207582..15d5b07e1c 100644 --- a/projects/RabbitMQ.Client/client/api/ConnectionFactory.cs +++ b/projects/RabbitMQ.Client/client/api/ConnectionFactory.cs @@ -329,14 +329,16 @@ public Uri Uri public IAuthMechanismFactory AuthMechanismFactory(IList mechanismNames) { // Our list is in order of preference, the server one is not. - foreach (IAuthMechanismFactory factory in AuthMechanisms) + for (int index = 0; index < AuthMechanisms.Count; index++) { + IAuthMechanismFactory factory = AuthMechanisms[index]; string factoryName = factory.Name; if (mechanismNames.Any(x => string.Equals(x, factoryName, StringComparison.OrdinalIgnoreCase))) { return factory; } } + return null; } diff --git a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs index c3d1593ecd..1f11a61a3c 100644 --- a/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs +++ b/projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs @@ -67,15 +67,15 @@ internal sealed class AutorecoveringConnection : IAutorecoveringConnection private readonly object _recordedEntitiesLock = new object(); - private readonly IDictionary _recordedExchanges = new Dictionary(); + private readonly Dictionary _recordedExchanges = new Dictionary(); - private readonly IDictionary _recordedQueues = new Dictionary(); + private readonly Dictionary _recordedQueues = new Dictionary(); - private readonly IDictionary _recordedBindings = new Dictionary(); + private readonly Dictionary _recordedBindings = new Dictionary(); - private readonly IDictionary _recordedConsumers = new Dictionary(); + private readonly Dictionary _recordedConsumers = new Dictionary(); - private readonly ICollection _models = new List(); + private readonly List _models = new List(); private EventHandler _recordedBlockedEventHandlers; private EventHandler _recordedShutdownEventHandlers; @@ -485,12 +485,11 @@ public void DeleteRecordedBinding(RecordedBinding rb) public RecordedConsumer DeleteRecordedConsumer(string consumerTag) { - RecordedConsumer rc = null; + RecordedConsumer rc; lock (_recordedEntitiesLock) { - if (_recordedConsumers.ContainsKey(consumerTag)) + if (_recordedConsumers.TryGetValue(consumerTag, out rc)) { - rc = _recordedConsumers[consumerTag]; _recordedConsumers.Remove(consumerTag); } } @@ -912,10 +911,12 @@ private void PropagateQueueNameChangeToBindings(string oldName, string newName) { lock (_recordedBindings) { - IEnumerable bs = _recordedBindings.Keys.Where(b => b.Destination.Equals(oldName)); - foreach (RecordedBinding b in bs) + foreach (RecordedBinding b in _recordedBindings.Keys) { - b.Destination = newName; + if (b.Destination.Equals(oldName)) + { + b.Destination = newName; + } } } } @@ -924,21 +925,22 @@ private void PropagateQueueNameChangeToConsumers(string oldName, string newName) { lock (_recordedConsumers) { - IEnumerable> cs = _recordedConsumers. - Where(pair => pair.Value.Queue.Equals(oldName)); - foreach (KeyValuePair c in cs) + foreach (KeyValuePair c in _recordedConsumers) { - c.Value.Queue = newName; + if (c.Value.Queue.Equals(oldName)) + { + c.Value.Queue = newName; + } } } } private void RecoverBindings() { - IDictionary recordedBindingsCopy = null; + Dictionary recordedBindingsCopy; lock (_recordedBindings) { - recordedBindingsCopy = _recordedBindings.ToDictionary(e => e.Key, e => e.Value); + recordedBindingsCopy = new Dictionary(_recordedBindings); } foreach (RecordedBinding b in recordedBindingsCopy.Keys) @@ -1031,10 +1033,10 @@ private void RecoverConsumers() throw new ObjectDisposedException(GetType().FullName); } - IDictionary recordedConsumersCopy = null; + Dictionary recordedConsumersCopy; lock (_recordedConsumers) { - recordedConsumersCopy = _recordedConsumers.ToDictionary(e => e.Key, e => e.Value); + recordedConsumersCopy = new Dictionary(_recordedConsumers); } foreach (KeyValuePair pair in recordedConsumersCopy) @@ -1091,10 +1093,10 @@ private void RecoverEntities() private void RecoverExchanges() { - IDictionary recordedExchangesCopy = null; + Dictionary recordedExchangesCopy; lock (_recordedEntitiesLock) { - recordedExchangesCopy = _recordedExchanges.ToDictionary(e => e.Key, e => e.Value); + recordedExchangesCopy = new Dictionary(_recordedExchanges); } foreach (RecordedExchange rx in recordedExchangesCopy.Values) @@ -1125,10 +1127,10 @@ private void RecoverModels() private void RecoverQueues() { - IDictionary recordedQueuesCopy = null; + Dictionary recordedQueuesCopy; lock (_recordedEntitiesLock) { - recordedQueuesCopy = _recordedQueues.ToDictionary(entry => entry.Key, entry => entry.Value); + recordedQueuesCopy = new Dictionary(_recordedQueues); } foreach (KeyValuePair pair in recordedQueuesCopy) diff --git a/projects/RabbitMQ.Client/client/impl/BasicProperties.cs b/projects/RabbitMQ.Client/client/impl/BasicProperties.cs index 05ffda72a2..dc7d7f6035 100644 --- a/projects/RabbitMQ.Client/client/impl/BasicProperties.cs +++ b/projects/RabbitMQ.Client/client/impl/BasicProperties.cs @@ -284,11 +284,7 @@ public override object Clone() var clone = MemberwiseClone() as BasicProperties; if (IsHeadersPresent()) { - clone.Headers = new Dictionary(); - foreach (KeyValuePair entry in Headers) - { - clone.Headers[entry.Key] = entry.Value; - } + clone.Headers = new Dictionary(Headers); } return clone; diff --git a/projects/RabbitMQ.Client/client/impl/Connection.cs b/projects/RabbitMQ.Client/client/impl/Connection.cs index aead6f6c92..24802bb3e1 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.cs @@ -730,10 +730,9 @@ public void PrettyPrintShutdownReport() { Console.Error.WriteLine( "Log of errors while closing connection {0}:", this); - foreach (ShutdownReportEntry entry in ShutdownReport) + for (int index = 0; index < ShutdownReport.Count; index++) { - Console.Error.WriteLine( -entry.ToString()); + Console.Error.WriteLine(ShutdownReport[index].ToString()); } } } diff --git a/projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs b/projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs index d9d3cf9672..35cb86d76c 100644 --- a/projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs +++ b/projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs @@ -45,7 +45,7 @@ namespace RabbitMQ.Client.Impl { - struct ContentHeaderPropertyReader + internal struct ContentHeaderPropertyReader { private ushort m_bitCount; private ushort m_flagWord; @@ -143,9 +143,9 @@ public string ReadShortstr() } /// A type of . - public IDictionary ReadTable() + public Dictionary ReadTable() { - IDictionary result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead); + Dictionary result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead); _memoryOffset += bytesRead; return result; } diff --git a/projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs b/projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs index 6adcc34731..7c658ffcce 100644 --- a/projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs +++ b/projects/RabbitMQ.Client/client/impl/MethodArgumentReader.cs @@ -45,7 +45,7 @@ namespace RabbitMQ.Client.Impl { - struct MethodArgumentReader + internal struct MethodArgumentReader { private int? _bit; private int _bits; @@ -125,10 +125,10 @@ public string ReadShortstr() return result; } - public IDictionary ReadTable() + public Dictionary ReadTable() { ClearBits(); - IDictionary result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead); + Dictionary result = WireFormatting.ReadTable(_memory.Slice(_memoryOffset), out int bytesRead); _memoryOffset += bytesRead; return result; } diff --git a/projects/RabbitMQ.Client/client/impl/ModelBase.cs b/projects/RabbitMQ.Client/client/impl/ModelBase.cs index e17d4f482d..7e4fc95ded 100644 --- a/projects/RabbitMQ.Client/client/impl/ModelBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ModelBase.cs @@ -55,12 +55,12 @@ namespace RabbitMQ.Client.Impl { abstract class ModelBase : IFullModel, IRecoverable { - public readonly IDictionary m_consumers = new Dictionary(); - ///Only used to kick-start a connection open ///sequence. See public BlockingCell m_connectionStartCell = null; + private readonly Dictionary _consumers = new Dictionary(); + private TimeSpan _handshakeContinuationTimeout = TimeSpan.FromSeconds(10); private TimeSpan _continuationTimeout = TimeSpan.FromSeconds(20); @@ -500,11 +500,11 @@ public void OnSessionShutdown(object sender, ShutdownEventArgs reason) ConsumerDispatcher.Quiesce(); SetCloseReason(reason); OnModelShutdown(reason); - BroadcastShutdownToConsumers(m_consumers, reason); + BroadcastShutdownToConsumers(_consumers, reason); ConsumerDispatcher.Shutdown(this).GetAwaiter().GetResult();; } - protected void BroadcastShutdownToConsumers(IDictionary cs, ShutdownEventArgs reason) + protected void BroadcastShutdownToConsumers(Dictionary cs, ShutdownEventArgs reason) { foreach (KeyValuePair c in cs) { @@ -578,10 +578,10 @@ public void HandleBasicAck(ulong deliveryTag, public void HandleBasicCancel(string consumerTag, bool nowait) { IBasicConsumer consumer; - lock (m_consumers) + lock (_consumers) { - consumer = m_consumers[consumerTag]; - m_consumers.Remove(consumerTag); + consumer = _consumers[consumerTag]; + _consumers.Remove(consumerTag); } if (consumer == null) { @@ -601,10 +601,10 @@ public void HandleBasicCancelOk(string consumerTag) consumerTag )); */ - lock (m_consumers) + lock (_consumers) { - k.m_consumer = m_consumers[consumerTag]; - m_consumers.Remove(consumerTag); + k.m_consumer = _consumers[consumerTag]; + _consumers.Remove(consumerTag); } ConsumerDispatcher.HandleBasicCancelOk(k.m_consumer, consumerTag); k.HandleCommand(null); // release the continuation. @@ -615,9 +615,9 @@ public void HandleBasicConsumeOk(string consumerTag) var k = (BasicConsumerRpcContinuation)_continuationQueue.Next(); k.m_consumerTag = consumerTag; - lock (m_consumers) + lock (_consumers) { - m_consumers[consumerTag] = k.m_consumer; + _consumers[consumerTag] = k.m_consumer; } ConsumerDispatcher.HandleBasicConsumeOk(k.m_consumer, consumerTag); k.HandleCommand(null); // release the continuation. @@ -632,9 +632,9 @@ public virtual void HandleBasicDeliver(string consumerTag, ReadOnlyMemory body) { IBasicConsumer consumer; - lock (m_consumers) + lock (_consumers) { - consumer = m_consumers[consumerTag]; + consumer = _consumers[consumerTag]; } if (consumer == null) { @@ -1005,9 +1005,9 @@ public void BasicCancel(string consumerTag) k.GetReply(ContinuationTimeout); } - lock (m_consumers) + lock (_consumers) { - m_consumers.Remove(consumerTag); + _consumers.Remove(consumerTag); } ModelShutdown -= k.m_consumer.HandleModelShutdown; @@ -1017,9 +1017,9 @@ public void BasicCancelNoWait(string consumerTag) { _Private_BasicCancel(consumerTag, true); - lock (m_consumers) + lock (_consumers) { - m_consumers.Remove(consumerTag); + _consumers.Remove(consumerTag); } } diff --git a/projects/RabbitMQ.Client/client/impl/ProtocolBase.cs b/projects/RabbitMQ.Client/client/impl/ProtocolBase.cs index be9788c72e..f4be4336aa 100644 --- a/projects/RabbitMQ.Client/client/impl/ProtocolBase.cs +++ b/projects/RabbitMQ.Client/client/impl/ProtocolBase.cs @@ -47,16 +47,19 @@ namespace RabbitMQ.Client.Framing.Impl { abstract class ProtocolBase : IProtocol { - public IDictionary Capabilities = new Dictionary(); + public IDictionary Capabilities; public ProtocolBase() { - Capabilities["publisher_confirms"] = true; - Capabilities["exchange_exchange_bindings"] = true; - Capabilities["basic.nack"] = true; - Capabilities["consumer_cancel_notify"] = true; - Capabilities["connection.blocked"] = true; - Capabilities["authentication_failure_close"] = true; + Capabilities = new Dictionary + { + ["publisher_confirms"] = true, + ["exchange_exchange_bindings"] = true, + ["basic.nack"] = true, + ["consumer_cancel_notify"] = true, + ["connection.blocked"] = true, + ["authentication_failure_close"] = true + }; } public abstract string ApiName { get; } diff --git a/projects/RabbitMQ.Client/client/impl/Session.cs b/projects/RabbitMQ.Client/client/impl/Session.cs index b448984ad7..474dffd54e 100644 --- a/projects/RabbitMQ.Client/client/impl/Session.cs +++ b/projects/RabbitMQ.Client/client/impl/Session.cs @@ -55,9 +55,10 @@ public Session(Connection connection, int channelNumber) public override void HandleFrame(in InboundFrame frame) { - using (Command cmd = _assembler.HandleFrame(in frame)) + Command cmd = _assembler.HandleFrame(in frame); + if (cmd != null) { - if (cmd != null) + using (cmd) { OnCommandReceived(cmd); } diff --git a/projects/RabbitMQ.Client/client/impl/SessionManager.cs b/projects/RabbitMQ.Client/client/impl/SessionManager.cs index 68963cfbd5..035d066998 100644 --- a/projects/RabbitMQ.Client/client/impl/SessionManager.cs +++ b/projects/RabbitMQ.Client/client/impl/SessionManager.cs @@ -55,7 +55,7 @@ class SessionManager public readonly ushort ChannelMax; private readonly IntAllocator _ints; private readonly Connection _connection; - private readonly IDictionary _sessionMap = new Dictionary(); + private readonly Dictionary _sessionMap = new Dictionary(); private bool _autoClose = false; public SessionManager(Connection connection, ushort channelMax) diff --git a/projects/RabbitMQ.Client/client/impl/StreamProperties.cs b/projects/RabbitMQ.Client/client/impl/StreamProperties.cs index 2821092b45..e97e4a53a9 100644 --- a/projects/RabbitMQ.Client/client/impl/StreamProperties.cs +++ b/projects/RabbitMQ.Client/client/impl/StreamProperties.cs @@ -55,11 +55,7 @@ public override object Clone() var clone = MemberwiseClone() as StreamProperties; if (IsHeadersPresent()) { - clone.Headers = new Dictionary(); - foreach (KeyValuePair entry in Headers) - { - clone.Headers[entry.Key] = entry.Value; - } + clone.Headers = new Dictionary(Headers); } return clone; diff --git a/projects/RabbitMQ.Client/client/impl/WireFormatting.cs b/projects/RabbitMQ.Client/client/impl/WireFormatting.cs index 1ade155571..6ae22715e5 100644 --- a/projects/RabbitMQ.Client/client/impl/WireFormatting.cs +++ b/projects/RabbitMQ.Client/client/impl/WireFormatting.cs @@ -49,7 +49,7 @@ namespace RabbitMQ.Client.Impl { - class WireFormatting + internal class WireFormatting { public static decimal AmqpToDecimal(byte scale, uint unsignedMantissa) { @@ -95,8 +95,8 @@ public static void DecimalToAmqp(decimal value, out byte scale, out int mantissa public static IList ReadArray(ReadOnlyMemory memory, out int bytesRead) { - IList array = new List(); - long arrayLength = NetworkOrderDeserializer.ReadUInt32(memory); + uint arrayLength = NetworkOrderDeserializer.ReadUInt32(memory); + List array = new List((int)arrayLength); bytesRead = 4; while (bytesRead - 4 < arrayLength) { @@ -138,7 +138,7 @@ public static object ReadFieldValue(ReadOnlyMemory memory, out int bytesRe bytesRead += 8; return ReadTimestamp(slice); case 'F': - IDictionary tableResult = ReadTable(slice, out int tableBytesRead); + Dictionary tableResult = ReadTable(slice, out int tableBytesRead); bytesRead += tableBytesRead; return tableResult; case 'A': @@ -207,10 +207,10 @@ public static string ReadShortstr(ReadOnlyMemory memory, out int bytesRead /// and F, as well as the QPid-0-8 specific b, d, f, l, s, t, /// x and V types and the AMQP 0-9-1 A type. /// - /// A . - public static IDictionary ReadTable(ReadOnlyMemory memory, out int bytesRead) + /// A . + public static Dictionary ReadTable(ReadOnlyMemory memory, out int bytesRead) { - IDictionary table = new Dictionary(); + Dictionary table = new Dictionary(); long tableLength = NetworkOrderDeserializer.ReadUInt32(memory); bytesRead = 4; while ((bytesRead - 4) < tableLength) @@ -247,9 +247,9 @@ public static int WriteArray(Memory memory, IList val) else { int bytesWritten = 0; - foreach (object entry in val) + for (int index = 0; index < val.Count; index++) { - bytesWritten += WriteFieldValue(memory.Slice(4 + bytesWritten), entry); ; + bytesWritten += WriteFieldValue(memory.Slice(4 + bytesWritten), val[index]); } NetworkOrderSerializer.WriteUInt32(memory, (uint)bytesWritten); @@ -265,9 +265,9 @@ public static int GetArrayByteCount(IList val) return byteCount; } - foreach (object entry in val) + for (int index = 0; index < val.Count; index++) { - byteCount += GetFieldValueByteCount(entry); + byteCount += GetFieldValueByteCount(val[index]); } return byteCount; @@ -476,7 +476,7 @@ public static int WriteTable(Memory memory, IDictionary va int bytesWritten = 0; foreach (KeyValuePair entry in val) { - bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key.ToString()); + bytesWritten += WriteShortstr(slice.Slice(bytesWritten), entry.Key); bytesWritten += WriteFieldValue(slice.Slice(bytesWritten), entry.Value); } @@ -512,7 +512,7 @@ public static int GetTableByteCount(IDictionary val) foreach (KeyValuePair entry in val) { - byteCount += Encoding.UTF8.GetByteCount(entry.Key.ToString()) + 1; + byteCount += Encoding.UTF8.GetByteCount(entry.Key) + 1; byteCount += GetFieldValueByteCount(entry.Value); }