/// <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) { if (!Commands.Contains(commandKeyword, StringComparer.InvariantCultureIgnoreCase)) { throw new TcpAppClientException("Invalid Command: " + commandKeyword); } } string tcpCommand = command + TcpAppCommon.Delimiter; CommandSend?.Invoke(this, new TcpAppEventArgs(command)); Write(tcpCommand); DateTime startTime = DateTime.Now; while ((DateTime.Now - startTime).TotalMilliseconds < timeout) //while(true) { string response = ReadString(); ResponseReceived?.Invoke(this, new TcpAppEventArgs(response)); string[] resultParams = response.Split(' '); if ((string.Compare(resultParams[0], commandKeyword, true) == 0)) { result.Status = (TcpAppCommandStatus)Enum.Parse(typeof(TcpAppCommandStatus), resultParams[1]); if (resultParams.Length > 2) { result.ReturnMessage = string.Join(" ", resultParams.Skip(2)).Trim(); //Remove trailing CRLF } return(result); } Thread.Sleep(100); //Wait 100ms, retry. }//while } catch (TcpAppClientException) { throw; } catch (Exception ex) { throw new TcpAppClientException("Exception raised!", ex); } finally { SuspendDataReceivedEvent = false; } throw new TcpAppClientException("TIMEOUT: No response received from server!"); }
/// <summary> /// Signout and disconnect client. /// </summary> public override void Disconnect() { if (base.Connected) { TcpAppCommandResult result = ExecuteTcpAppCommand("SignOut"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Failed to SignOut client! " + result.ReturnMessage); } } ConnectionID = string.Empty; Initialized = false; base.Disconnect(); }
/// <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> /// Connect and signin to <see cref="TcpAppServer"/> /// </summary> public override void Connect() { base.Connect(); Thread.Sleep(50); TcpAppCommandResult result = ExecuteTcpAppCommand("SignIn " + (Initialized ? ConnectionID : Environment.MachineName), 3000);; if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Initialization failed! " + result.ReturnMessage); } ConnectionID = result.ReturnMessage; if (!Initialized) { //First time login //TcpAppServer API Version Check - Always recommend user to use library from same major release. result = ExecuteTcpAppCommand("Version?"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Initialization failed! " + result.ReturnMessage); } TcpAppServerVersion = new Version(result.ReturnMessage); if (TcpAppServerVersion.Major > Version.Major) { Trace.WriteLine("WARNING: Server application created with newer library, version " + TcpAppServerVersion.ToString() + ". Some feature might not be available."); } result = ExecuteTcpAppCommand("FunctionList?"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Initialization failed! " + result.ReturnMessage); } Commands.AddRange(result.ReturnMessage.Split(' ')); RefreshPlugins(); Trace.WriteLine("TCP Application Connection Ready."); Initialized = true; } }
/// <summary> /// Connect to TCP server and initialize TCP Application. /// </summary> public override void Connect() { Initialized = false; if (!Connected) { base.Connect(); Thread.Sleep(50); } TcpAppCommandResult result = ExecuteTcpAppCommand("TcpAppInit", 3000); string[] param = result.ReturnMessage.Trim().Split(' '); ServerAppName = param[0]; ServerAppVersion = param[1]; result = ExecuteTcpAppCommand("TcpAppVersion?"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Initialization failed! " + result.ReturnMessage); } TcpAppServerVersion = new Version(result.ReturnMessage); //Version Check - Always recommend user to use library from same major release. if (TcpAppServerVersion.Major > Version.Major) { Trace.WriteLine("WARNING: Server application created with newer library, version " + TcpAppServerVersion.ToString() + ". Some feature might not be available."); } result = ExecuteTcpAppCommand("GetFunctionList"); if (result.Status == TcpAppCommandStatus.ERR) { throw new TcpAppClientException("Initialization failed! " + result.ReturnMessage); } Commands.AddRange(result.ReturnMessage.Split(' ')); Trace.WriteLine("TcpApp Initialized"); Initialized = true; }
/// <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; } }