Files
GameServer/MistoxServer/Interface/Interface.cs
T

59 lines
2.2 KiB
C#

using System;
using System.Net;
using Mistox.Attributes;
using Mistox.Client;
using Mistox.Server;
namespace Mistox {
public class GameServer {
public static MistoxServer newServer( string serverName, int port ) {
// Build the network packet list
NetworkPacketDictionary.BuildNetworkPacketList();
// Set the listener address
IPEndPoint listenerAddress = new IPEndPoint( IPAddress.Any, port );
// Return the initlized server instance
return new MistoxServer(listenerAddress, serverName);
}
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++ ) {
if( remoteAddress[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork || remoteAddress [i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6 ) {
index = i;
}
}
string ipAddress = remoteAddress[index].ToString();
// 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 );
}
}
}