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 FiddlerBodyMatch(FiddlerUriMatchMode matchMode, string matchData) //: base(matchMode, matchUri) { if (String.IsNullOrEmpty(matchData) && matchMode != FiddlerUriMatchMode.AllPass) { throw new Exception("empty data is illegal for this mode"); } if (matchData.StartsWith("<hex>")) { if (matchMode == FiddlerUriMatchMode.Regex) { throw new Exception("Regex can not use hex mode"); } MatchBodyBytes = MyBytes.HexStringToByte(matchData.Remove(0, "<hex>".Length), HexDecimal.hex16); if ((MatchBodyBytes == null || MatchBodyBytes.Length == 0) && matchMode != FiddlerUriMatchMode.AllPass) { throw new Exception("empty data is illegal for this mode"); } MatchMode = matchMode; MatchUri = string.Format("<hex>{0}", BitConverter.ToString(MatchBodyBytes)); } else { MatchMode = matchMode; MatchUri = matchData; } }
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"); } }
static void Main(string[] args) { TestChilkat(); DoMyTest(); byte[] clientHello = MyBytes.HexStringToByte("16030100c6010000c2030351adaa772a453edd1c42c48e85d98c671e1619b06fa8a88641f27b43d2797a3c00001c5a5ac02bc02fc02cc030cca9cca8c013c014009c009d002f0035000a0100007daaaa0000ff0100010000000014001200000f642e62616977616e6469616e2e636e0017000000230000000d00140012040308040401050308050501080606010201000500050100000000001200000010000e000c02683208687474702f312e3175500000000b00020100000a000a0008aaaa001d001700187a7a000100", HexaDecimal.hex16, ShowHexMode.@null); TLSPacket.TLSPlaintext tp = new TLSPacket.TLSPlaintext(TLSPacket.TLSContentType.Handshake, new TLSPacket.ProtocolVersion(0x03, 0x03)); TLSPacket.ClientHello ch = new TLSPacket.ClientHello("d.baiwandian.cn"); clientHello = tp.CreateRawData(ch.GetProtocolRawData()); Console.ReadLine(); MyTLS myTLS = new MyTLS(); myTLS.Connect(); Console.WriteLine("enter to say [ Handshake - Client Hello ]"); Console.ReadLine(); myTLS.SendData(clientHello); Console.ReadLine(); myTLS.Dispose(); Console.WriteLine("enter to exti"); Console.ReadLine(); }
public Task <IByteResult> WriteAsync <T>(T value) { var bytes = JsonSerializer.SerializeToUtf8Bytes(value, Options); var byteResult = new MyBytes(new ArraySegment <byte>(bytes)); return(Task.FromResult <IByteResult>(byteResult)); }
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(); }
public override int GetHashCode() { int hash = 1; if (MyDouble != 0D) { hash ^= pbc::ProtobufEqualityComparers.BitwiseDoubleEqualityComparer.GetHashCode(MyDouble); } if (MyFloat != 0F) { hash ^= pbc::ProtobufEqualityComparers.BitwiseSingleEqualityComparer.GetHashCode(MyFloat); } if (MyInt32 != 0) { hash ^= MyInt32.GetHashCode(); } if (MyUint32 != 0) { hash ^= MyUint32.GetHashCode(); } if (MyUint64 != 0UL) { hash ^= MyUint64.GetHashCode(); } if (MyBool != false) { hash ^= MyBool.GetHashCode(); } if (MyString.Length != 0) { hash ^= MyString.GetHashCode(); } if (MyBytes.Length != 0) { hash ^= MyBytes.GetHashCode(); } hash ^= myStringList_.GetHashCode(); if (_unknownFields != null) { hash ^= _unknownFields.GetHashCode(); } return(hash); }
// Notice the void return here... public static void MyClassExtension(this MyBytes buffer, byte[] header) { buffer.ByteArray = header; }
/// <summary> /// 获取运算后的值,掉用此法的该版本的重载将会改变涉及到的staticData数据的游标 /// </summary> /// <param name="yourActuatorStaticDataCollection">可用staticData集合</param> /// <param name="yourDataResultCollection">返回对所有staticData数据运算后的结果列表</param> /// <param name="errorMessage">错误消息(如果没有错误则为null)(在获取参数化数据产生错误后因对当前case设置警示)</param> /// <returns>运算结果</returns> public string GetTargetContentData(ActuatorStaticDataCollection yourActuatorStaticDataCollection, NameValueCollection yourDataResultCollection, out string errorMessage) { string myTargetContentData = contentData; errorMessage = null; if (hasParameter) { myTargetContentData = ParameterizationContentHelper.GetCurrentParametersData(contentData, MyConfiguration.ParametersDataSplitStr, yourActuatorStaticDataCollection, yourDataResultCollection, out errorMessage); } if (encodetype != ParameterizationContentEncodingType.encode_default) { switch (encodetype) { //base64 case ParameterizationContentEncodingType.encode_base64: myTargetContentData = Convert.ToBase64String(Encoding.UTF8.GetBytes(myTargetContentData)); break; case ParameterizationContentEncodingType.decode_base64: try { myTargetContentData = Encoding.UTF8.GetString(Convert.FromBase64String(myTargetContentData)); } catch (Exception ex) { myTargetContentData = "ContentEncoding Error:" + ex.Message; } break; //hex 16 case ParameterizationContentEncodingType.encode_hex16: myTargetContentData = MyBytes.StringToHexString(myTargetContentData); break; case ParameterizationContentEncodingType.decode_hex16: try { byte[] nowBytes = MyBytes.HexStringToByte(myTargetContentData, HexDecimal.hex16, ShowHexMode.space); myTargetContentData = Encoding.UTF8.GetString(nowBytes); } catch (Exception ex) { myTargetContentData = "ContentEncoding Error:" + ex.Message; } break; //hex 2 case ParameterizationContentEncodingType.encode_hex2: myTargetContentData = MyBytes.StringToHexString(myTargetContentData, Encoding.UTF8, HexDecimal.hex2, ShowHexMode.space); break; case ParameterizationContentEncodingType.decode_hex2: try { byte[] nowBytes = MyBytes.HexStringToByte(myTargetContentData, HexDecimal.hex2, ShowHexMode.space); myTargetContentData = Encoding.UTF8.GetString(nowBytes); } catch (Exception ex) { myTargetContentData = "ContentEncoding Error:" + ex.Message; } break; default: errorMessage = "[getTargetContentData] unknow or not supported this encodetype"; break; } } return(myTargetContentData); }
/// <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); }
private void bt_publish_Click(object sender, EventArgs e) { if (session == null) { ShowError("not conection"); return; } if (tb_sendTopic.Text == "") { MessageBox.Show("please put in topic or queues ", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } IMessageProducer prod; try { prod = cb_sendTopicQueues.SelectedIndex == 0 ? session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQTopic(tb_sendTopic.Text)) : session.CreateProducer(new Apache.NMS.ActiveMQ.Commands.ActiveMQQueue(tb_sendTopic.Text)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } finally { } IMessage msg; if (cb_sendTextByte.SelectedIndex == 0) { msg = prod.CreateTextMessage(); ((ITextMessage)msg).Text = rtb_dataToSend.Text; } else { msg = prod.CreateBytesMessage(); try { ((IBytesMessage)msg).Content = MyBytes.HexStringToByte(rtb_dataToSend.Text, HexaDecimal.hex16, ShowHexMode.space); } catch (Exception ex) { MessageBox.Show(ex.Message, "Stop", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } } int sendNum = 1; try { sendNum = int.Parse(tb_sendCount.Text); } catch { tb_sendCount.Text = "1"; } for (int i = 0; i < sendNum; i++) { prod.Send(msg, Apache.NMS.MsgDeliveryMode.NonPersistent, Apache.NMS.MsgPriority.Normal, TimeSpan.MinValue); } prod.Dispose(); ShowState("published"); tb_sendTopic.AutoCompleteCustomSource.Add(tb_sendTopic.Text); }
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); }