Пример #1
0
        private void PushThreadMethod()
        {
            try
            {
                while (State == ResonanceComponentState.Connected)
                {
                    List <byte[]> dataCollection = new List <byte[]>();

                    var data = _pushQueue.BlockDequeue();
                    if (data == null)
                    {
                        return;
                    }

                    var first = true;

                    while (_pushQueue.Count > 0 || first)
                    {
                        if (!first)
                        {
                            data = _pushQueue.BlockDequeue();
                        }
                        else
                        {
                            first = false;
                        }

                        dataCollection.Add(data);
                    }

                    if (dataCollection.Count > 0)
                    {
                        byte[] allData = dataCollection.SelectMany(a => a).ToArray();

                        try
                        {
                            OnWrite(allData);
                        }
                        catch
                        {
                            return;
                        }
                    }

                    System.Threading.Thread.Sleep(QueueWriteModeInterval);
                }
            }
            catch (System.Threading.ThreadAbortException)
            {
                //Ignore
            }
            catch (Exception ex)
            {
                OnFailed(ex, "Unexpected occurred on adapter write queue.");
            }
        }
        private void DispatchMessages()
        {
            while (true)
            {
                var action = _actions.BlockDequeue();
                if (action == null)
                {
                    return;
                }

                action();
            }
        }
Пример #3
0
        private void IncomingQueueThreadMethod()
        {
            Logger.LogInformation("Message processing thread started...");

            while (State == ResonanceComponentState.Connected)
            {
                try
                {
                    byte[] data = _incomingQueue.BlockDequeue();
                    if (data == null)
                    {
                        Logger.LogInformation("Message processing thread terminated.");
                        return;
                    }

                    if (_expectedSegments == 0)
                    {
                        _expectedSegments         = BitConverter.ToInt32(data, 0);
                        _expectedSegmentsCheckSum = data.Skip(4).Take(16).ToArray();

                        byte[] segment = data.TakeFrom(20);

                        if (_expectedSegments == 1) //Take the shortcut if only one.
                        {
                            _expectedSegments = 0;

                            var checkSum = MD5.Create().ComputeHash(segment);
                            if (checkSum.SequenceEqual(_expectedSegmentsCheckSum))
                            {
                                OnDataAvailable(segment);
                            }
                            else
                            {
                                Logger.LogError("Message check sum failed. The message will be ignored.");
                            }
                        }
                        else
                        {
                            _receivedSegments.Add(segment);
                        }
                    }
                    else
                    {
                        _receivedSegments.Add(data);

                        if (_receivedSegments.Count == _expectedSegments)
                        {
                            List <byte> allData = new List <byte>();

                            foreach (var segment in _receivedSegments)
                            {
                                allData.AddRange(segment);
                            }

                            _expectedSegments = 0;
                            _receivedSegments.Clear();

                            byte[] allDataBytes = allData.ToArray();

                            var checkSum = MD5.Create().ComputeHash(allDataBytes);

                            if (checkSum.SequenceEqual(_expectedSegmentsCheckSum))
                            {
                                OnDataAvailable(allDataBytes);
                            }
                            else
                            {
                                Logger.LogError("Message check sum failed. The message will be ignored.");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex, "Error occurred while processing incoming data.");
                }
            }
        }