private void ShowIMessage(IMessage message) { ShowLowLevelInfo(string.Format("NMSDestination : {0} \r\nNMSCorrelationID: {1} \r\nNMSMessageId: {2} \r\nNMSTimestamp: {3} \r\nNMSTimeToLive: {4} \r\nNMSType:{5} \r\nNMSPriority: {6}", message.NMSDestination.ToString(), message.NMSCorrelationID, message.NMSMessageId, message.NMSTimestamp.ToLocalTime(), message.NMSTimeToLive.ToString(), message.NMSType, message.NMSPriority.ToString())); if (message is ITextMessage) { ShowMessage(((ITextMessage)message).Text); } else if (message is IBytesMessage) { ShowMessage("[HEX16:]" + MyBytes.ByteToHexString(((IBytesMessage)message).Content, HexaDecimal.hex16, ShowHexMode.space)); try { ShowMessage(Encoding.UTF8.GetString(((IBytesMessage)message).Content)); } catch { ShowError("chnage to text by utf8 fail"); } } else if (message is IMapMessage) { ShowError("can not show IMapMessage"); } else if (message is IStreamMessage) { ShowError("can not show IStreamMessage"); } else { ShowError("find nuknow IMessage"); } }
private bool WriteRawData(byte[] yourData) { if (!mySocket.Connected) { return(false); } sendDone.WaitOne(); mySocket.BeginSend(yourData, 0, yourData.Length, SocketFlags.None, new AsyncCallback((IAsyncResult ar) => { try { Socket client = (Socket)ar.AsyncState; int bytesSent = client.EndSend(ar); #if INTEST System.Diagnostics.Debug.WriteLine("-------------------------------------"); System.Diagnostics.Debug.WriteLine(string.Format("Sent {0} bytes to server.", bytesSent)); System.Diagnostics.Debug.WriteLine(MyBytes.ByteToHexString(yourData, HexaDecimal.hex16, ShowHexMode.space)); System.Diagnostics.Debug.WriteLine(MyBytes.ByteToHexString(yourData, HexaDecimal.hex10, ShowHexMode.space)); System.Diagnostics.Debug.WriteLine(Encoding.ASCII.GetString(yourData)); System.Diagnostics.Debug.WriteLine(Encoding.UTF8.GetString(yourData)); #endif } catch (Exception ex) { ReportMes(string.Format("error in send data with :{0}", ex.Message), TelnetMessageType.Error); } finally { sendDone.Set(); } }), mySocket); return(true); }
public void TestMethod_HexStringToByte() { string testData_1 = " 0x01 0x02 0x03 0x04 0x05 0x06 0x06 0x00 0xff 0xff"; byte[] result = MyBytes.HexStringToByte(testData_1, HexaDecimal.hex16, ShowHexMode.spitSpace0x); Console.WriteLine(MyBytes.ByteToHexString(result, HexaDecimal.hex2, ShowHexMode.spitSpace0b)); Console.WriteLine(MyBytes.ByteToHexString(result, HexaDecimal.hex10, ShowHexMode.spitSpace0d)); Console.WriteLine(MyBytes.ByteToHexString(result, HexaDecimal.hex16, ShowHexMode.spitSpace0x)); }
public static void DotestForRC4() { Console.WriteLine(MyBytes.StringToHexString("test data for test !@#$%^&*()ZXCVBNM<QWERTYUIOASDFGHJK", Encoding.UTF8, HexaDecimal.hex16, ShowHexMode.space)); Console.WriteLine("RC4 Encrypt"); byte[] data1 = MyRC4.Encrypt("test data for test !@#$%^&*()ZXCVBNM<QWERTYUIOASDFGHJK", "123", Encoding.UTF8); Console.WriteLine(MyBytes.ByteToHexString(data1, HexaDecimal.hex16, ShowHexMode.space)); data1 = MyRC4.Encrypt("test data for test !@#$%^&*()ZXCVBNM<QWERTYUIOASDFGHJK", "123", Encoding.UTF8); Console.WriteLine(MyBytes.ByteToHexString(data1, HexaDecimal.hex16, ShowHexMode.space)); Console.WriteLine(Convert.ToBase64String(data1)); Console.WriteLine("RC4 Decrypt"); byte[] data2 = MyRC4.Decrypt(data1, "123", Encoding.UTF8); Console.WriteLine(MyBytes.ByteToHexString(data2, HexaDecimal.hex16, ShowHexMode.space)); Console.WriteLine(Encoding.UTF8.GetString(data2)); }
private void ReceviData(object yourSocket) { byte[] nowReciveBytes = new byte[1024 * 128]; System.Net.Sockets.Socket nowSocket = (System.Net.Sockets.Socket)yourSocket; int receiveCount = 0; while (true) { if (!nowSocket.Connected) { Console.WriteLine("the tcp is disconnect"); break; } try { receiveCount = nowSocket.Receive(nowReciveBytes); if (receiveCount > 0) { byte[] tempOutBytes = new byte[receiveCount]; Array.Copy(nowReciveBytes, tempOutBytes, receiveCount); //ReportPipeResponse(tempOutBytes); System.Diagnostics.Debug.WriteLine(string.Format("\r\n----------------------{0}------------------------", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff"))); //string respose = Encoding.UTF8.GetString(nowReciveBytes, 0, receiveCount); string respose = MyBytes.ByteToHexString(tempOutBytes, HexaDecimal.hex16, ShowHexMode.space); System.Diagnostics.Debug.Write(respose); } else { Thread.Sleep(10); } } catch (System.Threading.ThreadAbortException) { Console.WriteLine("Applications active close ");//应用程序主动关闭接收线程 nowSocket.Close(); break; } catch (Exception ex) { Console.WriteLine(ex.Message);//应用程序被动关闭接收线程 nowSocket.Close(); break; } finally { } } }
static void DoMyTest() { Console.ReadLine(); TLSPacket.TLSPlaintext tp = new TLSPacket.TLSPlaintext(TLSPacket.TLSContentType.Handshake, new TLSPacket.ProtocolVersion(0x03, 0x01)); string tempRaw = MyBytes.ByteToHexString(tp.GetRawData(198), HexaDecimal.hex16, ShowHexMode.space); Console.WriteLine(tempRaw); TLSPacket.ClientHello ch = new TLSPacket.ClientHello("d.baiwandian.cn"); tempRaw = MyBytes.ByteToHexString(ch.GetProtocolRawData(), HexaDecimal.hex16, ShowHexMode.space); Console.WriteLine(tempRaw); Console.WriteLine("enter to DoMyTest"); Console.ReadLine(); }
/// <summary> /// 获取协商答复 /// </summary> /// <param name="optionBytes">协商</param> /// <returns>答复(无法答复或错误返回null)</returns> private byte[] GetResponseOption(byte[] optionBytes) { byte[] responseOption = new byte[3]; responseOption[0] = IAC; //协商选项命令为3字节,附加选项超过3个 if (optionBytes.Length < 3) { ReportMes(string.Format("error option by errer length with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Error); return(null); } if (optionBytes[0] == IAC) { switch (optionBytes[1]) { //WILL: 发送方本身将激活( e n a b l e )选项 case WILL: if (optionBytes[2] == SHOWBACK || optionBytes[2] == RESTRAIN) { responseOption[2] = optionBytes[2]; responseOption[1] = DO; } else if (optionBytes[2] == TERMINAL) { responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } else { ReportMes(string.Format("unknow Assigned Number with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } break; //DO :发送方想叫接收端激活选项。 case DO: if (optionBytes[2] == SHOWBACK || optionBytes[2] == RESTRAIN) { responseOption[2] = optionBytes[2]; responseOption[1] = WILL; } else if (optionBytes[2] == TERMINAL) { responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } else { ReportMes(string.Format("unknow Assigned Number with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } break; //WONT :发送方本身想禁止选项。 case WONT: if (optionBytes[2] == SHOWBACK || optionBytes[2] == RESTRAIN) { responseOption[2] = optionBytes[2]; responseOption[1] = DONT; } else if (optionBytes[2] == TERMINAL) { responseOption[2] = optionBytes[2]; responseOption[1] = DONT; } else { ReportMes(string.Format("unknow Assigned Number with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } break; //DON’T:发送方想让接收端去禁止选项。 case DONT: if (optionBytes[2] == SHOWBACK || optionBytes[2] == RESTRAIN) { responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } else if (optionBytes[2] == TERMINAL) { responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } else { ReportMes(string.Format("unknow Assigned Number with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); responseOption[2] = optionBytes[2]; responseOption[1] = WONT; } break; //子选项协商 (暂不处理) case SB: ReportMes(string.Format("unsuport SB/SE option with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); return(null); default: ReportMes(string.Format("unknow option with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); responseOption[2] = optionBytes[2]; responseOption[1] = WONT; break; } } else { ReportMes(string.Format("error option by no IAC with :{0}", MyBytes.ByteToHexString(optionBytes, HexaDecimal.hex16, ShowHexMode.space)), TelnetMessageType.Warning); return(null); } return(responseOption); }
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.com) { //在调用该函数前保证nowExecutionContent.ErrorMessage为空,且as一定成功 MyComExecutionContent nowExecutionContent = yourExecutionContent as MyComExecutionContent; myResult.caseProtocol = CaseProtocol.com; 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}】[com]Executive···", caseId)); #region Send if (nowExecutionContent.isSend) { string nowComData = nowExecutionContent.comContentToSend.GetTargetContentData(yourActuatorStaticDataCollection, myResult.staticDataResultCollection, out tempError); if (tempError != null) { DealExecutiveError(string.Format("this case get static data errer with [{0}]", nowExecutionContent.comContentToSend.GetTargetContentData())); tempCaseOutContent.AppendLine("error with static data"); } else { byte[] nowSendBytes; if (nowExecutionContent.comSendEncoding == null) { try { nowSendBytes = MyBytes.HexStringToByte(nowComData, HexaDecimal.hex16, ShowHexMode.space); } catch { nowSendBytes = null; } } else { try { nowSendBytes = nowExecutionContent.comSendEncoding.GetBytes(nowComData); } catch { nowSendBytes = null; } } if (nowSendBytes == null) { DealExecutiveError(string.Format("can not change data to bytes with [{0}]", nowExecutionContent.comContentToSend.GetTargetContentData())); tempCaseOutContent.AppendLine("error with com data"); } else { if (mySerialPort.Send(nowSendBytes)) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, "send sucess"); tempCaseOutContent.AppendLine("send sucess"); } else { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, mySerialPort.ErroerMessage); tempCaseOutContent.AppendLine(mySerialPort.ErroerMessage); } } } } #endregion #region receive if (nowExecutionContent.isReceive) { if (nowExecutionContent.comSleepTime > 0) { System.Threading.Thread.Sleep(nowExecutionContent.comSleepTime); } byte[] recweiveBytes = mySerialPort.ReadAllBytes(); if (recweiveBytes != null) { string receiveStr; if (nowExecutionContent.comReceiveEncoding == null) { receiveStr = MyBytes.ByteToHexString(recweiveBytes, HexaDecimal.hex16, ShowHexMode.space); } else { try { receiveStr = nowExecutionContent.comReceiveEncoding.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); }