public MMFinterComT(string ctrIpcChannelNameStr, string ctrIpcThreadName, int ctrMMFSize) { this.DipositChlName = ctrIpcChannelNameStr; this.DipositSize = ctrMMFSize; this.DipositThrdName = ctrIpcThreadName; mmf = MemoryMappedFile.CreateOrOpen(DipositChlName, DipositSize); accessor = mmf.CreateViewAccessor(0, DipositSize, System.IO.MemoryMappedFiles.MemoryMappedFileAccess.ReadWrite); //if (started) //smLock = new System.Threading.Mutex(true, IpcMutxName, out locked); ReadPosition = -1; writePosition = -1; this.dataToSend = new List <byte[]>(); this.statusSet = new List <string>(); }
private void WriteHandle(string key, IntPtr handler) { System.IO.MemoryMappedFiles.MemoryMappedFile mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(key, 1024000); using (System.IO.MemoryMappedFiles.MemoryMappedViewStream stream = mmf.CreateViewStream()) { using (System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor()) { accessor.Write(0, ref handler); } } }
static IntPtr getHandle(string key) { using (System.IO.MemoryMappedFiles.MemoryMappedFile mmf = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(key)) { using (System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor()) { IntPtr handler = IntPtr.Zero; accessor.Read(0, out handler); return(handler); } } }
/// <summary> /// this funtion is calles from parent process to /// transfer the new data to the plugin class /// </summary> /// <param name="data">list with all new log data items</param> public void add_log_data(List <LogDataSet> data) { if (_in_progress) //the current process is locked { return; } //export current data to a buffer for file writing _in_progress = true; AquaComputer.Logdata.LogDataExport export = new Logdata.LogDataExport(); export.logdata = data; export.name = export_name; export.exportTime = DateTime.Now; byte[] buffer = export.ToBuffer(); if (buffer == null || export_name == null || export_name == string.Empty) { _in_progress = false; return; } //try to create a new shared memory file if (memory_file == null || memory_file.SafeMemoryMappedFileHandle.IsClosed || memory_file.SafeMemoryMappedFileHandle.IsInvalid) { try { memory_file = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew(export_name, buffer.LongLength * 2); memory_file_size = buffer.Length * 2; } catch { memory_file = null; memory_file_size = 0; } } //not able to craeate to use a shared memory file with the current settings if (memory_file == null) { _in_progress = false; return; } //check if the file used from external ressources //wait until all locks are released or timeout elapsed bool initial_owned; System.Threading.Mutex shm_mutex = new System.Threading.Mutex(true, "Global\\" + export_name + "_mutex", out initial_owned); if (!initial_owned) { bool mutex_is_free = shm_mutex.WaitOne(50); if (mutex_is_free == false) { _in_progress = false; return; } } if (memory_file_size < buffer.Length) { //file buffer is too small, create file buffer memory_file.Dispose(); memory_file = System.IO.MemoryMappedFiles.MemoryMappedFile.CreateNew(export_name, buffer.LongLength * 2); memory_file_size = buffer.Length * 2; } //acces file to write data to ram segement using (var accessor = memory_file.CreateViewAccessor(0, memory_file_size)) { if (accessor != null) { accessor.WriteArray <byte>(0, buffer, 0, buffer.Length); //reset rest of data array to 0 byte[] reset_buffer = new byte[memory_file_size - buffer.Length]; for (int i = 0; i < reset_buffer.Length; i++) { reset_buffer[i] = 0; } accessor.WriteArray <byte>(buffer.Length, reset_buffer, 0, reset_buffer.Length); } } //release ressources shm_mutex.ReleaseMutex(); //release mutex _in_progress = false; }