public bool TryProcess(CommandProcessorContext context, string[] args) { if (args == null || args.Length == 0) { _log.Error("Empty command"); throw new Exception("Empty command"); } var commandName = args[0].ToUpper(); var commandArgs = args.Skip(1).ToArray(); ICommandProcessor commandProcessor; if (!_processors.TryGetValue(commandName, out commandProcessor)) { _log.Info("Unknown command: '{0}'", commandName); new UsageProcessor(this).Execute(context, CancellationToken.None, new string[0]); return false; } var result = false; var timeout = context.Client.Options.Timeout; using (var source = new CancellationTokenSource()) { try { var token = source.Token; if (timeout > 0) { result = new WaitFor<bool>(TimeSpan.FromSeconds(timeout)).Run( () => commandProcessor.Execute(context, token, commandArgs)); } else { result = commandProcessor.Execute(context, token,commandArgs); } } catch (TimeoutException ex) { _log.Error("Command didn't finish in {0} seconds", timeout); } catch (Exception exc) { _log.ErrorException(exc, "Failure while processing {0}", commandName); } finally { source.Cancel(); } } return result; }
bool ExecuteLine(string line) { try { var args = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); //Log.Info("Processing command: {0}.", string.Join(" ", args)); var context = new CommandProcessorContext(this, Log); return _commands.TryProcess(context, args); } catch (Exception exception) { //Log.ErrorException(exception, "Error while executing command"); return false; } }