Rework client to be non static
This commit is contained in:
1 parent
6f910e969a
commit
b439805f3e
7 files changed
+107
-108
No files matched your search
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
@@ -8,28 +7,16 @@ using Mistox.Client;
|
||||
namespace Mistox {
|
||||
public class ClientInterface {
|
||||
|
||||
public static MistoxServerClient ServerClientInstance;
|
||||
public static mTCPClient serverConnection;
|
||||
public static ConcurrentDictionary<string, UDPEndpoint> clientConnections;
|
||||
public static MistoxClient serverInstance;
|
||||
|
||||
public event Action onServerConnected;
|
||||
public event Action<UDPEndpoint> onUserConnected;
|
||||
public event Action<mUDPClient> onUserConnected;
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public event Action<UDPEndpoint> onUserDisconnected;
|
||||
public event Action<mUDPClient> onUserDisconnected;
|
||||
public event Action onServerDisconnected;
|
||||
|
||||
public ClientInterface( string IpOrHostName, int Port ) {
|
||||
|
||||
// Wire up the Mistox server client
|
||||
ServerClientInstance = new MistoxServerClient() {
|
||||
Port = Port
|
||||
};
|
||||
ServerClientInstance.onServerConnected += onServerConnected;
|
||||
ServerClientInstance.onUserConnected += (UDPEndpoint endpoint) => { onUserConnected?.Invoke(endpoint); };
|
||||
ServerClientInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
ServerClientInstance.onUserDisconnected += (UDPEndpoint endpoint) => { onUserDisconnected?.Invoke(endpoint); };
|
||||
ServerClientInstance.onServerDisconnected += onServerDisconnected;
|
||||
|
||||
// Get Server IP
|
||||
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
|
||||
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
||||
@@ -38,30 +25,28 @@ namespace Mistox {
|
||||
ServerDenstination = new IPEndPoint( entry, Port );
|
||||
}
|
||||
}
|
||||
Console.WriteLine( "The client is initilized and trying to connect to the server at ip : " + ServerDenstination.Address );
|
||||
|
||||
// Make a TCP connection to the server
|
||||
try {
|
||||
serverConnection = new mTCPClient( ServerDenstination );
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
||||
Console.WriteLine( e.ToString() );
|
||||
Console.WriteLine( "}" );
|
||||
}
|
||||
|
||||
// Start and wire up the Mistox server client
|
||||
serverInstance = new MistoxClient( ServerDenstination );
|
||||
serverInstance.onServerConnected += onServerConnected;
|
||||
serverInstance.onUserConnected += (mUDPClient endpoint) => { onUserConnected?.Invoke(endpoint); };
|
||||
serverInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
serverInstance.onUserDisconnected += (mUDPClient endpoint) => { onUserDisconnected?.Invoke(endpoint); };
|
||||
serverInstance.onServerDisconnected += onServerDisconnected;
|
||||
}
|
||||
|
||||
public async Task SendAllClients<Packet>(Packet data) {
|
||||
foreach(KeyValuePair<string, UDPEndpoint> cur in clientConnections){
|
||||
await cur.Value.Connection.Send( data );
|
||||
foreach(KeyValuePair<string, mUDPClient> cur in serverInstance.clientConnections){
|
||||
await cur.Value.Send( data );
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||
await clientConnections[UserName].Connection.Send( data );
|
||||
await serverInstance.clientConnections[UserName].Send( data );
|
||||
}
|
||||
|
||||
public async Task SendServer<Packet>(Packet data) {
|
||||
await serverConnection.Send( data );
|
||||
await serverInstance.ServerConnection.Send( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user