basic implementation of UDP for web server

This commit is contained in:
Bitl 2020-06-29 13:36:48 -07:00
parent 8a3cf6e689
commit e19511f2f2
3 changed files with 93 additions and 61 deletions

View File

@ -7,6 +7,7 @@
* To change this template use Tools | Options | Coding | Edit Standard Headers. * To change this template use Tools | Options | Coding | Edit Standard Headers.
*/ */
using LiteNetLib;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
@ -142,7 +143,8 @@ public static class GlobalVars
public static readonly string ScriptName = "CSMPFunctions"; public static readonly string ScriptName = "CSMPFunctions";
public static readonly string ScriptGenName = "CSMPBoot"; public static readonly string ScriptGenName = "CSMPBoot";
public static SimpleHTTPServer WebServer = null; public static SimpleHTTPServer WebServer = null;
public static bool IsWebServerOn = false; public static NetManager WebServerUDPInstance = null;
public static bool IsWebServerOn = false;
public static bool IsSnapshot = false; public static bool IsSnapshot = false;
//vars for loader //vars for loader
public static bool ReadyToLaunch = false; public static bool ReadyToLaunch = false;

View File

@ -41,8 +41,9 @@ namespace NovetusLauncher
// TODO: Add constructor code after the InitializeComponent() call. // TODO: Add constructor code after the InitializeComponent() call.
// //
} }
public void InitUPnP() #region UPnP
public void InitUPnP()
{ {
if (GlobalVars.UPnP == true) if (GlobalVars.UPnP == true)
{ {
@ -123,8 +124,10 @@ namespace NovetusLauncher
ConsolePrint("UPnP: Unable to disconnect device. Reason - " + ex.Message, 2); ConsolePrint("UPnP: Unable to disconnect device. Reason - " + ex.Message, 2);
} }
} }
#endregion
public void ReadyCallback() #region Discord
public void ReadyCallback()
{ {
ConsolePrint("Discord RPC: Ready", 3); ConsolePrint("Discord RPC: Ready", 3);
} }
@ -167,50 +170,62 @@ namespace NovetusLauncher
LauncherFuncs.UpdateRichPresence(LauncherFuncs.LauncherState.InLauncher, "", true); LauncherFuncs.UpdateRichPresence(LauncherFuncs.LauncherState.InLauncher, "", true);
} }
} }
#endregion
void StartWebServer() #region Web Server
{ //udp clients will connect to the web server alongside the game.
if (SecurityFuncs.IsElevated) void StartWebServer()
{
if (SecurityFuncs.IsElevated)
{ {
try try
{ {
GlobalVars.WebServer = new SimpleHTTPServer(GlobalVars.ServerDir, GlobalVars.WebServer_Port); GlobalVars.WebServer = new SimpleHTTPServer(GlobalVars.ServerDir, GlobalVars.WebServer_Port);
ConsolePrint("WebServer: Server is running on port: " + GlobalVars.WebServer.Port.ToString(), 3); if (GlobalVars.UDP)
} {
catch (Exception ex) when (!Env.Debugging) GlobalVars.WebServerUDPInstance = UDP.StartServer(GlobalVars.WebServer_Port);
{ }
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (" + ex.Message + ")", 2); ConsolePrint("WebServer: Server is running on port: " + GlobalVars.WebServer.Port.ToString(), 3);
label17.Visible = false; }
} catch (Exception ex) when (!Env.Debugging)
{
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (" + ex.Message + ")", 2);
label17.Visible = false;
}
} }
else else
{ {
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (Did not run as Administrator)", 2); ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (Did not run as Administrator)", 2);
label17.Visible = false; label17.Visible = false;
} }
} }
void StopWebServer() void StopWebServer()
{ {
if (SecurityFuncs.IsElevated) if (SecurityFuncs.IsElevated)
{ {
try try
{ {
ConsolePrint("WebServer: Server has stopped on port: " + GlobalVars.WebServer.Port.ToString(), 2); ConsolePrint("WebServer: Server has stopped on port: " + GlobalVars.WebServer.Port.ToString(), 2);
GlobalVars.WebServer.Stop(); if (GlobalVars.UDP)
} {
catch (Exception ex) when (!Env.Debugging) GlobalVars.WebServerUDPInstance.Stop();
{ }
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (" + ex.Message + ")", 2); GlobalVars.WebServer.Stop();
} }
catch (Exception ex) when (!Env.Debugging)
{
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (" + ex.Message + ")", 2);
}
} }
else else
{ {
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (Did not run as Administrator)", 2); ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (Did not run as Administrator)", 2);
} }
} }
#endregion
async void tabControl1_SelectedIndexChanged(object sender, EventArgs e) async void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{ {
if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])//your specific tabname if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])//your specific tabname
{ {

View File

@ -40,8 +40,9 @@ namespace NovetusLauncher
// TODO: Add constructor code after the InitializeComponent() call. // TODO: Add constructor code after the InitializeComponent() call.
// //
} }
public void InitUPnP() #region UPnP
public void InitUPnP()
{ {
if (GlobalVars.UPnP == true) if (GlobalVars.UPnP == true)
{ {
@ -122,8 +123,10 @@ namespace NovetusLauncher
ConsolePrint("UPnP: Unable to disconnect device. Reason - " + ex.Message, 2); ConsolePrint("UPnP: Unable to disconnect device. Reason - " + ex.Message, 2);
} }
} }
#endregion
public void ReadyCallback() #region Discord
public void ReadyCallback()
{ {
ConsolePrint("Discord RPC: Ready", 3); ConsolePrint("Discord RPC: Ready", 3);
} }
@ -166,50 +169,62 @@ namespace NovetusLauncher
LauncherFuncs.UpdateRichPresence(LauncherFuncs.LauncherState.InLauncher, "", true); LauncherFuncs.UpdateRichPresence(LauncherFuncs.LauncherState.InLauncher, "", true);
} }
} }
#endregion
void StartWebServer() #region Web Server
{ //udp clients will connect to the web server alongside the game.
if (SecurityFuncs.IsElevated) void StartWebServer()
{
if (SecurityFuncs.IsElevated)
{ {
try try
{ {
GlobalVars.WebServer = new SimpleHTTPServer(GlobalVars.ServerDir, GlobalVars.WebServer_Port); GlobalVars.WebServer = new SimpleHTTPServer(GlobalVars.ServerDir, GlobalVars.WebServer_Port);
ConsolePrint("WebServer: Server is running on port: " + GlobalVars.WebServer.Port.ToString(), 3); if (GlobalVars.UDP)
} {
catch (Exception ex) when (!Env.Debugging) GlobalVars.WebServerUDPInstance = UDP.StartServer(GlobalVars.WebServer_Port);
{ }
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (" + ex.Message + ")", 2); ConsolePrint("WebServer: Server is running on port: " + GlobalVars.WebServer.Port.ToString(), 3);
label17.Visible = false; }
} catch (Exception ex) when (!Env.Debugging)
{
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (" + ex.Message + ")", 2);
label17.Visible = false;
}
} }
else else
{ {
ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (Did not run as Administrator)", 2); ConsolePrint("WebServer: Failed to launch WebServer. Some features may not function. (Did not run as Administrator)", 2);
label17.Visible = false; label17.Visible = false;
} }
} }
void StopWebServer() void StopWebServer()
{ {
if (SecurityFuncs.IsElevated) if (SecurityFuncs.IsElevated)
{ {
try try
{ {
ConsolePrint("WebServer: Server has stopped on port: " + GlobalVars.WebServer.Port.ToString(), 2); ConsolePrint("WebServer: Server has stopped on port: " + GlobalVars.WebServer.Port.ToString(), 2);
GlobalVars.WebServer.Stop(); if (GlobalVars.UDP)
} {
catch (Exception ex) when (!Env.Debugging) GlobalVars.WebServerUDPInstance.Stop();
{ }
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (" + ex.Message + ")", 2); GlobalVars.WebServer.Stop();
} }
catch (Exception ex) when (!Env.Debugging)
{
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (" + ex.Message + ")", 2);
}
} }
else else
{ {
ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (Did not run as Administrator)", 2); ConsolePrint("WebServer: Failed to stop WebServer. Some features may not function. (Did not run as Administrator)", 2);
} }
} }
#endregion
async void tabControl1_SelectedIndexChanged(object sender, EventArgs e) async void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{ {
if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])//your specific tabname if (tabControl1.SelectedTab == tabControl1.TabPages["tabPage2"])//your specific tabname
{ {