58 lines
2.3 KiB
C#
58 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
using Mistox.Client;
|
|
|
|
namespace Mistox {
|
|
public class ClientInterface {
|
|
|
|
public static MistoxClient serverInstance;
|
|
|
|
public event Action onServerConnected;
|
|
public event Action<mUDPClient> onUserConnected;
|
|
public event Action<dynamic> onDataReceived;
|
|
public event Action<mUDPClient> onUserDisconnected;
|
|
public event Action onServerDisconnected;
|
|
|
|
public ClientInterface( string IpOrHostName, int Port ) {
|
|
|
|
// Get Server IP
|
|
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
|
|
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
|
foreach( IPAddress entry in host.AddressList ) {
|
|
if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) {
|
|
ServerDenstination = new IPEndPoint( entry, Port );
|
|
}
|
|
}
|
|
|
|
// 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<UInt128, mUDPClient> cur in serverInstance.clientConnections ){
|
|
await cur.Value.Send( data );
|
|
}
|
|
}
|
|
|
|
public async Task SendClient<Packet>(string UserName, Packet data) {
|
|
foreach( KeyValuePair<UInt128, mUDPClient> cur in serverInstance.clientConnections) {
|
|
if (cur.Value.UserName.ToLower() == UserName.ToLower()) {
|
|
await cur.Value.Send( data );
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task SendServer<Packet>(Packet data) {
|
|
await serverInstance.ServerConnection.Send( data );
|
|
}
|
|
}
|
|
}
|