Skip to content

Commit

Permalink
Merge pull request #1418 from rabbitmq/rabbitmq-dotnet-client-1414-14…
Browse files Browse the repository at this point in the history
…15-followup-6.x

Make TcpClientAdapter public
  • Loading branch information
lukebakken committed Nov 16, 2023
2 parents d39f36b + 032eed7 commit e3c2483
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 44 deletions.
3 changes: 2 additions & 1 deletion projects/RabbitMQ.Client/client/api/ITcpClient.cs
Expand Up @@ -5,7 +5,8 @@
namespace RabbitMQ.Client
{
/// <summary>
/// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class.
/// Wrapper interface for <see cref="Socket"/>.
/// Provides the socket for socket frame handler class.
/// </summary>
/// <remarks>Contains all methods that are currenty in use in rabbitmq client.</remarks>
public interface ITcpClient : IDisposable
Expand Down
@@ -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
{
/// <summary>
/// Simple wrapper around TcpClient.
/// Simple wrapper around <see cref="Socket"/>.
/// </summary>
class TcpClientAdapter : ITcpClient
public class TcpClientAdapter : ITcpClient
{
private Socket _sock;

Expand All @@ -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}");
Expand All @@ -43,8 +45,7 @@ public virtual void Close()
_sock = null;
}

[Obsolete("Override Dispose(bool) instead.")]
public virtual void Dispose()
public void Dispose()
{
Dispose(true);
}
Expand All @@ -53,11 +54,8 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// dispose managed resources
Close();
}

// dispose unmanaged resources
}

public virtual NetworkStream GetStream()
Expand Down Expand Up @@ -104,5 +102,15 @@ private void AssertSocket()
throw new InvalidOperationException("Cannot perform operation as socket is null");
}
}

public static IPAddress GetMatchingHost(IReadOnlyCollection<IPAddress> 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;
}
}
}
20 changes: 0 additions & 20 deletions projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs

This file was deleted.

13 changes: 13 additions & 0 deletions projects/Unit/APIApproval.Approve.verified.txt
Expand Up @@ -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<System.Net.IPAddress> addresses, System.Net.Sockets.AddressFamily addressFamily) { }
}
public class TimerBasedCredentialRefresher : RabbitMQ.Client.ICredentialsRefresher
{
public TimerBasedCredentialRefresher() { }
Expand Down
19 changes: 12 additions & 7 deletions projects/Unit/TestConnectionFactory.cs
Expand Up @@ -35,7 +35,6 @@
using System.Text;
using NUnit.Framework;
using RabbitMQ.Client.Exceptions;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.Unit
{
Expand Down Expand Up @@ -77,17 +76,23 @@ 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
{
SocketFactory = (AddressFamily af) =>
{
var socket = new Socket(af, SocketType.Stream, ProtocolType.Tcp)
{
SendBufferSize = bufsz,
ReceiveBufferSize = bufsz,
SendBufferSize = testBufsz,
ReceiveBufferSize = testBufsz,
NoDelay = false
};
return new TcpClientAdapter(socket);
Expand All @@ -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);
}

Expand Down
12 changes: 5 additions & 7 deletions projects/Unit/TestTcpClientAdapter.cs
Expand Up @@ -31,9 +31,7 @@

using System.Net;
using System.Net.Sockets;

using NUnit.Framework;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.Unit
{
Expand All @@ -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);
}

Expand All @@ -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);
}
}
}

0 comments on commit e3c2483

Please sign in to comment.