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);
        }
 protected void OnNotify(SoapMessage <Notify> notify, byte[] rawData)
 {
     if (notify != null)
     {
         System.Diagnostics.Debug.WriteLine("Notify received: " + notify.Object.GetHashCode());
         _notify  = notify.Object;
         _rawData = rawData;
     }
     NotifyReceived.Set();
 }
Exemple #3
0
 void AddMessage(SoapMessage <Notify> notify, byte[] rawData)
 {
     System.Diagnostics.Debug.WriteLine("Notification received!");
     if (notify != null)
     {
         Notify newNotify = notify.Object;
         lock (_notificationsLock)
         {
             _notifications.Add(newNotify, rawData);
         }
     }
     NotifyReceived.Set();
     RaiseDataReceived(rawData);
 }
Exemple #4
0
 protected void OnNotify(SoapMessage <Notify> notify, byte[] rawData)
 {
     if (notify != null)
     {
         System.Diagnostics.Debug.WriteLine("Notify received: " + notify.Object.GetHashCode());
         Notify           newNotify = notify.Object;
         NotificationInfo info      = new NotificationInfo()
         {
             Notify = newNotify, RawData = rawData
         };
         lock (_notificationsLock)
         {
             _notifications.Enqueue(info);
         }
     }
     NotifyReceived.Set();
 }
        public void StartNotify()
        {
            _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;
            _consumer.Start();
        }
Exemple #6
0
        public void StartCollecting(WaitHandle stop)
        {
            lock (_notificationsLock)
            {
                _notifications = new Dictionary <Notify, byte[]>();
            }

            _consumer = new Events.NotificationConsumer(GetNotificationUri());

            _consumer.OnError +=
                new Action <Exception>(
                    (err) =>
            {
                RaiseDataReceived(_consumer.RawData);
                OnNotifyError(err);
            }
                    );

            _consumer.OnNotify +=
                new Action <SoapMessage <Notify>, byte[]>((n, raw) => AddMessage(n, raw));

            NotifyReceived.Reset();
            ErrorReceived.Reset();

            try
            {
                _consumer.Start();
                RaiseWaitStarted();
            }
            finally
            {
            }

            if (_error != null)
            {
                throw _error;
            }
        }
Exemple #7
0
        /// <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);
        }
Exemple #8
0
        private void recv_thread()
        {
            // decode
            int b;
            int chk;

            byte[] buf = new byte[128];
            while (_keepthread)
            {
                _datarecvevent.WaitOne();
                if (!_keepthread)
                {
                    break;
                }
                while (_sp.BytesToRead > 0)
                {
                    chk = 0;
                    buf = new byte[2];
                    b   = _sp.ReadByte();
                    if (0xaa != b && 0xcc != b)
                    {
                        continue;
                    }
                    chk ^= b;

                    /* Hex格式帧 */
                    if (0xaa == b)
                    {
                        b = _sp.ReadByte();
                        if (0x55 != b)
                        {
                            continue;
                        }
                        chk ^= b;
                        if (!waitforbytestoread(2, 1000))
                        {
                            continue;
                        }
                        _sp.Read(buf, 0, 2);
                        chk ^= buf[0];
                        chk ^= buf[1];
                        int len = buf[0] * 256 + buf[1] - 4;
                        buf = new byte[len];

                        if (!waitforbytestoread(len, 1000))
                        {
                            continue;
                        }
                        _sp.Read(buf, 0, len);
                        for (int i = 0; i < len - 1; i++)
                        {
                            chk ^= buf[i];
                        }
                        if (chk != buf[len - 1])
                        {
                            continue;
                        }
                        if (buf[0] == 0x01 || buf[0] == 0x02 || buf[0] == 0x03)
                        {
                            //写操作的应答,以及读操作的返回
                            _recvevent.Set();
                            //读操作应答
                            if (buf[0] == 0x02)
                            {
                                _regdata = (UInt16)(buf[2] * 256 + buf[3]);
                            }
                        }
                        if (buf[0] == 0x04)
                        {
                            Notify notify = (Notify)(buf[0] * 256 + buf[1]);
                            NotifyReceived?.Invoke(this, new NotifyEventArgs(notify));
                        }
                    }
                    /* ASCII码字符串帧 */
                    if (0xcc == b)
                    {
                        b = _sp.ReadByte();
                        if (0x33 != b)
                        {
                            continue;
                        }
                        chk ^= b;
                        if (!waitforbytestoread(2, 1000))
                        {
                            continue;
                        }
                        _sp.Read(buf, 0, 2);
                        int len = buf[0] * 256 + buf[1] - 4;
                        chk ^= buf[0];
                        chk ^= buf[1];
                        buf  = new byte[len];
                        if (!waitforbytestoread(len, 1000))
                        {
                            continue;
                        }
                        _sp.Read(buf, 0, len);
                        for (int i = 0; i < buf.Length - 1; i++)
                        {
                            chk ^= buf[i];
                        }
                        if (chk != buf[buf.Length - 1])
                        {
                            continue;
                        }
                        string s = System.Text.Encoding.ASCII.GetString(buf, 0, buf.Length - 1);
                        StringInfoReceived?.Invoke(this, s);
                    }
                }
            }
            Console.WriteLine("receive thread exit");
        }