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. } }
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); } }