52 lines
1.9 KiB
C#
52 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
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 event EventHandler onUserConnected;
|
|
public event EventHandler onReceive;
|
|
public event EventHandler onUserDisconnected;
|
|
|
|
public ServerInterface( int port, string serverName ) {
|
|
|
|
// Initilize the server
|
|
ServerInstance = new MistoxServer() {
|
|
ServerName = serverName,
|
|
BindAddress = IPAddress.Any,
|
|
Port = port,
|
|
MaxClients = 10
|
|
};
|
|
Connections = new ConcurrentDictionary<string, TcpClientConnection>();
|
|
|
|
// Setup the event handlers
|
|
ServerInstance.onUserConnected += onUserConnected;
|
|
ServerInstance.onReceived += onReceive;
|
|
ServerInstance.onUserDisconnected += onUserDisconnected;
|
|
|
|
// Start listening for connections and wire up the server
|
|
Listener = new ServerListener();
|
|
|
|
Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port );
|
|
}
|
|
|
|
public async Task SendAllClients<Packet>(Packet data) {
|
|
foreach( KeyValuePair<string, TcpClientConnection> cur in Connections ) {
|
|
await cur.Value.Connection.Send(data);
|
|
}
|
|
}
|
|
|
|
public async Task SendClient<Packet>(string UserName, Packet data) {
|
|
await Connections[UserName].Connection.Send( data );
|
|
}
|
|
}
|
|
}
|