Move Definitions to their own class

This commit is contained in:
Cyberboss
2017-12-08 10:26:26 -05:00
parent 29fbf308c1
commit 842cee45be
5 changed files with 68 additions and 73 deletions
+37
View File
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using TGS.Interface.Components;
namespace TGS.Interface
{
/// <summary>
/// Contains constants for the interface
/// </summary>
public static class Definitions
{
/// <summary>
/// The maximum message size to and from a local server
/// </summary>
public const long TransferLimitLocal = Int32.MaxValue; //2GB can't go higher
/// <summary>
/// The maximum message size to and from a remote server
/// </summary>
public const long TransferLimitRemote = 10485760; //10 MB
/// <summary>
/// Base name of communication URLs
/// </summary>
public const string MasterInterfaceName = "TGStationServerService";
/// <summary>
/// Base name of instance URLs
/// </summary>
public const string InstanceInterfaceName = MasterInterfaceName + "/Instance";
/// <summary>
/// Version of the interface
/// </summary>
public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version;
}
}
+5 -5
View File
@@ -3,12 +3,12 @@
namespace TGS.Interface
{
/// <summary>
/// Main <see langword="interface"/> for communicating the <see cref="ITGSService"/>
/// Main <see langword="interface"/> for communicating the <see cref="Components.ITGSService"/>
/// </summary>
public interface IServerInterface : IDisposable
{
/// <summary>
/// The <see cref="Version"/> of the connected <see cref="ITGSService"/>
/// The <see cref="Version"/> of the connected <see cref="Components.ITGSService"/>
/// </summary>
Version ServerVersion { get; }
@@ -52,17 +52,17 @@ namespace TGS.Interface
/// <summary>
/// Returns a root service component
/// </summary>
/// <returns>The <see cref="ITGSService"/> component for the service</returns>
/// <returns>The <see cref="Components.ITGSService"/> component for the service</returns>
T GetServiceComponent<T>();
/// <summary>
/// Used to test if the <see cref="ITGSService"/> 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 <see cref="Components.ITGSService"/> 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
/// </summary>
/// <returns><see langword="null"/> on successful connection, error message <see cref="string"/> on failure</returns>
ConnectivityLevel ConnectionStatus();
/// <summary>
/// Used to test if the <see cref="ITGSService"/> 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 <see cref="Components.ITGSService"/> 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
/// </summary>
/// <param name="error">String of the error that prevented an elevated connectivity level</param>
/// <returns>The apporopriate <see cref="ConnectivityLevel"/></returns>
+6 -59
View File
@@ -15,39 +15,6 @@ namespace TGS.Interface
/// <inheritdoc />
sealed public class ServerInterface : IServerInterface
{
/// <summary>
/// List of <see langword="interface"/>s that can be used with <see cref="GetServiceComponent{T}"/>
/// </summary>
public static readonly IList<Type> ValidServiceInterfaces = new List<Type> { typeof(ITGSService), typeof(ITGInstanceManager), typeof(ITGConnectivity), typeof(ITGLanding) };
/// <summary>
/// Version of the interface
/// </summary>
public static readonly Version Version = Assembly.GetExecutingAssembly().GetName().Version;
/// <summary>
/// List of <see langword="interface"/>s that can be used with <see cref="GetComponent{T}"/>
/// </summary>
public static readonly IList<Type> ValidInstanceInterfaces = CollectComponents();
/// <summary>
/// The maximum message size to and from a local server
/// </summary>
public const long TransferLimitLocal = Int32.MaxValue; //2GB can't go higher
/// <summary>
/// The maximum message size to and from a remote server
/// </summary>
public const long TransferLimitRemote = 10485760; //10 MB
/// <summary>
/// Base name of communication URLs
/// </summary>
public const string MasterInterfaceName = "TGStationServerService";
/// <summary>
/// Base name of instance URLs
/// </summary>
public const string InstanceInterfaceName = MasterInterfaceName + "/Instance";
/// <summary>
/// The <see cref="ServerVersion"/>
@@ -92,23 +59,6 @@ namespace TGS.Interface
/// </summary>
IDictionary<string, ChannelFactory> ChannelFactoryCache = new Dictionary<string, ChannelFactory>();
/// <summary>
/// Returns a <see cref="IList{T}"/> of <see langword="interface"/> <see cref="Type"/>s that can be used with the service
/// </summary>
/// <returns>A <see cref="IList{T}"/> of <see langword="interface"/> <see cref="Type"/>s that can be used with the service</returns>
static IList<Type> 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();
}
/// <summary>
/// Sets the function called when a remote login fails due to the server having an invalid SSL cert
/// </summary>
@@ -233,9 +183,10 @@ namespace TGS.Interface
/// <inheritdoc />
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<T>()
{
var ToT = typeof(T);
if (!ValidInstanceInterfaces.Contains(ToT))
throw new Exception("Invalid type!");
return GetComponentImpl<T>(true);
}
@@ -292,8 +241,6 @@ namespace TGS.Interface
public T GetServiceComponent<T>()
{
var ToT = typeof(T);
if (!ValidServiceInterfaces.Contains(ToT))
throw new Exception("Invalid type!");
return GetComponentImpl<T>(false);
}
@@ -305,7 +252,7 @@ namespace TGS.Interface
/// <returns>The correct <see cref="ChannelFactory{TChannel}"/></returns>
ChannelFactory<T> CreateChannel<T>(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<T>(instanceName, accessPath);
return CreateRemoteChannel<T>(instanceName, accessPath);
@@ -324,7 +271,7 @@ namespace TGS.Interface
{
var interfaceName = typeof(T).Name;
var res2 = new ChannelFactory<T>(
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;
+1
View File
@@ -62,6 +62,7 @@
<Compile Include="Components\Instance.cs" />
<Compile Include="Components\InstanceManager.cs" />
<Compile Include="Components\Landing.cs" />
<Compile Include="Definitions.cs" />
<Compile Include="Enumerations.cs" />
<Compile Include="Helpers.cs" />
<Compile Include="InstanceMetadata.cs" />
+19 -9
View File
@@ -198,9 +198,11 @@ namespace TGS.Server
/// </summary>
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);