Wire up the event handlers
This commit is contained in:
1 parent
a04516c470
commit
a689f2656f
11 files changed
+90
-76
No files matched your search
@@ -12,15 +12,23 @@ namespace Mistox {
|
||||
public static mTCPClient serverConnection;
|
||||
public static ConcurrentDictionary<string, UDPEndpoint> clientConnections;
|
||||
|
||||
public event EventHandler onUserConnected;
|
||||
public event EventHandler onReceive;
|
||||
public event EventHandler onUserDisconnected;
|
||||
public event Action onServerConnected;
|
||||
public event Action<UDPEndpoint> onUserConnected;
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public event Action<UDPEndpoint> 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 );
|
||||
@@ -35,9 +43,6 @@ namespace Mistox {
|
||||
// Make a TCP connection to the server
|
||||
try {
|
||||
serverConnection = new mTCPClient( ServerDenstination );
|
||||
serverConnection.on +=
|
||||
serverConnection.onReceive += ( object o, EventArgs e ) => { onReceive?.Invoke( o, e ); };
|
||||
serverConnection.onDisconnected += ( object o, EventArgs e ) => { onUserDisconnected?.Invoke( o, e ); };
|
||||
} catch( Exception e ) {
|
||||
Console.WriteLine( "An error has occured with the connection to the server. Error { " );
|
||||
Console.WriteLine( e.ToString() );
|
||||
@@ -47,12 +52,12 @@ namespace Mistox {
|
||||
|
||||
public async Task SendAllClients<Packet>(Packet data) {
|
||||
foreach(KeyValuePair<string, UDPEndpoint> cur in clientConnections){
|
||||
await cur.Value.Connection.Send( data, cur.Value.Destination );
|
||||
await cur.Value.Connection.Send( data );
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||
await clientConnections[UserName].Connection.Send( data, clientConnections[UserName].Destination );
|
||||
await clientConnections[UserName].Connection.Send( data );
|
||||
}
|
||||
|
||||
public async Task SendServer<Packet>(Packet data) {
|
||||
|
||||
Reference in new issue
Block a user