public Notify WaitForNotify(Action action, int timeout, WaitHandle stop) { Notify notify = null; Events_.NotificationConsumer consumer = new Events_.NotificationConsumer(GetNotificationUri()); NotifyReceived.Reset(); ErrorReceived.Reset(); consumer.OnError += new Action <Exception>( (err) => { _rawData = consumer.RawData; RaiseDataReceived(consumer.RawData); OnNotifyError(err); } ); consumer.OnNotify += OnNotify; try { consumer.Start(); if (action != null) { action(); } RaiseWaitStarted(); int res = WaitHandle.WaitAny(new WaitHandle[] { NotifyReceived, ErrorReceived, stop }, timeout); if (res == 0) { notify = _notify; RaiseDataReceived(_rawData); RaiseWaitFinished(); } else if (res == 1) { consumer.Stop(); throw _error; } else if (res == 2) { // stop event throw new StopEventException(); } else { RaiseTimeout(); } } finally { consumer.Stop(); } return(notify); }
/// <summary> /// /// </summary> /// <param name="timeout">Timeout for single "WaitAny" operation</param> /// <param name="stopHandle"></param> /// <returns></returns> public Dictionary <Notify, byte[]> Peek(int timeout, WaitHandle stopHandle) { System.Diagnostics.Debug.WriteLine("NotifyAsyncServer::Peek"); Dictionary <Notify, byte[]> result = new Dictionary <Notify, byte[]>(); lock (_notificationsLock) { if (_notifications.Count > 0) { MoveNotifications(result); System.Diagnostics.Debug.WriteLine(string.Format("Return immediately {0} notifications", result.Count)); return(result); } } WaitHandle[] handles = new WaitHandle[] { NotifyReceived, ErrorReceived, stopHandle }; int res = WaitHandle.WaitAny(handles, timeout); try { System.Diagnostics.Debug.WriteLine(string.Format("Result of WaitAny: {0}", res)); if (res == 0) { System.Diagnostics.Debug.WriteLine(" Return notifications list"); MoveNotifications(result); } else if (res == 1) { System.Diagnostics.Debug.WriteLine(" Throw error"); throw _error; } else if (res == 2) { System.Diagnostics.Debug.WriteLine(" Stop event - throw exception"); throw new Definitions.Exceptions.StopEventException(); // stop event } else { System.Diagnostics.Debug.WriteLine(" Timeout"); RaiseTimeout(); } } catch (Exception) { _consumer.Stop(); throw; } return(result); }
public Notify WaitForNotify(Action action, int timeout, WaitHandle stop) { Notify notify = null; Events.NotificationConsumer consumer = new Events.NotificationConsumer(GetNotificationUri()); _notifyReceived.Reset(); _errorReceived.Reset(); consumer.OnError += OnNotifyError; consumer.OnNotify += OnNotify; try { consumer.Start(); if (action != null) { action(); } if (WaitStarted != null) { WaitStarted(); } int res = WaitHandle.WaitAny(new WaitHandle[] { _notifyReceived, _errorReceived, stop }, timeout); if (res == 0) { notify = _notify; _rawData = consumer.RawData; if (WaitFinished != null) { WaitFinished(); } } else if (res == 1) { throw _error; } else if (res == 2) { // stop event } else { if (Timeout != null) { Timeout(); } } } finally { consumer.Stop(); } return(notify); }
/// <summary> /// Blocks calling thread and collects notifications. Notifications collecting will continue until /// - count of messages achieves <paramref name="limit"/>. If <paramref name="messageCheck"/> parameter /// is specified, messages will be checked before adding to the list. /// </summary> /// <param name="action">Action which should trigger notification sending</param> /// <param name="timeout">Timeout for waiting</param> /// <param name="limit">Limit of message to receive</param> /// <param name="messageCheck">Filter procedure</param> /// <param name="stop">Semaphore object</param> /// <returns></returns> public Dictionary <Notify, byte[]> CollectNotifications(Action action, int timeout, /* Milliseconds! */ int limit, Func <NotificationMessageHolderType, bool> messageCheck, WaitHandle startProcessing, WaitHandle stop) { Dictionary <Notify, byte[]> notifications = new Dictionary <Notify, byte[]>(); _notifications = new Queue <NotificationInfo>(); Events_.NotificationConsumer consumer = new Events_.NotificationConsumer(GetNotificationUri()); Action <Exception> errorHandling = new Action <Exception>( (err) => { RaiseDataReceived(consumer.RawData); OnNotifyError(err); } ); consumer.OnError += errorHandling; consumer.OnNotify += OnNotify; NotifyReceived.Reset(); ErrorReceived.Reset(); bool exitByStop = false; try { consumer.Start(); if (action != null) { action(); } RaiseWaitStarted(); int hndl = WaitHandle.WaitAny(new WaitHandle[] { startProcessing }, timeout); if (hndl == WaitHandle.WaitTimeout) { return(notifications); } int total = 0; while (true) { int res = WaitHandle.WaitAny(new WaitHandle[] { NotifyReceived, ErrorReceived, stop }, timeout); if (res == 0) { while (_notifications.Count > 0) { NotificationInfo info = _notifications.Dequeue(); Notify notify = info.Notify; System.Diagnostics.Debug.WriteLine("Process Notify: " + notify.GetHashCode()); RaiseDataReceived(info.RawData); bool add = false; if (notify.NotificationMessage != null) { if (messageCheck == null) { add = true; total += notify.NotificationMessage.Length; } else { foreach (NotificationMessageHolderType message in notify.NotificationMessage) { System.Diagnostics.Debug.WriteLine("Check if message passes the test"); if (messageCheck(message)) { System.Diagnostics.Debug.WriteLine("PASSED!"); add = true; total += 1; } else { System.Diagnostics.Debug.WriteLine("Message filtered out!"); } } } } if (add) { try { System.Diagnostics.Debug.WriteLine("Add Notify object: " + notify.GetHashCode()); notifications.Add(notify, info.RawData); } catch (Exception exc) { System.Diagnostics.Debug.WriteLine("Error when adding notify [" + notify.GetHashCode() + "]"); throw exc; } } if (total >= limit) { System.Diagnostics.Debug.WriteLine(string.Format("All {0} messages received", limit)); break; } } // while notifications.Count > 0 if (total >= limit) { break; // exit from the second cycle } } else if (res == 1) { // error - stop consumer.Stop(); break; } else if (res == 2) { // stop event exitByStop = true; break; } else { break; } } } finally { consumer.OnError -= errorHandling; consumer.OnNotify -= OnNotify; consumer.Stop(); } if (exitByStop) { throw new TestTool.Tests.Definitions.Exceptions.StopEventException(); } return(notifications); }
public void StopNotify() { _consumer.Stop(); }