Skip to content

Commit

Permalink
TcpListener should implement IDisposable (#88043)
Browse files Browse the repository at this point in the history
* TcpListener should implement IDisposable

* lint

* Code review

* Update src/libraries/System.Net.Sockets/src/System/Net/Sockets/TCPListener.cs

Co-authored-by: Anton Firszov <[email protected]>

* Update TCPListener.cs [EditorBrowsable(EditorBrowsableState.Never)]

* Code review

* lint

* Code review

---------

Co-authored-by: Anton Firszov <[email protected]>
  • Loading branch information
AlexRadch and antonfirsov authored Jul 3, 2023
1 parent 5a968d5 commit 475c9ac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/libraries/System.Net.Sockets/ref/System.Net.Sockets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public void EndConnect(System.IAsyncResult asyncResult) { }
~TcpClient() { }
public System.Net.Sockets.NetworkStream GetStream() { throw null; }
}
public partial class TcpListener
public partial class TcpListener : System.IDisposable
{
[System.ObsoleteAttribute("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")]
public TcpListener(int port) { }
Expand All @@ -720,6 +720,7 @@ public void AllowNatTraversal(bool allowed) { }
public System.IAsyncResult BeginAcceptSocket(System.AsyncCallback? callback, object? state) { throw null; }
public System.IAsyncResult BeginAcceptTcpClient(System.AsyncCallback? callback, object? state) { throw null; }
public static System.Net.Sockets.TcpListener Create(int port) { throw null; }
public void Dispose() { }
public System.Net.Sockets.Socket EndAcceptSocket(System.IAsyncResult asyncResult) { throw null; }
public System.Net.Sockets.TcpClient EndAcceptTcpClient(System.IAsyncResult asyncResult) { throw null; }
public bool Pending() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace System.Net.Sockets
// The System.Net.Sockets.TcpListener class provide TCP services at a higher level of abstraction
// than the System.Net.Sockets.Socket class. System.Net.Sockets.TcpListener is used to create a
// host process that listens for connections from TCP clients.
public class TcpListener
public class TcpListener : IDisposable
{
private readonly IPEndPoint _serverSocketEP;
private Socket? _serverSocket;
Expand Down Expand Up @@ -167,6 +167,11 @@ public void Stop()
_serverSocket = null;
}

/// <summary>
/// Releases all resources used by the current <see cref="TcpListener"/> instance.
/// </summary>
public void Dispose() => Stop();

// Determine if there are pending connection requests.
public bool Pending()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public void Active_TrueWhileRunning(int ctor)
Assert.False(listener.Active);
}

[Fact]
public void IDisposable_DisposeWorksAsStop()
{
var listener = new DerivedTcpListener(IPAddress.Loopback, 0);
using (listener)
{
Assert.False(listener.Active);
listener.Start();
Assert.True(listener.Active);
}
Assert.False(listener.Active);
listener.Dispose();
Assert.False(listener.Active);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
public void AllowNatTraversal_NotStarted_SetSuccessfully()
Expand Down

0 comments on commit 475c9ac

Please sign in to comment.