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" );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user