Skip to content

Commit e3c2483

Browse files
authored
Merge pull request #1418 from rabbitmq/rabbitmq-dotnet-client-1414-1415-followup-6.x
Make TcpClientAdapter public
2 parents d39f36b + 032eed7 commit e3c2483

File tree

6 files changed

+49
-44
lines changed

6 files changed

+49
-44
lines changed

projects/RabbitMQ.Client/client/api/ITcpClient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace RabbitMQ.Client
66
{
77
/// <summary>
8-
/// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class.
8+
/// Wrapper interface for <see cref="Socket"/>.
9+
/// Provides the socket for socket frame handler class.
910
/// </summary>
1011
/// <remarks>Contains all methods that are currenty in use in rabbitmq client.</remarks>
1112
public interface ITcpClient : IDisposable

projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs renamed to projects/RabbitMQ.Client/client/api/TcpClientAdapter.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Net;
35
using System.Net.Sockets;
46
using System.Threading.Tasks;
57

6-
namespace RabbitMQ.Client.Impl
8+
namespace RabbitMQ.Client
79
{
810
/// <summary>
9-
/// Simple wrapper around TcpClient.
11+
/// Simple wrapper around <see cref="Socket"/>.
1012
/// </summary>
11-
class TcpClientAdapter : ITcpClient
13+
public class TcpClientAdapter : ITcpClient
1214
{
1315
private Socket _sock;
1416

@@ -21,7 +23,7 @@ public virtual async Task ConnectAsync(string host, int port)
2123
{
2224
AssertSocket();
2325
IPAddress[] adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
24-
IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily);
26+
IPAddress ep = GetMatchingHost(adds, _sock.AddressFamily);
2527
if (ep == default(IPAddress))
2628
{
2729
throw new ArgumentException($"No ip address could be resolved for {host}");
@@ -43,8 +45,7 @@ public virtual void Close()
4345
_sock = null;
4446
}
4547

46-
[Obsolete("Override Dispose(bool) instead.")]
47-
public virtual void Dispose()
48+
public void Dispose()
4849
{
4950
Dispose(true);
5051
}
@@ -53,11 +54,8 @@ protected virtual void Dispose(bool disposing)
5354
{
5455
if (disposing)
5556
{
56-
// dispose managed resources
5757
Close();
5858
}
59-
60-
// dispose unmanaged resources
6159
}
6260

6361
public virtual NetworkStream GetStream()
@@ -104,5 +102,15 @@ private void AssertSocket()
104102
throw new InvalidOperationException("Cannot perform operation as socket is null");
105103
}
106104
}
105+
106+
public static IPAddress GetMatchingHost(IReadOnlyCollection<IPAddress> addresses, AddressFamily addressFamily)
107+
{
108+
IPAddress ep = addresses.FirstOrDefault(a => a.AddressFamily == addressFamily);
109+
if (ep is null && addresses.Count == 1 && addressFamily == AddressFamily.Unspecified)
110+
{
111+
return addresses.Single();
112+
}
113+
return ep;
114+
}
107115
}
108116
}

projects/RabbitMQ.Client/client/impl/TcpClientAdapterHelper.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

