private void Process()
        {
            List <byte> parseBuffer = new List <byte>();
            var         nestedCount = 0;
            var         exception   = default(Exception);
            var         unzipBuffer = new byte[BufferSize * 3];
            var         zipBuffer   = new byte[BufferSize];
            var         readBytes   = _innerStream.Read(zipBuffer, 0, zipBuffer.Length);

            while (readBytes > 0)
            {
                // We got some more bytes from the stream!
                var decodedBytes = Decode(zipBuffer, readBytes, unzipBuffer, out exception);
                while (decodedBytes > 0)
                {
                    // We decoded some more bytes from the stream!
                    for (int i = 0; i < decodedBytes; i++)
                    {
                        parseBuffer.Add(unzipBuffer[i]);
                        if (IsEnd(parseBuffer.Last()))
                        {
                            if (--nestedCount == 0 && parseBuffer.Count > 0)
                            {
                                // We have a complete JSON object or array ready for processing
                                var changes = Manager.GetObjectMapper().ReadValue <IList <object> >(parseBuffer);
                                foreach (var change in changes)
                                {
                                    if (ChunkFound != null)
                                    {
                                        ChunkFound(this, change.AsDictionary <string, object>());
                                    }
                                }

                                parseBuffer.Clear();
                            }
                        }
                        else if (IsStart(parseBuffer.Last()))
                        {
                            // Begin an embedded array
                            nestedCount++;
                        }
                    }

                    decodedBytes = Decode(null, 0, unzipBuffer, out exception);
                }

                if (decodedBytes == -1)
                {
                    // The decode process failed, unable to continue
                    break;
                }

                readBytes = _innerStream.Read(zipBuffer, 0, zipBuffer.Length);
            }

            if (Finished != null)
            {
                Finished(this, exception);
            }
        }
Beispiel #2
0
        private void Process()
        {
            List <byte> parseBuffer = new List <byte>();
            var         nestedCount = 0;
            var         exception   = default(Exception);
            var         unzipBuffer = new byte[BufferSize * 3];
            var         zipBuffer   = new byte[BufferSize];
            var         readBytes   = _innerStream.Read(zipBuffer, 0, zipBuffer.Length);

            while (readBytes > 0)
            {
                // We got some more bytes from the stream!
                var decodedBytes = Decode(zipBuffer, readBytes, unzipBuffer, out exception);
                while (decodedBytes > 0)
                {
                    // We decoded some more bytes from the stream!
                    for (int i = 0; i < decodedBytes; i++)
                    {
                        parseBuffer.Add(unzipBuffer[i]);
                        if (IsEnd(parseBuffer.Last()))
                        {
                            if (--nestedCount == 0 && parseBuffer.Count > 0)
                            {
                                // We have a complete JSON object or array ready for processing
                                var changes = Manager.GetObjectMapper().ReadValue <IList <object> >(parseBuffer);
                                Log.To.ChangeTracker.I(Tag, "Parse found {0} changes", changes.Count);
                                foreach (var change in changes)
                                {
                                    Log.To.ChangeTracker.D(Tag, "{0} sending next change: {1}", this, new LogJsonString(change));
                                    ChunkFound?.Invoke(this, change.AsDictionary <string, object>());
                                }

                                parseBuffer.Clear();
                            }
                            _pauseWait.Wait();
                        }
                        else if (IsStart(parseBuffer.Last()))
                        {
                            // Begin an embedded array
                            nestedCount++;
                        }
                    }

                    Log.To.ChangeTracker.I(Tag, "Parsed {0} (nested count: {1})", new LogString(unzipBuffer.Take(decodedBytes)),
                                           nestedCount);
                    decodedBytes = Decode(null, 0, unzipBuffer, out exception);
                }

                if (decodedBytes == -1)
                {
                    // The decode process failed, unable to continue
                    break;
                }

                readBytes = _innerStream.Read(zipBuffer, 0, zipBuffer.Length);
            }

            if (Finished != null)
            {
                Finished(this, exception);
            }
        }