Cleanup unnessisary interface classes
This commit is contained in:
1 parent
b83887cb2a
commit
46efb5bd65
8 files changed
+58
-117
No files matched your search
@@ -1,56 +0,0 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Mistox.Client;
|
||||
|
||||
namespace Mistox {
|
||||
public class ClientInterface {
|
||||
|
||||
public static MistoxClient serverInstance;
|
||||
|
||||
public event Action onServerConnected;
|
||||
public event Action<mUDPClient> onUserConnected;
|
||||
public event Action<dynamic> onDataReceived;
|
||||
public event Action<mUDPClient> onUserDisconnected;
|
||||
public event Action onServerDisconnected;
|
||||
|
||||
public ClientInterface( string UserName, string ConnectionKey, string IpOrHostName, int Port ) {
|
||||
|
||||
// Get Server IP
|
||||
IPHostEntry host = Dns.GetHostEntry( IpOrHostName );
|
||||
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
||||
foreach( IPAddress entry in host.AddressList ) {
|
||||
if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) {
|
||||
ServerDenstination = new IPEndPoint( entry, Port );
|
||||
}
|
||||
}
|
||||
|
||||
// Start and wire up the Mistox server client
|
||||
serverInstance = new MistoxClient( UserName, ConnectionKey, ServerDenstination );
|
||||
serverInstance.onServerConnected += onServerConnected;
|
||||
serverInstance.onUserConnected += (mUDPClient endpoint) => { onUserConnected?.Invoke(endpoint); };
|
||||
serverInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
serverInstance.onUserDisconnected += (mUDPClient endpoint) => { onUserDisconnected?.Invoke(endpoint); };
|
||||
serverInstance.onServerDisconnected += onServerDisconnected;
|
||||
}
|
||||
|
||||
public async Task SendAllClients<Packet>(Packet data) {
|
||||
foreach( mUDPClient cur in serverInstance.clientConnections.Values ){
|
||||
await cur.Send( data );
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||
foreach( mUDPClient cur in serverInstance.clientConnections.Values ) {
|
||||
if (cur.UserName.ToLower() == UserName.ToLower()) {
|
||||
await cur.Send( data );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendServer<Packet>(Packet data) {
|
||||
await serverInstance.ServerConnection.Send( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,30 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using Mistox.Attributes;
|
||||
using Mistox.Client;
|
||||
using Mistox.Server;
|
||||
|
||||
namespace Mistox {
|
||||
public class mServer {
|
||||
public class GameServer {
|
||||
|
||||
public static ServerInterface newServer( string serverName, int port ) {
|
||||
public static MistoxServer newServer( string serverName, int port ) {
|
||||
|
||||
// Build the network packet list
|
||||
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||
return new ServerInterface(port, serverName);
|
||||
|
||||
// Set the listener address
|
||||
IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port );
|
||||
|
||||
// Return the initlized server instance
|
||||
return new MistoxServer(listenerAddress, serverName);
|
||||
}
|
||||
|
||||
public static ClientInterface newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) {
|
||||
public static MistoxClient newClient( string UserName, string ServerIPOrHostName, int Port, string ConnectionKey = "" ) {
|
||||
|
||||
// Build the network packet list
|
||||
NetworkPacketDictionary.BuildNetworkPacketList();
|
||||
|
||||
// Get the remote IP from the DNSName
|
||||
int index = 0;
|
||||
IPAddress[] remoteAddress = Dns.GetHostAddresses(ServerIPOrHostName);
|
||||
for( int i=0; i< remoteAddress.Length; i++ ) {
|
||||
@@ -20,13 +33,25 @@ namespace Mistox {
|
||||
}
|
||||
}
|
||||
string ipAddress = remoteAddress[index].ToString();
|
||||
if ( remoteAddress.Length > 0) {
|
||||
return new ClientInterface(UserName, ConnectionKey, ipAddress, Port);
|
||||
} else {
|
||||
|
||||
// If a remote address exists
|
||||
if ( remoteAddress.Length == 0) {
|
||||
Console.WriteLine("The server at " + ServerIPOrHostName + " doesn't exit or cannot be found");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve to an IPEndPoint
|
||||
IPHostEntry host = Dns.GetHostEntry( ipAddress );
|
||||
IPEndPoint ServerDenstination = new IPEndPoint(0, 0);
|
||||
foreach( IPAddress entry in host.AddressList ) {
|
||||
if ( entry.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) {
|
||||
ServerDenstination = new IPEndPoint( entry, Port );
|
||||
}
|
||||
}
|
||||
|
||||
// Return the initlized client object
|
||||
return new MistoxClient( UserName, ConnectionKey, ServerDenstination );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Mistox.Server;
|
||||
|
||||
namespace Mistox {
|
||||
public class ServerInterface {
|
||||
|
||||
public static ServerEntity ServerInstance; // Server Data
|
||||
|
||||
public event Action<ServerConnection> onUserConnected; // User connected event handler
|
||||
public event Action<dynamic> onDataReceived; // Data received event handler
|
||||
public event Action<ServerConnection> onUserDisconnected; // User disconnected event handler
|
||||
|
||||
public ServerInterface( int port, string serverName ) {
|
||||
|
||||
IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port );
|
||||
|
||||
// Initilize the server
|
||||
ServerInstance = new ServerEntity(listenerAddress);
|
||||
ServerInstance.ServerName = serverName;
|
||||
|
||||
// Setup the event handlers
|
||||
ServerInstance.onUserConnected += (ServerConnection connectedUser) => { onUserConnected?.Invoke(connectedUser); };
|
||||
ServerInstance.onDataReceived += (dynamic data) => { onDataReceived?.Invoke(data); };
|
||||
ServerInstance.onUserDisconnected += (ServerConnection disconnectedUser) => { onUserDisconnected?.Invoke(disconnectedUser); };
|
||||
|
||||
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, ServerConnection> cur in ServerInstance.Connections ) {
|
||||
await cur.Value.Send(data);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SendClient<Packet>(string UserName, Packet data) {
|
||||
await ServerInstance.Connections[UserName].Send( data );
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user