projects/Unit/APIApproval.Approve.verified.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,19 @@ namespace RabbitMQ.Client
647647
public string ServerName { get; set; }
648648
public System.Security.Authentication.SslProtocols Version { get; set; }
649649
}
650+
public class TcpClientAdapter : RabbitMQ.Client.ITcpClient, System.IDisposable
651+
{
652+
public TcpClientAdapter(System.Net.Sockets.Socket socket) { }
653+
public virtual System.Net.Sockets.Socket Client { get; }
654+
public virtual bool Connected { get; }
655+
public virtual System.TimeSpan ReceiveTimeout { get; set; }
656+
public virtual void Close() { }
657+
public virtual System.Threading.Tasks.Task ConnectAsync(string host, int port) { }
658+
public void Dispose() { }
659+
protected virtual void Dispose(bool disposing) { }
660+
public virtual System.Net.Sockets.NetworkStream GetStream() { }
661+
public static System.Net.IPAddress GetMatchingHost(System.Collections.Generic.IReadOnlyCollection<System.Net.IPAddress> addresses, System.Net.Sockets.AddressFamily addressFamily) { }
662+
}
650663
public class TimerBasedCredentialRefresher : RabbitMQ.Client.ICredentialsRefresher
651664
{
652665
public TimerBasedCredentialRefresher() { }

projects/Unit/TestConnectionFactory.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
using System.Text;
3636
using NUnit.Framework;
3737
using RabbitMQ.Client.Exceptions;
38-
using RabbitMQ.Client.Impl;
3938

4039
namespace RabbitMQ.Client.Unit
4140
{
@@ -77,17 +76,23 @@ public void TestProperties()
7776
[Test]
7877
public void TestConnectionFactoryWithCustomSocketFactory()
7978
{
80-
const int defaultSocketBufsz = 8192; // From the docs
81-
const int bufsz = 1024;
79+
const int testBufsz = 1024;
80+
int defaultReceiveBufsz = 0;
81+
int defaultSendBufsz = 0;
82+
using (var defaultSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
83+
{
84+
defaultReceiveBufsz = defaultSocket.ReceiveBufferSize;
85+
defaultSendBufsz = defaultSocket.SendBufferSize;
86+
}
8287

8388
var cf = new ConnectionFactory
8489
{
8590
SocketFactory = (AddressFamily af) =>
8691
{
8792
var socket = new Socket(af, SocketType.Stream, ProtocolType.Tcp)
8893
{
89-
SendBufferSize = bufsz,
90-
ReceiveBufferSize = bufsz,
94+
SendBufferSize = testBufsz,
95+
ReceiveBufferSize = testBufsz,
9196
NoDelay = false
9297
};
9398
return new TcpClientAdapter(socket);
@@ -98,8 +103,8 @@ public void TestConnectionFactoryWithCustomSocketFactory()
98103
Assert.IsAssignableFrom(typeof(TcpClientAdapter), c);
99104
TcpClientAdapter tcpClientAdapter = (TcpClientAdapter)c;
100105
Socket s = tcpClientAdapter.Client;
101-
Assert.AreNotEqual(defaultSocketBufsz, s.ReceiveBufferSize);
102-
Assert.AreNotEqual(defaultSocketBufsz, s.SendBufferSize);
106+
Assert.AreNotEqual(defaultReceiveBufsz, s.ReceiveBufferSize);
107+
Assert.AreNotEqual(defaultSendBufsz, s.SendBufferSize);
103108
Assert.False(s.NoDelay);
104109
}
105110

projects/Unit/TestTcpClientAdapter.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131

3232
using System.Net;
3333
using System.Net.Sockets;
34-
3534
using NUnit.Framework;
36-
using RabbitMQ.Client.Impl;
3735

3836
namespace RabbitMQ.Client.Unit
3937
{
@@ -44,15 +42,15 @@ public class TestTcpClientAdapter
4442
public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyDoesNotMatch()
4543
{
4644
var address = IPAddress.Parse("127.0.0.1");
47-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6);
48-
Assert.IsNull(matchingAddress);
45+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.InterNetworkV6);
46+
Assert.Null(matchingAddress);
4947
}
5048

5149
[Test]
5250
public void TcpClientAdapterHelperGetMatchingHostReturnsSingleAddressIfFamilyIsUnspecified()
5351
{
5452
var address = IPAddress.Parse("1.1.1.1");
55-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address }, AddressFamily.Unspecified);
53+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address }, AddressFamily.Unspecified);
5654
Assert.AreEqual(address, matchingAddress);
5755
}
5856

@@ -61,8 +59,8 @@ public void TcpClientAdapterHelperGetMatchingHostReturnNoAddressIfFamilyIsUnspec
6159
{
6260
var address = IPAddress.Parse("1.1.1.1");
6361
var address2 = IPAddress.Parse("2.2.2.2");
64-
IPAddress matchingAddress = TcpClientAdapterHelper.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified);
65-
Assert.IsNull(matchingAddress);
62+
IPAddress matchingAddress = TcpClientAdapter.GetMatchingHost(new[] { address, address2 }, AddressFamily.Unspecified);
63+
Assert.Null(matchingAddress);
6664
}
6765
}
6866
}

0 commit comments

Comments
 (0)