Beispiel #1
0
        /// <summary>
        ///     Read from the file, but do not update the positition (in the position file)
        /// </summary>
        /// <param name="messages">Will be cleared and then filled with all available buffers</param>
        /// <param name="maxNumberOfMessages">Number of wanted records (will return less if less are available)</param>
        /// <remarks>
        ///     <para>
        ///         Caches peeked records and returns the same if no Dequeus have been made between the Peeks
        ///     </para>
        /// </remarks>
        public void Peek(List <byte[]> messages, int maxNumberOfMessages)
        {
            if (_peekedRecords.Any())
            {
                PeekRecord peekRecord = null;
                var        node       = _peekedRecords.First;
                while (messages.Count < maxNumberOfMessages && node != null)
                {
                    peekRecord = node.Value;
                    messages.Add(peekRecord.Buffer);
                    node = node.Next;
                }
            }


            do
            {
                var position = _readStream.Position;
                var record   = _queueRecordSerializer.Read(_readStream);
                if (record == null)
                {
                    return;
                }

                _peekedRecords.AddLast(new PeekRecord(position, record.Length, record));

                --maxNumberOfMessages;
                messages.Add(record);
            } while (maxNumberOfMessages > 0);
        }
Beispiel #2
0
        private int CalculateQueueLength()
        {
            var queueLength = 0;
            var serializer  = new QueueRecordSerializer();

            foreach (var file in _files)
            {
                var startPosition = 0;
                var positionFile  = file.Replace(".dat", ".position");
                if (FileExists(positionFile))
                {
                    using (
                        var stream = new FileStream(positionFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite,
                                                    16384, FileOptions.SequentialScan))
                    {
                        if (stream.Length > 4)
                        {
                            stream.Position = stream.Length - 4;
                            var intBuffer = new byte[4];
                            var pos       = stream.Read(intBuffer, 0, 4);
                            startPosition = BitConverter.ToInt32(intBuffer, 0);
                        }
                    }
                }

                using (
                    var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 16384,
                                                FileOptions.SequentialScan))
                {
                    stream.Position = startPosition;
                    while (true)
                    {
                        var record = serializer.Read(stream);
                        if (record != null)
                        {
                            ++queueLength;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return(queueLength);
        }