public async Task Insert(Guid guid, long dataOffset, long dataSize) { using (var indexAccessor = file.CreateViewAccessor(bstPosition, 0)) { // update BST var closestMatch = ClosestNode(guid); var parentNode = closestMatch.Item1; var parentNodeOffset = closestMatch.Item2; Task flushMetadata; // TODO: not happy with this check if (metadata.NextBSTNode == 0) { // needed to handle root node / first entry correctly metadata.NextBSTNode = 1; metadataAccessor.Write(0, ref metadata); flushMetadata = Task.Run(() => metadataAccessor.Flush()); parentNode.Guid = guid; parentNode.DataOffset = dataOffset; parentNode.DataSize = dataSize; } else { // normal path long newNodeId = metadata.NextBSTNode; metadata.NextBSTNode++; metadataAccessor.Write(0, ref metadata); flushMetadata = Task.Run(() => metadataAccessor.Flush()); var compRes = guid.CompareTo(parentNode.Guid); if (compRes < 0) { parentNode.Low = newNodeId; } else if (compRes > 0) { parentNode.High = newNodeId; } else { throw new ArgumentException("guid already exists"); } // add node var newNodeOffset = newNodeId * bstNodeSize; var node = new IndexEntry(guid, dataOffset, dataSize); indexAccessor.Write(newNodeOffset, ref node); } // update parent indexAccessor.Write(parentNodeOffset, ref parentNode); var flushIndex = Task.Run(() => { indexAccessor.Flush(); }); await Task.WhenAll(flushMetadata, flushIndex); } }
private void CloseWorker() { if (Interlocked.Decrement(ref _refCount) == 0) { _view.Flush(); _view.Dispose(); _file.Dispose(); _sourceStream = null; _view = null; _file = null; } }
public void Dispose() { try { if (null != memoryMappedViewAccessor) { memoryMappedViewAccessor.Flush(); _mLastMarkFlush = DateTime.Now; memoryMappedViewAccessor.Dispose(); } } catch (Exception ex) { Cat.lastException = ex; } }
// use: 1) autoRefresh called by timer, without calling the buttonRefresh 2)called from the buttonRefresh private void Timer_Tick_Tock(object sender, EventArgs e) { try { //call the 2 methods thar update the list of Online Users LANSharingApp.umu.RemoveOldUsers(); LANSharingApp.umu.AddNewUsers(); LANSharingApp.umu.UpdateImageButtons(); //LAUNCH GUI WITH NEW PATH SEND IF THE USER INTERACT WITH NEW FILES/DIRECTORIES WITH RIGTH CLICK ON THEM //.... // read the integer value at position 500 MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(); int l = accessor.ReadInt32(0); accessor.Flush(); // print it to the console if (l != 0) { //get path as bytes byte[] Buffer = new byte[l]; accessor.ReadArray(4, Buffer, 0, Buffer.Length); accessor.Flush(); //convert bytes to string string newPath = ASCIIEncoding.ASCII.GetString(Buffer); // Console.WriteLine("The newPath is " + newPath); lock (LANSharingApp.lockerPathSend) { LANSharingApp.pathSend = newPath; } this.pathBox.Text = newPath; LANSharingApp.umu.clearMetroButtons(); base.SetVisibleCore(true); this.WindowState = FormWindowState.Normal; //invalidate path as old one accessor.Write(0, 0); accessor.Flush(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); LANSharingApp.LogFile(ex.Message, ex.ToString(), ex.Source); } }
public unsafe BitmapDataBuffer Write(VideoBlock block) { lock (SyncLock) { if (IsDisposed) { return(null); } EnsureBuffers(block); // Compute a safe number of bytes to copy // At this point, we it is assumed the strides are equal var bufferLength = Math.Min(block.BufferLength, BackBufferView.Capacity); var scan0 = BackBufferView.SafeMemoryMappedViewHandle.DangerousGetHandle(); // Copy the block data into the back buffer of the target bitmap. Buffer.MemoryCopy( block.Buffer.ToPointer(), scan0.ToPointer(), bufferLength, bufferLength); if (BitmapData == null || BitmapData.Scan0 != scan0) { BitmapData = new BitmapDataBuffer( scan0, block.PictureBufferStride, block.PixelWidth, block.PixelHeight, Parent.DpiX, Parent.DpiY); } BackBufferView.Flush(); return(BitmapData); } }
private void UpdateMMF() { if (m_bInternalUpdate) { return; } // Update checksum FieldInfo[] Fields = m_Instance.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance); int Checksum = 0; foreach (FieldInfo Field in Fields) { object Value = Field.GetValue(m_Instance); int Hash = Value.GetHashCode(); Checksum ^= Hash; } m_Instance.SetChecksum(Checksum); if (m_View == null) { return; // Not initialized yet... } // Write the new content... m_View.Write(0, ref m_Instance); m_View.Flush(); }
public void Close() { if (fileAccessor != null) { fileAccessor.Flush(); } }
public void MultipleMapsForTheSameFileStream() { const int Capacity = 4096; using (TempFile file = new TempFile(GetTestFilePath(), Capacity)) using (FileStream fs = new FileStream(file.Path, FileMode.Open)) using (MemoryMappedFile mmf1 = MemoryMappedFile.CreateFromFile(fs, null, Capacity, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) using (MemoryMappedFile mmf2 = MemoryMappedFile.CreateFromFile(fs, null, Capacity, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) using (MemoryMappedViewAccessor acc1 = mmf1.CreateViewAccessor()) using (MemoryMappedViewAccessor acc2 = mmf2.CreateViewAccessor()) { // The capacity of the two maps should be equal Assert.Equal(acc1.Capacity, acc2.Capacity); var rand = new Random(); for (int i = 1; i <= 10; i++) { // Write a value to one map, then read it from the other, // ping-ponging between the two. int pos = rand.Next((int)acc1.Capacity - 1); MemoryMappedViewAccessor reader = acc1, writer = acc2; if (i % 2 == 0) { reader = acc2; writer = acc1; } writer.Write(pos, (byte)i); writer.Flush(); Assert.Equal(i, reader.ReadByte(pos)); } } }
public void Flush() { var last = Interlocked.Read(ref _last); if (last == _lastFlushed) { return; } _accessor.Write(0, last); _accessor.Flush(); //FlushViewOfFile(_accessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), (UIntPtr)sizeof(long)); if (!FileStreamExtensions.FlushFileBuffers(_fileHandle)) { throw new Exception(string.Format("FlushFileBuffers failed with err: {0}", Marshal.GetLastWin32Error())); } Interlocked.Exchange(ref _lastFlushed, last); lock (_flushLocker) { Monitor.PulseAll(_flushLocker); } }
// запись списка техпроцессов в разделяемую память private void WriteIntoMappedMemory() { DataGridViewSelectedRowCollection Collection = dataGridViewListOfDetails.SelectedRows; TechList list = new TechList(); for (int i = Collection.Count - 1; i >= 0; i--) // в коллекции строки находятся в обратном порядке { DataGridViewRow row = Collection[i]; int index = (int)dataGridViewListOfDetails["Number", row.Index].Value; list.Add(Globals.dictQwewe[index]); } using (MemoryStream ms = new MemoryStream()) { XmlSerializer xmlSer = new XmlSerializer(typeof(TechList)); xmlSer.Serialize(ms, list); ms.Position = 0; byte[] byteArray = ms.ToArray(); int size = byteArray.Length; if (size >= 1000000) { MessageBox.Show("Слишком большой объём передаваемых данных!"); return; } //Создание участка разделяемой памяти. Первый параметр - название участка, второй - длина участка памяти в байтах: тип byte занимает 1 байт плюс четыре байта для одного объекта типа Integer (для передачи длины сообщения) using (MemoryMappedViewAccessor writer = Globals.sharedMemory.CreateViewAccessor(0, size + 4)) //Создаем объект для записи в разделяемый участок памяти { writer.Write(0, size); // сначала записываем в разделяемую память, начиная с нулевой позиции, размер сообщения writer.WriteArray <byte>(4, byteArray, 0, size); //записываем в разделяемую память, начиная с четвёртой позиции, сообщение writer.Flush(); } } }
static void CloseFile() { StoreState(); LogAccessor.Flush(); LogAccessor.Dispose(); Log.Dispose(); }
public void Send(TMessage message) { receiverWaitHandle.WaitOne(); Int32 writeLength = Constants.HeaderSize + msgLength; var buffer = new WriteBuffer(accessor); buffer.Write(1); // header serializer(new[] { message }, buffer); accessor.Flush(); senderWaitHandle.Set(); if (MessageDispatched != null) { MessageDispatched(this, new MessageDispatchEventArgs <TMessage>(ref message)); } }
public void DataShared() { // Create a new file and load it into an MMF using (TempFile file = new TempFile(GetTestFilePath(), 4096)) using (FileStream fs = new FileStream(file.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) using (MemoryMappedViewAccessor acc = mmf.CreateViewAccessor()) { // Write some known data to the map long capacity = acc.Capacity; for (int i = 0; i < capacity; i++) { acc.Write(i, unchecked ((byte)i)); } acc.Flush(); // Spawn and then wait for the other process, which will verify the data and write its own known pattern RemoteInvoke(DataShared_OtherProcess, $"\"{file.Path}\"").Dispose(); // Now verify we're seeing the data from the other process for (int i = 0; i < capacity; i++) { Assert.Equal(unchecked ((byte)(capacity - i - 1)), acc.ReadByte(i)); } } }
private void sendConnectionToMMF(string connectionInfo) { // Get the process ID of the currently running Visual Studio IDE var vsId = System.Diagnostics.Process.GetCurrentProcess().Id; try { if (memoryMappedFile != null) { memoryMappedFile.Dispose(); } memoryMappedFile = MemoryMappedFile.CreateOrOpen("connection-info-" + vsId.ToString(), 10000, MemoryMappedFileAccess.ReadWrite); viewAccessor = memoryMappedFile.CreateViewAccessor(); byte[] fileContent = Encoding.UTF8.GetBytes(connectionInfo); viewAccessor.Write(0, fileContent.Length); viewAccessor.WriteArray <byte>(4, fileContent, 0, fileContent.Length); viewAccessor.Flush(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void InformCompleted(int serie) { lock (_locker) { _viewAccessor.Write(serie, (byte)2); _viewAccessor.Flush(); } }
public void Flush() { _accessor.Write(0, Interlocked.Read(ref _last)); _accessor.Flush(); Interlocked.Exchange(ref _lastFlushed, _last); FlushFileBuffers(_file.SafeMemoryMappedFileHandle.DangerousGetHandle()); }
public void Flush(bool flushToDisk = false) { _va.Flush(); if (flushToDisk) { _fileStream.Flush(true); } }
public void FreeVirtualMemory() { if (null != memMapViewAccessor) { memMapViewAccessor.Flush(); memMapViewAccessor.Dispose(); } memMapViewAccessor = null; }
internal void WriteArray(long position, byte[] array) { if (!_accessor.CanWrite) { return; } _accessor.WriteArray(position, array, 0, array.Length); _accessor.Flush(); }
/// <summary> /// Updates entries in the current index with file sizes and times /// Algorithm: /// 1) If there was an index in place when this object was constructed, then: /// a) Copy all valid entries (below) from the previous index to the new index /// b) Conditionally (below) get times/sizes from the working tree for files not updated from the previous index /// /// 2) If there was no index in place, conditionally populate all entries from disk /// /// Conditions: /// - Working tree is only searched if allowUpdateFromWorkingTree is specified /// - A valid entry is an entry that exist and has a non-zero creation time (ctime) /// </summary> /// <param name="addedOrEditedLocalFiles">A collection of added or edited files</param> /// <param name="allowUpdateFromWorkingTree">Set to true if the working tree is known good and can be used during the update.</param> /// <param name="backupIndex">An optional index to source entry values from</param> public void UpdateFileSizesAndTimes(BlockingCollection <string> addedOrEditedLocalFiles, bool allowUpdateFromWorkingTree, bool shouldSignIndex, Index backupIndex = null) { if (this.readOnly) { throw new InvalidOperationException("Cannot update a readonly index."); } using (ITracer activity = this.tracer.StartActivity("UpdateFileSizesAndTimes", EventLevel.Informational, Keywords.Telemetry, null)) { File.Copy(this.indexPath, this.updatedIndexPath, overwrite: true); this.Parse(); bool anyEntriesUpdated = false; using (MemoryMappedFile mmf = this.GetMemoryMappedFile()) using (MemoryMappedViewAccessor indexView = mmf.CreateViewAccessor()) { // Only populate from the previous index if we believe it's good to populate from // For now, a current FastFetch version marker is the only criteria if (backupIndex != null) { if (this.IsFastFetchVersionMarkerCurrent()) { using (this.tracer.StartActivity("UpdateFileInformationFromPreviousIndex", EventLevel.Informational, Keywords.Telemetry, null)) { anyEntriesUpdated |= this.UpdateFileInformationForAllEntries(indexView, backupIndex, allowUpdateFromWorkingTree); } if (addedOrEditedLocalFiles != null) { // always update these files from disk or the index won't have good information // for them and they'll show as modified even those not actually modified. anyEntriesUpdated |= this.UpdateFileInformationFromDiskForFiles(indexView, addedOrEditedLocalFiles); } } } else if (allowUpdateFromWorkingTree) { // If we didn't update from a previous index, update from the working tree if allowed. anyEntriesUpdated |= this.UpdateFileInformationFromWorkingTree(indexView); } indexView.Flush(); } if (anyEntriesUpdated) { this.MoveUpdatedIndexToFinalLocation(shouldSignIndex); } else { File.Delete(this.updatedIndexPath); } } }
/// <summary> /// метод записи в созданный mms /// </summary> /// <param name="content"></param> /// <param name="writer"></param> private static void writeString(string content, MemoryMappedViewAccessor writer) { var contentBytes = System.Text.ASCIIEncoding.Unicode.GetBytes(content); int count = contentBytes.Length; writer.Write <Int32>(0, ref count); writer.WriteArray <byte>(sizeof(Int32), contentBytes, 0, contentBytes.Length); writer.Flush(); }
public void Shutdown() { if (ipcConnectionExists) { accessor.Flush(); accessor.Dispose(); mmf.Dispose(); ipcConnectionExists = false; } }
/// <summary> /// Escribe los datos de memoria a disco /// </summary> public void Flush() { try { sharedMutex.WaitOne(); mmva.Flush(); } finally { sharedMutex.ReleaseMutex(); } }
public override void Delete(byte[] key) { lock (index) { long OO = index [key]; MemoryMappedViewAccessor accessor = mmfBlock.CreateViewAccessor(reservedBytes + OO, 1, MemoryMappedFileAccess.ReadWrite); accessor.Write(0, true); accessor.Flush(); accessor.Dispose(); index.Remove(key); index.Commit(); } }
public void WriteObjectToMMF(string mmfFile, object objectData, bool createNewMmf) { // Convert .NET object to byte array byte[] buffer = ObjectToByteArray(objectData); MemoryMappedFile mmf; if (createNewMmf) { var security = new MemoryMappedFileSecurity(); security.AddAccessRule(new System.Security.AccessControl.AccessRule <MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.WorldSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); security.AddAccessRule(new System.Security.AccessControl.AccessRule <MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.AnonymousSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); security.AddAccessRule(new System.Security.AccessControl.AccessRule <MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.NullSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); // printing the SID from the screensaver shows that we are running as Local Service security.AddAccessRule(new System.Security.AccessControl.AccessRule <MemoryMappedFileRights>( new SecurityIdentifier(WellKnownSidType.LocalServiceSid, null), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); // Create a new memory mapped file mmf = MemoryMappedFile.CreateOrOpen(mmfFile, buffer.Length, MemoryMappedFileAccess.ReadWriteExecute, MemoryMappedFileOptions.None, security, System.IO.HandleInheritability.Inheritable); } else { try { mmf = MemoryMappedFile.OpenExisting(mmfFile, MemoryMappedFileRights.ReadWriteExecute, System.IO.HandleInheritability.Inheritable); } catch (FileNotFoundException) { return; } } bool mutexCreated; Mutex mutex = new Mutex(true, "mmfMutex", out mutexCreated); // Create a view accessor into the file to accommmodate binary data size MemoryMappedViewAccessor mmfWriter = mmf.CreateViewAccessor(0, buffer.Length); // Write the data mmfWriter.WriteArray <byte>(0, buffer, 0, buffer.Length); mmfWriter.Flush(); if (mutexCreated) { mutex.ReleaseMutex(); } }
public void FlushSupportedOnBothReadAndWriteAccessors(MemoryMappedFileAccess access) { const int Capacity = 256; foreach (MemoryMappedFile mmf in CreateSampleMaps(Capacity)) { using (mmf) using (MemoryMappedViewAccessor acc = mmf.CreateViewAccessor(0, Capacity, access)) { acc.Flush(); } } }
public override void Put(byte[] key, byte[] value) { try { Console.WriteLine("{0}:", name); Console.Write("{0}:", key.ToHexadecimal()); try { Console.WriteLine("{0}:", System.Text.Encoding.Unicode.GetString(value)); } catch { Console.WriteLine("{0}", value.ToHexadecimal()); } } catch { Console.WriteLine("XXX"); } long OO; lock (index) if (index.TryGetValue(key, out OO)) { //TODO:resize object if we can Delete(key); Put(key, value); } else { ObjectHeader ObjectHeader = new ObjectHeader(); ObjectHeader.Deleted = false; ObjectHeader.Length = value.Length; ObjectHeader.Key = key; int ObjectSize = value.Length; const int ObjectHeaderSize = 40; OO = mh.WriteOffset; mh.WriteOffset += ObjectSize + ObjectHeaderSize; EnsureCapacity(mh.WriteOffset); MemoryMappedViewStream accessor = mmfBlock.CreateViewStream(reservedBytes + OO, ObjectSize + ObjectHeaderSize, MemoryMappedFileAccess.Write); byte[] ObjectHeaderBytes = new byte[ObjectHeaderSize]; System.IO.BinaryWriter bw = new BinaryWriter(new MemoryStream(ObjectHeaderBytes, true)); bw.Write(ObjectHeader.Deleted); bw.BaseStream.Seek(3, SeekOrigin.Current); bw.Write(ObjectHeader.Length); bw.Write(ObjectHeader.Key); accessor.Write(ObjectHeaderBytes, 0, ObjectHeaderSize); accessor.Write(value, 0, value.Length); accessor.Flush(); accessor.Dispose(); maMeta.Write(0, ref mh); maMeta.Flush(); index.Add(key, OO); index.Commit(); } }
private static void ReturnResult(MemoryMappedViewAccessor resultBufferAccessor, Stream outputStream, long res, long ns, long sonar, Upstream upstream) { byte[] resBuffer = new byte[8]; byte[] nsBuffer = new byte[8]; byte[] sonarBuffer = new byte[8]; Serialize56(resBuffer, res); Serialize56(nsBuffer, ns); Serialize56(sonarBuffer, sonar); Console.WriteLine("serializing upstream"); /*int maxReturnSize = FlatBufferSerializer.Default.GetMaxSize(upstream); * var returnBuffer = new byte[maxReturnSize]; * returnLength = FlatBufferSerializer.Default.Serialize(upstream, returnBuffer);*/ byte[] returnLengthBuffer = new byte[8]; var returnResultBuffer = JsonSerializer.SerializeToUtf8Bytes <Upstream>(upstream); var jsonString = Encoding.UTF8.GetString(returnResultBuffer); Console.WriteLine("upstream json: " + jsonString); Serialize56(returnLengthBuffer, returnResultBuffer.Length); for (int i = 0; i < 8; i++) { resultBufferAccessor.Write(i, returnLengthBuffer[i]); } for (int i = 0; i < returnResultBuffer.Length; i++) { resultBufferAccessor.Write(i + 8, returnResultBuffer[i]); } resultBufferAccessor.Flush(); Console.WriteLine("wrote to comm1Accessor"); byte[] outputBuffer = new byte[24]; for (int i = 0; i < 8; i++) { outputBuffer[i] = resBuffer[i]; outputBuffer[i + 8] = nsBuffer[i]; outputBuffer[i + 16] = sonarBuffer[i]; } outputStream.Write(outputBuffer, 0, outputBuffer.Length); outputStream.Flush(); Console.WriteLine("wrote outputbuffer"); }
private void DisposeView() { view.Flush(); if (ptr != null) { view.SafeMemoryMappedViewHandle.ReleasePointer(); ptr = null; } if (!view.SafeMemoryMappedViewHandle.IsClosed) { view.SafeMemoryMappedViewHandle.Close(); } view.SafeMemoryMappedViewHandle.Dispose(); view.Dispose(); }
public long Flush() { lock (_view) { _view.Flush(); foreach (var write in _awaiting) { write(); } _awaiting.Clear(); return(_position); } }