Rework the server connection to be more structured

This commit is contained in:
derek.holloway committed 2026-07-02 16:36:23 -07:00
1 parent 7b58ca0338
commit 64dbd02874
10 files changed
+139 -141

No files matched your search

+4 -19
View File
@@ -1,16 +1,15 @@
using System;
using System.Net;
using System.Threading.Tasks;
namespace MistoxServer {
namespace Mistox {
public class mServer {
public static IMistoxServer newServer(int port) {
public static ServerInterface newServer(int port, string serverName) {
DataPacket.BuildNetworkPacketList();
return new ServerInterface(port);
return new ServerInterface(port, serverName);
}
public static IMistoxServer newClient(string ServerIPOrHostName, int Port) {
public static ClientInterface newClient(string ServerIPOrHostName, int Port) {
DataPacket.BuildNetworkPacketList();
int index = 0;
IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName);
@@ -30,18 +29,4 @@ namespace MistoxServer {
}
public interface IMistoxServer {
public event EventHandler onConnected;
public event EventHandler onSlowReceive;
public event EventHandler onFastReceive;
public event EventHandler onDisconnected;
public Task Send<Packet>(Packet data, SendType speed);
}
public enum SendType {
SlowUpdate,
FastUpdate
}
}
+29 -41
View File
@@ -1,59 +1,47 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using MistoxServer.Client;
using MistoxServer.Server;
using Mistox.Server;
namespace MistoxServer {
public class ServerInterface : IMistoxServer {
namespace Mistox {
public class ServerInterface {
mUDPClient FastUpdateServer;
mTCPListener SlowUpdateServer;
List<Connection> Connections = new List<Connection>();
MistoxServer ServerInstance;
public event EventHandler onConnected;
public event EventHandler onSlowReceive;
public event EventHandler onFastReceive;
public event EventHandler onDisconnected;
public event EventHandler onUserConnected;
public event EventHandler onReceive;
public event EventHandler onUserDisconnected;
public ServerInterface( int port ) {
public ServerInterface( int port, string serverName ) {
// Initilize the server listening connections
SlowUpdateServer = new mTCPListener( port );
SlowUpdateServer.onConnected += OnConnected;
SlowUpdateServer.onDisconnected += OnDisconnected;
// Initilize the server
ServerInstance = new MistoxServer() {
ServerName = serverName,
BindAddress = IPAddress.Any,
Port = port,
MaxClients = 10,
Connections = new ConcurrentDictionary<string, TcpClientConnection>()
};
FastUpdateServer = new mUDPClient( new IPEndPoint( IPAddress.Any, port ) );
FastUpdateServer.onReceived += ( object o, EventArgs e ) => { onFastReceive?.Invoke( o, e ); };
// 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 );
}
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>( 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 );
}
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 );
}
}
}