Skip to content

Commit

Permalink
✨ TCPClient works!
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Dec 5, 2023
1 parent c6bb978 commit 4465eb6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 34 deletions.
23 changes: 10 additions & 13 deletions source/Cosmos.System2_Plugs/System/Net/IPAddressImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand All @@ -47,11 +52,11 @@ public static void Ctor(IPAddress aThis, ReadOnlySpan<byte> 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)
{
Expand All @@ -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
Expand All @@ -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);
}
}
}
25 changes: 6 additions & 19 deletions source/Cosmos.System2_Plugs/System/Net/Sockets/SocketImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}

Expand All @@ -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;
Expand All @@ -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!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4465eb6

Please sign in to comment.