From d689fed141c534664f942a5b973a2212813070fa Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Tue, 31 Oct 2023 15:21:47 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Use=20dynamic=20TCP=20ports=20+=20p?= =?UTF-8?q?revent=20user=20to=20choose=20TCP=20client=20port?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tests/Kernels/NetworkTest/Kernel.cs | 2 +- source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Tests/Kernels/NetworkTest/Kernel.cs b/Tests/Kernels/NetworkTest/Kernel.cs index 0e8221c29e..22bd4c8502 100644 --- a/Tests/Kernels/NetworkTest/Kernel.cs +++ b/Tests/Kernels/NetworkTest/Kernel.cs @@ -154,7 +154,7 @@ private void TestTcpConnection() { Global.debugger.Send("Creating TCP client..."); - using (var xClient = new TcpClient(4242)) + using (var xClient = new TcpClient()) { Assert.IsTrue(xClient.IsClosed(), "TCP connexion created."); diff --git a/source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs b/source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs index 8cd58c2541..87a990b9c8 100644 --- a/source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs +++ b/source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs @@ -15,6 +15,15 @@ public class TcpClient : IDisposable /// public Tcp StateMachine; + /// + /// Initializes a new instance of the class. + /// + /// Thrown on fatal error. + /// Thrown if TcpClient with localPort 0 exists. + public TcpClient() : this(Tcp.GetDynamicPort()) + { + } + /// /// Initializes a new instance of the class. /// @@ -32,7 +41,7 @@ internal TcpClient(Tcp stateMachine) /// The local port. /// Thrown on fatal error. /// Thrown if localPort already exists. - public TcpClient(int localPort) + internal TcpClient(int localPort) { StateMachine = new((ushort)localPort, 0, Address.Zero, Address.Zero); StateMachine.RxBuffer = new Queue(8); @@ -47,7 +56,7 @@ public TcpClient(int localPort) /// Thrown on fatal error. /// Thrown if TcpClient with localPort 0 exists. public TcpClient(Address dest, int destPort) - : this(0) + : this(Tcp.GetDynamicPort()) { StateMachine.RemoteEndPoint.Address = dest; StateMachine.RemoteEndPoint.Port = (ushort)destPort;