/// <summary>
        /// Reads incoming command requests and processes them. See <see cref="DuetAPI.Commands"/> namespace for a list
        /// of supported commands. The actual implementations can be found in <see cref="Commands"/>.
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public override async Task Process()
        {
            do
            {
                try
                {
                    // Read another command
                    DuetAPI.Commands.BaseCommand command = await Connection.ReceiveCommand();

                    if (command == null)
                    {
                        break;
                    }

                    if (!SupportedCommands.Contains(command.GetType()))
                    {
                        throw new ArgumentException($"Invalid command {command.Command} (wrong mode?)");
                    }

                    // Execute it and send back the result
                    object result = await command.Invoke();

                    await Connection.SendResponse(result);
                }
                catch (Exception e)
                {
                    if (Connection.IsConnected)
                    {
                        // Inform the client about this error
                        await Connection.SendResponse(e);

                        Console.WriteLine(e);
                    }
                    else
                    {
                        throw;
                    }
                }
            } while (Connection.IsConnected);
        }
Esempio n. 2
0
        /// <summary>
        /// Reads incoming command requests and processes them. See <see cref="DuetAPI.Commands"/> namespace for a list
        /// of supported commands. The actual implementations can be found in <see cref="Commands"/>.
        /// </summary>
        /// <returns>Asynchronous task</returns>
        public override async Task Process()
        {
            DuetAPI.Commands.BaseCommand command = null;
            do
            {
                try
                {
                    // Read another command
                    command = await Connection.ReceiveCommand();

                    if (command == null)
                    {
                        break;
                    }

                    // Check if it is supported at all
                    if (!SupportedCommands.Contains(command.GetType()))
                    {
                        throw new ArgumentException($"Invalid command {command.Command} (wrong mode?)");
                    }

                    // Execute it and send back the result
                    object result = await command.Invoke();

                    await Connection.SendResponse(result);
                }
                catch (Exception e)
                {
                    // Send errors back to the client
                    if (!(e is OperationCanceledException))
                    {
                        Console.WriteLine($"[err] Failed to execute command {command.Command}: {e}");
                    }
                    await Connection.SendResponse(e);
                }
            }while (!Program.CancelSource.IsCancellationRequested);
        }