/// <summary> /// Execute Plugin Callback. Call by ITcpAppServerPlugin /// </summary> /// <param name="commandArguments">Command keyword and arguments in string array.</param> public TcpAppInputCommand GetPluginCommand(string [] commandArguments) { if (commandArguments == null) { throw new ArgumentNullException(nameof(commandArguments), "Invalid command / empty string!"); } TcpAppInputCommand command = TcpAppCommon.CreateInputCommand(Commands, commandArguments); if (command == null) { throw new ArgumentException("Unknown command: " + commandArguments.FirstOrDefault()); } return(command); }
private void Client_ProcessReceivedMessage(TcpServerConnection client, string message, byte[] messageBytes) { //Parse and Execute Commands string[] cmdArg = TcpAppCommon.ParseCommand(message.Trim()); try { //Register Client Connection TcpAppServerConnection ptrClient = AppClients.FirstOrDefault(x => x.Connection == client); if (ptrClient == null) { //Reconstruct device which had already signed out ptrClient = AddClientToAppClientsList(client); } Debug.WriteLine("AppServer[" + ptrClient.Name + "]-RX: " + message); TcpAppInputCommand inputCommand = TcpAppCommon.CreateInputCommand(Commands, cmdArg); if (inputCommand != null) { inputCommand.AppClient = ptrClient; } else//Command keyword not exist { //Check if command keyword is alias name ITcpAppServerPlugin plugin = _Plugins.FirstOrDefault(x => string.Compare(x.Alias, cmdArg[0], true) == 0); if (plugin != null) { //Execute plugin command inputCommand = plugin.GetPluginCommand(cmdArg.Skip(1).ToArray()); inputCommand.AppClient = ptrClient; BeforeExecutePluginCommand?.Invoke(this, new TcpAppServerExEventArgs(ptrClient) { Plugin = plugin }); } else { //Error - Unrecognized command. inputCommand = new TcpAppInputCommand() { Status = TcpAppCommandStatus.ERR, OutputMessage = "Invalid Command " + cmdArg[0] }; WriteResultToClient(ptrClient, inputCommand); return; } } //Verify Client had signed in. if (!inputCommand.AppClient.SignedIn && !inputCommand.Command.IsSystemCommand) { throw new Exception("Client not signed in! Execute SignIn first."); } if (inputCommand.Command.UseMessageQueue && !inputCommand.Command.IsSystemCommand) { //Single thread execution, post message to message queue. lock (CommandQueue) { inputCommand.ID = inputCommand.GetHashCode(); if (ptrClient.NextQueuedCommand == null) { ptrClient.NextQueuedCommand = inputCommand; //Set pointer to next queued command. } //Add Command to Queue CommandQueue.Add(inputCommand); CommandQueueWaitSignal?.Set(); inputCommand.OutputMessage = inputCommand.ID.ToString(); inputCommand.Status = TcpAppCommandStatus.QUEUED; } } else if (inputCommand.Command.IsSystemCommand) { inputCommand.ExecuteCallback(); } else { //Execute command, wait until return if (ExecutionTimeout == 0) { inputCommand.ExecuteCallback(); } //Execute command, terminate on timeout else { ptrClient.ExecuteCommandAsync(inputCommand, ExecutionTimeout); } } WriteResultToClient(ptrClient, inputCommand); //Send result back to client. } catch (Exception ex) { WriteExceptionErrorToClient(client, ex); } }