Update the CLI example

This commit is contained in:
derek.holloway committed 2026-08-07 13:00:23 -07:00
1 parent 30feb15821
commit 068c88de4b
5 files changed
+134 -83

No files matched your search

+42 -83
View File
@@ -1,85 +1,48 @@
using System;
using System.Threading.Tasks;
using MistoxServer;
namespace MistoxHolePunch {
enum Mode {
Server,
Client
}
class Program {
static IMistoxServer serverObj;
static bool running = true;
static serverExample? server;
static clientExample? client;
static Mode mode;
void onConnected( object sender, EventArgs e ) {
// sender and e are always null
// put connected functions in here
}
void slowReceive( object obj, EventArgs e ) {
if ( serverObj is ServerInterface ) {
// If ServerMode is passive obj is byte[] and Send takes in byte[]
// If Servermode is Athoritiative obj is the Class type sent and Send takes in any Class type except generic object
// Check to make sure data is correct before relaying
// Also perform server specific checks in here
// IE player didnt teleport or is shooting from 20 feet from his body
serverObj.Send( obj, SendType.SlowUpdate );
}
Console.Write( "Received TCP : " );
Console.WriteLine( obj );
}
void fastReceive( object obj, EventArgs e ) {
if( serverObj is ServerInterface ) {
// If ServerMode is passive obj is byte[] and Send takes in byte[]
// If Servermode is Athoritiative obj is the Class type sent and Send takes in any Class type except generic object
// Check to make sure data is correct before relaying
// Also perform server specific checks in here
// IE player didnt teleport or is shooting from 20 feet from his body
serverObj.Send( obj, SendType.FastUpdate );
}
Console.Write( "Received UDP : " );
Console.WriteLine( obj );
}
void onDisconnected( object sender, EventArgs e ) {
// sender and e are always null
// put disconnected functions in here
}
static string host = "example.com";
static int port = 7300;
static ServerMode mode = ServerMode.Passive;
async Task RunServer() {
serverObj = mServer.newServer( Convert.ToInt32( port ), mode );
serverObj.onConnected += onConnected;
serverObj.onSlowReceive += slowReceive;
serverObj.onFastReceive += fastReceive;
serverObj.onDisconnected += onDisconnected;
while ( running ) {
// Stop this thread for 1 second so the CPU isnt 100%
// All the real work is on different threads
// While loop needs to exist so our main thread doesnt close
await Task.Delay( 1000 );
async Task StartServer(string name, int port) {
server = new serverExample();
await server.StartServer(name, port);
// On a game this sleep loop isnt needed
if (server.serverObject == null) return;
while (server.serverObject.Alive) {
// becuase this is a console app if we dont keep the main thread alive the app will close
await Task.Delay(5000);
}
}
async Task RunClient() {
serverObj = mServer.newClient( host, Convert.ToInt32( port ) );
serverObj.onConnected += onConnected;
serverObj.onSlowReceive += slowReceive;
serverObj.onFastReceive += fastReceive;
serverObj.onDisconnected += onDisconnected;
while( running ) {
// Stop this thread for 1 second so the CPU isnt 100%
// All the real work is on different threads
// While loop needs to exist so our main thread doesnt close
await Task.Delay( 1000 );
async Task StartClient(string username, string host, int port, string password) {
client = new clientExample();
await client.StartClient(username, host, port, password);
// On a game this sleep loop isnt needed
if (client.clientObject == null) return;
while (client.clientObject.Alive) {
// becuase this is a console app if we dont keep the main thread alive the app will close
await Task.Delay(5000);
}
}
static async Task Main(string[] args) {
string Task = args.Length > 0 ? args[0].ToLower() : null;
async Task Main(string[] args) {
string Job = args.Length > 0 ? args[0].ToLower() : "";
string host = "";
string name = "";
string pass = "";
int port = 0;
for( int i = 0; i < args.Length; i++ ) {
string cur = args[i].ToLower();
@@ -87,25 +50,21 @@ namespace MistoxHolePunch {
host = args [i + 1];
} else if( cur == "/p" || cur == "-p" ) {
port = Convert.ToInt32( args [i + 1] );
} else if( cur == "/a" || cur == "-a" ) {
mode = ServerMode.Authoritative;
} else if( cur == "/n" || cur == "-n" ) {
name = args [i + 1];
} else if( cur == "/u" || cur == "-u" ) {
pass = args [i + 1];
}
}
if (Task == "/?" || Task == "--help") {
if (Job == "/?" || Job == "--help") {
Console.WriteLine(HelpDocumentation.HelpText);
} else if (Task == "/s" || Task == "-s") {
Program prog = new Program();
await prog.RunServer();
} else if (Task == "/c" || Task == "-c") {
Program prog = new Program();
await prog.RunClient();
} else if (Job == "/s" || Job == "-s") {
await StartServer(name, port);
} else if (Job == "/c" || Job == "-c") {
await StartClient(name, host, port, pass);
} else {
host = "example.com";
port = 1500;
Program prog = new Program();
//prog.RunClient();
await prog.RunServer();
await StartServer("example.com", 4500);
}
}