using System; using System.Collections.Generic; using System.Net; using System.Threading.Tasks; using MistoxServer.Client; using MistoxServer.Server; namespace MistoxServer { public class ServerInterface : IMistoxServer { mUDPClient FastUpdateServer; mTCPListener SlowUpdateServer; List Connections = new List(); public event EventHandler onConnected; public event EventHandler onSlowReceive; public event EventHandler onFastReceive; public event EventHandler onDisconnected; public ServerInterface( int port ) { // Initilize the server listening connections SlowUpdateServer = new mTCPListener( port ); SlowUpdateServer.onConnected += OnConnected; SlowUpdateServer.onDisconnected += OnDisconnected; FastUpdateServer = new mUDPClient( new IPEndPoint( IPAddress.Any, port ) ); FastUpdateServer.onReceived += ( object o, EventArgs e ) => { onFastReceive?.Invoke( o, e ); }; Console.WriteLine( "The Server is initilized and waiting for clients to connect at port : " + port ); } void OnConnected( object sender, EventArgs e ) { Connection user = (Connection)sender; onConnected?.Invoke( sender, e ); user.slowClient.onReceived += ( object o, EventArgs e ) => { onSlowReceive?.Invoke( o, e ); }; user.slowClient.onDisconnected += OnDisconnected; Connections.Add( user ); } void OnDisconnected( object sender, EventArgs e ) { Connection user = (Connection)sender; onDisconnected?.Invoke( sender, e ); Connections.Remove( user ); } public async Task Send( Packet data, SendType speed ) { if (speed == SendType.SlowUpdate) { foreach( Connection cur in Connections ) { await cur.slowClient.Send( data ); } } else { foreach( Connection cur in Connections ) { await FastUpdateServer.Send( data, cur.fastClient ); } } } } }