From 4465eb6fe36b9771b42510eea0d0efc262933ef2 Mon Sep 17 00:00:00 2001 From: valentinbreiz Date: Tue, 5 Dec 2023 10:44:01 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20TCPClient=20works!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../System/Net/IPAddressImpl.cs | 23 ++++++++--------- .../System/Net/Sockets/SocketImpl.cs | 25 +++++-------------- .../System/Net/Sockets/TcpClientImpl.cs | 3 +-- 3 files changed, 17 insertions(+), 34 deletions(-) diff --git a/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs b/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs index 7903553ec0..3c451ee61c 100644 --- a/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs @@ -16,16 +16,21 @@ public static class IPAddressImpl private const int IPv6AddressBytes = 16; private static uint PrivateAddress; - public static uint get_PrivateAddress() + public static uint get_PrivateAddress(IPAddress aThis) { return PrivateAddress; } - public static void set_PrivateAddress(uint address) + public static void set_PrivateAddress(IPAddress aThis, uint address) { PrivateAddress = address; } + public static AddressFamily get_AddressFamily(IPAddress aThis) + { + return AddressFamily.InterNetwork; + } + public static void CCtor(IPAddress aThis) { } @@ -47,11 +52,11 @@ public static void Ctor(IPAddress aThis, ReadOnlySpan address, long scopeI public static void Ctor(IPAddress aThis, byte[] address) { - Cosmos.HAL.Global.debugger.Send(address[0] + "." + address[1] + "." + address[2] + "." + address[3]); - if (address.Length == IPv4AddressBytes) { - PrivateAddress = (uint)((address[0] << 24) | (address[1] << 16) | (address[2] << 8) | (address[3] << 0)); + PrivateAddress = (uint)((address[0] << 0) | (address[1] << 8) | (address[2] << 16) | (address[3] << 24)); + + Cosmos.HAL.Global.debugger.Send("Ctor address=" + aThis.ToString()); } else if (address.Length == IPv6AddressBytes) { @@ -77,9 +82,6 @@ public static IPAddress Parse(string address) addressArray[2] = byte.Parse(fragments[2]); addressArray[3] = byte.Parse(fragments[3]); - Cosmos.HAL.Global.debugger.Send(fragments[0] + "." + fragments[1] + "." + fragments[2] + "." + fragments[3]); - Cosmos.HAL.Global.debugger.Send(addressArray[0] + "." + addressArray[1] + "." + addressArray[2] + "." + addressArray[3]); - return new IPAddress(addressArray); } catch @@ -92,10 +94,5 @@ public static IPAddress Parse(string address) return null; } } - - public static string ToString(IPAddress aThis) - { - return (byte)(PrivateAddress >> 24) + "." + (byte)(PrivateAddress >> 16 & 0xff) + "." + (byte)(PrivateAddress >> 8 & 0xff) + "." + (byte)(PrivateAddress & 0xff); - } } } diff --git a/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs b/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs index 18a18194a7..e3fb0a0562 100644 --- a/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs @@ -17,8 +17,8 @@ public static class SocketImpl private static Tcp StateMachine; private static IPEndPoint EndPoint = null; - private static EndPoint _localEndPoint; - private static EndPoint _remoteEndPoint; + private static IPEndPoint _localEndPoint; + private static IPEndPoint _remoteEndPoint; private const int MinPort = 49152; private const int MaxPort = 65535; @@ -125,12 +125,9 @@ public static void Connect(Socket aThis, IPAddress address, int port) { Start(); - Cosmos.HAL.Global.debugger.Send("Connect - start okay"); - Cosmos.HAL.Global.debugger.Send("address - " + address.ToString()); - Cosmos.HAL.Global.debugger.Send("port - " + port); - if (StateMachine.Status == Status.ESTABLISHED) { + Cosmos.HAL.Global.debugger.Send("Socket - Client must be closed before setting a new connection.."); throw new Exception("Client must be closed before setting a new connection."); } @@ -139,20 +136,12 @@ public static void Connect(Socket aThis, IPAddress address, int port) StateMachine.LocalEndPoint.Address = NetworkConfiguration.CurrentAddress; StateMachine.LocalEndPoint.Port = (ushort)GetRandomPort(); - Cosmos.HAL.Global.debugger.Send("StateMachine.RemoteEndPoint.Address=" + StateMachine.RemoteEndPoint.Address.ToString()); - Cosmos.HAL.Global.debugger.Send("StateMachine.LocalEndPoint.Address=" + StateMachine.LocalEndPoint.Address.ToString()); - _remoteEndPoint = new IPEndPoint(address, StateMachine.RemoteEndPoint.Port); _localEndPoint = new IPEndPoint(StateMachine.LocalEndPoint.Address.ToUInt32(), StateMachine.LocalEndPoint.Port); - Cosmos.HAL.Global.debugger.Send("_remoteEndPoint=" + _remoteEndPoint.ToString()); - Cosmos.HAL.Global.debugger.Send("_localEndPoint=" + _localEndPoint.ToString()); - - Cosmos.HAL.Global.debugger.Send("Connect - endpoints okay."); - //Generate Random Sequence Number - var rnd = new Random(); - var SequenceNumber = (uint)(rnd.Next(0, Int32.MaxValue) << 32) | (uint)rnd.Next(0, Int32.MaxValue); + Random rnd = new(); + var SequenceNumber = (uint)(rnd.Next(0, int.MaxValue) << 32) | (uint)rnd.Next(0, int.MaxValue); //Fill TCB StateMachine.TCB.SndUna = SequenceNumber; @@ -169,12 +158,10 @@ public static void Connect(Socket aThis, IPAddress address, int port) StateMachine.TCB.IRS = 0; Tcp.Connections.Add(StateMachine); - StateMachine.SendEmptyPacket(Flags.SYN); - StateMachine.Status = Status.SYN_SENT; - if (StateMachine.WaitStatus(Status.ESTABLISHED, 5) == false) + if (StateMachine.WaitStatus(Status.ESTABLISHED, 5000) == false) { throw new Exception("Failed to open TCP connection!"); } diff --git a/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs b/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs index 1a37f9c9b6..cfb2dbb115 100644 --- a/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs +++ b/source/Cosmos.System2_Plugs/System/Net/Sockets/TcpClientImpl.cs @@ -36,8 +36,7 @@ public static Socket get_Client(TcpClient aThis) public static int get_ReceiveBufferSize(TcpClient aThis) { - //TODO implement Socket.SetSocketOption Socket.GetSocketOption - return 8192; + return Cosmos.System.Network.IPv4.TCP.Tcp.TcpWindowSize; } public static void Connect(TcpClient aThis, string hostname, int port, [FieldAccess(Name = "System.Boolean System.Net.Sockets.TcpClient._active")] ref bool _active)