From f722b96f1aa58b7146ce921d668ebf894eeb35ee Mon Sep 17 00:00:00 2001 From: bollhals Date: Wed, 19 Aug 2020 00:39:50 +0200 Subject: [PATCH] change AmqpVersion to struct --- .../client/impl/AmqpVersion.cs | 21 ++++++++++++++----- .../RabbitMQ.Client/client/impl/Connection.cs | 8 ++----- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/projects/RabbitMQ.Client/client/impl/AmqpVersion.cs b/projects/RabbitMQ.Client/client/impl/AmqpVersion.cs index e78cd5b35f..f0d10d80a3 100644 --- a/projects/RabbitMQ.Client/client/impl/AmqpVersion.cs +++ b/projects/RabbitMQ.Client/client/impl/AmqpVersion.cs @@ -29,6 +29,8 @@ // Copyright (c) 2007-2020 VMware, Inc. All rights reserved. //--------------------------------------------------------------------------- +using System; + namespace RabbitMQ.Client.Framing.Impl { /// Represents a version of the AMQP specification. @@ -45,7 +47,7 @@ namespace RabbitMQ.Client.Framing.Impl /// special-cases 8-0, rewriting it at construction time to be 0-8 instead. /// /// - class AmqpVersion + internal readonly struct AmqpVersion : IEquatable { /// /// Construct an from major and minor version numbers. @@ -70,27 +72,36 @@ public AmqpVersion(int major, int minor) /// /// The AMQP specification major version number. /// - public int Major { get; private set; } + public int Major { get; } /// /// The AMQP specification minor version number. /// - public int Minor { get; private set; } + public int Minor { get; } /// /// Implement value-equality comparison. /// public override bool Equals(object other) { - return (other is AmqpVersion version) && (version.Major == Major) && (version.Minor == Minor); + return other is AmqpVersion version && Equals(version); } + public bool Equals(AmqpVersion other) => Major == other.Major && Minor == other.Minor; + + public static bool operator ==(AmqpVersion left, AmqpVersion right) => left.Equals(right); + + public static bool operator !=(AmqpVersion left, AmqpVersion right) => !left.Equals(right); + /// /// Implement hashing as for value-equality. /// public override int GetHashCode() { - return 31*Major.GetHashCode() + Minor.GetHashCode(); + unchecked + { + return (Major * 397) ^ Minor; + } } /// diff --git a/projects/RabbitMQ.Client/client/impl/Connection.cs b/projects/RabbitMQ.Client/client/impl/Connection.cs index eee58a246b..e2f031c5df 100644 --- a/projects/RabbitMQ.Client/client/impl/Connection.cs +++ b/projects/RabbitMQ.Client/client/impl/Connection.cs @@ -1060,16 +1060,12 @@ void StartAndTune() ServerProperties = connectionStart.m_serverProperties; - var serverVersion = new AmqpVersion(connectionStart.m_versionMajor, - connectionStart.m_versionMinor); + var serverVersion = new AmqpVersion(connectionStart.m_versionMajor, connectionStart.m_versionMinor); if (!serverVersion.Equals(Protocol.Version)) { TerminateMainloop(); FinishClose(); - throw new ProtocolVersionMismatchException(Protocol.MajorVersion, - Protocol.MinorVersion, - serverVersion.Major, - serverVersion.Minor); + throw new ProtocolVersionMismatchException(Protocol.MajorVersion, Protocol.MinorVersion, serverVersion.Major, serverVersion.Minor); } ClientProperties = new Dictionary(_factory.ClientProperties)