Beispiel #1
0
 private void MoveMemoryToQueue()
 {
     using (memoryLocker.Using())
     {
         streamsToWrite.Enqueue(fileBlock, 0L);
     }
     if (streamsAvailable.Count == 0)
     {
         fileBlock = new FileBlock(fileHeader.blockSize);
     }
     else
     {
         using (memoryLocker.Using())
         {
             streamsAvailable.Dequeue(out fileBlock);
         }
     }
 }
Beispiel #2
0
        private unsafe void CreateFileForWriting()
        {
            fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read, 1024, FileOptions.WriteThrough);
            log.Debug("OpenFileForWriting()");
            fileHeader.blockHeader.version = 1;
            fileHeader.blockHeader.type    = BlockType.FileHeader;
            fileHeader.blockSize           = 1024 * 8;
            fileHeader.utcTimeStamp        = Factory.Parallel.UtcNow.Internal;
            fileHeader.SetChecksum();
            var headerBytes = new byte[fileHeader.blockSize];

            fixed(byte *bptr = headerBytes)
            {
                *((TickFileHeader *)bptr) = fileHeader;
            }

            fs.Write(headerBytes, 0, fileHeader.blockSize);
            fileBlock = new FileBlock(fileHeader.blockSize);
        }
Beispiel #3
0
        private unsafe void OpenFileForReading()
        {
            for (int retry = 0; retry < 3; retry++)
            {
                try
                {
                    if (!quietMode)
                    {
                        LogInfo("Reading from file: " + fileName);
                    }

                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    var headerBytes = new byte[sizeof(TickFileHeader)];
                    var headerSize  = sizeof(TickFileHeader);
                    var readBytes   = fs.Read(headerBytes, 0, headerSize);
                    if (readBytes != headerSize)
                    {
                        throw new InvalidOperationException("Number of header bytes " + readBytes + " differs from size of the header " + headerSize);
                    }
                    fixed(byte *headerPtr = headerBytes)
                    {
                        fileHeader = *((TickFileHeader *)headerPtr);
                    }
                    if (!fileHeader.VerifyChecksum())
                    {
                        throw new InvalidOperationException("Checksum failed for file header.");
                    }

                    // Read the entire header block including all padding.
                    fs.Seek(fileHeader.blockSize, SeekOrigin.Begin);
                    // Verify the version number.
                    switch (fileHeader.blockHeader.version)
                    {
                    case 1:
                        break;

                    default:
                        throw new InvalidOperationException("Unrecognized tick file version " + fileHeader.blockHeader.version);
                    }

                    if (!quietMode || debug)
                    {
                        if (debug)
                        {
                            log.Debug("Starting to read data.");
                        }
                    }
                    fileBlock         = new FileBlock(fileHeader.blockSize);
                    readFileStopwatch = new Stopwatch();
                    readFileStopwatch.Start();
                    break;
                }
                catch (InvalidOperationException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    if (e is CollectionTerminatedException)
                    {
                        log.Warn("Reader queue was terminated.");
                    }
                    else if (e is ThreadAbortException)
                    {
                        //
                    }
                    else if (e is FileNotFoundException)
                    {
                        log.Error("ERROR: " + e.Message);
                    }
                    else
                    {
                        log.Error("ERROR: " + e);
                        Factory.Parallel.Sleep(1000);
                    }
                }
            }
        }