/// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            // there must be a separator in the method name.
            if (!invocationDescriptor.MethodName.Contains(Separator))
            {
                throw new Exception($"Invalid controller or method name '{invocationDescriptor.MethodName}'.");
            }

            // find the controller and the method name.
            string[] names      = invocationDescriptor.MethodName.Split(Separator);
            string   controller = names[0].ToLower();
            string   command    = Prefix + names[1];

            // find the desired controller.
            if (Controllers.TryGetValue(controller, out object self))
            {
                // use reflection to find the method in the desired controller.
                MethodInfo method = self.GetType().GetMethod(command);

                // if the method could not be found:
                if (method == null)
                {
                    throw new Exception($"Received unknown command '{command}' for controller '{controller}'.");
                }

                // optionally insert client as parameter.
                List <object> args = invocationDescriptor.Arguments.ToList();
                if (!NoWebsocketArgument)
                {
                    args.Insert(0, socket);
                }

                // call the method asynchronously.
                try
                {
                    return(await Task.Run(() => method.Invoke(self, args.ToArray())));
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
            }
            else
            {
                throw new Exception($"Received command '{command}' for unknown controller '{controller}'.");
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            // create the method name that has to be found.
            string command = Prefix + invocationDescriptor.MethodName;

            // use reflection to find the method in the desired controller.
            MethodInfo method = Controller.GetType().GetMethod(command);

            // if the method could not be found:
            if (method == null)
            {
                throw new Exception($"Received unknown command '{command}' for controller '{Controller.GetType().Name}'.");
            }

            // optionally insert client as parameter.
            List <object> args = invocationDescriptor.Arguments.ToList();

            if (!NoWebsocketArgument)
            {
                args.Insert(0, socket);
            }

            // call the method asynchronously.
            try
            {
                return(await Task.Run(() => method.Invoke(Controller, args.ToArray())));
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Called when an invoke method call has been received.
 /// </summary>
 /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
 /// <param name="invocationDescriptor">
 /// The invocation descriptor containing the method name and parameters.
 /// </param>
 /// <returns>Awaitable Task.</returns>
 public virtual Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
 {
     throw new NotImplementedException();
 }
Esempio n. 4
0
        /// <summary>
        /// Called when an invoke method call has been received.
        /// </summary>
        /// <param name="socket">The web-socket of the client that wants to invoke a method.</param>
        /// <param name="invocationDescriptor">
        /// The invocation descriptor containing the method name and parameters.
        /// </param>
        /// <returns>Awaitable Task.</returns>
        public override async Task <object> OnInvokeMethodReceivedAsync(WebSocket socket, InvocationDescriptor invocationDescriptor)
        {
            if (!_handlers.ContainsKey(invocationDescriptor.MethodName))
            {
                throw new Exception($"Received unknown command '{invocationDescriptor.MethodName}'.");
            }
            var invocationHandler = _handlers[invocationDescriptor.MethodName];

            if (invocationHandler != null)
            {
                return(await Task.Run(() => invocationHandler.Handler(invocationDescriptor.Arguments)));
            }
            return(await Task.FromResult <object>(null));
        }