Skip to content

Commit 21ac6d9

Browse files
Merge pull request #265 from rabbitmq/rabbitmq-dotnet-client-263
Null socket field in TcpClientAdapter on close
2 parents 751fc8a + 052ad74 commit 21ac6d9

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public TcpClientAdapter(Socket socket)
2424

2525
public virtual async Task ConnectAsync(string host, int port)
2626
{
27+
AssertSocket();
2728
var adds = await Dns.GetHostAddressesAsync(host).ConfigureAwait(false);
2829
var ep = adds.FirstOrDefault(a => a.AddressFamily == sock.AddressFamily);
2930
if(ep == default(IPAddress))
@@ -40,11 +41,14 @@ public virtual async Task ConnectAsync(string host, int port)
4041

4142
public virtual void Close()
4243
{
43-
sock.Dispose();
44+
if(sock != null)
45+
sock.Dispose();
46+
sock = null;
4447
}
4548

4649
public virtual NetworkStream GetStream()
4750
{
51+
AssertSocket();
4852
return new NetworkStream(sock);
4953
}
5054

@@ -58,20 +62,34 @@ public virtual Socket Client
5862

5963
public virtual bool Connected
6064
{
61-
get { return sock.Connected; }
65+
get
66+
{
67+
if(sock == null) return false;
68+
return sock.Connected;
69+
}
6270
}
6371

6472
public virtual int ReceiveTimeout
6573
{
6674
get
6775
{
76+
AssertSocket();
6877
return sock.ReceiveTimeout;
6978
}
7079
set
7180
{
81+
AssertSocket();
7282
sock.ReceiveTimeout = value;
7383
}
7484
}
85+
86+
private void AssertSocket()
87+
{
88+
if(sock == null)
89+
{
90+
throw new InvalidOperationException("Cannot perform operation as socket is null");
91+
}
92+
}
7593
}
7694
}
7795
#endif

0 commit comments

Comments
 (0)