public static TcpAppInputCommand CreateInputCommand(List <TcpAppCommand> commandList, string[] commandArguments) { TcpAppInputCommand result = null; //Process Command Keyword TcpAppCommand cmdHandler = commandList.FirstOrDefault(x => x.Keyword.Equals(commandArguments[0], StringComparison.InvariantCultureIgnoreCase)); if (cmdHandler == null) { return(null); } result = new TcpAppInputCommand() { Command = cmdHandler.Clone() as TcpAppCommand }; result.Arguments = commandArguments.Skip(1).ToArray(); //Arguments exclude command keyword //Process Parameters cmdHandler.ResetParametersValue(); int argID = 0; //First Parameter foreach (TcpAppParameter item in cmdHandler.Parameters) { if (argID >= result.Arguments.Length) { //Argument with no input if (!item.IsOptional) { //Error - Missing required parameter throw new ArgumentException("Missing required parameter: " + item.Name + "!"); } } else if (item.IsArray) { item.Values.Clear(); //Parameter Array is last parameters consume all arguments in command for (int m = argID; m < result.Arguments.Length; m++) { item.Values.Add(result.Arguments[m]); } break; } else { item.Value = result.Arguments[argID]; //Assign parameter value } argID++; } return(result); }
/// <summary> /// Execute Plugin Callback. Call by ITcpAppServerPlugin /// </summary> /// <param name="sender"></param> public void ExecutePluginCommand(TcpAppInputCommand sender) { sender.Status = TcpAppCommandStatus.ERR; string[] cmdArg = sender.Arguments.Skip(1).ToArray(); //Process Command Keyword TcpAppCommand cmdHandler = GetCommand(cmdArg[0]); if (cmdHandler == null) { //Error - Unrecognized command. sender.OutputMessage = string.Format("Invalid Command: {0}!", cmdArg[0]); return; } sender.Command = cmdHandler; //Process Parameters cmdHandler.ResetParametersValue(); int argID = 1; //First Parameter foreach (TcpAppParameter item in cmdHandler.Parameters) { if (argID >= cmdArg.Length) { //Argument with no input if (!item.IsOptional) { //Error - Missing required parameter sender.OutputMessage = "Missing required parameter: " + item.Name + "!"; return; } } else { item.Value = cmdArg[argID]; //Assign parameter value } argID++; } //Execute Command. //Note: Error handling not required. Will handle by TcpAppServer class. sender.Command.ExecuteCallback(sender); }
private void Client_ProcessReceivedMessage(TcpServerConnection client, string message, byte[] messageBytes) { //Parse and Execute Commands string[] cmdArg = TcpAppCommon.ParseCommand(message.Trim()); //Process Command Keyword TcpAppCommand cmdHandler = GetCommand(cmdArg[0]); if (cmdHandler == null) { //Error - Unrecognized command. client.WriteLineToClient(string.Format("{0} {1} Invalid Command!", cmdArg[0], TcpAppCommandStatus.ERR.ToString())); return; } TcpAppInputCommand cmdInput = new TcpAppInputCommand() { Command = cmdHandler }; cmdInput.Arguments = cmdArg.Skip(1).ToArray(); //Move to TcpAppInputCommand //Process Parameters cmdHandler.ResetParametersValue(); int argID = 1; //First Parameter foreach (TcpAppParameter item in cmdHandler.Parameters) { if (argID >= cmdArg.Length) { //Argument with no input if (!item.IsOptional) { //Error - Missing required parameter cmdInput.OutputMessage = "Missing required parameter: " + item.Name + "!"; WriteResultToClient(client, cmdInput); return; } } else { item.Value = cmdArg[argID]; //Assign parameter value } argID++; } //Execute Commands try { cmdInput.Command.ExecuteCallback(cmdInput); } catch (Exception ex) { //Catch and report all execution error cmdInput.OutputMessage = "Exception Raised! " + ex.Message; cmdInput.Status = TcpAppCommandStatus.ERR; //Force status to error, make sure no surprise. } finally { WriteResultToClient(client, cmdInput); //Send result back to client. } }