/// <summary> /// Read plugin objects created by Server's application /// </summary> public void RefreshPlugins() { PluginObjects.Clear(); TcpAppCommandResult result = ExecuteTcpAppCommand("Plugins?"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Failed to get plugins list from server! " + result.ReturnMessage); } if (result.ReturnMessage.Equals("-NONE-")) { return; } PluginObjects.AddRange(result.ReturnMessage.Split('\r').Select(x => x.Split('(')[0].Trim()).ToArray()); }
/// <summary> /// Execute TCP Application Client Command. /// </summary> /// <param name="command"></param> /// <param name="timeout"></param> /// <returns></returns> private TcpAppCommandResult ExecuteTcpAppCommand(string command, int timeout = 1000) { TcpAppCommandResult result = new TcpAppCommandResult(); try { SuspendDataReceivedEvent = true; string commandKeyword = command.Split(' ').First(); //Verify command registered in function list, only active after Connect() sequence completed. if (Initialized) { //Compare command keyword if (!Commands.Contains(commandKeyword, StringComparer.InvariantCultureIgnoreCase)) { //Compare plugin object list if (!PluginObjects.Contains(commandKeyword, StringComparer.InvariantCultureIgnoreCase)) { RefreshPlugins(); //Get latest plugin objects from server if (!PluginObjects.Contains(commandKeyword, StringComparer.InvariantCultureIgnoreCase)) { throw new TcpAppClientException("Invalid Command: " + commandKeyword); //Still no match, FAILED! } } } } string tcpCommand = command + TcpAppCommon.Delimiter; CommandSend?.Invoke(this, new TcpAppClientEventArgs(command)); FlushInputBuffer(); Write(tcpCommand); DateTime startTime = DateTime.Now; while ((DateTime.Now - startTime).TotalMilliseconds < timeout) { string response = ReadString(); ResponseReceived?.Invoke(this, new TcpAppClientEventArgs(response)); if (!string.IsNullOrEmpty(response)) { string[] resultParams = response.Split(' '); //BUSY and Queue statue handle by application result.Status = (TcpAppCommandStatus)Enum.Parse(typeof(TcpAppCommandStatus), resultParams[0]); if (resultParams.Length > 1) { result.ReturnMessage = string.Join(" ", resultParams.Skip(1)).Trim(); //Remove trailing CRLF } return(result); } Thread.Sleep(100); //Wait 100ms, retry. }//while throw new TcpAppClientException("TIMEOUT: No response received from server!"); } catch (TcpAppClientException) { throw; } catch (Exception ex) { throw new TcpAppClientException("Exception raised!", ex); } finally { SuspendDataReceivedEvent = false; } }