/// <summary> /// Open a new output file. /// </summary> /// <remarks>Any existing file will be closed.</remarks> private void OpenFile() { //clear the existing file pointer to make sure if we fail, it's gone. //we also rely on this to distinguish adding a new file to an existing stream. CloseFile(false); //increment our session file counter since we're going to open a new file m_CurrentSessionFile++; //Calculate our candidate file name (with path) based on what we know. string fileNamePath = Path.Combine(m_RepositoryFolder, MakeFileName()); //now double check that the candidate path is unique fileNamePath = FileSystemTools.MakeFileNamePathUnique(fileNamePath); //we now have a unique file name, create the file. FileSystemTools.EnsurePathExists(fileNamePath); m_CurrentFile = new FileStream(fileNamePath, FileMode.CreateNew, FileAccess.Write); //and open a serializer on it m_CurrentSerializer = new GLFWriter(m_CurrentFile, Publisher.SessionSummary, m_CurrentSessionFile, DateTimeOffset.Now); //write out every header packet to the stream ICachedMessengerPacket[] headerPackets = Publisher.HeaderPackets; if (headerPackets != null) { foreach (ICachedMessengerPacket packet in headerPackets) { m_CurrentSerializer.Write(packet); } } //and set a time for us to do our next index update. m_FileExpiration = DateTime.Now.AddSeconds(m_MaxLogDurationSeconds); }
private void CloseFile(bool isLastFile) { //close any existing serializer if (m_CurrentSerializer != null) { try { // The order of these two operations is related in a non-obvious way: closing the current // serializer updates the session header we write to the index, so it must be done first. m_CurrentSerializer.Close(isLastFile); // Now update our index information with the final session header info. if (isLastFile == false) { //we need to keep our state as being running, Close just changed it. m_CurrentSerializer.SessionHeader.StatusName = SessionStatus.Running.ToString(); } } finally { m_CurrentSerializer = null; //and pack the string reference list since we dumped the unique string list, which may be holding a lot of strings. GC.Collect(); StringReference.Pack(); //We used to wait for pending finalizers but that turns out to be bad if a UI Thread is waiting on us. } } //close any existing file stream if (m_CurrentFile != null) { try { m_CurrentFile.Flush(); } finally { m_CurrentFile.Dispose(); m_CurrentFile = null; } } // And if it's the last file, release our unique lock for this session. if (isLastFile) { ReleaseSessionFileLock(); } }