48 lines
1.7 KiB
C#
48 lines
1.7 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 {
|
|
|
|
MistoxServer ServerInstance;
|
|
|
|
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>()
|
|
};
|
|
|
|
// Start listening for connections and wire up the server
|
|
ServerInstance.serverListener = new ServerListener( ServerInstance );
|
|
ServerInstance.serverListener.onConnected += onUserConnected;
|
|
ServerInstance.serverListener.onReceive += onReceive;
|
|
ServerInstance.serverListener.onDisconnected += onUserDisconnected;
|
|
|
|
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 ServerInstance.Connections ) {
|
|
await cur.Value.Connection.Send(data);
|
|
}
|
|
}
|
|
|
|
public async Task SendClient<Packet>(string UserName, Packet data) {
|
|
await ServerInstance.Connections[UserName].Connection.Send( data );
|
|
}
|
|
}
|
|
}
|