Set all the required information on server when auth.
This commit is contained in:
1 parent
d446464f2a
commit
007f87f986
5 files changed
+27
-15
No files matched your search
@@ -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<string, ServerConnection>();
|
||||
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<dynamic> 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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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" );
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user