private void Consumer_Received(object sender, BasicDeliverEventArgs e) { try { string eventName = System.Text.Encoding.UTF8.GetString((byte[])e.BasicProperties.Headers["event.name"]); string source = e.BasicProperties.Headers["event.source"] != null? System.Text.Encoding.UTF8.GetString((byte[])e.BasicProperties.Headers["event.source"]) : null; DateTime timestamp = Newtonsoft.Json.JsonConvert.DeserializeObject <DateTime>(System.Text.Encoding.UTF8.GetString((byte[])e.BasicProperties.Headers["event.timestamp"])); int version = (int)e.BasicProperties.Headers["event.version"]; var dispatchEvent = new DispatchEvent() { Id = Guid.Parse(e.BasicProperties.MessageId), EventName = eventName, Source = source, Timestamp = timestamp, Version = version, Payload = System.Text.Encoding.UTF8.GetString(e.Body) }; System.Console.WriteLine($"Received event type: {dispatchEvent.EventName}, Id: {dispatchEvent.Id}"); incomingEvents[dispatchEvent.Id] = e.DeliveryTag; ReceivedEventHandler?.Invoke(dispatchEvent); } catch (Exception ex) { System.Console.Error.WriteLine($"Error unpacking incoming event {e.BasicProperties.MessageId}"); System.Console.Error.WriteLine(ex); } }
public void ReceivedEvent_Chars() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); Debug.WriteLine("Verifying ReceivedChars event"); com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; for (int i = 0; i < NUM_TRYS; i++) { com2.Write(new byte[com1.ReceivedBytesThreshold], 0, com1.ReceivedBytesThreshold); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, 1); rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold); if (0 != rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Eof)) { Fail("Err_21087qpua!!! Unexpected EofReceived event fireed {0}", i); } if (0 != rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Chars)) { Fail("Err_32417!!! Unexpected EofReceived event fireed {0}", i); } com1.DiscardInBuffer(); } } }
public void ReceivedBytesThreshold_Default() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Debug.WriteLine("Verifying default ReceivedBytesThreshold"); com2.Write(new byte[1], 0, 1); rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT); com1.DiscardInBuffer(); serPortProp.VerifyPropertiesAndPrint(com1); rcvEventHandler.Validate(SerialData.Chars, 1, 0); } }
public void ReceivedBytesThreshold_Rnd_MultipleWrite() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); Random rndGen = new Random(-55); int receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD); com1.ReceivedBytesThreshold = receivedBytesThreshold; com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("ReceivedBytesThreshold", receivedBytesThreshold); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Debug.WriteLine("Verifying writing the number of bytes of ReceivedBytesThreshold after several write calls"); com2.Write(new byte[(int)Math.Floor(com1.ReceivedBytesThreshold / 2.0)], 0, (int)Math.Floor(com1.ReceivedBytesThreshold / 2.0)); com2.Write(new byte[(int)Math.Ceiling(com1.ReceivedBytesThreshold / 2.0)], 0, (int)Math.Ceiling(com1.ReceivedBytesThreshold / 2.0)); rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT); com1.DiscardInBuffer(); serPortProp.VerifyPropertiesAndPrint(com1); rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0); } }
/// <summary> /// Constructor /// </summary> /// <param name="stream">TCP stream to a POP3 server</param> /// <param name="ReceiveEvent">The receive event handler</param> /// <param name="MessageSize">The message size in byte</param> public POPReader(System.IO.StreamReader stream, ReceivedEventHandler ReceiveEvent, int MessageSize) { TCPStream = stream; Received = ReceiveEvent; m_byteread = 0; m_bytetotal = MessageSize; }
public bool ReceivedEvent_Chars() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); bool retValue = true; Random rndGen = new Random(-55); Console.WriteLine("Verifying ReceivedChars event"); com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); for (int i = 0; i < NUM_TRYS; i++) { com2.Write(new byte[com1.ReceivedBytesThreshold], 0, com1.ReceivedBytesThreshold); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, 1); if (!rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold)) { Console.WriteLine("Err_2097asd!!! ReceivedChars Event not fired {0}", i); retValue = false; } if (0 != rcvEventHandler.NumberOfOccurencesOfType(SerialData.Eof)) { Console.WriteLine("Err_21087qpua!!! Unexpected EofReceived event fireed {0}", i); retValue = false; } if (0 != rcvEventHandler.NumberOfOccurencesOfType(SerialData.Chars)) { Console.WriteLine("Err_32417!!! Unexpected EofReceived event fireed {0}", i); retValue = false; } com1.DiscardInBuffer(); } if (!retValue) { Console.WriteLine("Err_002!!! Verifying ReceivedChars event FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
private void ReceiveCallbackProc(StringBuilder irEventString, IntPtr userState) { ReceivedEventHandler temp = _received; if (null != temp) { temp(this, new ReceivedEventArgs(irEventString.ToString())); } }
private void Main_Load(object sender, EventArgs e) { availHandler = new AvailEventHandler(im_UserAvailable); receivedHandler = new ReceivedEventHandler(im_MessageReceived); user.UserAvailable += availHandler; user.MessageReceived += receivedHandler; disconnectHandler = new EventHandler(im_Disconnected); user.Disconnected += disconnectHandler; updateHandler = new EventHandler(im_UpdateForm); user.UpdateForm += updateHandler; }
public bool ReceivedBytesThreshold_Twice() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Random rndGen = new Random(-55); int receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD); com1.ReceivedBytesThreshold = receivedBytesThreshold; com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("ReceivedBytesThreshold", receivedBytesThreshold); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Console.WriteLine("Verifying writing twice the number of bytes of ReceivedBytesThreshold and ReceivedEvent firered twice"); com2.Write(new byte[com1.ReceivedBytesThreshold * 2], 0, com1.ReceivedBytesThreshold * 2); if (!rcvEventHandler.WaitForEvent(SerialData.Chars, 2, MAX_TIME_WAIT)) { Console.WriteLine("ERROR!!!: Event never fired"); retValue = false; } com1.DiscardInBuffer(); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); retValue &= rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0); retValue &= rcvEventHandler.Validate(SerialData.Chars, 2 * com1.ReceivedBytesThreshold, 1); if (!retValue) { Console.WriteLine("Err_007!!! Verifying writing twice the number of bytes of ReceivedBytesThreshold and ReceivedEvent firered twice FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public void ReceivedBytesThreshold_Above_Below() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); Random rndGen = new Random(-55); int receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD); com1.ReceivedBytesThreshold = receivedBytesThreshold + 1; com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("ReceivedBytesThreshold", receivedBytesThreshold - 1); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Debug.WriteLine("Verifying writing less then number of bytes of ReceivedBytesThreshold then setting " + "ReceivedBytesThreshold to less then the number of bytes written"); com2.Write(new byte[receivedBytesThreshold], 0, receivedBytesThreshold); while (com1.BytesToRead < receivedBytesThreshold) { System.Threading.Thread.Sleep(100); } if (0 != rcvEventHandler.NumEventsHandled) { Fail("ERROR!!! Unexpected ReceivedEvent was firered NumEventsHandled={0}", rcvEventHandler.NumEventsHandled); } else { com1.ReceivedBytesThreshold = receivedBytesThreshold - 1; rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT); } com1.DiscardInBuffer(); serPortProp.VerifyPropertiesAndPrint(com1); rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0); } }
public bool Thread_In_ReceivedEvent() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); Thread closeThread = new Thread(delegate() { com1.Close(); }); bool retValue = true; Console.WriteLine("Verifying that if a thread is blocked in a RecevedEvent handler the port can still be closed"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.DataReceived += new SerialDataReceivedEventHandler(receivedEventHandler.HandleEvent); //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 1)) { Console.WriteLine("Err_122808aoeid Expected 1 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); retValue = false; } closeThread.Start(); Thread.Sleep(5000); receivedEventHandler.ResumeHandleEvent(); closeThread.Join(); if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public bool ReceivedBytesThreshold_Default() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Console.WriteLine("Verifying default ReceivedBytesThreshold"); com2.Write(new byte[1], 0, 1); if (!rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT)) { Console.WriteLine("ERROR!!!: Event never fired"); retValue = false; } com1.DiscardInBuffer(); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); retValue &= rcvEventHandler.Validate(SerialData.Chars, 1, 0); if (!retValue) { Console.WriteLine("Err_001!!! Verifying default ReceivedBytesThreshold FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public void NewConnectionTCP(string server, string sport) { int port = Convert.ToInt32(sport); if (port == 0) { port = 3490; } tcpClient = new TcpClient(); ipAddress = Dns.GetHostEntry(server).AddressList[0]; Application.DoEvents(); try { tcpClient.Connect(ipAddress, port); } catch (Exception ex) { MessageBox.Show(appwin, ex.Message); tcpStream = null; return; } // if (bwReceiver != null) // { // if (bwReceiver.IsBusy) // { // bwReceiver.CancelAsync(); // } // bwReceiver = null; // } if (tcpClient.Connected) { tcpStream = new com.thosmos.TcpStream(tcpClient.GetStream()); tcpStream.DataReceived += new EventHandler <com.thosmos.TcpStream.TcpDataEventArgs>(tcpStream_DataReceived); tcpStream.ErrorOccurred += new EventHandler <com.thosmos.TcpStream.TcpErrorEventArgs>(tcpStream_ErrorOccurred); ReceivedFromServer += new ReceivedEventHandler(n_ReceivedFromServer); tcpStream.CloseOccurred += new EventHandler <com.thosmos.TcpStream.TcpCloseEventArgs>(tcpStream_CloseOccurred); tcpStream.BeginRead(); } else { tcpStream = null; } }
public MainWindow() { //Events InitializeComponent(); client.LoginOK += new EventHandler(_LoginOK); client.RegisterOK += new EventHandler(_RegisterOK); client.LoginFailed += new ErrorEventHandler(_LoginFailed); client.RegisterFailed += new ErrorEventHandler(_RegisterFailed); client.Disconnected += new EventHandler(_Disconnected); AvailableHandler = new AvailableEventHandler(_UserAvailable); ReceivedHandler = new ReceivedEventHandler(_MessegeReceived); client.UserAvailable += AvailableHandler; client.MessageReceived += ReceivedHandler; }
/// <summary> /// Open port with settings /// </summary> /// <param name="portName">Port name</param> /// <param name="baudRate">Baudrate</param> /// <param name="parity">Parity</param> /// <param name="dataBits">Number of data bits</param> /// <param name="stopBits">Number of stop bits</param> /// <param name="receivedEventHandler">Callback to run to when something is received</param> public void Open(string portName, EBaudrate baudRate, Parity parity, EDataBits dataBits, StopBits stopBits, ReceivedEventHandler receivedEventHandler) { if (IsOpen) { return; } _serialPort = new SerialPort(portName, (int)baudRate, parity, (int)dataBits, stopBits); _serialPort.Open(); if (receivedEventHandler != null) { _serialPort.DataReceived += delegate(object sender, SerialDataReceivedEventArgs args) { receivedEventHandler(sender, args); } } ; _receivedBuffer = string.Empty; }
public void ReceivedEvent_CharsEof() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); byte[] xmitBytes = new byte[3]; Debug.WriteLine("Verifying EofReceived event"); com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; //EOF char xmitBytes[0] = 56; xmitBytes[1] = 26; xmitBytes[2] = 55; for (int i = 0; i < NUM_TRYS; i++) { com2.Write(xmitBytes, 0, xmitBytes.Length); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, SerialData.Eof); rcvEventHandler.Validate(SerialData.Eof, i * xmitBytes.Length); rcvEventHandler.Validate(SerialData.Chars, (i * xmitBytes.Length) + com1.ReceivedBytesThreshold); if (0 != rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Eof)) { Fail("Err_20712asdfhow!!! Unexpected EofReceived event fired {0} iteration:{1}", rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Eof), i); } rcvEventHandler.Clear(); } } }
public void ReceivedEvent_Eof() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); byte[] xmitBytes = new byte[1]; Debug.WriteLine("Verifying EofReceived event"); com1.Open(); com2.Open(); com1.DataReceived += rcvEventHandler.HandleEvent; //EOF char xmitBytes[0] = 26; for (int i = 0; i < NUM_TRYS; i++) { com2.Write(xmitBytes, 0, xmitBytes.Length); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, 2); rcvEventHandler.Validate(SerialData.Eof, i); rcvEventHandler.Validate(SerialData.Chars, i + com1.ReceivedBytesThreshold); if (0 != rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Eof)) { Fail("Err_01278qaods!!! Unexpected EofReceived event fireed {0}", i); } if (1 < rcvEventHandler.NumberOfOccurrencesOfType(SerialData.Chars)) { Fail("Err_2972qoypa!!! Unexpected ReceivedChars event fireed {0}", i); } } } }
public void Thread_In_ReceivedEvent() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); Debug.WriteLine( "Verifying that if a thread is blocked in a RecevedEvent handler the port can still be closed"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.DataReceived += receivedEventHandler.HandleEvent; //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 1)) { Fail("Err_122808aoeid Expected 1 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } Task task = Task.Run(() => com1.Close()); Thread.Sleep(5000); receivedEventHandler.ResumeHandleEvent(); TCSupport.WaitForTaskCompletion(task); } }
public bool ReceivedEvent_Eof() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); bool retValue = true; byte[] xmitBytes = new byte[1]; Console.WriteLine("Verifying EofReceived event"); com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); //EOF char xmitBytes[0] = 26; for (int i = 0; i < NUM_TRYS; i++) { com2.Write(xmitBytes, 0, xmitBytes.Length); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, 2); if (!rcvEventHandler.Validate(SerialData.Eof, i)) { Console.WriteLine("Err_1048apqa!!! EofReceived Event not fired {0}", i); retValue = false; } if (!rcvEventHandler.Validate(SerialData.Chars, i + com1.ReceivedBytesThreshold)) { Console.WriteLine("Err_16489qayas!!! ReceivedChars Event not fired {0}", i); retValue = false; } if (0 != rcvEventHandler.NumberOfOccurencesOfType(SerialData.Eof)) { Console.WriteLine("Err_01278qaods!!! Unexpected EofReceived event fireed {0}", i); retValue = false; } if (1 < rcvEventHandler.NumberOfOccurencesOfType(SerialData.Chars)) { Console.WriteLine("Err_2972qoypa!!! Unexpected ReceivedChars event fireed {0}", i); retValue = false; } } if (!retValue) { Console.WriteLine("Err_002!!! Verifying EofReceived event FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
//Method for firing the event public virtual void onReceive(MessageSend_EventArgs msg) { ReceivedEventHandler?.Invoke(this, msg); }
public void EventHandlers_CalledSerially() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, false, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true); int numPinChangedEvents = 0, numErrorEvents = 0, numReceivedEvents = 0; int iterationWaitTime = 100; /*************************************************************** * Scenario Description: All of the event handlers should be called sequentially never * at the same time on multiple thread. Basically we will block each event handler caller thread and verify * that no other thread is in another event handler * ***************************************************************/ Debug.WriteLine("Verifying that event handlers are called serially"); com1.WriteTimeout = 5000; com2.WriteTimeout = 5000; com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += pinChangedEventHandler.HandleEvent; com1.DataReceived += receivedEventHandler.HandleEvent; com1.ErrorReceived += errorEventHandler.HandleEvent; //This should cause ErrorEvent to be fired with a parity error since the //8th bit on com1 is the parity bit, com1 one expest this bit to be 1(Mark), //and com2 is writing 0 for this bit com1.DataBits = 7; com1.Parity = Parity.Mark; com2.BaseStream.Write(new byte[1], 0, 1); Debug.Print("ERROREvent Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Debug.WriteLine("PinChange Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Debug.WriteLine("RxEvent Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause a frame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Debug.WriteLine("FrameError Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Debug.WriteLine("PinChange Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Debug.WriteLine("RxEOF Triggered"); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); bool threadFound = true; Stopwatch sw = Stopwatch.StartNew(); while (threadFound && sw.ElapsedMilliseconds < MAX_TIME_WAIT) { threadFound = false; for (int i = 0; i < MAX_TIME_WAIT / iterationWaitTime; ++i) { Debug.WriteLine("Event counts: PinChange {0}, Rx {1}, error {2}", numPinChangedEvents, numReceivedEvents, numErrorEvents); Debug.WriteLine("Waiting for pinchange event {0}ms", iterationWaitTime); if (pinChangedEventHandler.WaitForEvent(iterationWaitTime, numPinChangedEvents + 1)) { // A thread is in PinChangedEvent: verify that it is not in any other handler at the same time if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Fail("Err_191818ahied A thread is in PinChangedEvent and ReceivedEvent"); } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Fail("Err_198119hjaheid A thread is in PinChangedEvent and ErrorEvent"); } ++numPinChangedEvents; pinChangedEventHandler.ResumeHandleEvent(); threadFound = true; break; } Debug.WriteLine("Waiting for rx event {0}ms", iterationWaitTime); if (receivedEventHandler.WaitForEvent(iterationWaitTime, numReceivedEvents + 1)) { // A thread is in ReceivedEvent: verify that it is not in any other handler at the same time if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Fail("Err_2288ajed A thread is in ReceivedEvent and PinChangedEvent"); } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Fail("Err_25158ajeiod A thread is in ReceivedEvent and ErrorEvent"); } ++numReceivedEvents; receivedEventHandler.ResumeHandleEvent(); threadFound = true; break; } Debug.WriteLine("Waiting for error event {0}ms", iterationWaitTime); if (errorEventHandler.WaitForEvent(iterationWaitTime, numErrorEvents + 1)) { // A thread is in ErrorEvent: verify that it is not in any other handler at the same time if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Fail("Err_01208akiehd A thread is in ErrorEvent and PinChangedEvent"); } if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Fail("Err_1254847ajied A thread is in ErrorEvent and ReceivedEvent"); } ++numErrorEvents; errorEventHandler.ResumeHandleEvent(); threadFound = true; break; } } } if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Fail("Err_2288ajied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_122808aoeid Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_215887ajeid Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); } //[] Verify all PinChangedEvents should have occured pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0); pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0); pinChangedEventHandler.Validate(SerialPinChange.Break, 0); //[] Verify all ReceivedEvent should have occured receivedEventHandler.Validate(SerialData.Chars, 0); receivedEventHandler.Validate(SerialData.Eof, 0); //[] Verify all ErrorEvents should have occured errorEventHandler.Validate(SerialError.RXParity, 0); errorEventHandler.Validate(SerialError.Frame, 0); // It's important that we close com1 BEFORE com2 (the using() block would do this the other way around normally) // This is because we have our special blocking event handlers hooked onto com1, and closing com2 is likely to // cause a pin-change event which then hangs and prevents com1 from closing. // An alternative approach would be to unhook all the event-handlers before leaving the using() block. com1.Close(); } }
public void SetUpEvents(LoggedEventHandler logged, UnloggedEventHandler unlogged, ReceivedEventHandler received) { if (!(this.chat is ChatImp)) throw new InvalidCastException("Chat must be derived of ChatImp"); var chatImp = this.chat as ChatImp; chatImp.OnLogged += logged; chatImp.OnUnlogged += unlogged; chatImp.OnReceived += received; }
public bool ReceivedBytesThreshold_Above_1() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); SerialPortProperties serPortProp = new SerialPortProperties(); bool retValue = true; Random rndGen = new Random(-55); int receivedBytesThreshold = rndGen.Next(MIN_RND_THRESHOLD, MAX_RND_THRESHOLD); com1.ReceivedBytesThreshold = receivedBytesThreshold + 1; com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); serPortProp.SetAllPropertiesToOpenDefaults(); serPortProp.SetProperty("ReceivedBytesThreshold", 1); serPortProp.SetProperty("PortName", TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); Console.WriteLine("Verifying writing less then number of bytes of ReceivedBytesThreshold then " + "setting ReceivedBytesThreshold to 1"); com2.Write(new byte[receivedBytesThreshold], 0, receivedBytesThreshold); while (com1.BytesToRead < receivedBytesThreshold) { System.Threading.Thread.Sleep(100); } if (0 != rcvEventHandler.NumEventsHandled) { Console.WriteLine("ERROR!!! Unexpected ReceivedEvent was firered NumEventsHandled={0}", rcvEventHandler.NumEventsHandled); retValue = false; } else { com1.ReceivedBytesThreshold = 1; if (!rcvEventHandler.WaitForEvent(SerialData.Chars, MAX_TIME_WAIT)) { Console.WriteLine("ERROR!!!: Event never fired"); retValue = false; } } com1.DiscardInBuffer(); retValue &= serPortProp.VerifyPropertiesAndPrint(com1); retValue &= rcvEventHandler.Validate(SerialData.Chars, com1.ReceivedBytesThreshold, 0); if (!retValue) { Console.WriteLine("Err_006!!! Verifying writing less then number of bytes of ReceivedBytesThreshold then " + "setting ReceivedBytesThreshold to 1 FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public void EventHandler_ThrowsException() { using (SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName)) using (SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName)) { PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, true); /*************************************************************** * Scenario Description: All of the event handler are going to throw and we are * going to verify that the event handler will still get called. * * We want to verify that throwing does not cause the thread calling the event * handlers to die. ***************************************************************/ Debug.WriteLine("Verifying where the event handlers throws"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += pinChangedEventHandler.HandleEvent; com1.DataReceived += receivedEventHandler.HandleEvent; com1.ErrorReceived += errorEventHandler.HandleEvent; //This should cause ErrorEvent to be fired with a parity error since the //8th bit on com1 is the parity bit, com1 one expest this bit to be 1(Mark), //and com2 is writing 0 for this bit com1.DataBits = 7; com1.Parity = Parity.Mark; com2.BaseStream.Write(new byte[1], 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause a fame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Fail("Err_28282haied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_2912hsie Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Fail("Err_191291jaied Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); } //[] Verify all PinChangedEvents should have occured if (!pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Fail("Err_24597aqqoo!!! PinChangedEvent DsrChanged event not fired"); } if (!pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Fail("Err_144754ajied!!! PinChangedEvent CtsChanged event not fired"); } if (!pinChangedEventHandler.Validate(SerialPinChange.Break, 0)) { Fail("Err_15488ahied!!! PinChangedEvent Break event not fired"); } //[] Verify all ReceivedEvent should have occured if (!receivedEventHandler.Validate(SerialData.Chars, 0)) { Fail("Err_54552aheied!!! ReceivedEvent ReceivedChars event not fired"); } if (!receivedEventHandler.Validate(SerialData.Eof, 0)) { Fail("Err_4588ajeod!!! ReceivedEvent EofReceived event not fired"); } //[] Verify all ErrorEvents should have occured if (!errorEventHandler.Validate(SerialError.RXParity, 0)) { Fail("Err_1051ajheid!!! ErrorEvent RxParity event not fired"); } if (!errorEventHandler.Validate(SerialError.Frame, 0)) { Fail("Err_61805aheud!!! ErrorEvent Frame event not fired"); } } }
public bool EventHandlers_CalledSerially() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); PinChangedEventHandler pinChangedEventHandler = new PinChangedEventHandler(com1, false, true); ReceivedEventHandler receivedEventHandler = new ReceivedEventHandler(com1, false, true); ErrorEventHandler errorEventHandler = new ErrorEventHandler(com1, false, true); bool retValue = true; int numPinChangedEvents = 0, numErrorEvents = 0, numReceivedEvents = 0; int iterationWaitTime = 100; bool threadFound; /*************************************************************** * Scenario Description: All of the event handlers should be called sequentiall never * at the same time on multiple thread. Basically we will block a thread and verify * that no other thread is in another event handler * ***************************************************************/ Console.WriteLine("Verifying where the event handlers are called serially"); com1.Open(); com2.Open(); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); com1.PinChanged += new SerialPinChangedEventHandler(pinChangedEventHandler.HandleEvent); com1.DataReceived += new SerialDataReceivedEventHandler(receivedEventHandler.HandleEvent); com1.ErrorReceived += new SerialErrorReceivedEventHandler(errorEventHandler.HandleEvent); //This should cause ErrorEvent to be fired with a parity error since the //8th bit on com1 is the parity bit, com1 one expest this bit to be 1(Mark), //and com2 is writing 0 for this bit com1.DataBits = 7; com1.Parity = Parity.Mark; com2.BaseStream.Write(new byte[1], 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.DsrChanged //since we are setting DtrEnable to true com2.DtrEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with ReceivedChars //since we are writing some bytes com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 40 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause a fame error since the 8th bit is not set, //and com1 is set to 7 data bits so the 8th bit will +12v where //com1 expects the stop bit at the 8th bit to be -12v com1.DataBits = 7; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 0x01 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.CtsChanged //since we are setting RtsEnable to true com2.RtsEnable = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause ReceivedEvent to be fired with EofReceived //since we are writing the EOF char com1.DataBits = 8; com1.Parity = Parity.None; com2.BaseStream.Write(new byte[] { 26 }, 0, 1); Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); //This should cause PinChangedEvent to be fired with SerialPinChanges.Break //since we are setting BreakState to true com2.BreakState = true; Thread.Sleep(TRIGERING_EVENTS_WAIT_TIME); threadFound = true; while (threadFound) { threadFound = false; for (int i = 0; i < MAX_TIME_WAIT / iterationWaitTime; ++i) { if (pinChangedEventHandler.WaitForEvent(iterationWaitTime, numPinChangedEvents + 1)) {//A thread is in PinChangedEvent verify that it is not in any other if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Console.WriteLine("Err_191818ahied A thread is in PinChangedEvent and ReceivedEvent"); retValue = false; } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Console.WriteLine("Err_198119hjaheid A thread is in PinChangedEvent and ErrorEvent"); retValue = false; } ++numPinChangedEvents; pinChangedEventHandler.ResumeHandleEvent(); threadFound = true; break; } if (receivedEventHandler.WaitForEvent(iterationWaitTime, numReceivedEvents + 1)) {//A thread is in ReceivedEvent verify that it is not in any other if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Console.WriteLine("Err_2288ajed A thread is in ReceivedEvent and PinChangedEvent"); retValue = false; } if (errorEventHandler.NumEventsHandled != numErrorEvents) { Console.WriteLine("Err_25158ajeiod A thread is in ReceivedEvent and ErrorEvent"); retValue = false; } ++numReceivedEvents; receivedEventHandler.ResumeHandleEvent(); threadFound = true; break; } if (errorEventHandler.WaitForEvent(iterationWaitTime, numErrorEvents + 1)) {//A thread is in ErrorEvent verify that it is not in any other if (pinChangedEventHandler.NumEventsHandled != numPinChangedEvents) { Console.WriteLine("Err_01208akiehd A thread is in ErrorEvent and PinChangedEvent"); retValue = false; } if (receivedEventHandler.NumEventsHandled != numReceivedEvents) { Console.WriteLine("Err_1254847ajied A thread is in ErrorEvent and ReceivedEvent"); retValue = false; } ++numErrorEvents; errorEventHandler.ResumeHandleEvent(); threadFound = true; break; } } } if (!pinChangedEventHandler.WaitForEvent(MAX_TIME_WAIT, 3)) { Console.WriteLine("Err_2288ajied Expected 3 PinChangedEvents to be fired and only {0} occured", pinChangedEventHandler.NumEventsHandled); retValue = false; } if (!receivedEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Console.WriteLine("Err_122808aoeid Expected 2 ReceivedEvents to be fired and only {0} occured", receivedEventHandler.NumEventsHandled); retValue = false; } if (!errorEventHandler.WaitForEvent(MAX_TIME_WAIT, 2)) { Console.WriteLine("Err_215887ajeid Expected 3 ErrorEvents to be fired and only {0} occured", errorEventHandler.NumEventsHandled); retValue = false; } //[] Verify all PinChangedEvents should have occured if (!pinChangedEventHandler.Validate(SerialPinChange.DsrChanged, 0)) { Console.WriteLine("Err_258087aieid!!! PinChangedEvent DsrChanged event not fired"); retValue = false; } if (!pinChangedEventHandler.Validate(SerialPinChange.CtsChanged, 0)) { Console.WriteLine("Err_5548ajhied!!! PinChangedEvent CtsChanged event not fired"); retValue = false; } if (!pinChangedEventHandler.Validate(SerialPinChange.Break, 0)) { Console.WriteLine("Err_25848ajiied!!! PinChangedEvent Break event not fired"); retValue = false; } //[] Verify all ReceivedEvent should have occured if (!receivedEventHandler.Validate(SerialData.Chars, 0)) { Console.WriteLine("Err_0211558ajoied!!! ReceivedEvent ReceivedChars event not fired"); retValue = false; } if (!receivedEventHandler.Validate(SerialData.Eof, 0)) { Console.WriteLine("Err_215588zahid!!! ReceivedEvent EofReceived event not fired"); retValue = false; } //[] Verify all ErrorEvents should have occured if (!errorEventHandler.Validate(SerialError.RXParity, 0)) { Console.WriteLine("Err_515188ahjid!!! ErrorEvent RxParity event not fired"); retValue = false; } if (!errorEventHandler.Validate(SerialError.Frame, 0)) { Console.WriteLine("Err_55874884ajie!!! ErrorEvent Frame event not fired"); retValue = false; } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }
public bool ReceivedEvent_CharsEof() { SerialPort com1 = new SerialPort(TCSupport.LocalMachineSerialInfo.FirstAvailablePortName); SerialPort com2 = new SerialPort(TCSupport.LocalMachineSerialInfo.SecondAvailablePortName); ReceivedEventHandler rcvEventHandler = new ReceivedEventHandler(com1); bool retValue = true; byte[] xmitBytes = new byte[3]; Console.WriteLine("Verifying EofReceived event"); com1.Open(); com2.Open(); com1.DataReceived += new SerialDataReceivedEventHandler(rcvEventHandler.HandleEvent); //EOF char xmitBytes[0] = 56; xmitBytes[1] = 26; xmitBytes[2] = 55; for (int i = 0; i < NUM_TRYS; i++) { com2.Write(xmitBytes, 0, xmitBytes.Length); rcvEventHandler.WaitForEvent(MAX_TIME_WAIT, SerialData.Eof); if (!rcvEventHandler.Validate(SerialData.Eof, i * xmitBytes.Length)) { Console.WriteLine("Err_09727ahsp!!!EOF Event not fired {0}", i); retValue = false; } if (!rcvEventHandler.Validate(SerialData.Chars, (i * xmitBytes.Length) + com1.ReceivedBytesThreshold)) { Console.WriteLine("Err_27928adshs !!!ReceivedChars Event not fired {0}", i); retValue = false; } if (0 != rcvEventHandler.NumberOfOccurencesOfType(SerialData.Eof)) { Console.WriteLine("Err_20712asdfhow!!! Unexpected EofReceived event fired {0} iteration:{1}", rcvEventHandler.NumberOfOccurencesOfType(SerialData.Eof), i); retValue = false; } rcvEventHandler.Clear(); } if (!retValue) { Console.WriteLine("Err_3468eadhs!!! Verifying CharsReceived and EofReceived event FAILED"); } if (com1.IsOpen) { com1.Close(); } if (com2.IsOpen) { com2.Close(); } return(retValue); }