Tons of minor fixes
This commit is contained in:
1 parent
873a6fa455
commit
37a6b5d59b
12 files changed
+219
-147
No files matched your search
@@ -2,35 +2,51 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading.Tasks;
|
||||
using Mistox.ResevedPackets;
|
||||
|
||||
namespace Mistox.Client {
|
||||
|
||||
public class MistoxClient {
|
||||
|
||||
public string ServerName { get; set; } = ""; // The Name of the server
|
||||
public string ServerName { get; private set; } = ""; // The Name of the server
|
||||
public string UserName { get; private set; } = ""; // My UserName
|
||||
public bool Alive { get; set; } = true; // Is the server running
|
||||
public mTCPClient ServerConnection;
|
||||
public UdpClient ClientSocket;
|
||||
public UDPReceiver UdpReceiver;
|
||||
public ConcurrentDictionary<UInt128, mUDPClient> clientConnections; // IP Address, Client
|
||||
public bool isAuthenticated { get; set; } = false; // Is the server authenticated
|
||||
public ConcurrentDictionary<IPEndPoint, mUDPClient> clientConnections; // UserName, Connection
|
||||
|
||||
public mTCPClient ServerConnection {get; private set; } // Global TCP Server Connection
|
||||
public UdpClient ClientSocket { get; private set; } // Global UDP Sender
|
||||
public UDPReceiver UdpReceiver { get; private set; } // Global UDP Receiver
|
||||
private readonly string ConnectionKey = ""; // Server Connection Key
|
||||
|
||||
public MistoxClient(IPEndPoint Server) {
|
||||
public MistoxClient(string userName, string connectionKey, IPEndPoint Server) {
|
||||
// Create and listen on UDP
|
||||
ClientSocket = new UdpClient( Server.Port );
|
||||
UdpReceiver = new UDPReceiver( this );
|
||||
UserName = userName;
|
||||
ConnectionKey = connectionKey;
|
||||
|
||||
// Create and connect to server on TCP
|
||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + Server.Address );
|
||||
try {
|
||||
ServerConnection = new mTCPClient( this, Server );
|
||||
Console.WriteLine( "Connected to the server sucessfully" );
|
||||
Alive = true;
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
||||
Console.WriteLine( e.ToString() );
|
||||
Console.WriteLine( "}" );
|
||||
Alive = false;
|
||||
}
|
||||
Alive = true;
|
||||
}
|
||||
|
||||
public async Task TryAuth() {
|
||||
Console.WriteLine("Attempting to authenticate against the server...");
|
||||
await ServerConnection.Send(new UserAuthPacket {
|
||||
UserName = UserName,
|
||||
ConnectionKey = ConnectionKey
|
||||
});
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
@@ -38,27 +54,33 @@ namespace Mistox.Client {
|
||||
/////////////////////////////
|
||||
|
||||
public void UserConnected(UserConnectedPacket packet) {
|
||||
mUDPClient user = new mUDPClient(this, packet.ClientIPAddress, packet.ClientPort) {
|
||||
IPEndPoint remoteUser = new IPEndPoint(packet.ClientIPAddress, packet.ClientPort);
|
||||
mUDPClient user = new mUDPClient(this, remoteUser) {
|
||||
UserName = packet.UserName
|
||||
};
|
||||
clientConnections[user.GetConnectionKey()] = user;
|
||||
clientConnections[remoteUser] = user;
|
||||
InvokeOnUserConnected(user);
|
||||
}
|
||||
|
||||
public void UserDisconnected(UserDisconnectedPacket packet) {
|
||||
mUDPClient client;
|
||||
bool found = clientConnections.TryGetValue(packet.ConnectionKey, out client);
|
||||
if (found) {
|
||||
mUDPClient client = null;
|
||||
foreach(mUDPClient cur in clientConnections.Values) {
|
||||
if (cur.UserName.ToLower() == packet.UserName.ToLower()) {
|
||||
client = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (client != null) {
|
||||
InvokeOnUserDisconnected( client );
|
||||
client.Alive = false;
|
||||
client.Dispose();
|
||||
clientConnections[packet.ConnectionKey] = null;
|
||||
clientConnections[client._remoteHost] = null;
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Events
|
||||
/////////////////////////////
|
||||
/////////////////////////////
|
||||
// Events
|
||||
/////////////////////////////
|
||||
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public void InvokeOnReceived(dynamic DataPacket, mUDPClient remote = null){
|
||||
@@ -81,6 +103,11 @@ namespace Mistox.Client {
|
||||
public event Action onServerConnected;
|
||||
public void InvokeOnServerConnected(){
|
||||
Alive = true;
|
||||
Task.Run(async () => {
|
||||
try {
|
||||
await TryAuth();
|
||||
} catch {}
|
||||
});
|
||||
onServerConnected?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user