Exemple #1
0
 /// <summary>
 /// Sends a custom message to the other process and awaits the result
 /// </summary>
 /// <param name="data">The item to transmit</param>
 /// <param name="preEmitHandler">A method invoked prior to sending the message, while the send lock is being held</param>
 /// <param name="postEmitHandler">A method invoked after sending the message, while the send lock is being held</param>
 public Task <RequestHandlerResponse> SendAndWaitAsync(Type[] types, object[] data, Func <Task> preEmitHandler = null, Func <Task> postEmitHandler = null)
 {
     return(m_connection.SendAndWaitAsync(types, data, preEmitHandler, postEmitHandler));
 }
Exemple #2
0
 public static async Task <object> GenericInvokeHelper <T>(ClientServerTester setup, InterProcessConnection con, T value)
 {
     return((object)await setup.GuardedOperationAsync(() => con.SendAndWaitAsync <T, T>(value)));
 }
Exemple #3
0
 public static async Task <RequestHandlerResponse> GenericInvokeHelper2 <T1, T2>(ClientServerTester setup, InterProcessConnection con, T1 v1, T2 v2)
 {
     return(await setup.GuardedOperationAsync(() => con.SendAndWaitAsync(new Type[] { typeof(T1), typeof(T2) }, new object[] { v1, v2 })));
 }
Exemple #4
0
        /// <summary>
        /// Invokes a method remotely, and returns the result
        /// </summary>
        /// <returns>The result of the remote invocation.</returns>
        /// <param name="handle">The remote instance handle, or zero for null.</param>
        /// <param name="remotetype">A type descriptor for the type the method is invoked on.</param>
        /// <param name="remotemethod">A serialized description of the method to invoke.</param>
        /// <param name="types">The types of the input arguments.</param>
        /// <param name="arguments">The values given to the method when invoked.</param>
        /// <param name="isWrite">If set to <c>true</c> this is a write field or property request.</param>
        public async Task <object> InvokeRemoteMethodAsync(long handle, string remotetype, string remotemethod, Type[] types, object[] arguments, bool isWrite)
        {
            if (string.IsNullOrWhiteSpace(remotetype))
            {
                throw new ArgumentNullException(remotetype);
            }
            if (string.IsNullOrWhiteSpace(remotemethod))
            {
                throw new ArgumentNullException(remotemethod);
            }

            types     = types ?? new Type[0];
            arguments = arguments ?? new object[0];

            if (types.Length != arguments.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(types), $"The length of  {nameof(types)} must be the same as the length of {nameof(arguments)}");
            }

            // Register each local reference item with the remote
            for (var i = 0; i < arguments.Length; i++)
            {
                var arg = arguments[i];
                if (arg == null)
                {
                    continue;
                }

                var tp = arg.GetType();

                var action = m_typeSerializer.GetAction(tp);
                if (action == SerializationAction.Fail)
                {
                    var ta = m_typeSerializer.GetAction(types[i]);
                    if (ta != SerializationAction.Fail)
                    {
                        action = ta;
                        tp     = types[i];
                    }
                }

                if (action == SerializationAction.Fail)
                {
                    throw new Exception($"Cannot pass item with type {arg.GetType()} ({types[i]})");
                }

                if (m_preSendHooks.Count > 0)
                {
                    await Task.WhenAll(m_preSendHooks.Select(x => x(arg, tp)));
                }

                if (action == SerializationAction.Reference || m_remoteObjects.IsLocalObject(arg))
                {
                    await RegisterLocalObjectOnRemote(arg, tp);
                }
            }

            var res = await m_connection.SendAndWaitAsync(
                Command.InvokeRemoteMethod,
                new InvokeRemoteMethodRequest(handle, remotetype, remotemethod, isWrite, types, arguments)
                );

            if (res.Types.Length == 0 || res.Types[0] == typeof(void))
            {
                return(null);
            }

            var resp = (InvokeRemoteMethodResponse)res.Values[0];

            if (resp.ResultType == typeof(void) || resp.ResultType == typeof(Task))
            {
                return(null);
            }
            if (resp.ResultType.IsConstructedGenericType && resp.ResultType.GetGenericTypeDefinition() == typeof(Task <>))
            {
                return(resp.Result);
            }

            return(resp.Result);
        }