Skip to content

Commit

Permalink
✨ Use dynamic TCP ports + prevent user to choose TCP client port
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Oct 31, 2023
1 parent 7ef43d1 commit d689fed
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Tests/Kernels/NetworkTest/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down
13 changes: 11 additions & 2 deletions source/Cosmos.System2/Network/IPv4/TCP/TCPClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public class TcpClient : IDisposable
/// </summary>
public Tcp StateMachine;

/// <summary>
/// Initializes a new instance of the <see cref="TcpClient"/> class.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error.</exception>
/// <exception cref="ArgumentException">Thrown if TcpClient with localPort 0 exists.</exception>
public TcpClient() : this(Tcp.GetDynamicPort())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TcpClient"/> class.
/// </summary>
Expand All @@ -32,7 +41,7 @@ internal TcpClient(Tcp stateMachine)
/// <param name="localPort">The local port.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error.</exception>
/// <exception cref="ArgumentException">Thrown if localPort already exists.</exception>
public TcpClient(int localPort)
internal TcpClient(int localPort)
{
StateMachine = new((ushort)localPort, 0, Address.Zero, Address.Zero);
StateMachine.RxBuffer = new Queue<TCPPacket>(8);
Expand All @@ -47,7 +56,7 @@ public TcpClient(int localPort)
/// <exception cref="ArgumentOutOfRangeException">Thrown on fatal error.</exception>
/// <exception cref="ArgumentException">Thrown if TcpClient with localPort 0 exists.</exception>
public TcpClient(Address dest, int destPort)
: this(0)
: this(Tcp.GetDynamicPort())
{
StateMachine.RemoteEndPoint.Address = dest;
StateMachine.RemoteEndPoint.Port = (ushort)destPort;
Expand Down

0 comments on commit d689fed

Please sign in to comment.