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) {
|
||||
|
||||
@@ -8,13 +8,13 @@ using Mistox.Server;
|
||||
namespace Mistox {
|
||||
public class ServerInterface {
|
||||
|
||||
public static MistoxServer ServerInstance; // Server Data
|
||||
public static ServerListener Listener; // Server Listener
|
||||
public static ConcurrentDictionary<string, TcpClientConnection> Connections; // Server Connections
|
||||
public static MistoxServer ServerInstance; // Server Data
|
||||
public static ServerListener Listener; // Server Listener
|
||||
public static ConcurrentDictionary<string, TcpClientConnection> Connections; // Server Connections
|
||||
|
||||
public event EventHandler onUserConnected;
|
||||
public event EventHandler onReceive;
|
||||
public event EventHandler onUserDisconnected;
|
||||
public event Action<TcpClientConnection> onUserConnected; // User connected event handler
|
||||
public event Action<dynamic> onDataReceived; // Data received event handler
|
||||
public event Action<TcpClientConnection> onUserDisconnected; // User disconnected event handler
|
||||
|
||||
public ServerInterface( int port, string serverName ) {
|
||||
|
||||
@@ -28,9 +28,9 @@ namespace Mistox {
|
||||
Connections = new ConcurrentDictionary<string, TcpClientConnection>();
|
||||
|
||||
// Setup the event handlers
|
||||
ServerInstance.onUserConnected += onUserConnected;
|
||||
ServerInstance.onReceived += onReceive;
|
||||
ServerInstance.onUserDisconnected += onUserDisconnected;
|
||||
ServerInstance.onUserConnected += (TcpClientConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); };
|
||||
ServerInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
ServerInstance.onUserDisconnected += (TcpClientConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); };
|
||||
|
||||
// Start listening for connections and wire up the server
|
||||
Listener = new ServerListener();
|
||||
|
||||
Reference in new issue
Block a user