From 842cee45be6bf4d57f40bd9d1477baabc690f90c Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Fri, 8 Dec 2017 10:26:26 -0500 Subject: [PATCH] Move Definitions to their own class --- TGS.Interface/Definitions.cs | 37 +++++++++++++++++ TGS.Interface/IServerInterface.cs | 10 ++--- TGS.Interface/ServerInterface.cs | 65 +++--------------------------- TGS.Interface/TGS.Interface.csproj | 1 + TGS.Server/Server.cs | 28 ++++++++----- 5 files changed, 68 insertions(+), 73 deletions(-) create mode 100644 TGS.Interface/Definitions.cs diff --git a/TGS.Interface/Definitions.cs b/TGS.Interface/Definitions.cs new file mode 100644 index 0000000000..a39fd6ed7c --- /dev/null +++ b/TGS.Interface/Definitions.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using TGS.Interface.Components; + +namespace TGS.Interface +{ + /// + /// Contains constants for the interface + /// + public static class Definitions + { + /// + /// The maximum message size to and from a local server + /// + public const long TransferLimitLocal = Int32.MaxValue; //2GB can't go higher + + /// + /// The maximum message size to and from a remote server + /// + public const long TransferLimitRemote = 10485760; //10 MB + + /// + /// Base name of communication URLs + /// + public const string MasterInterfaceName = "TGStationServerService"; + /// + /// Base name of instance URLs + /// + public const string InstanceInterfaceName = MasterInterfaceName + "/Instance"; + + /// + /// Version of the interface + /// + public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version; + } +} diff --git a/TGS.Interface/IServerInterface.cs b/TGS.Interface/IServerInterface.cs index 5f96e83302..fee48c91b9 100644 --- a/TGS.Interface/IServerInterface.cs +++ b/TGS.Interface/IServerInterface.cs @@ -3,12 +3,12 @@ namespace TGS.Interface { /// - /// Main for communicating the + /// Main for communicating the /// public interface IServerInterface : IDisposable { /// - /// The of the connected + /// The of the connected /// Version ServerVersion { get; } @@ -52,17 +52,17 @@ namespace TGS.Interface /// /// Returns a root service component /// - /// The component for the service + /// The component for the service T GetServiceComponent(); /// - /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors + /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors /// /// on successful connection, error message on failure ConnectivityLevel ConnectionStatus(); /// - /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors + /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors /// /// String of the error that prevented an elevated connectivity level /// The apporopriate diff --git a/TGS.Interface/ServerInterface.cs b/TGS.Interface/ServerInterface.cs index 728f1d253f..5045a3e2f8 100644 --- a/TGS.Interface/ServerInterface.cs +++ b/TGS.Interface/ServerInterface.cs @@ -15,39 +15,6 @@ namespace TGS.Interface /// sealed public class ServerInterface : IServerInterface { - /// - /// List of s that can be used with - /// - public static readonly IList ValidServiceInterfaces = new List { typeof(ITGSService), typeof(ITGInstanceManager), typeof(ITGConnectivity), typeof(ITGLanding) }; - - /// - /// Version of the interface - /// - public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version; - - /// - /// List of s that can be used with - /// - public static readonly IList ValidInstanceInterfaces = CollectComponents(); - - /// - /// The maximum message size to and from a local server - /// - public const long TransferLimitLocal = Int32.MaxValue; //2GB can't go higher - - /// - /// The maximum message size to and from a remote server - /// - public const long TransferLimitRemote = 10485760; //10 MB - - /// - /// Base name of communication URLs - /// - public const string MasterInterfaceName = "TGStationServerService"; - /// - /// Base name of instance URLs - /// - public const string InstanceInterfaceName = MasterInterfaceName + "/Instance"; /// /// The @@ -92,23 +59,6 @@ namespace TGS.Interface /// IDictionary ChannelFactoryCache = new Dictionary(); - /// - /// Returns a of s that can be used with the service - /// - /// A of s that can be used with the service - static IList CollectComponents() - { - var ConnectivityComponent = typeof(ITGConnectivity); - //find all interfaces in this assembly in this namespace that have the service contract attribute - var query = from t in Assembly.GetExecutingAssembly().GetTypes() - where t.IsInterface - && t.Namespace == ConnectivityComponent.Namespace - && t.GetCustomAttribute(typeof(ServiceContractAttribute)) != null - && (t == ConnectivityComponent || !ValidServiceInterfaces.Contains(t)) - select t; - return query.ToList(); - } - /// /// Sets the function called when a remote login fails due to the server having an invalid SSL cert /// @@ -233,9 +183,10 @@ namespace TGS.Interface /// public bool VersionMismatch(out string errorMessage) { - if(ServerVersion.Major != Version.Major || ServerVersion.Minor != Version.Minor || ServerVersion.Build != Version.Build) //don't care about the patch level + var version = Definitions.Version; + if (ServerVersion.Major != version.Major || ServerVersion.Minor != version.Minor || ServerVersion.Build != version.Build) //don't care about the patch level { - errorMessage = String.Format("Version mismatch between interface version ({0}) and service version ({1}). Some functionality may crash this program.", Version, ServerVersion); + errorMessage = String.Format("Version mismatch between interface version ({0}) and service version ({1}). Some functionality may crash this program.", version, ServerVersion); return true; } errorMessage = null; @@ -246,8 +197,6 @@ namespace TGS.Interface public T GetComponent() { var ToT = typeof(T); - if (!ValidInstanceInterfaces.Contains(ToT)) - throw new Exception("Invalid type!"); return GetComponentImpl(true); } @@ -292,8 +241,6 @@ namespace TGS.Interface public T GetServiceComponent() { var ToT = typeof(T); - if (!ValidServiceInterfaces.Contains(ToT)) - throw new Exception("Invalid type!"); return GetComponentImpl(false); } @@ -305,7 +252,7 @@ namespace TGS.Interface /// The correct ChannelFactory CreateChannel(string instanceName) { - var accessPath = instanceName == null ? MasterInterfaceName : String.Format("{0}/{1}", InstanceInterfaceName, instanceName); + var accessPath = instanceName == null ? Definitions.MasterInterfaceName : String.Format("{0}/{1}", Definitions.InstanceInterfaceName, instanceName); if (!IsRemoteConnection) return CreateLocalChannel(instanceName, accessPath); return CreateRemoteChannel(instanceName, accessPath); @@ -324,7 +271,7 @@ namespace TGS.Interface { var interfaceName = typeof(T).Name; var res2 = new ChannelFactory( - new NetNamedPipeBinding { SendTimeout = new TimeSpan(0, 0, 30), MaxReceivedMessageSize = TransferLimitLocal }, new EndpointAddress(String.Format("net.pipe://localhost/{0}/{1}", accessPath, interfaceName))); //10 megs + new NetNamedPipeBinding { SendTimeout = new TimeSpan(0, 0, 30), MaxReceivedMessageSize = Definitions.TransferLimitLocal }, new EndpointAddress(String.Format("net.pipe://localhost/{0}/{1}", accessPath, interfaceName))); //10 megs res2.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation; return res2; } @@ -342,7 +289,7 @@ namespace TGS.Interface var binding = new BasicHttpsBinding() { SendTimeout = new TimeSpan(0, 0, 40), - MaxReceivedMessageSize = TransferLimitRemote + MaxReceivedMessageSize = Definitions.TransferLimitRemote }; var interfaceName = typeof(T).Name; diff --git a/TGS.Interface/TGS.Interface.csproj b/TGS.Interface/TGS.Interface.csproj index a6a0df7701..9bb024c521 100644 --- a/TGS.Interface/TGS.Interface.csproj +++ b/TGS.Interface/TGS.Interface.csproj @@ -62,6 +62,7 @@ + diff --git a/TGS.Server/Server.cs b/TGS.Server/Server.cs index 513e25e20f..4b4cfc7fcb 100644 --- a/TGS.Server/Server.cs +++ b/TGS.Server/Server.cs @@ -198,9 +198,11 @@ namespace TGS.Server /// void SetupService() { - serviceHost = CreateHost(this, ServerInterface.MasterInterfaceName); - foreach (var I in ServerInterface.ValidServiceInterfaces) - AddEndpoint(serviceHost, I); + serviceHost = CreateHost(this, Definitions.MasterInterfaceName); + AddEndpoint(serviceHost, typeof(ITGConnectivity)); + AddEndpoint(serviceHost, typeof(ITGInstanceManager)); + AddEndpoint(serviceHost, typeof(ITGLanding)); + AddEndpoint(serviceHost, typeof(ITGSService)); serviceHost.Authorization.ServiceAuthorizationManager = new RootAuthorizationManager(); //only admins can diddle us serviceHost.Authentication.ServiceAuthenticationManager = new AuthenticationHeaderDecoder(); } @@ -313,11 +315,19 @@ namespace TGS.Server return null; } - var host = CreateHost(instance, String.Format("{0}/{1}", ServerInterface.InstanceInterfaceName, instanceName)); + var host = CreateHost(instance, String.Format("{0}/{1}", Definitions.InstanceInterfaceName, instanceName)); hosts.Add(instanceName, host); - - foreach (var J in ServerInterface.ValidInstanceInterfaces) - AddEndpoint(host, J); + + AddEndpoint(host, typeof(ITGConnectivity)); + AddEndpoint(host, typeof(ITGAdministration)); + AddEndpoint(host, typeof(ITGChat)); + AddEndpoint(host, typeof(ITGCompiler)); + AddEndpoint(host, typeof(ITGConfig)); + AddEndpoint(host, typeof(ITGConnectivity)); + AddEndpoint(host, typeof(ITGDreamDaemon)); + AddEndpoint(host, typeof(ITGInstance)); + AddEndpoint(host, typeof(ITGInterop)); + AddEndpoint(host, typeof(ITGRepository)); host.Authorization.ServiceAuthorizationManager = instance; host.Authentication.ServiceAuthenticationManager = new AuthenticationHeaderDecoder(); @@ -332,11 +342,11 @@ namespace TGS.Server void AddEndpoint(ServiceHost host, Type typetype) { var bindingName = typetype.Name; - host.AddServiceEndpoint(typetype, new NetNamedPipeBinding() { SendTimeout = new TimeSpan(0, 0, 30), MaxReceivedMessageSize = ServerInterface.TransferLimitLocal }, bindingName); + host.AddServiceEndpoint(typetype, new NetNamedPipeBinding() { SendTimeout = new TimeSpan(0, 0, 30), MaxReceivedMessageSize = Definitions.TransferLimitLocal }, bindingName); var httpsBinding = new BasicHttpsBinding() { SendTimeout = new TimeSpan(0, 0, 40), - MaxReceivedMessageSize = ServerInterface.TransferLimitRemote + MaxReceivedMessageSize = Definitions.TransferLimitRemote }; var requireAuth = typetype.Name != typeof(ITGConnectivity).Name; host.AddServiceEndpoint(typetype, httpsBinding, bindingName);