From 38f30945824cb20f2193410009abb1bdabc10ef8 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Wed, 18 Oct 2017 12:49:33 -0400 Subject: [PATCH] Document Server.cs --- TGControlPanel/Login.cs | 3 ++ TGControlPanel/Main.cs | 6 +++ TGControlPanel/Program.cs | 1 - TGInstallerWrapper/Main.cs | 3 ++ TGServerService/ProcessExtension.cs | 13 +++++- TGServerService/Program.cs | 2 +- TGServerService/ProjectInstaller.cs | 3 +- TGServiceInterface/Helpers.cs | 2 +- TGServiceInterface/Server.cs | 66 ++++++++++++++++++++--------- 9 files changed, 74 insertions(+), 25 deletions(-) diff --git a/TGControlPanel/Login.cs b/TGControlPanel/Login.cs index 557d484924..c09f9ac8aa 100644 --- a/TGControlPanel/Login.cs +++ b/TGControlPanel/Login.cs @@ -6,6 +6,9 @@ namespace TGControlPanel { public partial class Login : Form { + /// + /// Create a + /// public Login() { InitializeComponent(); diff --git a/TGControlPanel/Main.cs b/TGControlPanel/Main.cs index 969e926771..07e62f8b0a 100644 --- a/TGControlPanel/Main.cs +++ b/TGControlPanel/Main.cs @@ -5,8 +5,14 @@ using TGServiceInterface; namespace TGControlPanel { + /// + /// The main form + /// public partial class Main : Form { + /// + /// Create the control panel. Requires the has had it's connection info setup + /// public Main() { InitializeComponent(); diff --git a/TGControlPanel/Program.cs b/TGControlPanel/Program.cs index d5c1ee1988..12753f1962 100644 --- a/TGControlPanel/Program.cs +++ b/TGControlPanel/Program.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; using System.Windows.Forms; using TGServiceInterface; diff --git a/TGInstallerWrapper/Main.cs b/TGInstallerWrapper/Main.cs index e8fbd719d2..de713fcfb1 100644 --- a/TGInstallerWrapper/Main.cs +++ b/TGInstallerWrapper/Main.cs @@ -33,6 +33,9 @@ namespace TGInstallerWrapper bool cancelled = false; bool pathIsDefault = true; + /// + /// Construct an installer form + /// public Main() { InitializeComponent(); diff --git a/TGServerService/ProcessExtension.cs b/TGServerService/ProcessExtension.cs index 3fc590b89b..cae8cb43f3 100644 --- a/TGServerService/ProcessExtension.cs +++ b/TGServerService/ProcessExtension.cs @@ -4,7 +4,9 @@ using System.Runtime.InteropServices; namespace TGServerService { - //lightly massaged code from https://stackoverflow.com/a/13109774 + /// + /// Helpers to ing and a . Lightly massaged code from https://stackoverflow.com/a/13109774 + /// public static class ProcessExtension { enum ThreadAccess : int @@ -29,6 +31,10 @@ namespace TGServerService [DllImport("kernel32.dll")] static extern int ResumeThread(IntPtr hThread); + /// + /// Suspends all threads for a running + /// + /// The to suspend public static void Suspend(this Process process) { foreach (ProcessThread thread in process.Threads) @@ -40,6 +46,11 @@ namespace TGServerService CloseHandle(pOpenThread); } } + + /// + /// Resumes all threads for a running + /// + /// The to suspend public static void Resume(this Process process) { foreach (ProcessThread thread in process.Threads) diff --git a/TGServerService/Program.cs b/TGServerService/Program.cs index ef6ab4bfc1..3a03de02f0 100644 --- a/TGServerService/Program.cs +++ b/TGServerService/Program.cs @@ -4,7 +4,7 @@ using System.IO; namespace TGServerService { - public static class Program + static class Program { static void Main() => new TGServerService(); //wondows diff --git a/TGServerService/ProjectInstaller.cs b/TGServerService/ProjectInstaller.cs index 3c405c0328..0580a2c6b7 100644 --- a/TGServerService/ProjectInstaller.cs +++ b/TGServerService/ProjectInstaller.cs @@ -1,11 +1,10 @@ using System.ComponentModel; using System.Configuration.Install; -using System.ServiceProcess; namespace ServerService { [RunInstaller(true)] - public partial class ProjectInstaller : Installer + partial class ProjectInstaller : Installer { public ProjectInstaller() { diff --git a/TGServiceInterface/Helpers.cs b/TGServiceInterface/Helpers.cs index c5435c70a4..471d84ccfd 100644 --- a/TGServiceInterface/Helpers.cs +++ b/TGServiceInterface/Helpers.cs @@ -22,7 +22,7 @@ namespace TGServiceInterface using (var rng = new RNGCryptoServiceProvider()) rng.GetBytes(bentropy); - byte[] ciphertext = ProtectedData.Protect(Encoding.UTF8.GetBytes(data), bentropy, DataProtectionScope.CurrentUser); + byte[] ciphertext = ProtectedData.Protect(Encoding.UTF8.GetBytes(cleartext), bentropy, DataProtectionScope.CurrentUser); entropy = Convert.ToBase64String(bentropy, 0, bentropy.Length); return Convert.ToBase64String(ciphertext, 0, ciphertext.Length); diff --git a/TGServiceInterface/Server.cs b/TGServiceInterface/Server.cs index a0bf48284b..5a656e5e3e 100644 --- a/TGServiceInterface/Server.cs +++ b/TGServiceInterface/Server.cs @@ -7,10 +7,13 @@ using System.Security.Principal; using System.ServiceModel; namespace TGServiceInterface { + /// + /// Main inteface class for the service + /// public class Server { /// - /// List of types that can be used with GetComponen + /// List of s that can be used with GetComponen /// public static readonly IList ValidInterfaces = new List { typeof(ITGByond), typeof(ITGChat), typeof(ITGCompiler), typeof(ITGConfig), typeof(ITGDreamDaemon), typeof(ITGRepository), typeof(ITGSService), typeof(ITGConnectivity), typeof(ITGAdministration), typeof(ITGInterop) }; @@ -50,7 +53,15 @@ namespace TGServiceInterface /// static string HTTPSPassword; + /// + /// Associated list of open s keyed by type. A in this list may close or fault at any time. Must be locked before being accessed + /// static Dictionary ChannelFactoryCache = new Dictionary(); + + /// + /// Sets the function called when a remote login fails due to the server having an invalid SSL cert + /// + /// The to be called when a remote login is attempted while the server posesses a bad certificate. Passed a of error information about the and should return if it the connection should be made anyway public static void SetBadCertificateHandler(Func handler) { ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) => @@ -88,17 +99,28 @@ namespace TGServiceInterface ClearCachedChannels(); } + /// + /// Closes all s stored in and clears it + /// static void ClearCachedChannels() { - foreach (var I in ChannelFactoryCache) - CloseChannel(I.Value); - ChannelFactoryCache.Clear(); + lock (ChannelFactoryCache) + { + foreach (var I in ChannelFactoryCache) + CloseChannel(I.Value); + ChannelFactoryCache.Clear(); + } } + /// + /// Returns if the interface being used to connect to a service does not have the same release version as the service + /// + /// An error message to display to the user should this function return + /// if the interface being used to connect to a service does not have the same release version as the service public static bool VersionMismatch(out string errorMessage) { - var splits = Server.GetComponent().Version().Split(' '); - var theirs = new Version(splits[splits.Length - 1].Substring(1)); + var splits = Server.GetComponent().Version().Split(' '); + var theirs = new Version(splits[splits.Length - 1].Substring(1)); var ours = new Version(FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).FileVersion); if(theirs != ours) { @@ -123,6 +145,10 @@ namespace TGServiceInterface ClearCachedChannels(); } + /// + /// Safely shuts down a single + /// + /// public static void CloseChannel(ChannelFactory cf) { try @@ -136,10 +162,10 @@ namespace TGServiceInterface } /// - /// Returns the requested server component interface. This does not guarantee a successful connection + /// Returns the requested component . This does not guarantee a successful connection. s created this way are recycled for minimum latency and bandwidth usage /// - /// The type of the component to retrieve - /// The correct component + /// The component to retrieve + /// The correct component public static T GetComponent() { var tot = typeof(T); @@ -166,6 +192,12 @@ namespace TGServiceInterface return cf.CreateChannel(); } + /// + /// Directly creates a for without caching. This should be eventually closed by the caller + /// + /// The component of the channel to be created + /// The correct + /// Thrown if isn't a valid component public static ChannelFactory CreateChannel() { var ToT = typeof(T); @@ -201,11 +233,9 @@ namespace TGServiceInterface } /// - /// Used to test if the service is avaiable on the machine - /// Note that state can technically change at any time - /// and any call to the service may throw an exception because it failed + /// Used to test if the service is avaiable on the machine. Note that state can technically change at any time and any call to the service may throw an exception because it failed /// - /// null on successful connection, error message on failure + /// on successful connection, error message on failure public static string VerifyConnection() { try @@ -220,10 +250,9 @@ namespace TGServiceInterface } /// - /// As opposed to VerifyConnection(), this check user credentials - /// Requires a prior call to + /// Checks if the supplied user's credentials have permission to use the service. Requires a successful prior call to /// - /// true if credentials are valid, false otherwise + /// if credentials are valid, otherwise public static bool Authenticate() { try @@ -238,10 +267,9 @@ namespace TGServiceInterface } /// - /// As opposed to Authentication() this returns true if the current login can use the interface. - /// Requires a prior call to + /// Checks if the current login can use . Requires a successful prior call to /// - /// true if the connection may use the interface, false otherwise + /// if the connection may use , otherwise public static bool AuthenticateAdmin() { try