Esempio n. 1
0
 public static object[] DeserializeRpcCallArguments(RpcInfo rpcInfo, BinaryReader reader)
 {
     return(rpcInfo.ParameterTypes
            .Select(parameterType =>
                    Deserialize(
                        reader, parameterType, isNullableIfReferenceType: false,
                        areElementsNullableIfReferenceType: false
                        )
                    )
            .ToArray());
 }
Esempio n. 2
0
        public static void GetRpcInfo(out Dictionary <string, byte> rpcIdByName, out Dictionary <byte, RpcInfo> rpcInfoById)
        {
            rpcIdByName = new Dictionary <string, byte>();
            rpcInfoById = new Dictionary <byte, RpcInfo>();

            var assembly = Assembly.GetExecutingAssembly();

            foreach (var type in assembly.GetTypes())
            {
                var methodBindingFlags =
                    BindingFlags.Public |
                    BindingFlags.NonPublic |
                    BindingFlags.Instance;

                foreach (var methodInfo in type.GetMethods(methodBindingFlags))
                {
                    var rpcAttribute = (RpcAttribute)methodInfo.GetCustomAttributes(typeof(RpcAttribute), inherit: false)
                                       .FirstOrDefault();
                    var parameterInfos = methodInfo.GetParameters();

                    if (rpcAttribute != null)
                    {
                        var rpcInfo = new RpcInfo
                        {
                            Id             = (byte)(1 + rpcInfoById.Count),
                            Name           = methodInfo.Name,
                            ExecuteOn      = rpcAttribute.ExecuteOn,
                            MethodInfo     = methodInfo,
                            ParameterNames = parameterInfos
                                             .Select(parameterInfo => parameterInfo.Name)
                                             .ToArray(),
                            ParameterTypes = parameterInfos
                                             .Select(parameterInfo => parameterInfo.ParameterType)
                                             .ToArray()
                        };

                        rpcIdByName.Add(rpcInfo.Name, rpcInfo.Id);
                        rpcInfoById.Add(rpcInfo.Id, rpcInfo);
                    }
                }
            }
        }
Esempio n. 3
0
        public static byte[] SerializeRpcCall(RpcInfo rpcInfo, object argumentsObj)
        {
            var argumentsType      = argumentsObj.GetType();
            var argumentProperties = argumentsType.GetProperties();

            Assert.IsTrue(argumentProperties.Length == rpcInfo.ParameterTypes.Length);

            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    binaryWriter.Write(rpcInfo.Id);

                    for (var i = 0; i < rpcInfo.ParameterTypes.Length; i++)
                    {
                        var parameterName = rpcInfo.ParameterNames[i];
                        var parameterType = rpcInfo.ParameterTypes[i];

                        var argumentProperty = argumentProperties.First(argField =>
                                                                        argField.Name == parameterName
                                                                        );
                        var argumentType = argumentProperty.PropertyType;
                        var argument     = argumentProperty.GetValue(argumentsObj);

                        Assert.IsTrue(
                            argumentType.IsEquivalentTo(parameterType),
                            $"RPC parameter {parameterName} has type {parameterType.AssemblyQualifiedName} but was passed {argumentType.AssemblyQualifiedName}."
                            );

                        SerializeObject(
                            binaryWriter, argument, argumentType, isNullableIfReferenceType: false,
                            areElementsNullableIfReferenceType: false
                            );
                    }
                }

                return(memoryStream.ToArray());
            }
        }