Moving RpcAttribute into networking project

This commit is contained in:
Chris Cameron
2018-02-02 14:38:58 -05:00
parent 38584ff565
commit 3a693d2e71
4 changed files with 0 additions and 241 deletions

View File

@@ -1,9 +0,0 @@
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Attributes
{
[TestFixture]
public sealed class KrangPluginAttributeTest : AbstractIcdAttributeTest
{
}
}

View File

@@ -1,55 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using ICD.Common.Utils.Attributes.Rpc;
using NUnit.Framework;
namespace ICD.Common.Utils.Tests.Attributes.Rpc
{
[TestFixture]
public sealed class RpcAttributeTest : AbstractIcdAttributeTest
{
private class TestClass
{
[Rpc("A")]
public string TestProperty { get; set; }
[Rpc("A")]
public void TestMethod()
{
}
[Rpc("B")]
public void TestMethod(int a)
{
}
[Rpc("C")]
public void TestMethod(int a, string b)
{
}
}
[Test]
public void GetMethodTest()
{
TestClass instance = new TestClass();
Assert.NotNull(RpcAttribute.GetMethod(instance, "A", new object[] { }));
Assert.Null(RpcAttribute.GetMethod(instance, "B", new object[] { }));
Assert.NotNull(RpcAttribute.GetMethod(instance, "B", new object[] { 1 }));
Assert.NotNull(RpcAttribute.GetMethod(instance, "C", new object[] { 1, null }));
Assert.NotNull(RpcAttribute.GetMethod(instance, "C", new object[] { 1, "test" }));
}
[Test]
public void GetPropertyTest()
{
TestClass instance = new TestClass();
Assert.NotNull(RpcAttribute.GetProperty(instance, "A", "test"));
Assert.NotNull(RpcAttribute.GetProperty(instance, "A", null));
Assert.Null(RpcAttribute.GetProperty(instance, "B", "test"));
Assert.Null(RpcAttribute.GetProperty(instance, "B", 1));
}
}
}

View File

@@ -1,176 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ICD.Common.Properties;
using ICD.Common.Utils.Collections;
using ICD.Common.Utils.Extensions;
#if SIMPLSHARP
using Crestron.SimplSharp.Reflection;
#else
using System.Reflection;
#endif
namespace ICD.Common.Utils.Attributes.Rpc
{
/// <summary>
/// Represents a method that can be called by the server via RPC.
/// </summary>
[PublicAPI]
[MeansImplicitUse]
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class RpcAttribute : AbstractIcdAttribute
{
private static readonly Dictionary<Type, Dictionary<string, IcdHashSet<MethodInfo>>> s_MethodCache;
private static readonly Dictionary<Type, Dictionary<string, IcdHashSet<PropertyInfo>>> s_PropertyCache;
private static readonly SafeCriticalSection s_MethodCacheSection;
private static readonly SafeCriticalSection s_PropertyCacheSection;
private readonly string m_Key;
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
static RpcAttribute()
{
s_MethodCache = new Dictionary<Type, Dictionary<string, IcdHashSet<MethodInfo>>>();
s_PropertyCache = new Dictionary<Type, Dictionary<string, IcdHashSet<PropertyInfo>>>();
s_MethodCacheSection = new SafeCriticalSection();
s_PropertyCacheSection = new SafeCriticalSection();
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="key"></param>
public RpcAttribute(string key)
{
m_Key = key;
}
#endregion
#region Methods
/// <summary>
/// Gets the method on the client with the given key, matching the parameter types.
/// </summary>
/// <param name="client"></param>
/// <param name="key"></param>
/// <param name="parameters"></param>
/// <returns></returns>
[CanBeNull]
public static MethodInfo GetMethod(object client, string key, IEnumerable<object> parameters)
{
return GetMethods(client, key).FirstOrDefault(m => ReflectionUtils.MatchesMethodParameters(m, parameters));
}
/// <summary>
/// Gets the property on the client with the given key, matching the parameter type.
/// </summary>
/// <param name="client"></param>
/// <param name="key"></param>
/// <param name="parameter"></param>
/// <returns></returns>
[CanBeNull]
public static PropertyInfo GetProperty(object client, string key, object parameter)
{
return GetProperties(client, key).FirstOrDefault(p => ReflectionUtils.MatchesPropertyParameter(p, parameter));
}
#endregion
#region Private Methods
/// <summary>
/// Returns the methods on the client with the given key.
/// </summary>
/// <param name="client"></param>
/// <param name="key"></param>
/// <returns></returns>
private static IEnumerable<MethodInfo> GetMethods(object client, string key)
{
s_MethodCacheSection.Enter();
try
{
Type clientType = client.GetType();
// Cache the methods for the key so subsequent calls are faster.
if (!s_MethodCache.ContainsKey(clientType))
s_MethodCache[clientType] = new Dictionary<string, IcdHashSet<MethodInfo>>();
if (!s_MethodCache[clientType].ContainsKey(key))
{
s_MethodCache[clientType][key] =
clientType
#if SIMPLSHARP
.GetCType()
#else
.GetTypeInfo()
#endif
.GetMethods(BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance)
.Where(m => ReflectionExtensions.GetCustomAttributes<RpcAttribute>(m, true)
.Any(a => a.m_Key == key))
.ToIcdHashSet();
}
return s_MethodCache[clientType][key];
}
finally
{
s_MethodCacheSection.Leave();
}
}
/// <summary>
/// Returns the properties on the client with the given key.
/// </summary>
/// <param name="client"></param>
/// <param name="key"></param>
/// <returns></returns>
private static IEnumerable<PropertyInfo> GetProperties(object client, string key)
{
s_PropertyCacheSection.Enter();
try
{
Type clientType = client.GetType();
// Cache the properties for the key so subsequent calls are faster.
if (!s_PropertyCache.ContainsKey(clientType))
s_PropertyCache[clientType] = new Dictionary<string, IcdHashSet<PropertyInfo>>();
if (!s_PropertyCache[clientType].ContainsKey(key))
{
s_PropertyCache[clientType][key] =
clientType
#if SIMPLSHARP
.GetCType()
#else
.GetTypeInfo()
#endif
.GetProperties(BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance)
.Where(p => p.CanWrite && ReflectionExtensions.GetCustomAttributes<RpcAttribute>(p, true)
.Any(a => a.m_Key == key))
.ToIcdHashSet();
}
return s_PropertyCache[clientType][key];
}
finally
{
s_PropertyCacheSection.Leave();
}
}
#endregion
}
}

View File

@@ -74,7 +74,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Attributes\AbstractIcdAttribute.cs" /> <Compile Include="Attributes\AbstractIcdAttribute.cs" />
<Compile Include="Attributes\Rpc\RpcAttribute.cs" />
<Compile Include="ConsoleColor.cs" /> <Compile Include="ConsoleColor.cs" />
<Compile Include="EventArguments\BoolEventArgs.cs" /> <Compile Include="EventArguments\BoolEventArgs.cs" />
<Compile Include="EventArguments\CharEventArgs.cs" /> <Compile Include="EventArguments\CharEventArgs.cs" />