diff --git a/MistoxServer/Server/MistoxServer.cs b/MistoxServer/Server/MistoxServer.cs index 18620bc..46aca6d 100644 --- a/MistoxServer/Server/MistoxServer.cs +++ b/MistoxServer/Server/MistoxServer.cs @@ -21,18 +21,24 @@ namespace Mistox.Server { private readonly ServerListener Listener; // The Connection Listener singleton public MistoxServer(IPEndPoint ListenerAddress, string serverName) { - Listener = new ServerListener(ListenerAddress, this); Connections = new ConcurrentDictionary(); ServerName = serverName; + Listener = new ServerListener(ListenerAddress, this); Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + ListenerAddress.Port ); } - async Task ProcessAuthRequest(UserAuthPacket packet) { - // Need to set the connection userName via this packet - string user = packet.UserName; + async Task ProcessAuthRequest(ServerConnection user, UserAuthPacket packet) { packet.ServerName = ServerName; - packet.AuthSuccessful = packet.ConnectionKey == ConnectionKey ? 1 : 0; + if (packet.ConnectionKey == ConnectionKey) { + packet.AuthSuccessful = 1; + Connections.TryRemove(user.UserName, out _); + user.UserName = packet.UserName; + Connections[packet.UserName] = user; + user.IsAuthenticated = true; + } else { + packet.AuthSuccessful = -1; + } await SendClient(packet.UserName, packet); } @@ -44,12 +50,12 @@ namespace Mistox.Server { } public event Action onDataReceived; - public void InvokeDataReceived( dynamic data ){ + public void InvokeDataReceived( ServerConnection user, dynamic data ){ switch (data as object) { case UserAuthPacket packet: Task.Run(async () => { try { - await ProcessAuthRequest(packet); + await ProcessAuthRequest(user, packet); } catch {} }); break; diff --git a/MistoxServer/Server/ServerConnection.cs b/MistoxServer/Server/ServerConnection.cs index 1d2c380..38bd741 100644 --- a/MistoxServer/Server/ServerConnection.cs +++ b/MistoxServer/Server/ServerConnection.cs @@ -59,7 +59,7 @@ namespace Mistox.Server { dynamic dataobject = DataPacket.Deserialize( Header, payloadBuffer.AsMemory(DataPacket.HeaderLength) ); // Return the object - _parent.InvokeDataReceived(dataobject); + _parent.InvokeDataReceived(this, dataobject); } } catch (Exception e) { diff --git a/MistoxServer/Server/ServerListener.cs b/MistoxServer/Server/ServerListener.cs index 0327466..2f02329 100644 --- a/MistoxServer/Server/ServerListener.cs +++ b/MistoxServer/Server/ServerListener.cs @@ -32,9 +32,11 @@ namespace Mistox.Server { } IPEndPoint remote = client.Client.RemoteEndPoint as IPEndPoint; - string connectionID = Guid.NewGuid().ToString(); - _parent.Connections[connectionID] = new ServerConnection( client, _parent, remote ); + string connectionID = Guid.NewGuid().ToString(); + _parent.Connections[connectionID] = new ServerConnection( client, _parent, remote) { + UserName = connectionID + }; Console.WriteLine( "New User Connected" ); } diff --git a/MistoxServer/mPacket/ResevedPackets.cs b/MistoxServer/mPacket/ResevedPackets.cs index 2465f01..bc936a6 100644 --- a/MistoxServer/mPacket/ResevedPackets.cs +++ b/MistoxServer/mPacket/ResevedPackets.cs @@ -4,13 +4,14 @@ using Mistox.Attributes; namespace Mistox.ResevedPackets { + // Packet sent to clients when they are in range telling them to connect together [mPacket] public class UserConnectedPacket { public string UserName { get; set; } public IPAddress ClientIPAddress { get; set; } - public int ClientPort { get; set; } } + // Packet send to server for Auth and responded to the client with Auth Success [mPacket] public class UserAuthPacket { // Sent to the server @@ -21,6 +22,7 @@ namespace Mistox.ResevedPackets { public int AuthSuccessful { get; set; } = 0; // 0 is standard, -1 is auth failure, 1 is auth success } + // Generic networked object packet [mPacket] public class EntityPacket { @@ -32,6 +34,7 @@ namespace Mistox.ResevedPackets { public byte[] InnerPayload { get; set; } } + // Packet sent to all clients when a users TCP stream disconnects from the server [mPacket] public class UserDisconnectedPacket { public string UserName { get; set; } diff --git a/Todo.txt b/Todo.txt index 0095cf8..e40a64e 100644 --- a/Todo.txt +++ b/Todo.txt @@ -1,9 +1,10 @@ The server receive thread needs to push the user it was received from up the chain -1. Allow the server to setup a server name -2. Allow the client to setup a user name -3. Have the client connect to the server with a welcome packet -4. Have the server use the welcome packet to fillout the user information on the server +1. Allow the server to setup a server name Done +2. Allow the client to setup a user name Done +3. When a user connects add them to the Connections dictionary with a GUID. Done +3. Have the client send the server a welcome packet Done +4. Have the server use the welcome packet to fillout the user information on the server Done 5. Respond to the client from the server with a server list of UDPEndpoints 6. Instead of looping through a list of avalible users have a local user dictionary that can be used to lookup clients IPEndPoints