Skip to content

Commit e09615d

Browse files
committed
Wrap standard .net tcp client with interface so that ConnectionFactoryBase.DefaultSocketFactory can be overriden to return an extended tcp client.
1 parent bb7a835 commit e09615d

File tree

7 files changed

+129
-8
lines changed

7 files changed

+129
-8
lines changed

projects/client/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<Compile Include="src\client\api\IQueueingBasicConsumer.cs" />
147147
<Compile Include="src\client\api\IRecoverable.cs" />
148148
<Compile Include="src\client\api\IStreamProperties.cs" />
149+
<Compile Include="src\client\api\ITcpClient.cs" />
149150
<Compile Include="src\client\api\NetworkConnection.cs" />
150151
<Compile Include="src\client\api\PlainMechanism.cs" />
151152
<Compile Include="src\client\api\PlainMechanismFactory.cs" />
@@ -252,6 +253,7 @@
252253
<Compile Include="src\client\impl\SoftProtocolException.cs" />
253254
<Compile Include="src\client\impl\StreamProperties.cs" />
254255
<Compile Include="src\client\impl\SyntaxError.cs" />
256+
<Compile Include="src\client\impl\TcpClientAdapter.cs" />
255257
<Compile Include="src\client\impl\UnexpectedFrameException.cs" />
256258
<Compile Include="src\client\impl\UnknownClassOrMethodException.cs" />
257259
<Compile Include="src\client\impl\WireFormatting.cs" />

projects/client/RabbitMQ.Client/src/client/api/ConnectionFactoryBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ConnectionFactoryBase
5656
#if NETFX_CORE
5757
public Func<StreamSocket> SocketFactory = DefaultSocketFactory;
5858
#else
59-
public Func<AddressFamily, TcpClient> SocketFactory = DefaultSocketFactory;
59+
public Func<AddressFamily, ITcpClient> SocketFactory = DefaultSocketFactory;
6060
#endif
6161

6262
/// <summary>
@@ -72,13 +72,13 @@ public static StreamSocket DefaultSocketFactory()
7272
return tcpClient;
7373
}
7474
#else
75-
public static TcpClient DefaultSocketFactory(AddressFamily addressFamily)
75+
public static ITcpClient DefaultSocketFactory(AddressFamily addressFamily)
7676
{
7777
var tcpClient = new TcpClient(addressFamily)
7878
{
7979
NoDelay = true
8080
};
81-
return tcpClient;
81+
return new TcpClientAdapter(tcpClient);
8282
}
8383
#endif
8484
}

projects/client/RabbitMQ.Client/src/client/api/IProtocol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public interface IProtocol
104104
IFrameHandler CreateFrameHandler(
105105
AmqpTcpEndpoint endpoint,
106106
#if !NETFX_CORE
107-
Func<AddressFamily, TcpClient> socketFactory,
107+
Func<AddressFamily, ITcpClient> socketFactory,
108108
#else
109109
Func<StreamSocket> socketFactory,
110110
#endif
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Net.Sockets;
3+
4+
namespace RabbitMQ.Client
5+
{
6+
/// <summary>
7+
/// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class.
8+
/// </summary>
9+
/// <remarks>Contains all methods that are currenty in use in rabbitmq client.</remarks>
10+
public interface ITcpClient
11+
{
12+
bool Connected { get; }
13+
14+
int ReceiveTimeout { get; set; }
15+
16+
Socket Client { get; set; }
17+
18+
19+
IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state);
20+
21+
void EndConnect(IAsyncResult asyncResult);
22+
23+
NetworkStream GetStream();
24+
25+
void Close();
26+
27+
}
28+
}

projects/client/RabbitMQ.Client/src/client/impl/ProtocolBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public IConnection CreateConnection(ConnectionFactory factory,
147147
public IFrameHandler CreateFrameHandler(
148148
AmqpTcpEndpoint endpoint,
149149
#if !NETFX_CORE
150-
Func<AddressFamily, TcpClient> socketFactory,
150+
Func<AddressFamily, ITcpClient> socketFactory,
151151
#else
152152
Func<StreamSocket> socketFactory,
153153
#endif

projects/client/RabbitMQ.Client/src/client/impl/SocketFrameHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public class SocketFrameHandler : IFrameHandler
5959
protected int m_writeableStateTimeout = 30000;
6060

6161
public NetworkBinaryReader m_reader;
62-
public TcpClient m_socket;
62+
public ITcpClient m_socket;
6363
public NetworkBinaryWriter m_writer;
6464
private readonly object _semaphore = new object();
6565
private bool _closed;
6666

6767
public SocketFrameHandler(AmqpTcpEndpoint endpoint,
68-
Func<AddressFamily, TcpClient> socketFactory,
68+
Func<AddressFamily, ITcpClient> socketFactory,
6969
int timeout)
7070
{
7171
Endpoint = endpoint;
@@ -250,7 +250,7 @@ public void Flush()
250250
}
251251
}
252252

253-
private void Connect(TcpClient socket, AmqpTcpEndpoint endpoint, int timeout)
253+
private void Connect(ITcpClient socket, AmqpTcpEndpoint endpoint, int timeout)
254254
{
255255
IAsyncResult ar = null;
256256
try
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net.Sockets;
5+
using System.Text;
6+
7+
namespace RabbitMQ.Client
8+
{
9+
10+
11+
/// <summary>
12+
/// Simple wrapper around TcpClient.
13+
/// </summary>
14+
public class TcpClientAdapter : ITcpClient
15+
{
16+
protected TcpClient _tcpClient;
17+
18+
19+
public TcpClientAdapter(TcpClient tcpClient)
20+
{
21+
_tcpClient = tcpClient;
22+
}
23+
24+
public virtual IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state)
25+
{
26+
assertTcpClient();
27+
28+
return _tcpClient.BeginConnect(host, port, requestCallback, state);
29+
}
30+
31+
private void assertTcpClient()
32+
{
33+
if (_tcpClient == null)
34+
throw new InvalidOperationException("Field tcpClient is null. Should have been passed to constructor.");
35+
}
36+
37+
public virtual void EndConnect(IAsyncResult asyncResult)
38+
{
39+
assertTcpClient();
40+
41+
_tcpClient.EndConnect(asyncResult);
42+
}
43+
44+
public virtual void Close()
45+
{
46+
assertTcpClient();
47+
48+
_tcpClient.Close();
49+
}
50+
51+
public virtual NetworkStream GetStream()
52+
{
53+
assertTcpClient();
54+
55+
return _tcpClient.GetStream();
56+
}
57+
58+
public virtual Socket Client
59+
{
60+
get
61+
{
62+
assertTcpClient();
63+
64+
return _tcpClient.Client;
65+
}
66+
set
67+
{
68+
_tcpClient.Client = value;
69+
}
70+
}
71+
72+
public virtual bool Connected
73+
{
74+
get { return _tcpClient!=null && _tcpClient.Connected; }
75+
}
76+
77+
public virtual int ReceiveTimeout
78+
{
79+
get
80+
{
81+
return _tcpClient.ReceiveTimeout;
82+
}
83+
set
84+
{
85+
_tcpClient.ReceiveTimeout = value;
86+
}
87+
}
88+
89+
}
90+
91+
}

0 commit comments

Comments
 (0)