public void StartTcp() { //if(!ms.ConnectClient()) if (!ms.ConnectAsync()) { Console.WriteLine(ms.ErroerMessage); } System.Threading.Thread.Sleep(2000); for (int i = 0; i < 10; i++) { if (!ms.SendData("it is test data", Encoding.UTF8)) { Console.WriteLine(ms.ErroerMessage); } } Console.WriteLine("any key to disconnect"); Console.ReadKey(); for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(10); if (!ms.SendData("it is test data", Encoding.UTF8)) { Console.WriteLine(ms.ErroerMessage); } } for (int i = 0; i < 10; i++) { Console.ReadKey(); byte[] bs = ms.ReceiveAllData(); if (bs != null) { ms_OnReceiveData(bs); } } //ms.disConnectClient(); }
public MyExecutionDeviceResult ExecutionDeviceRun(ICaseExecutionContent yourExecutionContent, CaseActionActuator.delegateGetExecutiveData yourExecutiveDelegate, string sender, ActuatorStaticDataCollection yourActuatorStaticDataCollection, int caseId) { List <string> errorList = new List <string>(); string tempError = null; MyExecutionDeviceResult myResult = new MyExecutionDeviceResult(); myResult.staticDataResultCollection = new System.Collections.Specialized.NameValueCollection(); //向UI推送执行过程信息 Action <string, CaseActuatorOutPutType, string> ExecutiveDelegate = (innerSender, outType, yourContent) => { if (yourExecutiveDelegate != null) { yourExecutiveDelegate(innerSender, outType, yourContent); } }; //处理执行错误(执行器无法执行的错误) Action <string> DealExecutiveError = (errerData) => { if (errerData != null) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, errerData); errorList.Add(errerData); } }; if (yourExecutionContent.MyCaseProtocol == CaseProtocol.tcp) { //在调用该函数前保证nowExecutionContent.ErrorMessage为空,且as一定成功 MyTcpExecutionContent nowExecutionContent = yourExecutionContent as MyTcpExecutionContent; myResult.caseProtocol = CaseProtocol.mysql; myResult.caseTarget = nowExecutionContent.MyExecutionTarget; myResult.startTime = DateTime.Now.ToString("HH:mm:ss"); StringBuilder tempCaseOutContent = new StringBuilder(); System.Diagnostics.Stopwatch myWatch = new System.Diagnostics.Stopwatch(); myWatch.Start(); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, string.Format("【ID:{0}】[tcp]Executive···", caseId)); #region Send if (nowExecutionContent.isSend) { string nowTcpData = nowExecutionContent.tcpContentToSend.GetTargetContentData(yourActuatorStaticDataCollection, myResult.staticDataResultCollection, out tempError); if (tempError != null) { DealExecutiveError(string.Format("this case get static data errer with [{0}]", nowExecutionContent.tcpContentToSend.GetTargetContentData())); tempCaseOutContent.AppendLine("error with static data"); } else { byte[] nowSendBytes; if (nowExecutionContent.tcpSendEncoding == null) { try { nowSendBytes = MyBytes.HexStringToByte(nowTcpData, HexaDecimal.hex16, ShowHexMode.space); } catch { nowSendBytes = null; } } else { try { nowSendBytes = nowExecutionContent.tcpSendEncoding.GetBytes(nowTcpData); } catch { nowSendBytes = null; } } if (nowSendBytes == null) { DealExecutiveError(string.Format("can not change data to bytes with [{0}]", nowExecutionContent.tcpContentToSend.GetTargetContentData())); tempCaseOutContent.AppendLine("error with tcp data"); } else { if (myTcpClient.SendData(nowSendBytes)) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, "send sucess"); tempCaseOutContent.AppendLine("send sucess"); } else { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, myTcpClient.ErroerMessage); tempCaseOutContent.AppendLine(myTcpClient.ErroerMessage); } } } } #endregion #region receive if (nowExecutionContent.isReceive) { if (nowExecutionContent.tcpSleepTime > 0) { System.Threading.Thread.Sleep(nowExecutionContent.tcpSleepTime); } byte[] recweiveBytes = myTcpClient.ReceiveAllData(); if (recweiveBytes != null) { string receiveStr; if (nowExecutionContent.tcpReceiveEncoding == null) { receiveStr = MyBytes.ByteToHexString(recweiveBytes, HexaDecimal.hex16, ShowHexMode.space); } else { try { receiveStr = nowExecutionContent.tcpReceiveEncoding.GetString(recweiveBytes); } catch { receiveStr = null; } } if (receiveStr != null) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, receiveStr); tempCaseOutContent.AppendLine(receiveStr); } else { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, string.Format("can not Encoding your data with {0}", MyBytes.ByteToHexString(recweiveBytes, HexaDecimal.hex16, ShowHexMode.space))); tempCaseOutContent.AppendLine("[error]receive data error can not encoding receive data"); } } } #endregion myWatch.Stop(); myResult.spanTime = myResult.requestTime = myWatch.ElapsedMilliseconds.ToString(); myResult.backContent = tempCaseOutContent.ToString(); } else { myResult.backContent = "error:your CaseProtocol is not Matching RunTimeActuator"; DealExecutiveError(myResult.backContent); } if (errorList.Count > 0) { myResult.additionalError = errorList.MyToString("\r\n"); } return(myResult); }