diff --git a/projects/RabbitMQ.Client/client/api/ITcpClient.cs b/projects/RabbitMQ.Client/client/api/ITcpClient.cs index d068f04b22..49497d95a3 100644 --- a/projects/RabbitMQ.Client/client/api/ITcpClient.cs +++ b/projects/RabbitMQ.Client/client/api/ITcpClient.cs @@ -5,7 +5,8 @@ namespace RabbitMQ.Client { /// - /// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class. + /// Wrapper interface for . + /// Provides the socket for socket frame handler class. /// /// Contains all methods that are currenty in use in rabbitmq client. public interface ITcpClient : IDisposable diff --git a/projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs b/projects/RabbitMQ.Client/client/api/TcpClientAdapter.cs similarity index 76% rename from projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs rename to projects/RabbitMQ.Client/client/api/TcpClientAdapter.cs index 98e8de484f..a849d26bf5 100644 --- a/projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs +++ b/projects/RabbitMQ.Client/client/api/TcpClientAdapter.cs @@ -1,14 +1,16 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; -namespace RabbitMQ.Client.Impl +namespace RabbitMQ.Client { /// - /// Simple wrapper around TcpClient. + /// Simple wrapper around . /// - class TcpClientAdapter : ITcpClient + public class TcpClientAdapter : ITcpClient { private Socket _sock; @@ -21,7 +23,7 @@ public virtual async Task ConnectAsync(string host, int port) { AssertSocket(); IPAddress[] adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false); - IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily); + IPAddress ep = GetMatchingHost(adds, _sock.AddressFamily); if (ep == default(IPAddress)) { throw new ArgumentException($"No ip address could be resolved for {host}"); @@ -43,8 +45,7 @@ public virtual void Close() _sock = null; } - [Obsolete("Override Dispose(bool) instead.")] - public virtual void Dispose() + public void Dispose() { Dispose(true); } @@ -53,11 +54,8 @@ protected virtual void Dispose(bool disposing) { if (disposing) { - // dispose managed resources Close(); } - - // dispose unmanaged resources } public virtual NetworkStream GetStream() @@ -104,5 +102,15 @@ private void AssertSocket() throw new InvalidOperationException("Cannot perform operation as socket is null"); } } + + public static IPAddress GetMatchingHost(IReadOnlyCollection addresses, AddressFamily addressFamily) + { + IPAddress ep = addresses.FirstOrDefault(a => a.AddressFamily == addressFamily); + if (ep is null && addresses.Count == 1 && addressFamily == AddressFamily.Unspecified) + { + return addresses.Single(); + } + return ep; + } } } diff --git a/projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs b/projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs deleted file mode 100644 index 4fc8b46697..0000000000 --- a/projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Sockets; - -namespace RabbitMQ.Client.Impl -{ - static class TcpClientAdapterHelper - { - public static IPAddress GetMatchingHost(IReadOnlyCollection addresses, AddressFamily addressFamily) - { - IPAddress ep = addresses.FirstOrDefault(a => a.AddressFamily == addressFamily); - if (ep == null && addresses.Count == 1 && addressFamily == AddressFamily.Unspecified) - { - return addresses.Single(); - } - return ep; - } - } -} diff --git a/projects/Unit/APIApproval.Approve.verified.txt b/projects/Unit/APIApproval.Approve.verified.txt index 9223669cf1..d7bcdaab0c 100644 --- a/projects/Unit/APIApproval.Approve.verified.txt +++ b/projects/Unit/APIApproval.Approve.verified.txt @@ -647,6 +647,19 @@ namespace RabbitMQ.Client public string ServerName { get; set; } public System.Security.Authentication.SslProtocols Version { get; set; } } + public class TcpClientAdapter : RabbitMQ.Client.ITcpClient, System.IDisposable + { + public TcpClientAdapter(System.Net.Sockets.Socket socket) { } + public virtual System.Net.Sockets.Socket Client { get; } + public virtual bool Connected { get; } + public virtual System.TimeSpan ReceiveTimeout { get; set; } + public virtual void Close() { } + public virtual System.Threading.Tasks.Task ConnectAsync(string host, int port) { } + public void Dispose() { } + protected virtual void Dispose(bool disposing) { } + public virtual System.Net.Sockets.NetworkStream GetStream() { } + public static System.Net.IPAddress GetMatchingHost(System.Collections.Generic.IReadOnlyCollection addresses, System.Net.Sockets.AddressFamily addressFamily) { } + } public class TimerBasedCredentialRefresher : RabbitMQ.Client.ICredentialsRefresher { public TimerBasedCredentialRefresher() { } diff --git a/projects/Unit/TestConnectionFactory.cs b/projects/Unit/TestConnectionFactory.cs index 58b358890a..c21b19dedc 100644 --- a/projects/Unit/TestConnectionFactory.cs +++ b/projects/Unit/TestConnectionFactory.cs @@ -35,7 +35,6 @@ using System.Text; using NUnit.Framework; using RabbitMQ.Client.Exceptions; -using RabbitMQ.Client.Impl; namespace RabbitMQ.Client.Unit { @@ -77,8 +76,14 @@ public void TestProperties() [Test] public void TestConnectionFactoryWithCustomSocketFactory() { - const int defaultSocketBufsz = 8192; // From the docs - const int bufsz = 1024; + const int testBufsz = 1024; + int defaultReceiveBufsz = 0; + int defaultSendBufsz = 0; + using (var defaultSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) + { + defaultReceiveBufsz = defaultSocket.ReceiveBufferSize; + defaultSendBufsz = defaultSocket.SendBufferSize; + } var cf = new ConnectionFactory { @@ -86,8 +91,8 @@ public void TestConnectionFactoryWithCustomSocketFactory() { var socket = new Socket(af, SocketType.Stream, ProtocolType.Tcp) { - SendBufferSize = bufsz, - ReceiveBufferSize = bufsz, + SendBufferSize = testBufsz, + ReceiveBufferSize = testBufsz, NoDelay = false }; return new TcpClientAdapter(socket); @@ -98,8 +103,8 @@ public void TestConnectionFactoryWithCustomSocketFactory() Assert.IsAssignableFrom(typeof(TcpClientAdapter), c); TcpClientAdapter tcpClientAdapter = (TcpClientAdapter)c; Socket s = tcpClientAdapter.Client; - Assert.AreNotEqual(defaultSocketBufsz, s.ReceiveBufferSize); - Assert.AreNotEqual(defaultSocketBufsz, s.SendBufferSize); + Assert.AreNotEqual(defaultReceiveBufsz, s.ReceiveBufferSize); + Assert.AreNotEqual(defaultSendBufsz, s.SendBufferSize); Assert.False(s.NoDelay); } diff --git a/projects/Unit/TestTcpClientAdapter.cs b/projects/Unit/TestTcpClientAdapter.cs index 3adb7dcca2..0530ce2648 100644 --- a/projects/Unit/TestTcpClientAdapter.cs +++ b/projects/Unit/TestTcpClientAdapter.cs @@ -31,9 +31,7 @@ using System.Net; using System.Net.Sockets; - using NUnit.Framework; -using RabbitMQ.Client.Impl; namespace RabbitMQ.Client.Unit { @@ -44,15 +42,15 @@ public class TestTcpClientAdapter public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyDoesNotMatch() { var address = IPAddress.Parse("127.0.0.1"); - IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6); - Assert.IsNull(matchingAddress); + IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6); + Assert.Null(matchingAddress); } [Test] public void TcpClientAdapterHelperGetMatchingHostReturnsSingleAddressIfFamilyIsUnspecified() { var address = IPAddress.Parse("1.1.1.1"); - IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.Unspecified); + IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.Unspecified); Assert.AreEqual(address, matchingAddress); } @@ -61,8 +59,8 @@ public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyIsUnspec { var address = IPAddress.Parse("1.1.1.1"); var address2 = IPAddress.Parse("2.2.2.2"); - IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified); - Assert.IsNull(matchingAddress); + IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified); + Assert.Null(matchingAddress); } } }